]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/gentest/gentest.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / tools / gentest / gentest.c
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2006, 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 "unewdata.h"
26 #include "cmemory.h"
27 #include "cstring.h"
28 #include "uoptions.h"
29 #include "gentest.h"
30
31 #define DATA_NAME "test"
32 #define DATA_TYPE "icu"
33
34 /* UDataInfo cf. udata.h */
35 static const UDataInfo dataInfo={
36 sizeof(UDataInfo),
37 0,
38
39 U_IS_BIG_ENDIAN,
40 U_CHARSET_FAMILY,
41 sizeof(UChar),
42 0,
43
44 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
45 {1, 0, 0, 0}, /* formatVersion */
46 {1, 0, 0, 0} /* dataVersion */
47 };
48
49 static void createData(const char*, UErrorCode *);
50
51 static UOption options[]={
52 /*0*/ UOPTION_HELP_H,
53 /*1*/ UOPTION_HELP_QUESTION_MARK,
54 /*2*/ UOPTION_DESTDIR,
55 /*3*/ UOPTION_DEF("genres", 'r', UOPT_NO_ARG)
56 };
57
58 extern int
59 main(int argc, char* argv[]) {
60 UErrorCode errorCode = U_ZERO_ERROR;
61
62 /* preset then read command line options */
63 options[2].value=u_getDataDirectory();
64 argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
65
66 /* error handling, printing usage message */
67 if(argc<0) {
68 fprintf(stderr,
69 "error in command line argument \"%s\"\n",
70 argv[-argc]);
71 }
72 if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
73 fprintf(stderr,
74 "usage: %s [-options]\n"
75 "\tcreate the test file " DATA_NAME "." DATA_TYPE " unless the -r option is given.\n"
76 "\toptions:\n"
77 "\t\t-h or -? or --help this usage text\n"
78 "\t\t-d or --destdir destination directory, followed by the path\n"
79 "\t\t-r or --genres generate resource file testtable32.txt instead of UData test \n",
80 argv[0]);
81 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
82 }
83
84 if ( options[3].doesOccur ) {
85 return genres32( argv[0], options[2].value );
86 } else {
87 /* printf("Generating the test memory mapped file\n"); */
88 createData(options[2].value, &errorCode);
89 }
90 return U_FAILURE(errorCode);
91 }
92
93 /* Create data file ----------------------------------------------------- */
94 static void
95 createData(const char* outputDirectory, UErrorCode *errorCode) {
96 UNewDataMemory *pData;
97 char stringValue[]={'Y', 'E', 'A', 'R', '\0'};
98 uint16_t intValue=2000;
99
100 long dataLength;
101 uint32_t size;
102
103 pData=udata_create(outputDirectory, DATA_TYPE, DATA_NAME, &dataInfo,
104 U_COPYRIGHT_STRING, errorCode);
105 if(U_FAILURE(*errorCode)) {
106 fprintf(stderr, "gentest: unable to create data memory, error %d\n", *errorCode);
107 exit(*errorCode);
108 }
109
110 /* write the data to the file */
111 /* a 16 bit value and a String*/
112 udata_write16(pData, intValue);
113 udata_writeString(pData, stringValue, sizeof(stringValue));
114
115 /* finish up */
116 dataLength=udata_finish(pData, errorCode);
117 if(U_FAILURE(*errorCode)) {
118 fprintf(stderr, "gentest: error %d writing the output file\n", *errorCode);
119 exit(*errorCode);
120 }
121 size=sizeof(stringValue) + sizeof(intValue);
122
123
124 if(dataLength!=(long)size) {
125 fprintf(stderr, "gentest: data length %ld != calculated size %lu\n",
126 dataLength, (unsigned long)size);
127 exit(U_INTERNAL_PROGRAM_ERROR);
128 }
129 }