]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 1998-2003, International Business Machines | |
5 | * Corporation and others. All Rights Reserved. | |
6 | * | |
7 | ******************************************************************************* | |
8 | * | |
9 | * File ucbuf.c | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 05/10/01 Ram Creation. | |
15 | ******************************************************************************* | |
16 | */ | |
17 | ||
18 | #include "unicode/utypes.h" | |
19 | #include "unicode/ucnv.h" | |
20 | #include "unicode/ucnv_err.h" | |
21 | #include "filestrm.h" | |
22 | #include "cstring.h" | |
23 | #include "cmemory.h" | |
24 | #include "ustrfmt.h" | |
25 | #include "unicode/ustring.h" | |
26 | #include "unicode/uchar.h" | |
27 | #include "ucbuf.h" | |
28 | #include <stdio.h> | |
29 | ||
30 | #define MAX_IN_BUF 1000 | |
31 | #define MAX_U_BUF 1500 | |
32 | #define CONTEXT_LEN 15 | |
33 | ||
34 | struct UCHARBUF { | |
35 | UChar* buffer; | |
36 | UChar* currentPos; | |
37 | UChar* bufLimit; | |
38 | int32_t bufCapacity; | |
39 | int32_t remaining; | |
40 | int32_t signatureLength; | |
41 | FileStream* in; | |
42 | UConverter* conv; | |
43 | UBool showWarning; /* makes this API not produce any errors */ | |
44 | UBool isBuffered; | |
45 | }; | |
46 | ||
47 | U_CAPI UBool U_EXPORT2 | |
48 | ucbuf_autodetect_fs(FileStream* in, const char** cp, UConverter** conv, int32_t* signatureLength, UErrorCode* error){ | |
49 | char start[8]; | |
50 | int32_t numRead; | |
51 | ||
52 | UChar target[1]={ 0 }; | |
53 | UChar* pTarget; | |
54 | const char* pStart; | |
55 | ||
56 | /* read a few bytes */ | |
57 | numRead=T_FileStream_read(in, start, sizeof(start)); | |
58 | ||
59 | *cp = ucnv_detectUnicodeSignature(start, numRead, signatureLength, error); | |
60 | ||
61 | /* unread the bytes beyond what was consumed for U+FEFF */ | |
62 | T_FileStream_rewind(in); | |
63 | if (*signatureLength > 0) { | |
64 | numRead = T_FileStream_read(in, start, *signatureLength); | |
65 | } | |
66 | ||
67 | if(*cp==NULL){ | |
68 | *conv =NULL; | |
69 | return FALSE; | |
70 | } | |
71 | ||
72 | /* open the converter for the detected Unicode charset */ | |
73 | *conv = ucnv_open(*cp,error); | |
74 | ||
75 | /* convert and ignore initial U+FEFF, and the buffer overflow */ | |
76 | pTarget = target; | |
77 | pStart = start; | |
78 | ucnv_toUnicode(*conv, &pTarget, target+1, &pStart, start+*signatureLength, NULL, FALSE, error); | |
79 | *signatureLength = pStart - start; | |
80 | if(*error==U_BUFFER_OVERFLOW_ERROR) { | |
81 | *error=U_ZERO_ERROR; | |
82 | } | |
83 | ||
84 | /* verify that we successfully read exactly U+FEFF */ | |
85 | if(U_SUCCESS(*error) && (pTarget!=(target+1) || target[0]!=0xfeff)) { | |
86 | *error=U_INTERNAL_PROGRAM_ERROR; | |
87 | } | |
88 | ||
89 | ||
90 | ||
91 | return TRUE; | |
92 | } | |
93 | static UBool ucbuf_isCPKnown(const char* cp){ | |
94 | if(ucnv_compareNames("UTF-8",cp)==0){ | |
95 | return TRUE; | |
96 | } | |
97 | if(ucnv_compareNames("UTF-16BE",cp)==0){ | |
98 | return TRUE; | |
99 | } | |
100 | if(ucnv_compareNames("UTF-16LE",cp)==0){ | |
101 | return TRUE; | |
102 | } | |
103 | if(ucnv_compareNames("UTF-16",cp)==0){ | |
104 | return TRUE; | |
105 | } | |
106 | if(ucnv_compareNames("UTF-32",cp)==0){ | |
107 | return TRUE; | |
108 | } | |
109 | if(ucnv_compareNames("UTF-32BE",cp)==0){ | |
110 | return TRUE; | |
111 | } | |
112 | if(ucnv_compareNames("UTF-32LE",cp)==0){ | |
113 | return TRUE; | |
114 | } | |
115 | if(ucnv_compareNames("UTF-32BE",cp)==0){ | |
116 | return TRUE; | |
117 | } | |
118 | if(ucnv_compareNames("SCSU",cp)==0){ | |
119 | return TRUE; | |
120 | } | |
121 | if(ucnv_compareNames("BOCU",cp)==0){ | |
122 | return TRUE; | |
123 | } | |
124 | if(ucnv_compareNames("UTF-7",cp)==0){ | |
125 | return TRUE; | |
126 | } | |
127 | return FALSE; | |
128 | } | |
129 | ||
130 | U_CAPI FileStream * U_EXPORT2 | |
131 | ucbuf_autodetect(const char* fileName, const char** cp,UConverter** conv, int32_t* signatureLength,UErrorCode* error){ | |
132 | FileStream* in=NULL; | |
133 | if(error==NULL || U_FAILURE(*error)){ | |
134 | return NULL; | |
135 | } | |
136 | if(conv==NULL || cp==NULL || fileName==NULL){ | |
137 | *error = U_ILLEGAL_ARGUMENT_ERROR; | |
138 | return NULL; | |
139 | } | |
140 | /* open the file */ | |
141 | in= T_FileStream_open(fileName,"rb"); | |
142 | ||
143 | if(in == NULL){ | |
144 | *error=U_FILE_ACCESS_ERROR; | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | if(ucbuf_autodetect_fs(in,cp,conv,signatureLength,error)) { | |
149 | return in; | |
150 | } else { | |
151 | ucnv_close(*conv); | |
152 | *conv=NULL; | |
153 | T_FileStream_close(in); | |
154 | return NULL; | |
155 | } | |
156 | } | |
157 | ||
158 | /* fill the uchar buffer */ | |
159 | static UCHARBUF* | |
160 | ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* error){ | |
161 | UChar* pTarget=NULL; | |
162 | UChar* target=NULL; | |
163 | const char* source=NULL; | |
164 | char carr[MAX_IN_BUF] = {'\0'}; | |
165 | char* cbuf = carr; | |
166 | int32_t inputRead=0; | |
167 | int32_t outputWritten=0; | |
168 | int32_t offset=0; | |
169 | const char* sourceLimit =NULL; | |
170 | int32_t cbufSize=0; | |
171 | pTarget = buf->buffer; | |
172 | /* check if we arrived here without exhausting the buffer*/ | |
173 | if(buf->currentPos<buf->bufLimit){ | |
174 | offset = (int32_t)(buf->bufLimit-buf->currentPos); | |
175 | memmove(buf->buffer,buf->currentPos,offset* sizeof(UChar)); | |
176 | } | |
177 | ||
178 | #if DEBUG | |
179 | memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_IN_BUF-offset)); | |
180 | #endif | |
181 | if(buf->isBuffered){ | |
182 | cbufSize = MAX_IN_BUF; | |
183 | /* read the file */ | |
184 | inputRead=T_FileStream_read(buf->in,cbuf,cbufSize-offset); | |
185 | buf->remaining-=inputRead; | |
186 | ||
187 | }else{ | |
188 | cbufSize = T_FileStream_size(buf->in); | |
189 | cbuf = (char*)uprv_malloc(cbufSize); | |
190 | inputRead= T_FileStream_read(buf->in,cbuf,cbufSize); | |
191 | buf->remaining-=inputRead; | |
192 | } | |
193 | ||
194 | /* just to be sure...*/ | |
195 | if ( 0 == inputRead ) | |
196 | buf->remaining = 0; | |
197 | ||
198 | target=pTarget; | |
199 | /* convert the bytes */ | |
200 | if(buf->conv){ | |
201 | /* set the callback to stop */ | |
202 | UConverterToUCallback toUOldAction ; | |
203 | void* toUOldContext; | |
204 | void* toUNewContext=NULL; | |
205 | ucnv_setToUCallBack(buf->conv, | |
206 | UCNV_TO_U_CALLBACK_STOP, | |
207 | toUNewContext, | |
208 | &toUOldAction, | |
209 | (const void**)&toUOldContext, | |
210 | error); | |
211 | /* since state is saved in the converter we add offset to source*/ | |
212 | target = pTarget+offset; | |
213 | source = cbuf; | |
214 | sourceLimit = source + inputRead; | |
215 | ucnv_toUnicode(buf->conv,&target,target+(buf->bufCapacity-offset), | |
216 | &source,sourceLimit,NULL, | |
217 | (UBool)(buf->remaining==0),error); | |
218 | ||
219 | if(U_FAILURE(*error)){ | |
220 | char context[CONTEXT_LEN]; | |
221 | char preContext[CONTEXT_LEN]; | |
222 | char postContext[CONTEXT_LEN]; | |
223 | int8_t len = CONTEXT_LEN; | |
224 | int32_t start=0; | |
225 | int32_t stop =0; | |
226 | int32_t pos =0; | |
227 | /* use erro1 to preserve the error code */ | |
228 | UErrorCode error1 =U_ZERO_ERROR; | |
229 | ||
230 | if( buf->showWarning==TRUE){ | |
231 | fprintf(stderr,"\n###WARNING: Encountered abnormal bytes while" | |
232 | " converting input stream to target encoding: %s\n", | |
233 | u_errorName(*error)); | |
234 | } | |
235 | ||
236 | ||
237 | /* now get the context chars */ | |
238 | ucnv_getInvalidChars(buf->conv,context,&len,&error1); | |
239 | context[len]= 0 ; /* null terminate the buffer */ | |
240 | ||
241 | pos = (int32_t)(source - cbuf - len); | |
242 | ||
243 | /* for pre-context */ | |
244 | start = (pos <=CONTEXT_LEN)? 0 : (pos - (CONTEXT_LEN-1)); | |
245 | stop = pos-len; | |
246 | ||
247 | memcpy(preContext,cbuf+start,stop-start); | |
248 | /* null terminate the buffer */ | |
249 | preContext[stop-start] = 0; | |
250 | ||
251 | /* for post-context */ | |
252 | start = pos+len; | |
253 | stop = (int32_t)(((pos+CONTEXT_LEN)<= (sourceLimit-cbuf) )? (pos+(CONTEXT_LEN-1)) : (sourceLimit-cbuf)); | |
254 | ||
255 | memcpy(postContext,source,stop-start); | |
256 | /* null terminate the buffer */ | |
257 | postContext[stop-start] = 0; | |
258 | ||
259 | if(buf->showWarning ==TRUE){ | |
260 | /* print out the context */ | |
261 | fprintf(stderr,"\tPre-context: %s\n",preContext); | |
262 | fprintf(stderr,"\tContext: %s\n",context); | |
263 | fprintf(stderr,"\tPost-context: %s\n", postContext); | |
264 | } | |
265 | ||
266 | /* reset the converter */ | |
267 | ucnv_reset(buf->conv); | |
268 | ||
269 | /* set the call back to substitute | |
270 | * and restart conversion | |
271 | */ | |
272 | ucnv_setToUCallBack(buf->conv, | |
273 | UCNV_TO_U_CALLBACK_SUBSTITUTE, | |
274 | toUNewContext, | |
275 | &toUOldAction, | |
276 | (const void**)&toUOldContext, | |
277 | &error1); | |
278 | ||
279 | /* reset source and target start positions */ | |
280 | target = pTarget+offset; | |
281 | source = cbuf; | |
282 | ||
283 | /* re convert */ | |
284 | ucnv_toUnicode(buf->conv,&target,target+(buf->bufCapacity-offset), | |
285 | &source,sourceLimit,NULL, | |
286 | (UBool)(buf->remaining==0),&error1); | |
287 | ||
288 | } | |
289 | outputWritten = (int32_t)(target - pTarget); | |
290 | ||
291 | ||
292 | #if DEBUG | |
293 | { | |
294 | int i; | |
295 | target = pTarget; | |
296 | for(i=0;i<numRead;i++){ | |
297 | /* printf("%c", (char)(*target++));*/ | |
298 | } | |
299 | } | |
300 | #endif | |
301 | ||
302 | }else{ | |
303 | u_charsToUChars(cbuf,target+offset,inputRead); | |
304 | outputWritten=((buf->remaining>cbufSize)? cbufSize:inputRead+offset); | |
305 | } | |
306 | buf->currentPos = pTarget; | |
307 | buf->bufLimit=pTarget+outputWritten; | |
308 | if(cbuf!=carr){ | |
309 | uprv_free(cbuf); | |
310 | } | |
311 | return buf; | |
312 | } | |
313 | ||
314 | ||
315 | ||
316 | /* get a UChar from the stream*/ | |
317 | U_CAPI int32_t U_EXPORT2 | |
318 | ucbuf_getc(UCHARBUF* buf,UErrorCode* error){ | |
319 | if(error==NULL || U_FAILURE(*error)){ | |
320 | return FALSE; | |
321 | } | |
322 | if(buf->currentPos>=buf->bufLimit){ | |
323 | if(buf->remaining==0){ | |
324 | return U_EOF; | |
325 | } | |
326 | buf=ucbuf_fillucbuf(buf,error); | |
327 | if(U_FAILURE(*error)){ | |
328 | return U_EOF; | |
329 | } | |
330 | } | |
331 | ||
332 | return *(buf->currentPos++); | |
333 | } | |
334 | ||
335 | /* get a UChar32 from the stream*/ | |
336 | U_CAPI int32_t U_EXPORT2 | |
337 | ucbuf_getc32(UCHARBUF* buf,UErrorCode* error){ | |
338 | int32_t retVal = (int32_t)U_EOF; | |
339 | if(error==NULL || U_FAILURE(*error)){ | |
340 | return FALSE; | |
341 | } | |
342 | if(buf->currentPos+1>=buf->bufLimit){ | |
343 | if(buf->remaining==0){ | |
344 | return U_EOF; | |
345 | } | |
346 | buf=ucbuf_fillucbuf(buf,error); | |
347 | if(U_FAILURE(*error)){ | |
348 | return U_EOF; | |
349 | } | |
350 | } | |
351 | if(UTF_IS_LEAD(*(buf->currentPos))){ | |
352 | retVal=UTF16_GET_PAIR_VALUE(*(buf->currentPos++),*(buf->currentPos++)); | |
353 | }else{ | |
354 | retVal = *(buf->currentPos++); | |
355 | } | |
356 | return retVal; | |
357 | } | |
358 | ||
359 | /* u_unescapeAt() callback to return a UChar*/ | |
360 | static UChar U_CALLCONV | |
361 | _charAt(int32_t offset, void *context) { | |
362 | return ((UCHARBUF*) context)->currentPos[offset]; | |
363 | } | |
364 | ||
365 | /* getc and escape it */ | |
366 | U_CAPI int32_t U_EXPORT2 | |
367 | ucbuf_getcx32(UCHARBUF* buf,UErrorCode* error) { | |
368 | int32_t length; | |
369 | int32_t offset; | |
370 | UChar32 c32,c1,c2; | |
371 | if(error==NULL || U_FAILURE(*error)){ | |
372 | return FALSE; | |
373 | } | |
374 | /* Fill the buffer if it is empty */ | |
375 | if (buf->currentPos >=buf->bufLimit-2) { | |
376 | ucbuf_fillucbuf(buf,error); | |
377 | } | |
378 | ||
379 | /* Get the next character in the buffer */ | |
380 | if (buf->currentPos < buf->bufLimit) { | |
381 | c1 = *(buf->currentPos)++; | |
382 | } else { | |
383 | c1 = U_EOF; | |
384 | } | |
385 | ||
386 | c2 = *(buf->currentPos); | |
387 | ||
388 | /* If it isn't a backslash, return it */ | |
389 | if (c1 != 0x005C) { | |
390 | return c1; | |
391 | } | |
392 | ||
393 | /* Determine the amount of data in the buffer */ | |
394 | length = (int32_t)(buf->bufLimit - buf->currentPos); | |
395 | ||
396 | /* The longest escape sequence is \Uhhhhhhhh; make sure | |
397 | we have at least that many characters */ | |
398 | if (length < 10) { | |
399 | ||
400 | /* fill the buffer */ | |
401 | ucbuf_fillucbuf(buf,error); | |
402 | length = (int32_t)(buf->bufLimit - buf->buffer); | |
403 | } | |
404 | ||
405 | /* Process the escape */ | |
406 | offset = 0; | |
407 | c32 = u_unescapeAt(_charAt, &offset, length, (void*)buf); | |
408 | ||
409 | /* check if u_unescapeAt unescaped and converted | |
410 | * to c32 or not | |
411 | */ | |
412 | if(c32==0xFFFFFFFF){ | |
413 | if(buf->showWarning) { | |
414 | char context[20]; | |
415 | int32_t len = 20; | |
416 | if(length < len) { | |
417 | len = length; | |
418 | } | |
419 | context[len]= 0 ; /* null terminate the buffer */ | |
420 | u_UCharsToChars( buf->currentPos, context, len); | |
421 | fprintf(stderr,"Bad escape: [%c%s]...\n", c1,context); | |
422 | } | |
423 | *error= U_ILLEGAL_ESCAPE_SEQUENCE; | |
424 | return c1; | |
425 | }else if(c32!=c2 || (c32==0x0075 && c2==0x0075 && c1==0x005C) /* for \u0075 c2=0x0075 and c32==0x0075*/){ | |
426 | /* Update the current buffer position */ | |
427 | buf->currentPos += offset; | |
428 | }else{ | |
429 | /* unescaping failed so we just return | |
430 | * c1 and not consume the buffer | |
431 | * this is useful for rules with escapes | |
432 | * in resouce bundles | |
433 | * eg: \' \\ \" | |
434 | */ | |
435 | return c1; | |
436 | } | |
437 | ||
438 | return c32; | |
439 | } | |
440 | ||
441 | U_CAPI UCHARBUF* U_EXPORT2 | |
442 | ucbuf_open(const char* fileName,const char** cp,UBool showWarning, UBool buffered, UErrorCode* error){ | |
443 | ||
444 | FileStream* in = NULL; | |
445 | int32_t fileSize=0; | |
446 | const char* knownCp; | |
447 | if(error==NULL || U_FAILURE(*error)){ | |
448 | return NULL; | |
449 | } | |
450 | if(cp==NULL || fileName==NULL){ | |
451 | *error = U_ILLEGAL_ARGUMENT_ERROR; | |
452 | return FALSE; | |
453 | } | |
454 | if (!uprv_strcmp(fileName, "-")) { | |
455 | in = T_FileStream_stdin(); | |
456 | }else{ | |
457 | in = T_FileStream_open(fileName, "rb"); | |
458 | } | |
459 | ||
460 | if(in!=NULL){ | |
461 | UCHARBUF* buf =(UCHARBUF*) uprv_malloc(sizeof(UCHARBUF)); | |
462 | fileSize = T_FileStream_size(in); | |
463 | if(buf){ | |
464 | buf->in=in; | |
465 | buf->conv=NULL; | |
466 | buf->showWarning = showWarning; | |
467 | buf->isBuffered = buffered; | |
468 | buf->signatureLength=0; | |
469 | if(*cp==NULL || **cp=='\0'){ | |
470 | /* don't have code page name... try to autodetect */ | |
471 | ucbuf_autodetect_fs(in,cp,&buf->conv,&buf->signatureLength,error); | |
472 | }else if(ucbuf_isCPKnown(*cp)){ | |
473 | /* discard BOM */ | |
474 | ucbuf_autodetect_fs(in,&knownCp,&buf->conv,&buf->signatureLength,error); | |
475 | } | |
476 | if(U_SUCCESS(*error) && buf->conv==NULL) { | |
477 | buf->conv=ucnv_open(*cp,error); | |
478 | } | |
479 | if(U_FAILURE(*error)){ | |
480 | ucnv_close(buf->conv); | |
481 | uprv_free(buf); | |
482 | return NULL; | |
483 | } | |
484 | ||
485 | if((buf->conv==NULL) && (buf->showWarning==TRUE)){ | |
486 | fprintf(stderr,"###WARNING: No converter defined. Using codepage of system.\n"); | |
487 | } | |
488 | buf->remaining=fileSize-buf->signatureLength; | |
489 | if(buf->isBuffered){ | |
490 | buf->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR* MAX_U_BUF); | |
491 | buf->bufCapacity=MAX_U_BUF; | |
492 | }else{ | |
493 | buf->buffer=(UChar*) uprv_malloc(U_SIZEOF_UCHAR * (buf->remaining+buf->signatureLength)); | |
494 | buf->bufCapacity=buf->remaining+buf->signatureLength; | |
495 | } | |
496 | if (buf->buffer == NULL) { | |
497 | *error = U_MEMORY_ALLOCATION_ERROR; | |
498 | return NULL; | |
499 | } | |
500 | buf->currentPos=buf->buffer; | |
501 | buf->bufLimit=buf->buffer; | |
502 | if(U_FAILURE(*error)){ | |
503 | fprintf(stderr, "Could not open codepage [%s]: %s\n", *cp, u_errorName(*error)); | |
504 | return NULL; | |
505 | } | |
506 | buf=ucbuf_fillucbuf(buf,error); | |
507 | return buf; | |
508 | }else{ | |
509 | *error = U_MEMORY_ALLOCATION_ERROR; | |
510 | return NULL; | |
511 | } | |
512 | ||
513 | } | |
514 | *error =U_FILE_ACCESS_ERROR; | |
515 | return NULL; | |
516 | } | |
517 | ||
518 | ||
519 | ||
520 | /* TODO: this method will fail if at the | |
521 | * begining of buffer and the uchar to unget | |
522 | * is from the previous buffer. Need to implement | |
523 | * system to take care of that situation. | |
524 | */ | |
525 | U_CAPI void U_EXPORT2 | |
526 | ucbuf_ungetc(int32_t c,UCHARBUF* buf){ | |
527 | /* decrement currentPos pointer | |
528 | * if not at the begining of buffer | |
529 | */ | |
530 | UChar escaped[8] ={'\0'}; | |
531 | int32_t len =0; | |
532 | if(c > 0xFFFF){ | |
533 | len = uprv_itou(escaped,8,c,16,8); | |
534 | }else{ | |
535 | len=uprv_itou(escaped,8,c,16,4); | |
536 | } | |
537 | if(buf->currentPos!=buf->buffer){ | |
538 | if(*(buf->currentPos-1)==c){ | |
539 | buf->currentPos--; | |
540 | }else if(u_strncmp(buf->currentPos-len,escaped,len) == 0){ | |
541 | while(--len>0){ | |
542 | buf->currentPos--; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | /* frees the resources of UChar* buffer */ | |
549 | static void | |
550 | ucbuf_closebuf(UCHARBUF* buf){ | |
551 | uprv_free(buf->buffer); | |
552 | buf->buffer = NULL; | |
553 | } | |
554 | ||
555 | /* close the buf and release resources*/ | |
556 | U_CAPI void U_EXPORT2 | |
557 | ucbuf_close(UCHARBUF* buf){ | |
558 | if(buf!=NULL){ | |
559 | if(buf->conv){ | |
560 | ucnv_close(buf->conv); | |
561 | } | |
562 | T_FileStream_close(buf->in); | |
563 | ucbuf_closebuf(buf); | |
564 | uprv_free(buf); | |
565 | } | |
566 | } | |
567 | ||
568 | /* rewind the buf and file stream */ | |
569 | U_CAPI void U_EXPORT2 | |
570 | ucbuf_rewind(UCHARBUF* buf,UErrorCode* error){ | |
571 | if(error==NULL || U_FAILURE(*error)){ | |
572 | return; | |
573 | } | |
574 | if(buf){ | |
575 | buf->currentPos=buf->buffer; | |
576 | buf->bufLimit=buf->buffer; | |
577 | T_FileStream_rewind(buf->in); | |
578 | buf->remaining=T_FileStream_size(buf->in)-buf->signatureLength; | |
579 | ||
580 | ucnv_resetToUnicode(buf->conv); | |
581 | if(buf->signatureLength>0) { | |
582 | UChar target[1]={ 0 }; | |
583 | UChar* pTarget; | |
584 | char start[8]; | |
585 | const char* pStart; | |
586 | int32_t numRead; | |
587 | ||
588 | /* read the signature bytes */ | |
589 | numRead=T_FileStream_read(buf->in, start, buf->signatureLength); | |
590 | ||
591 | /* convert and ignore initial U+FEFF, and the buffer overflow */ | |
592 | pTarget = target; | |
593 | pStart = start; | |
594 | ucnv_toUnicode(buf->conv, &pTarget, target+1, &pStart, start+numRead, NULL, FALSE, error); | |
595 | if(*error==U_BUFFER_OVERFLOW_ERROR) { | |
596 | *error=U_ZERO_ERROR; | |
597 | } | |
598 | ||
599 | /* verify that we successfully read exactly U+FEFF */ | |
600 | if(U_SUCCESS(*error) && (numRead!=buf->signatureLength || pTarget!=(target+1) || target[0]!=0xfeff)) { | |
601 | *error=U_INTERNAL_PROGRAM_ERROR; | |
602 | } | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | ||
608 | U_CAPI int32_t U_EXPORT2 | |
609 | ucbuf_size(UCHARBUF* buf){ | |
610 | if(buf){ | |
611 | if(buf->isBuffered){ | |
612 | return (T_FileStream_size(buf->in)-buf->signatureLength)/ucnv_getMinCharSize(buf->conv); | |
613 | }else{ | |
614 | return buf->bufLimit-buf->buffer; | |
615 | } | |
616 | } | |
617 | return 0; | |
618 | } | |
619 | ||
620 | U_CAPI const UChar* U_EXPORT2 | |
621 | ucbuf_getBuffer(UCHARBUF* buf,int32_t* len,UErrorCode* error){ | |
622 | if(error==NULL || U_FAILURE(*error)){ | |
623 | return NULL; | |
624 | } | |
625 | if(buf==NULL || len==NULL){ | |
626 | *error = U_ILLEGAL_ARGUMENT_ERROR; | |
627 | return NULL; | |
628 | } | |
629 | *len = buf->bufLimit-buf->buffer; | |
630 | return buf->buffer; | |
631 | } | |
632 | ||
633 | U_CAPI const char* U_EXPORT2 | |
634 | ucbuf_resolveFileName(const char* inputDir, const char* fileName, char* target, int32_t* len, UErrorCode* status){ | |
635 | int32_t requiredLen = 0; | |
636 | int32_t dirlen = 0; | |
637 | int32_t filelen = 0; | |
638 | if(status==NULL || U_FAILURE(*status)){ | |
639 | return NULL; | |
640 | } | |
641 | ||
642 | if(inputDir == NULL || fileName == NULL || len==NULL || (target==NULL && *len>0)){ | |
643 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
644 | return NULL; | |
645 | } | |
646 | ||
647 | ||
648 | dirlen = (int32_t)uprv_strlen(inputDir); | |
649 | filelen = (int32_t)uprv_strlen(fileName); | |
650 | if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { | |
651 | requiredLen = dirlen + filelen + 2; | |
652 | if((*len < requiredLen) || target==NULL){ | |
653 | *len = requiredLen; | |
654 | *status = U_BUFFER_OVERFLOW_ERROR; | |
655 | return NULL; | |
656 | } | |
657 | ||
658 | target[0] = '\0'; | |
659 | /* | |
660 | * append the input dir to openFileName if the first char in | |
661 | * filename is not file seperation char and the last char input directory is not '.'. | |
662 | * This is to support : | |
663 | * genrb -s. /home/icu/data | |
664 | * genrb -s. icu/data | |
665 | * The user cannot mix notations like | |
666 | * genrb -s. /icu/data --- the absolute path specified. -s redundant | |
667 | * user should use | |
668 | * genrb -s. icu/data --- start from CWD and look in icu/data dir | |
669 | */ | |
670 | if( (fileName[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ | |
671 | uprv_strcpy(target, inputDir); | |
672 | target[dirlen] = U_FILE_SEP_CHAR; | |
673 | } | |
674 | target[dirlen + 1] = '\0'; | |
675 | } else { | |
676 | requiredLen = dirlen + filelen + 1; | |
677 | if((*len < requiredLen) || target==NULL){ | |
678 | *len = requiredLen; | |
679 | *status = U_BUFFER_OVERFLOW_ERROR; | |
680 | return NULL; | |
681 | } | |
682 | ||
683 | uprv_strcpy(target, inputDir); | |
684 | } | |
685 | ||
686 | uprv_strcat(target, fileName); | |
687 | return target; | |
688 | } | |
689 | /* | |
690 | * Unicode TR 13 says any of the below chars is | |
691 | * a new line char in a readline function in addition | |
692 | * to CR+LF combination which needs to be | |
693 | * handled seperately | |
694 | */ | |
695 | static UBool ucbuf_isCharNewLine(UChar c){ | |
696 | switch(c){ | |
697 | case 0x000A: /* LF */ | |
698 | case 0x000D: /* CR */ | |
699 | case 0x000C: /* FF */ | |
700 | case 0x0085: /* NEL */ | |
701 | case 0x2028: /* LS */ | |
702 | case 0x2029: /* PS */ | |
703 | return TRUE; | |
704 | default: | |
705 | return FALSE; | |
706 | } | |
707 | } | |
708 | ||
709 | U_CAPI const UChar* U_EXPORT2 | |
710 | ucbuf_readline(UCHARBUF* buf,int32_t* len,UErrorCode* err){ | |
711 | UChar* temp = buf->currentPos; | |
712 | UChar* savePos =NULL; | |
713 | UChar c=0x0000; | |
714 | if(buf->isBuffered){ | |
715 | /* The input is buffered we have to do more | |
716 | * for returning a pointer U_TRUNCATED_CHAR_FOUND | |
717 | */ | |
718 | for(;;){ | |
719 | c = *temp++; | |
720 | if(buf->remaining==0){ | |
721 | *err = (UErrorCode) U_EOF; | |
722 | } | |
723 | if(temp>=buf->bufLimit && buf->currentPos == buf->buffer){ | |
724 | *err= U_TRUNCATED_CHAR_FOUND; | |
725 | return NULL; | |
726 | }else{ | |
727 | ucbuf_fillucbuf(buf,err); | |
728 | if(U_FAILURE(*err)){ | |
729 | return NULL; | |
730 | } | |
731 | } | |
732 | /* | |
733 | * Accoding to TR 13 readLine functions must interpret | |
734 | * CR, CR+LF, LF, NEL, PS, LS or FF as line seperators | |
735 | */ | |
736 | /* Windows CR LF */ | |
737 | if(c ==0x0d && temp+1<=buf->bufLimit && *(temp+1) == 0x0a ){ | |
738 | *len = temp++ - buf->currentPos; | |
739 | savePos = buf->currentPos; | |
740 | buf->currentPos = temp; | |
741 | return savePos; | |
742 | } | |
743 | /* else */ | |
744 | ||
745 | if (temp>=buf->bufLimit|| ucbuf_isCharNewLine(c)){ /* Unipad inserts 2028 line separators! */ | |
746 | *len = temp - buf->currentPos; | |
747 | savePos = buf->currentPos; | |
748 | buf->currentPos = temp; | |
749 | return savePos; | |
750 | } | |
751 | } | |
752 | }else{ | |
753 | /* we know that all input is read into the internal | |
754 | * buffer so we can safely return pointers | |
755 | */ | |
756 | for(;;){ | |
757 | c = *temp++; | |
758 | ||
759 | if(buf->currentPos==buf->bufLimit){ | |
760 | *err = (UErrorCode) U_EOF; | |
761 | return NULL; | |
762 | } | |
763 | /* Windows CR LF */ | |
764 | if(c ==0x0d && temp+1<=buf->bufLimit && *(temp+1) == 0x0a ){ | |
765 | *len = temp++ - buf->currentPos; | |
766 | savePos = buf->currentPos; | |
767 | buf->currentPos = temp; | |
768 | return savePos; | |
769 | } | |
770 | /* else */ | |
771 | if (temp>=buf->bufLimit|| ucbuf_isCharNewLine(c)) { /* Unipad inserts 2028 line separators! */ | |
772 | *len = temp - buf->currentPos; | |
773 | savePos = buf->currentPos; | |
774 | buf->currentPos = temp; | |
775 | return savePos; | |
776 | } | |
777 | } | |
778 | } | |
779 | /* not reached */ | |
780 | /* A compiler warning will appear if all paths don't contain a return statement. */ | |
781 | /* return NULL;*/ | |
782 | } |