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