2 *******************************************************************************
4 * Copyright (C) 1999,2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: unewdata.c
10 * tab size: 8 (not used)
13 * created on: 1999oct25
14 * created by: Markus W. Scherer
17 #include "unicode/utypes.h"
18 #include "unicode/putil.h"
19 #include "unicode/ustring.h"
23 #include "unicode/udata.h"
26 struct UNewDataMemory
{
29 uint8_t magic1
, magic2
;
32 U_CAPI UNewDataMemory
* U_EXPORT2
33 udata_create(const char *dir
, const char *type
, const char *name
,
34 const UDataInfo
*pInfo
,
36 UErrorCode
*pErrorCode
) {
37 UNewDataMemory
*pData
;
38 uint16_t headerSize
, commentLength
;
42 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
44 } else if(name
==NULL
|| *name
==0 || pInfo
==NULL
) {
45 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
49 /* allocate the data structure */
50 pData
=(UNewDataMemory
*)uprv_malloc(sizeof(UNewDataMemory
));
52 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
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
) {
64 } else { /* otherwise, we'll output to the current dir */
67 uprv_strcat(filename
, name
);
68 if(type
!=NULL
&& *type
!=0) {
69 uprv_strcat(filename
, ".");
70 uprv_strcat(filename
, type
);
72 pData
->file
=T_FileStream_open(filename
, "wb");
73 if(pData
->file
==NULL
) {
75 *pErrorCode
=U_FILE_ACCESS_ERROR
;
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
;
88 /* write the size of the header, take padding into account */
89 pData
->headerSize
=(uint16_t)((headerSize
+15)&~0xf);
92 T_FileStream_write(pData
->file
, &pData
->headerSize
, 4);
94 /* write the information data */
95 T_FileStream_write(pData
->file
, pInfo
, pInfo
->size
);
97 /* write the comment */
99 T_FileStream_write(pData
->file
, comment
, commentLength
);
102 /* write padding bytes to align the data section to 16 bytes */
105 headerSize
=(uint16_t)(16-headerSize
);
106 uprv_memset(bytes
, 0, headerSize
);
107 T_FileStream_write(pData
->file
, bytes
, headerSize
);
113 U_CAPI
uint32_t U_EXPORT2
114 udata_finish(UNewDataMemory
*pData
, UErrorCode
*pErrorCode
) {
115 uint32_t fileLength
=0;
117 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
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
;
128 fileLength
-=pData
->headerSize
;
130 T_FileStream_close(pData
->file
);
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);
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);
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);
159 U_CAPI
void U_EXPORT2
160 udata_writeBlock(UNewDataMemory
*pData
, const void *s
, int32_t length
) {
161 if(pData
!=NULL
&& pData
->file
!=NULL
) {
163 T_FileStream_write(pData
->file
, s
, length
);
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
176 if(pData
!=NULL
&& pData
->file
!=NULL
) {
178 T_FileStream_write(pData
->file
, padding
, 16);
182 T_FileStream_write(pData
->file
, padding
, length
);
187 U_CAPI
void U_EXPORT2
188 udata_writeString(UNewDataMemory
*pData
, const char *s
, int32_t length
) {
189 if(pData
!=NULL
&& pData
->file
!=NULL
) {
191 length
=(int32_t)uprv_strlen(s
);
194 T_FileStream_write(pData
->file
, s
, length
);
199 U_CAPI
void U_EXPORT2
200 udata_writeUString(UNewDataMemory
*pData
, const UChar
*s
, int32_t length
) {
201 if(pData
!=NULL
&& pData
->file
!=NULL
) {
206 T_FileStream_write(pData
->file
, s
, length
*sizeof(UChar
));
212 * Hey, Emacs, please set the following:
215 * indent-tabs-mode: nil