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