]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/udata/writer.c
2 *******************************************************************************
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 1999-2006, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
16 * tab size: 8 (not used)
19 * created on: 2000sep5
20 * created by: Vladimir Weinstein
23 /******************************************************************************
24 * A program to write simple binary data readable by udata - example for
26 ******************************************************************************/
36 #include "unicode/utypes.h"
37 #include "unicode/udata.h"
39 /* this is private - available only through toolutil */
42 #define DATA_NAME "mypkg_example"
43 #define DATA_TYPE "dat"
45 /* UDataInfo cf. udata.h */
46 static const UDataInfo dataInfo
={
55 0x4D, 0x79, 0x44, 0x74, /* dataFormat="MyDt" */
56 1, 0, 0, 0, /* formatVersion */
57 1, 0, 0, 0 /* dataVersion */
61 /* Excersise: add writing out other data types */
62 /* see icu/source/tools/toolutil/unewdata.h */
63 /* for other possibilities */
66 main(int argc
, const char *argv
[]) {
67 UNewDataMemory
*pData
;
68 UErrorCode errorCode
=U_ZERO_ERROR
;
69 char stringValue
[]={'E', 'X', 'A', 'M', 'P', 'L', 'E', '\0'};
70 uint16_t intValue
=2000;
75 char *currdir
= _getcwd(NULL
, 0);
77 char *currdir
= getcwd(NULL
, 0);
80 pData
=udata_create(currdir
, DATA_TYPE
, DATA_NAME
, &dataInfo
,
81 U_COPYRIGHT_STRING
, &errorCode
);
88 if(U_FAILURE(errorCode
)) {
89 fprintf(stderr
, "Error: unable to create data memory, error %d\n", errorCode
);
93 /* write the data to the file */
94 /* a 16 bit value and a String*/
95 printf("Writing uint16_t value of %d\n", intValue
);
96 udata_write16(pData
, intValue
);
97 printf("Writing string value of %s\n", stringValue
);
98 udata_writeString(pData
, stringValue
, sizeof(stringValue
));
101 dataLength
=udata_finish(pData
, &errorCode
);
102 if(U_FAILURE(errorCode
)) {
103 fprintf(stderr
, "Error: error %d writing the output file\n", errorCode
);
106 size
=sizeof(stringValue
) + sizeof(intValue
);
109 if(dataLength
!=(long)size
) {
110 fprintf(stderr
, "Error: data length %ld != calculated size %lu\n", dataLength
, size
);
111 exit(U_INTERNAL_PROGRAM_ERROR
);