Home | History | Annotate | Download | only in gentest
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2010, 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 "udbgutil.h"
     26 #include "unewdata.h"
     27 #include "cmemory.h"
     28 #include "cstring.h"
     29 #include "uoptions.h"
     30 #include "gentest.h"
     31 #include "toolutil.h"
     32 
     33 #define DATA_NAME "test"
     34 #define DATA_TYPE "icu"
     35 
     36 /* UDataInfo cf. udata.h */
     37 static const UDataInfo dataInfo={
     38     sizeof(UDataInfo),
     39     0,
     40 
     41     U_IS_BIG_ENDIAN,
     42     U_CHARSET_FAMILY,
     43     sizeof(UChar),
     44     0,
     45 
     46     {0x54, 0x65, 0x73, 0x74},     /* dataFormat="Test" */
     47     {1, 0, 0, 0},                 /* formatVersion */
     48     {1, 0, 0, 0}                  /* dataVersion */
     49 };
     50 
     51 static void createData(const char*, UErrorCode *);
     52 
     53 static int outputJavaStuff(const char * progname, const char *outputDir);
     54 
     55 static UOption options[]={
     56   /*0*/ UOPTION_HELP_H,
     57   /*1*/ UOPTION_HELP_QUESTION_MARK,
     58   /*2*/ UOPTION_DESTDIR,
     59   /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG),
     60   /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG),
     61 };
     62 
     63 extern int
     64 main(int argc, char* argv[]) {
     65     UErrorCode errorCode = U_ZERO_ERROR;
     66 
     67     /* preset then read command line options */
     68     options[2].value=u_getDataDirectory();
     69     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
     70 
     71     /* error handling, printing usage message */
     72     if(argc<0) {
     73         fprintf(stderr,
     74             "error in command line argument \"%s\"\n",
     75             argv[-argc]);
     76     }
     77     if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
     78         fprintf(stderr,
     79             "usage: %s [-options]\n"
     80             "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
     81             "\toptions:\n"
     82             "\t\t-h or -? or --help  this usage text\n"
     83             "\t\t-d or --destdir     destination directory, followed by the path\n"
     84             "\t\t-r or --genres      generate resource file testtable32.txt instead of UData test \n"
     85             "\t\t-j or --javastuff   generate Java source for DebugUtilities. \n",
     86             argv[0]);
     87         return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
     88     }
     89 
     90     if( options[4].doesOccur ) {
     91     	return outputJavaStuff( argv[0], options[2].value );
     92     } else if ( options[3].doesOccur ) {
     93         return genres32( argv[0], options[2].value );
     94     } else {
     95         /* printf("Generating the test memory mapped file\n"); */
     96         createData(options[2].value, &errorCode);
     97     }
     98     return U_FAILURE(errorCode);
     99 }
    100 
    101 /* Create data file ----------------------------------------------------- */
    102 static void
    103 createData(const char* outputDirectory, UErrorCode *errorCode) {
    104     UNewDataMemory *pData;
    105     char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
    106     uint16_t intValue=2000;
    107 
    108     long dataLength;
    109     uint32_t size;
    110 
    111     pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
    112                        U_COPYRIGHT_STRING, errorCode);
    113     if(U_FAILURE(*errorCode)) {
    114         fprintf(stderr, "gentest: unable to create data memory, %s\n", u_errorName(*errorCode));
    115         exit(*errorCode);
    116     }
    117 
    118     /* write the data to the file */
    119     /* a 16 bit value  and a String*/
    120     udata_write16(pData, intValue);
    121     udata_writeString(pData, stringValue, sizeof(stringValue));
    122 
    123     /* finish up */
    124     dataLength=udata_finish(pData, errorCode);
    125     if(U_FAILURE(*errorCode)) {
    126         fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode);
    127         exit(*errorCode);
    128     }
    129     size=sizeof(stringValue) + sizeof(intValue);
    130 
    131 
    132     if(dataLength!=(long)size) {
    133         fprintf(stderr, "gentest: data length %ld != calculated size %lu\n",
    134             dataLength, (unsigned long)size);
    135         exit(U_INTERNAL_PROGRAM_ERROR);
    136     }
    137 }
    138 
    139 /* Create Java file ----------------------------------------------------- */
    140 
    141 static int
    142 outputJavaStuff(const char* progname, const char *outputDir) {
    143     int32_t i,t,count;
    144     char file[512];
    145     FILE *out;
    146     int32_t year = getCurrentYear();
    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) 2007-%d, International Business Machines Corporation and Others. All Rights Reserved. **/\n\n", year);
    164     fprintf(out, "/* NOTE: this file is AUTOMATICALLY GENERATED by gentest.\n"
    165                  " * See: {ICU4C}/source/data/icu4j-readme.txt for more information. \n"
    166                  " **/\n\n");
    167     fprintf(out, "package com.ibm.icu.dev.test.util;\n\n");
    168     fprintf(out, "public class DebugUtilitiesData extends Object {\n");
    169     fprintf(out, "    public static final String ICU4C_VERSION=\"%s\";\n", U_ICU_VERSION);
    170     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    171         fprintf(out, "    public static final int %s = %d;\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    172     }
    173     fprintf(out, "    public static final String [] TYPES = { \n");
    174     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    175         fprintf(out, "        \"%s\", /* %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    176     }
    177     fprintf(out, "    };\n\n");
    178 
    179     fprintf(out, "    public static final String [][] NAMES = { \n");
    180     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    181         count = udbg_enumCount((UDebugEnumType)t);
    182         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    183         fprintf(out, "        { \n");
    184         for(i=0;i<count;i++) {
    185             fprintf(out,
    186                 "           \"%s\", /* %d */ \n", udbg_enumName((UDebugEnumType)t,i), i);
    187         }
    188         fprintf(out, "        },\n");
    189     }
    190     fprintf(out, "    };\n\n");
    191 
    192     fprintf(out, "    public static final int [][] VALUES = { \n");
    193     for(t=0;t<UDBG_ENUM_COUNT;t++) {
    194         count = udbg_enumCount((UDebugEnumType)t);
    195         fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
    196         fprintf(out, "        { \n");
    197         for(i=0;i<count;i++) {
    198             fprintf(out,
    199                 "           ");
    200             switch(t) {
    201 #if !UCONFIG_NO_FORMATTING
    202             case UDBG_UCalendarDateFields:
    203             case UDBG_UCalendarMonths:
    204                 /* Temporary workaround for IS_LEAP_MONTH #6051 */
    205                 if (t == UDBG_UCalendarDateFields && i == 22) {
    206                   fprintf(out, "com.ibm.icu.util.ChineseCalendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
    207                 } else {
    208                   fprintf(out, "com.ibm.icu.util.Calendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
    209                 }
    210                 break;
    211 #endif
    212             case UDBG_UDebugEnumType:
    213             default:
    214                 fprintf(out,"%d, /* %s */", i, udbg_enumName((UDebugEnumType)t,i));
    215             }
    216             fprintf(out,"\n");
    217         }
    218         fprintf(out, "        },\n");
    219     }
    220     fprintf(out, "    };\n\n");
    221     fprintf(out, "}\n");
    222 
    223     fclose(out);
    224 
    225     return 0;
    226 
    227 }
    228