1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5*
6*   Copyright (C) 1999-2016, International Business Machines
7*   Corporation and others.  All Rights Reserved.
8*
9*******************************************************************************
10*   file name:  gentest.c
11*   encoding:   UTF-8
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"
25#include "unicode/uclean.h"
26#include "unicode/udata.h"
27#include "udbgutil.h"
28#include "unewdata.h"
29#include "cmemory.h"
30#include "cstring.h"
31#include "uoptions.h"
32#include "gentest.h"
33
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
52static void createData(const char*, UErrorCode *);
53
54static int outputJavaStuff(const char * progname, const char *outputDir);
55
56static UOption options[]={
57  /*0*/ UOPTION_HELP_H,
58  /*1*/ UOPTION_HELP_QUESTION_MARK,
59  /*2*/ UOPTION_DESTDIR,
60  /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG),
61  /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG),
62};
63
64extern int
65main(int argc, char* argv[]) {
66    UErrorCode errorCode = U_ZERO_ERROR;
67
68    /* preset then read command line options */
69    options[2].value=u_getDataDirectory();
70    argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
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"
81            "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
82            "\toptions:\n"
83            "\t\t-h or -? or --help  this usage text\n"
84            "\t\t-d or --destdir     destination directory, followed by the path\n"
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",
87            argv[0]);
88        return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
89    }
90
91    if( options[4].doesOccur ) {
92    	return outputJavaStuff( argv[0], options[2].value );
93    } else if ( options[3].doesOccur ) {
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);
100}
101
102/* Create data file ----------------------------------------------------- */
103static void
104createData(const char* outputDirectory, UErrorCode *errorCode) {
105    UNewDataMemory *pData;
106    char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
107    uint16_t intValue=2000;
108
109    long dataLength;
110    uint32_t size;
111
112    pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
113                       U_COPYRIGHT_STRING, errorCode);
114    if(U_FAILURE(*errorCode)) {
115        fprintf(stderr, "gentest: unable to create data memory, %s\n", u_errorName(*errorCode));
116        exit(*errorCode);
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 */
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);
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}
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;
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
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");
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");
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) {
203#if !UCONFIG_NO_FORMATTING
204            case UDBG_UCalendarDateFields:
205            case UDBG_UCalendarMonths:
206                /* Temporary workaround for IS_LEAP_MONTH #6051 */
207                if (t == UDBG_UCalendarDateFields && i == 22) {
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                }
212                break;
213#endif
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
229}
230