1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: writer.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2000sep5
14 * created by: Vladimir Weinstein
15 */
16
17 /******************************************************************************
18 * A program to write simple binary data readable by udata - example for
19 * ICU workshop
20 ******************************************************************************/
21
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifdef WIN32
26 #include <direct.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include "unicode/utypes.h"
31 #include "unicode/udata.h"
32
33 /* this is private - available only through toolutil */
34 #include "unewdata.h"
35
36 #define DATA_NAME "mypkg_example"
37 #define DATA_TYPE "dat"
38
39 /* UDataInfo cf. udata.h */
40 static const UDataInfo dataInfo={
41 sizeof(UDataInfo),
42 0,
43
44 U_IS_BIG_ENDIAN,
45 U_CHARSET_FAMILY,
46 sizeof(UChar),
47 0,
48
49 0x4D, 0x79, 0x44, 0x74, /* dataFormat="MyDt" */
50 1, 0, 0, 0, /* formatVersion */
51 1, 0, 0, 0 /* dataVersion */
52 };
53
54
55 /* Excersise: add writing out other data types */
56 /* see icu/source/tools/toolutil/unewdata.h */
57 /* for other possibilities */
58
59 extern int
main(int argc,const char * argv[])60 main(int argc, const char *argv[]) {
61 UNewDataMemory *pData;
62 UErrorCode errorCode=U_ZERO_ERROR;
63 char stringValue[]={'E', 'X', 'A', 'M', 'P', 'L', 'E', '\0'};
64 uint16_t intValue=2000;
65
66 long dataLength;
67 uint32_t size;
68 #ifdef WIN32
69 char *currdir = _getcwd(NULL, 0);
70 #else
71 char *currdir = getcwd(NULL, 0);
72 #endif
73
74 pData=udata_create(currdir, DATA_TYPE, DATA_NAME, &dataInfo,
75 U_COPYRIGHT_STRING, &errorCode);
76
77 if(currdir != NULL) {
78 free(currdir);
79 }
80
81
82 if(U_FAILURE(errorCode)) {
83 fprintf(stderr, "Error: unable to create data memory, error %d\n", errorCode);
84 exit(errorCode);
85 }
86
87 /* write the data to the file */
88 /* a 16 bit value and a String*/
89 printf("Writing uint16_t value of %d\n", intValue);
90 udata_write16(pData, intValue);
91 printf("Writing string value of %s\n", stringValue);
92 udata_writeString(pData, stringValue, sizeof(stringValue));
93
94 /* finish up */
95 dataLength=udata_finish(pData, &errorCode);
96 if(U_FAILURE(errorCode)) {
97 fprintf(stderr, "Error: error %d writing the output file\n", errorCode);
98 exit(errorCode);
99 }
100 size=sizeof(stringValue) + sizeof(intValue);
101
102
103 if(dataLength!=(long)size) {
104 fprintf(stderr, "Error: data length %ld != calculated size %lu\n", dataLength, size);
105 exit(U_INTERNAL_PROGRAM_ERROR);
106 }
107 return 0;
108 }
109
110
111
112
113
114
115
116
117
118
119
120