]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/unewdata.c
ICU-6.2.15.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / unewdata.c
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999,2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: unewdata.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 1999oct25
14 * created by: Markus W. Scherer
15 */
16
17 #include "unicode/utypes.h"
18 #include "unicode/putil.h"
19 #include "unicode/ustring.h"
20 #include "cmemory.h"
21 #include "cstring.h"
22 #include "filestrm.h"
23 #include "unicode/udata.h"
24 #include "unewdata.h"
25
26 struct UNewDataMemory {
27 FileStream *file;
28 uint16_t headerSize;
29 uint8_t magic1, magic2;
30 };
31
32 U_CAPI UNewDataMemory * U_EXPORT2
33 udata_create(const char *dir, const char *type, const char *name,
34 const UDataInfo *pInfo,
35 const char *comment,
36 UErrorCode *pErrorCode) {
37 UNewDataMemory *pData;
38 uint16_t headerSize, commentLength;
39 char filename[512];
40 uint8_t bytes[16];
41
42 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
43 return NULL;
44 } else if(name==NULL || *name==0 || pInfo==NULL) {
45 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
46 return NULL;
47 }
48
49 /* allocate the data structure */
50 pData=(UNewDataMemory *)uprv_malloc(sizeof(UNewDataMemory));
51 if(pData==NULL) {
52 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
53 return NULL;
54 }
55
56 /* open the output file */
57 if(dir!=NULL && *dir!=0) { /* if dir has a value, we prepend it to the filename */
58 char *p=filename+strlen(dir);
59 uprv_strcpy(filename, dir);
60 if (*(p-1)!=U_FILE_SEP_CHAR) {
61 *p++=U_FILE_SEP_CHAR;
62 *p=0;
63 }
64 } else { /* otherwise, we'll output to the current dir */
65 filename[0]=0;
66 }
67 uprv_strcat(filename, name);
68 if(type!=NULL && *type!=0) {
69 uprv_strcat(filename, ".");
70 uprv_strcat(filename, type);
71 }
72 pData->file=T_FileStream_open(filename, "wb");
73 if(pData->file==NULL) {
74 uprv_free(pData);
75 *pErrorCode=U_FILE_ACCESS_ERROR;
76 return NULL;
77 }
78
79 /* write the header information */
80 headerSize=(uint16_t)(pInfo->size+4);
81 if(comment!=NULL && *comment!=0) {
82 commentLength=(uint16_t)(uprv_strlen(comment)+1);
83 headerSize+=commentLength;
84 } else {
85 commentLength=0;
86 }
87
88 /* write the size of the header, take padding into account */
89 pData->headerSize=(uint16_t)((headerSize+15)&~0xf);
90 pData->magic1=0xda;
91 pData->magic2=0x27;
92 T_FileStream_write(pData->file, &pData->headerSize, 4);
93
94 /* write the information data */
95 T_FileStream_write(pData->file, pInfo, pInfo->size);
96
97 /* write the comment */
98 if(commentLength>0) {
99 T_FileStream_write(pData->file, comment, commentLength);
100 }
101
102 /* write padding bytes to align the data section to 16 bytes */
103 headerSize&=0xf;
104 if(headerSize!=0) {
105 headerSize=(uint16_t)(16-headerSize);
106 uprv_memset(bytes, 0, headerSize);
107 T_FileStream_write(pData->file, bytes, headerSize);
108 }
109
110 return pData;
111 }
112
113 U_CAPI uint32_t U_EXPORT2
114 udata_finish(UNewDataMemory *pData, UErrorCode *pErrorCode) {
115 uint32_t fileLength=0;
116
117 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
118 return 0;
119 }
120
121 if(pData!=NULL) {
122 if(pData->file!=NULL) {
123 /* fflush(pData->file);*/
124 fileLength=T_FileStream_size(pData->file);
125 if(T_FileStream_error(pData->file)) {
126 *pErrorCode=U_FILE_ACCESS_ERROR;
127 } else {
128 fileLength-=pData->headerSize;
129 }
130 T_FileStream_close(pData->file);
131 }
132 uprv_free(pData);
133 }
134
135 return fileLength;
136 }
137
138 U_CAPI void U_EXPORT2
139 udata_write8(UNewDataMemory *pData, uint8_t byte) {
140 if(pData!=NULL && pData->file!=NULL) {
141 T_FileStream_write(pData->file, &byte, 1);
142 }
143 }
144
145 U_CAPI void U_EXPORT2
146 udata_write16(UNewDataMemory *pData, uint16_t word) {
147 if(pData!=NULL && pData->file!=NULL) {
148 T_FileStream_write(pData->file, &word, 2);
149 }
150 }
151
152 U_CAPI void U_EXPORT2
153 udata_write32(UNewDataMemory *pData, uint32_t wyde) {
154 if(pData!=NULL && pData->file!=NULL) {
155 T_FileStream_write(pData->file, &wyde, 4);
156 }
157 }
158
159 U_CAPI void U_EXPORT2
160 udata_writeBlock(UNewDataMemory *pData, const void *s, int32_t length) {
161 if(pData!=NULL && pData->file!=NULL) {
162 if(length>0) {
163 T_FileStream_write(pData->file, s, length);
164 }
165 }
166 }
167
168 U_CAPI void U_EXPORT2
169 udata_writePadding(UNewDataMemory *pData, int32_t length) {
170 static const uint8_t padding[16]={
171 0xaa, 0xaa, 0xaa, 0xaa,
172 0xaa, 0xaa, 0xaa, 0xaa,
173 0xaa, 0xaa, 0xaa, 0xaa,
174 0xaa, 0xaa, 0xaa, 0xaa
175 };
176 if(pData!=NULL && pData->file!=NULL) {
177 while(length>=16) {
178 T_FileStream_write(pData->file, padding, 16);
179 length-=16;
180 }
181 if(length>0) {
182 T_FileStream_write(pData->file, padding, length);
183 }
184 }
185 }
186
187 U_CAPI void U_EXPORT2
188 udata_writeString(UNewDataMemory *pData, const char *s, int32_t length) {
189 if(pData!=NULL && pData->file!=NULL) {
190 if(length==-1) {
191 length=(int32_t)uprv_strlen(s);
192 }
193 if(length>0) {
194 T_FileStream_write(pData->file, s, length);
195 }
196 }
197 }
198
199 U_CAPI void U_EXPORT2
200 udata_writeUString(UNewDataMemory *pData, const UChar *s, int32_t length) {
201 if(pData!=NULL && pData->file!=NULL) {
202 if(length==-1) {
203 length=u_strlen(s);
204 }
205 if(length>0) {
206 T_FileStream_write(pData->file, s, length*sizeof(UChar));
207 }
208 }
209 }
210
211 /*
212 * Hey, Emacs, please set the following:
213 *
214 * Local Variables:
215 * indent-tabs-mode: nil
216 * End:
217 *
218 */
219