2 *******************************************************************************
4 * Copyright (C) 1999,2008, 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
;
43 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
45 } else if(name
==NULL
|| *name
==0 || pInfo
==NULL
) {
46 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
50 /* allocate the data structure */
51 pData
=(UNewDataMemory
*)uprv_malloc(sizeof(UNewDataMemory
));
53 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
57 /* Check that the full path won't be too long */
58 length
= 0; /* Start with nothing */
59 if(dir
!= NULL
&& *dir
!=0) /* Add directory length if one was given */
61 length
+= strlen(dir
);
63 /* Add 1 if dir doesn't end with path sep */
64 if (dir
[strlen(dir
) - 1]!= U_FILE_SEP_CHAR
) {
68 length
+= strlen(name
); /* Add the filename length */
70 if(type
!= NULL
&& *type
!=0) { /* Add directory length if given */
71 length
+= strlen(type
);
75 /* LDH buffer Length error check */
76 if(length
> (sizeof(filename
) - 1))
78 *pErrorCode
= U_BUFFER_OVERFLOW_ERROR
;
83 /* open the output file */
84 if(dir
!=NULL
&& *dir
!=0) { /* if dir has a value, we prepend it to the filename */
85 char *p
=filename
+strlen(dir
);
86 uprv_strcpy(filename
, dir
);
87 if (*(p
-1)!=U_FILE_SEP_CHAR
) {
91 } else { /* otherwise, we'll output to the current dir */
94 uprv_strcat(filename
, name
);
95 if(type
!=NULL
&& *type
!=0) {
96 uprv_strcat(filename
, ".");
97 uprv_strcat(filename
, type
);
99 pData
->file
=T_FileStream_open(filename
, "wb");
100 if(pData
->file
==NULL
) {
102 *pErrorCode
=U_FILE_ACCESS_ERROR
;
106 /* write the header information */
107 headerSize
=(uint16_t)(pInfo
->size
+4);
108 if(comment
!=NULL
&& *comment
!=0) {
109 commentLength
=(uint16_t)(uprv_strlen(comment
)+1);
110 headerSize
+=commentLength
;
115 /* write the size of the header, take padding into account */
116 pData
->headerSize
=(uint16_t)((headerSize
+15)&~0xf);
119 T_FileStream_write(pData
->file
, &pData
->headerSize
, 4);
121 /* write the information data */
122 T_FileStream_write(pData
->file
, pInfo
, pInfo
->size
);
124 /* write the comment */
125 if(commentLength
>0) {
126 T_FileStream_write(pData
->file
, comment
, commentLength
);
129 /* write padding bytes to align the data section to 16 bytes */
132 headerSize
=(uint16_t)(16-headerSize
);
133 uprv_memset(bytes
, 0, headerSize
);
134 T_FileStream_write(pData
->file
, bytes
, headerSize
);
140 U_CAPI
uint32_t U_EXPORT2
141 udata_finish(UNewDataMemory
*pData
, UErrorCode
*pErrorCode
) {
142 uint32_t fileLength
=0;
144 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
149 if(pData
->file
!=NULL
) {
150 /* fflush(pData->file);*/
151 fileLength
=T_FileStream_size(pData
->file
);
152 if(T_FileStream_error(pData
->file
)) {
153 *pErrorCode
=U_FILE_ACCESS_ERROR
;
155 fileLength
-=pData
->headerSize
;
157 T_FileStream_close(pData
->file
);
165 U_CAPI
void U_EXPORT2
166 udata_write8(UNewDataMemory
*pData
, uint8_t byte
) {
167 if(pData
!=NULL
&& pData
->file
!=NULL
) {
168 T_FileStream_write(pData
->file
, &byte
, 1);
172 U_CAPI
void U_EXPORT2
173 udata_write16(UNewDataMemory
*pData
, uint16_t word
) {
174 if(pData
!=NULL
&& pData
->file
!=NULL
) {
175 T_FileStream_write(pData
->file
, &word
, 2);
179 U_CAPI
void U_EXPORT2
180 udata_write32(UNewDataMemory
*pData
, uint32_t wyde
) {
181 if(pData
!=NULL
&& pData
->file
!=NULL
) {
182 T_FileStream_write(pData
->file
, &wyde
, 4);
186 U_CAPI
void U_EXPORT2
187 udata_writeBlock(UNewDataMemory
*pData
, const void *s
, int32_t length
) {
188 if(pData
!=NULL
&& pData
->file
!=NULL
) {
190 T_FileStream_write(pData
->file
, s
, length
);
195 U_CAPI
void U_EXPORT2
196 udata_writePadding(UNewDataMemory
*pData
, int32_t length
) {
197 static const uint8_t padding
[16]={
198 0xaa, 0xaa, 0xaa, 0xaa,
199 0xaa, 0xaa, 0xaa, 0xaa,
200 0xaa, 0xaa, 0xaa, 0xaa,
201 0xaa, 0xaa, 0xaa, 0xaa
203 if(pData
!=NULL
&& pData
->file
!=NULL
) {
205 T_FileStream_write(pData
->file
, padding
, 16);
209 T_FileStream_write(pData
->file
, padding
, length
);
214 U_CAPI
void U_EXPORT2
215 udata_writeString(UNewDataMemory
*pData
, const char *s
, int32_t length
) {
216 if(pData
!=NULL
&& pData
->file
!=NULL
) {
218 length
=(int32_t)uprv_strlen(s
);
221 T_FileStream_write(pData
->file
, s
, length
);
226 U_CAPI
void U_EXPORT2
227 udata_writeUString(UNewDataMemory
*pData
, const UChar
*s
, int32_t length
) {
228 if(pData
!=NULL
&& pData
->file
!=NULL
) {
233 T_FileStream_write(pData
->file
, s
, length
*sizeof(UChar
));
239 * Hey, Emacs, please set the following:
242 * indent-tabs-mode: nil