• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   © 2016 and later: Unicode, Inc. and others.
5 *   License & terms of use: http://www.unicode.org/copyright.html
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 *   Copyright (C) 1999-2006, International Business Machines
11 *   Corporation and others.  All Rights Reserved.
12 *
13 *******************************************************************************
14 *   file name:  writer.c
15 *   encoding:   UTF-8
16 *   tab size:   8 (not used)
17 *   indentation:4
18 *
19 *   created on: 2000sep5
20 *   created by: Vladimir Weinstein
21 */
22 
23 /******************************************************************************
24  * A program to write simple binary data readable by udata - example for
25  * ICU workshop
26  ******************************************************************************/
27 
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #ifdef WIN32
32 #include <direct.h>
33 #else
34 #include <unistd.h>
35 #endif
36 #include "unicode/utypes.h"
37 #include "unicode/udata.h"
38 
39 /* this is private - available only through toolutil */
40 #include "unewdata.h"
41 
42 #define DATA_NAME "mypkg_example"
43 #define DATA_TYPE "dat"
44 
45 /* UDataInfo cf. udata.h */
46 static const UDataInfo dataInfo={
47     sizeof(UDataInfo),
48     0,
49 
50     U_IS_BIG_ENDIAN,
51     U_CHARSET_FAMILY,
52     sizeof(UChar),
53     0,
54 
55     0x4D, 0x79, 0x44, 0x74,     /* dataFormat="MyDt" */
56     1, 0, 0, 0,                 /* formatVersion */
57     1, 0, 0, 0                  /* dataVersion */
58 };
59 
60 
61 /* Excersise: add writing out other data types */
62 /* see icu/source/tools/toolutil/unewdata.h    */
63 /* for other possibilities                     */
64 
65 extern int
main(int argc,const char * argv[])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;
71 
72     long dataLength;
73     size_t size;
74 #ifdef WIN32
75     char *currdir = _getcwd(NULL, 0);
76 #else
77     char *currdir = getcwd(NULL, 0);
78 #endif
79 
80     pData=udata_create(currdir, DATA_TYPE, DATA_NAME, &dataInfo,
81                        U_COPYRIGHT_STRING, &errorCode);
82 
83     if(currdir != NULL) {
84         free(currdir);
85     }
86 
87 
88     if(U_FAILURE(errorCode)) {
89         fprintf(stderr, "Error: unable to create data memory, error %d\n", errorCode);
90         exit(errorCode);
91     }
92 
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));
99 
100     /* finish up */
101     dataLength=udata_finish(pData, &errorCode);
102     if(U_FAILURE(errorCode)) {
103         fprintf(stderr, "Error: error %d writing the output file\n", errorCode);
104         exit(errorCode);
105     }
106     size=sizeof(stringValue) + sizeof(intValue);
107 
108 
109     if(dataLength!=(long)size) {
110         fprintf(stderr, "Error: data length %ld != calculated size %zu\n", dataLength, size);
111         exit(U_INTERNAL_PROGRAM_ERROR);
112     }
113     return 0;
114 }
115