intl/icu/source/tools/gentest/gentest.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/gentest/gentest.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,227 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1999-2010, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*   file name:  gentest.c
    1.12 +*   encoding:   US-ASCII
    1.13 +*   tab size:   8 (not used)
    1.14 +*   indentation:4
    1.15 +*
    1.16 +*   created on: 2000mar03
    1.17 +*   created by: Madhu Katragadda
    1.18 +*
    1.19 +*   This program writes a little data file for testing the udata API.
    1.20 +*/
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include "unicode/utypes.h"
    1.25 +#include "unicode/putil.h"
    1.26 +#include "unicode/uclean.h"
    1.27 +#include "unicode/udata.h"
    1.28 +#include "udbgutil.h"
    1.29 +#include "unewdata.h"
    1.30 +#include "cmemory.h"
    1.31 +#include "cstring.h"
    1.32 +#include "uoptions.h"
    1.33 +#include "gentest.h"
    1.34 +#include "toolutil.h"
    1.35 +
    1.36 +#define DATA_NAME "test"
    1.37 +#define DATA_TYPE "icu"
    1.38 +
    1.39 +/* UDataInfo cf. udata.h */
    1.40 +static const UDataInfo dataInfo={
    1.41 +    sizeof(UDataInfo),
    1.42 +    0,
    1.43 +
    1.44 +    U_IS_BIG_ENDIAN,
    1.45 +    U_CHARSET_FAMILY,
    1.46 +    sizeof(UChar),
    1.47 +    0,
    1.48 +
    1.49 +    {0x54, 0x65, 0x73, 0x74},     /* dataFormat="Test" */
    1.50 +    {1, 0, 0, 0},                 /* formatVersion */
    1.51 +    {1, 0, 0, 0}                  /* dataVersion */
    1.52 +};
    1.53 +
    1.54 +static void createData(const char*, UErrorCode *);
    1.55 +
    1.56 +static int outputJavaStuff(const char * progname, const char *outputDir);
    1.57 +
    1.58 +static UOption options[]={
    1.59 +  /*0*/ UOPTION_HELP_H,
    1.60 +  /*1*/ UOPTION_HELP_QUESTION_MARK,
    1.61 +  /*2*/ UOPTION_DESTDIR,
    1.62 +  /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG),
    1.63 +  /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG),  
    1.64 +};
    1.65 +
    1.66 +extern int
    1.67 +main(int argc, char* argv[]) {
    1.68 +    UErrorCode errorCode = U_ZERO_ERROR;
    1.69 +
    1.70 +    /* preset then read command line options */
    1.71 +    options[2].value=u_getDataDirectory();
    1.72 +    argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
    1.73 +
    1.74 +    /* error handling, printing usage message */
    1.75 +    if(argc<0) {
    1.76 +        fprintf(stderr,
    1.77 +            "error in command line argument \"%s\"\n",
    1.78 +            argv[-argc]);
    1.79 +    }
    1.80 +    if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
    1.81 +        fprintf(stderr,
    1.82 +            "usage: %s [-options]\n"
    1.83 +            "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
    1.84 +            "\toptions:\n"
    1.85 +            "\t\t-h or -? or --help  this usage text\n"
    1.86 +            "\t\t-d or --destdir     destination directory, followed by the path\n"
    1.87 +            "\t\t-r or --genres      generate resource file testtable32.txt instead of UData test \n"
    1.88 +            "\t\t-j or --javastuff   generate Java source for DebugUtilities. \n",
    1.89 +            argv[0]);
    1.90 +        return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
    1.91 +    }
    1.92 +
    1.93 +    if( options[4].doesOccur ) {
    1.94 +    	return outputJavaStuff( argv[0], options[2].value );
    1.95 +    } else if ( options[3].doesOccur ) {
    1.96 +        return genres32( argv[0], options[2].value );
    1.97 +    } else { 
    1.98 +        /* printf("Generating the test memory mapped file\n"); */
    1.99 +        createData(options[2].value, &errorCode);
   1.100 +    }
   1.101 +    return U_FAILURE(errorCode);
   1.102 +}
   1.103 +
   1.104 +/* Create data file ----------------------------------------------------- */
   1.105 +static void
   1.106 +createData(const char* outputDirectory, UErrorCode *errorCode) {
   1.107 +    UNewDataMemory *pData;
   1.108 +    char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
   1.109 +    uint16_t intValue=2000;
   1.110 +
   1.111 +    long dataLength;
   1.112 +    uint32_t size;
   1.113 +
   1.114 +    pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
   1.115 +                       U_COPYRIGHT_STRING, errorCode);
   1.116 +    if(U_FAILURE(*errorCode)) {
   1.117 +        fprintf(stderr, "gentest: unable to create data memory, %s\n", u_errorName(*errorCode));
   1.118 +        exit(*errorCode);
   1.119 +    }
   1.120 +
   1.121 +    /* write the data to the file */
   1.122 +    /* a 16 bit value  and a String*/
   1.123 +    udata_write16(pData, intValue);
   1.124 +    udata_writeString(pData, stringValue, sizeof(stringValue));
   1.125 +
   1.126 +    /* finish up */
   1.127 +    dataLength=udata_finish(pData, errorCode);
   1.128 +    if(U_FAILURE(*errorCode)) {
   1.129 +        fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode);
   1.130 +        exit(*errorCode);
   1.131 +    }
   1.132 +    size=sizeof(stringValue) + sizeof(intValue);
   1.133 +
   1.134 +
   1.135 +    if(dataLength!=(long)size) {
   1.136 +        fprintf(stderr, "gentest: data length %ld != calculated size %lu\n",
   1.137 +            dataLength, (unsigned long)size);
   1.138 +        exit(U_INTERNAL_PROGRAM_ERROR);
   1.139 +    }
   1.140 +}
   1.141 +
   1.142 +/* Create Java file ----------------------------------------------------- */
   1.143 +
   1.144 +static int
   1.145 +outputJavaStuff(const char* progname, const char *outputDir) {
   1.146 +    int32_t i,t,count;
   1.147 +    char file[512];
   1.148 +    FILE *out;
   1.149 +    int32_t year = getCurrentYear();
   1.150 +
   1.151 +    uprv_strcpy(file,outputDir);
   1.152 +    if(*outputDir &&  /* don't put a trailing slash if outputDir is empty */ 
   1.153 +        file[strlen(file)-1]!=U_FILE_SEP_CHAR) {
   1.154 +            uprv_strcat(file,U_FILE_SEP_STRING);
   1.155 +    }
   1.156 +    uprv_strcat(file,"DebugUtilitiesData.java");
   1.157 +    out = fopen(file, "w");
   1.158 +    /*puts(file);*/
   1.159 +    printf("%s: Generating %s\n", progname, file);
   1.160 +    if(out == NULL) {
   1.161 +        fprintf(stderr, "%s: Couldn't create resource test file %s\n",
   1.162 +            progname, file);
   1.163 +        return 1;
   1.164 +    }
   1.165 +
   1.166 +    fprintf(out, "/** Copyright (C) 2007-%d, International Business Machines Corporation and Others. All Rights Reserved. **/\n\n", year);
   1.167 +    fprintf(out, "/* NOTE: this file is AUTOMATICALLY GENERATED by gentest.\n"
   1.168 +                 " * See: {ICU4C}/source/data/icu4j-readme.txt for more information. \n"
   1.169 +                 " **/\n\n");
   1.170 +    fprintf(out, "package com.ibm.icu.dev.test.util;\n\n");
   1.171 +    fprintf(out, "public class DebugUtilitiesData extends Object {\n");
   1.172 +    fprintf(out, "    public static final String ICU4C_VERSION=\"%s\";\n", U_ICU_VERSION);
   1.173 +    for(t=0;t<UDBG_ENUM_COUNT;t++) {
   1.174 +        fprintf(out, "    public static final int %s = %d;\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
   1.175 +    }
   1.176 +    fprintf(out, "    public static final String [] TYPES = { \n");
   1.177 +    for(t=0;t<UDBG_ENUM_COUNT;t++) {
   1.178 +        fprintf(out, "        \"%s\", /* %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
   1.179 +    }
   1.180 +    fprintf(out, "    };\n\n");
   1.181 +
   1.182 +    fprintf(out, "    public static final String [][] NAMES = { \n");
   1.183 +    for(t=0;t<UDBG_ENUM_COUNT;t++) {
   1.184 +        count = udbg_enumCount((UDebugEnumType)t);
   1.185 +        fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
   1.186 +        fprintf(out, "        { \n");
   1.187 +        for(i=0;i<count;i++) {
   1.188 +            fprintf(out, 
   1.189 +                "           \"%s\", /* %d */ \n", udbg_enumName((UDebugEnumType)t,i), i);
   1.190 +        }
   1.191 +        fprintf(out, "        },\n");
   1.192 +    }
   1.193 +    fprintf(out, "    };\n\n");
   1.194 +
   1.195 +    fprintf(out, "    public static final int [][] VALUES = { \n");
   1.196 +    for(t=0;t<UDBG_ENUM_COUNT;t++) {
   1.197 +        count = udbg_enumCount((UDebugEnumType)t);
   1.198 +        fprintf(out, "        /* %s, %d */\n", udbg_enumName(UDBG_UDebugEnumType,t), t);
   1.199 +        fprintf(out, "        { \n");
   1.200 +        for(i=0;i<count;i++) {
   1.201 +            fprintf(out, 
   1.202 +                "           ");
   1.203 +            switch(t) {
   1.204 +#if !UCONFIG_NO_FORMATTING
   1.205 +            case UDBG_UCalendarDateFields:
   1.206 +            case UDBG_UCalendarMonths:
   1.207 +                /* Temporary workaround for IS_LEAP_MONTH #6051 */
   1.208 +                if (t == UDBG_UCalendarDateFields && i == 22) {
   1.209 +                  fprintf(out, "com.ibm.icu.util.ChineseCalendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
   1.210 +                } else {
   1.211 +                  fprintf(out, "com.ibm.icu.util.Calendar.%s, /* %d */", udbg_enumName((UDebugEnumType)t,i), i);
   1.212 +                }
   1.213 +                break;
   1.214 +#endif
   1.215 +            case UDBG_UDebugEnumType:
   1.216 +            default:
   1.217 +                fprintf(out,"%d, /* %s */", i, udbg_enumName((UDebugEnumType)t,i));
   1.218 +            }
   1.219 +            fprintf(out,"\n");
   1.220 +        }
   1.221 +        fprintf(out, "        },\n");
   1.222 +    }
   1.223 +    fprintf(out, "    };\n\n");
   1.224 +    fprintf(out, "}\n");
   1.225 +
   1.226 +    fclose(out);
   1.227 +
   1.228 +    return 0;
   1.229 +
   1.230 +}

mercurial