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