michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2010, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: gentest.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2000mar03 michael@0: * created by: Madhu Katragadda michael@0: * michael@0: * This program writes a little data file for testing the udata API. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/uclean.h" michael@0: #include "unicode/udata.h" michael@0: #include "udbgutil.h" michael@0: #include "unewdata.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uoptions.h" michael@0: #include "gentest.h" michael@0: #include "toolutil.h" michael@0: michael@0: #define DATA_NAME "test" michael@0: #define DATA_TYPE "icu" michael@0: michael@0: /* UDataInfo cf. udata.h */ michael@0: static const UDataInfo dataInfo={ michael@0: sizeof(UDataInfo), michael@0: 0, michael@0: michael@0: U_IS_BIG_ENDIAN, michael@0: U_CHARSET_FAMILY, michael@0: sizeof(UChar), michael@0: 0, michael@0: michael@0: {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */ michael@0: {1, 0, 0, 0}, /* formatVersion */ michael@0: {1, 0, 0, 0} /* dataVersion */ michael@0: }; michael@0: michael@0: static void createData(const char*, UErrorCode *); michael@0: michael@0: static int outputJavaStuff(const char * progname, const char *outputDir); michael@0: michael@0: static UOption options[]={ michael@0: /*0*/ UOPTION_HELP_H, michael@0: /*1*/ UOPTION_HELP_QUESTION_MARK, michael@0: /*2*/ UOPTION_DESTDIR, michael@0: /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG), michael@0: /*4*/ UOPTION_DEF("javastuff", 'j', UOPT_NO_ARG), michael@0: }; michael@0: michael@0: extern int michael@0: main(int argc, char* argv[]) { michael@0: UErrorCode errorCode = U_ZERO_ERROR; michael@0: michael@0: /* preset then read command line options */ michael@0: options[2].value=u_getDataDirectory(); michael@0: argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); michael@0: michael@0: /* error handling, printing usage message */ michael@0: if(argc<0) { michael@0: fprintf(stderr, michael@0: "error in command line argument \"%s\"\n", michael@0: argv[-argc]); michael@0: } michael@0: if(argc<0 || options[0].doesOccur || options[1].doesOccur) { michael@0: fprintf(stderr, michael@0: "usage: %s [-options]\n" michael@0: "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n" michael@0: "\toptions:\n" michael@0: "\t\t-h or -? or --help this usage text\n" michael@0: "\t\t-d or --destdir destination directory, followed by the path\n" michael@0: "\t\t-r or --genres generate resource file testtable32.txt instead of UData test \n" michael@0: "\t\t-j or --javastuff generate Java source for DebugUtilities. \n", michael@0: argv[0]); michael@0: return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; michael@0: } michael@0: michael@0: if( options[4].doesOccur ) { michael@0: return outputJavaStuff( argv[0], options[2].value ); michael@0: } else if ( options[3].doesOccur ) { michael@0: return genres32( argv[0], options[2].value ); michael@0: } else { michael@0: /* printf("Generating the test memory mapped file\n"); */ michael@0: createData(options[2].value, &errorCode); michael@0: } michael@0: return U_FAILURE(errorCode); michael@0: } michael@0: michael@0: /* Create data file ----------------------------------------------------- */ michael@0: static void michael@0: createData(const char* outputDirectory, UErrorCode *errorCode) { michael@0: UNewDataMemory *pData; michael@0: char stringValue[]={'Y', 'E', 'A', 'R', '\0'}; michael@0: uint16_t intValue=2000; michael@0: michael@0: long dataLength; michael@0: uint32_t size; michael@0: michael@0: pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo, michael@0: U_COPYRIGHT_STRING, errorCode); michael@0: if(U_FAILURE(*errorCode)) { michael@0: fprintf(stderr, "gentest: unable to create data memory, %s\n", u_errorName(*errorCode)); michael@0: exit(*errorCode); michael@0: } michael@0: michael@0: /* write the data to the file */ michael@0: /* a 16 bit value and a String*/ michael@0: udata_write16(pData, intValue); michael@0: udata_writeString(pData, stringValue, sizeof(stringValue)); michael@0: michael@0: /* finish up */ michael@0: dataLength=udata_finish(pData, errorCode); michael@0: if(U_FAILURE(*errorCode)) { michael@0: fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode); michael@0: exit(*errorCode); michael@0: } michael@0: size=sizeof(stringValue) + sizeof(intValue); michael@0: michael@0: michael@0: if(dataLength!=(long)size) { michael@0: fprintf(stderr, "gentest: data length %ld != calculated size %lu\n", michael@0: dataLength, (unsigned long)size); michael@0: exit(U_INTERNAL_PROGRAM_ERROR); michael@0: } michael@0: } michael@0: michael@0: /* Create Java file ----------------------------------------------------- */ michael@0: michael@0: static int michael@0: outputJavaStuff(const char* progname, const char *outputDir) { michael@0: int32_t i,t,count; michael@0: char file[512]; michael@0: FILE *out; michael@0: int32_t year = getCurrentYear(); michael@0: michael@0: uprv_strcpy(file,outputDir); michael@0: if(*outputDir && /* don't put a trailing slash if outputDir is empty */ michael@0: file[strlen(file)-1]!=U_FILE_SEP_CHAR) { michael@0: uprv_strcat(file,U_FILE_SEP_STRING); michael@0: } michael@0: uprv_strcat(file,"DebugUtilitiesData.java"); michael@0: out = fopen(file, "w"); michael@0: /*puts(file);*/ michael@0: printf("%s: Generating %s\n", progname, file); michael@0: if(out == NULL) { michael@0: fprintf(stderr, "%s: Couldn't create resource test file %s\n", michael@0: progname, file); michael@0: return 1; michael@0: } michael@0: michael@0: fprintf(out, "/** Copyright (C) 2007-%d, International Business Machines Corporation and Others. All Rights Reserved. **/\n\n", year); michael@0: fprintf(out, "/* NOTE: this file is AUTOMATICALLY GENERATED by gentest.\n" michael@0: " * See: {ICU4C}/source/data/icu4j-readme.txt for more information. \n" michael@0: " **/\n\n"); michael@0: fprintf(out, "package com.ibm.icu.dev.test.util;\n\n"); michael@0: fprintf(out, "public class DebugUtilitiesData extends Object {\n"); michael@0: fprintf(out, " public static final String ICU4C_VERSION=\"%s\";\n", U_ICU_VERSION); michael@0: for(t=0;t