• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 1999-2007, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  gentest.c
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2000mar03
14 *   created by: Madhu Katragadda
15 *
16 *   This program writes a little data file for testing the udata API.
17 */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
23 #include "unicode/uclean.h"
24 #include "unicode/udata.h"
25 #include "unicode/udbgutil.h"
26 #include "unewdata.h"
27 #include "cmemory.h"
28 #include "cstring.h"
29 #include "uoptions.h"
30 #include "gentest.h"
31 
32 #define DATA_NAME "test"
33 #define DATA_TYPE "icu"
34 
35 /* UDataInfo cf. udata.h */
36 static const UDataInfo dataInfo={
37     sizeof(UDataInfo),
38     0,
39 
40     U_IS_BIG_ENDIAN,
41     U_CHARSET_FAMILY,
42     sizeof(UChar),
43     0,
44 
45     {0x54, 0x65, 0x73, 0x74},     /* dataFormat="Test" */
46     {1, 0, 0, 0},                 /* formatVersion */
47     {1, 0, 0, 0}                  /* dataVersion */
48 };
49 
50 static void createData(const char*, UErrorCode *);
51 
52 static int outputJavaStuff(const char * progname, const char *outputDir);
53 
54 static UOption options[]={
55   /*0*/ UOPTION_HELP_H,
56   /*1*/ UOPTION_HELP_QUESTION_MARK,
57   /*2*/ UOPTION_DESTDIR,
58   /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG),
59   /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG),
60 };
61 
62 extern int
main(int argc,char * argv[])63 main(int argc, char* argv[]) {
64     UErrorCode errorCode = U_ZERO_ERROR;
65 
66     /* preset then read command line options */
67     options[2].value=u_getDataDirectory();
68     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
69 
70     /* error handling, printing usage message */
71     if(argc<0) {
72         fprintf(stderr,
73             "error in command line argument \"%s\"\n",
74             argv[-argc]);
75     }
76     if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
77         fprintf(stderr,
78             "usage: %s [-options]\n"
79             "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
80             "\toptions:\n"
81             "\t\t-h or -? or --help  this usage text\n"
82             "\t\t-d or --destdir     destination directory, followed by the path\n"
83             "\t\t-r or --genres      generate resource file testtable32.txt instead of UData test \n"
84             "\t\t-j or --javastuff   generate Java source for DebugUtilities. \n",
85             argv[0]);
86         return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
87     }
88 
89     if( options[4].doesOccur ) {
90     	return outputJavaStuff( argv[0], options[2].value );
91     } else if ( options[3].doesOccur ) {
92         return genres32( argv[0], options[2].value );
93     } else {
94         /* printf("Generating the test memory mapped file\n"); */
95         createData(options[2].value, &errorCode);
96     }
97     return U_FAILURE(errorCode);
98 }
99 
100 /* Create data file ----------------------------------------------------- */
101 static void
createData(const char * outputDirectory,UErrorCode * errorCode)102 createData(const char* outputDirectory, UErrorCode *errorCode) {
103     UNewDataMemory *pData;
104     char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
105     uint16_t intValue=2000;
106 
107     long dataLength;
108     uint32_t size;
109 
110     pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
111                        U_COPYRIGHT_STRING, errorCode);
112     if(U_FAILURE(*errorCode)) {
113         fprintf(stderr, "gentest: unable to create data memory, error %d\n", *errorCode);
114         exit(*errorCode);
115     }
116 
117     /* write the data to the file */
118     /* a 16 bit value  and a String*/
119     udata_write16(pData, intValue);
120     udata_writeString(pData, stringValue, sizeof(stringValue));
121 
122     /* finish up */
123     dataLength=udata_finish(pData, errorCode);
124     if(U_FAILURE(*errorCode)) {
125         fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode);
126         exit(*errorCode);
127     }
128     size=sizeof(stringValue) + sizeof(intValue);
129 
130 
131     if(dataLength!=(long)size) {
132         fprintf(stderr, "gentest: data length %ld != calculated size %lu\n",
133             dataLength, (unsigned long)size);
134         exit(U_INTERNAL_PROGRAM_ERROR);
135     }
136 }
137 
138 /* Create Java file ----------------------------------------------------- */
139 
140 static int
outputJavaStuff(const char * progname,const char * outputDir)141 outputJavaStuff(const char* progname, const char *outputDir) {
142     int32_t i,t,count;
143     char file[512];
144     FILE *out;
145 
146     uprv_strcpy(file,outputDir);
147     if(*outputDir &&  /* don't put a trailing slash if outputDir is empty */
148         file[strlen(file)-1]!=U_FILE_SEP_CHAR) {
149             uprv_strcat(file,U_FILE_SEP_STRING);
150     }
151     uprv_strcat(file,"DebugUtilitiesData.java");
152     out = fopen(file, "w");
153     /*puts(file);*/
154     printf("%s: Generating %s\n", progname, file);
155     if(out == NULL) {
156         fprintf(stderr, "%s: Couldn't create resource test file %s\n",
157             progname, file);
158         return 1;
159     }
160 
161     fprintf(out, "/** Copyright (C) 2007, International Business Machines Corporation and Others. All Rights Reserved. **/\n\n");
162     fprintf(out, "/* NOTE: this file is AUTOMATICALLY GENERATED by gentest. */\n\n");
163     fprintf(out, "package com.ibm.icu.dev.test.util;\n\n");
164     fprintf(out, "public class DebugUtilitiesData extends Object {\n");
165     fprintf(out, "    public static final String ICU4C_VERSION=\"%s\";\n", U_ICU_VERSION);
166     for(t=0;t<UDBG_ENUM_COUNT;t++) {
167         fprintf(out, "    public static final int %s = %d;\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
168     }
169     fprintf(out, "    public static final String [] TYPES = { \n");
170     for(t=0;t<UDBG_ENUM_COUNT;t++) {
171         fprintf(out, "        \"%s\", /* %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
172     }
173     fprintf(out, "    };\n\n");
174 
175     fprintf(out, "    public static final String [][] NAMES = { \n");
176     for(t=0;t<UDBG_ENUM_COUNT;t++) {
177         count = udbg_enumCount((UDebugEnumType)t);
178         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
179         fprintf(out, "        { \n");
180         for(i=0;i<count;i++) {
181             fprintf(out,
182                 "           \"%s\", /* %d */ \n", udbg_enumName((UDebugEnumType)t,i), i);
183         }
184         fprintf(out, "        },\n");
185     }
186     fprintf(out, "    };\n\n");
187 
188     fprintf(out, "    public static final int [][] VALUES = { \n");
189     for(t=0;t<UDBG_ENUM_COUNT;t++) {
190         count = udbg_enumCount((UDebugEnumType)t);
191         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
192         fprintf(out, "        { \n");
193         for(i=0;i<count;i++) {
194             fprintf(out,
195                 "           ");
196             switch(t) {
197             case UDBG_UCalendarDateFields:
198             case UDBG_UCalendarMonths:
199                 fprintf(out, "com.ibm.icu.util.Calendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
200                 break;
201             case UDBG_UDebugEnumType:
202             default:
203                 fprintf(out,"%d, /* %s */", i, udbg_enumName((UDebugEnumType)t,i));
204             }
205             fprintf(out,"\n");
206         }
207         fprintf(out, "        },\n");
208     }
209     fprintf(out, "    };\n\n");
210     fprintf(out, "}\n");
211 
212     fclose(out);
213 
214     return 0;
215 
216 #if 0
217     int32_t count = udbg_enumCount((UDebugEnumType)t);
218     if(count == -1) {
219         fprintf(stderr,"%s: enumCount(%d) returned -1\n", progname, count);
220         return 1;
221     }
222     for(t=0;t<=UDBG_ENUM_COUNT;t++) {
223         int32_t count = udbg_enumCount((UDebugEnumType)t);
224         if(count == -1) {
225             fprintf(stderr,"%s: enumCount(%d) returned -1\n", progname, count);
226             return 1;
227         }
228         for(i=0;i<=count;i++) {
229             if(i<count) {
230                 if( i!=udbg_enumArrayValue((UDebugEnumType)t, i)) {
231                     fprintf(stderr, "%s: FAIL: udbg_enumArrayValue(%d,%d) returned %d, expected %d\n", progname, t, i, udbg_enumArrayValue((UDebugEnumType)t,i), i);
232                     return 1;
233                 }
234             }
235             fprintf(stderr, "%s: udbg_enumArrayValue(%d,%d) = %s, returned %d\n", progname, t, i,
236                 udbg_enumName((UDebugEnumType)t,i), udbg_enumArrayValue((UDebugEnumType)t,i));
237         }
238         if(udbg_enumExpectedCount((UDebugEnumType)t) != count) {
239             fprintf(stderr, "%s: FAIL: udbg_enumExpectedCount(%d): %d, != UCAL_FIELD_COUNT=%d \n", progname, t, udbg_enumExpectedCount((UDebugEnumType)t), count);
240             return 1;
241         } else {
242             fprintf(stderr, "%s: udbg_ucal_fieldCount: %d, UCAL_FIELD_COUNT=udbg_enumCount %d ", progname, udbg_enumExpectedCount((UDebugEnumType)t), count);
243         }
244     }
245 #endif
246 }
247