2 *******************************************************************************
4 * Copyright (C) 2000-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2000apr18
14 * created by: Markus W. Scherer
16 * This file provides a parser for files that are delimited by one single
17 * character like ';' or TAB. Example: the Unicode Character Properties files
18 * like UnicodeData.txt are semicolon-delimited.
21 #include "unicode/utypes.h"
22 #include "unicode/uchar.h"
23 #include "unicode/ustring.h"
24 #include "unicode/utf16.h"
32 U_CAPI
const char * U_EXPORT2
33 u_skipWhitespace(const char *s
) {
34 while(U_IS_INV_WHITESPACE(*s
)) {
40 U_CAPI
char * U_EXPORT2
42 char *end
=uprv_strchr(s
, 0);
43 while(s
<end
&& U_IS_INV_WHITESPACE(*(end
-1))) {
50 * If the string starts with # @missing: then return the pointer to the
51 * following non-whitespace character.
52 * Otherwise return the original pointer.
53 * Unicode 5.0 adds such lines in some data files to document
54 * default property values.
55 * Poor man's regex for variable amounts of white space.
58 getMissingLimit(const char *s
) {
61 *(s
=u_skipWhitespace(s
))=='#' &&
62 *(s
=u_skipWhitespace(s
+1))=='@' &&
63 0==strncmp((s
=u_skipWhitespace(s
+1)), "missing", 7) &&
64 *(s
=u_skipWhitespace(s
+7))==':'
66 return u_skipWhitespace(s
+1);
73 u_parseDelimitedFile(const char *filename
, char delimiter
,
74 char *fields
[][2], int32_t fieldCount
,
75 UParseLineFn
*lineFn
, void *context
,
76 UErrorCode
*pErrorCode
) {
82 if(U_FAILURE(*pErrorCode
)) {
86 if(fields
==NULL
|| lineFn
==NULL
|| fieldCount
<=0) {
87 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
91 if(filename
==NULL
|| *filename
==0 || (*filename
=='-' && filename
[1]==0)) {
93 file
=T_FileStream_stdin();
95 file
=T_FileStream_open(filename
, "r");
98 *pErrorCode
=U_FILE_ACCESS_ERROR
;
102 while(T_FileStream_readLine(file
, line
, sizeof(line
))!=NULL
) {
103 /* remove trailing newline characters */
104 length
=(int32_t)(u_rtrim(line
)-line
);
107 * detect a line with # @missing:
108 * start parsing after that, or else from the beginning of the line
109 * set the default warning for @missing lines
111 start
=(char *)getMissingLimit(line
);
113 *pErrorCode
=U_ZERO_ERROR
;
115 *pErrorCode
=U_USING_DEFAULT_WARNING
;
118 /* skip this line if it is empty or a comment */
119 if(*start
==0 || *start
=='#') {
123 /* remove in-line comments */
124 limit
=uprv_strchr(start
, '#');
126 /* get white space before the pound sign */
127 while(limit
>start
&& U_IS_INV_WHITESPACE(*(limit
-1))) {
131 /* truncate the line */
135 /* skip lines with only whitespace */
136 if(u_skipWhitespace(start
)[0]==0) {
140 /* for each field, call the corresponding field function */
141 for(i
=0; i
<fieldCount
; ++i
) {
142 /* set the limit pointer of this field */
144 while(*limit
!=delimiter
&& *limit
!=0) {
148 /* set the field start and limit in the fields array */
152 /* set start to the beginning of the next field, if any */
156 } else if(i
+1<fieldCount
) {
157 *pErrorCode
=U_PARSE_ERROR
;
164 /* error in a field function? */
165 if(U_FAILURE(*pErrorCode
)) {
169 /* call the field function */
170 lineFn(context
, fields
, fieldCount
, pErrorCode
);
171 if(U_FAILURE(*pErrorCode
)) {
177 T_FileStream_close(file
);
182 * parse a list of code points
183 * store them as a UTF-32 string in dest[destCapacity]
184 * return the number of code points
186 U_CAPI
int32_t U_EXPORT2
187 u_parseCodePoints(const char *s
,
188 uint32_t *dest
, int32_t destCapacity
,
189 UErrorCode
*pErrorCode
) {
194 if(U_FAILURE(*pErrorCode
)) {
197 if(s
==NULL
|| destCapacity
<0 || (destCapacity
>0 && dest
==NULL
)) {
198 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
204 s
=u_skipWhitespace(s
);
205 if(*s
==';' || *s
==0) {
209 /* read one code point */
210 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
211 if(end
<=s
|| (!U_IS_INV_WHITESPACE(*end
) && *end
!=';' && *end
!=0) || value
>=0x110000) {
212 *pErrorCode
=U_PARSE_ERROR
;
216 /* append it to the destination array */
217 if(count
<destCapacity
) {
220 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
223 /* go to the following characters */
229 * parse a list of code points
230 * store them as a string in dest[destCapacity]
231 * set the first code point in *pFirst
232 * @return The length of the string in numbers of UChars.
234 U_CAPI
int32_t U_EXPORT2
235 u_parseString(const char *s
,
236 UChar
*dest
, int32_t destCapacity
,
238 UErrorCode
*pErrorCode
) {
243 if(U_FAILURE(*pErrorCode
)) {
246 if(s
==NULL
|| destCapacity
<0 || (destCapacity
>0 && dest
==NULL
)) {
247 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
257 s
=u_skipWhitespace(s
);
258 if(*s
==';' || *s
==0) {
259 if(destLength
<destCapacity
) {
261 } else if(destLength
==destCapacity
) {
262 *pErrorCode
=U_STRING_NOT_TERMINATED_WARNING
;
264 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
269 /* read one code point */
270 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
271 if(end
<=s
|| (!U_IS_INV_WHITESPACE(*end
) && *end
!=';' && *end
!=0) || value
>=0x110000) {
272 *pErrorCode
=U_PARSE_ERROR
;
276 /* store the first code point */
282 /* append it to the destination array */
283 if((destLength
+U16_LENGTH(value
))<=destCapacity
) {
284 U16_APPEND_UNSAFE(dest
, destLength
, value
);
286 destLength
+=U16_LENGTH(value
);
289 /* go to the following characters */
294 /* read a range like start or start..end */
295 U_CAPI
int32_t U_EXPORT2
296 u_parseCodePointRangeAnyTerminator(const char *s
,
297 uint32_t *pStart
, uint32_t *pEnd
,
298 const char **terminator
,
299 UErrorCode
*pErrorCode
) {
303 if(U_FAILURE(*pErrorCode
)) {
306 if(s
==NULL
|| pStart
==NULL
|| pEnd
==NULL
) {
307 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
311 /* read the start code point */
312 s
=u_skipWhitespace(s
);
313 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
314 if(end
<=s
|| value
>=0x110000) {
315 *pErrorCode
=U_PARSE_ERROR
;
320 /* is there a "..end"? */
321 s
=u_skipWhitespace(end
);
322 if(*s
!='.' || s
[1]!='.') {
326 s
=u_skipWhitespace(s
+2);
328 /* read the end code point */
329 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
330 if(end
<=s
|| value
>=0x110000) {
331 *pErrorCode
=U_PARSE_ERROR
;
336 /* is this a valid range? */
338 *pErrorCode
=U_PARSE_ERROR
;
343 return value
-*pStart
+1;
346 U_CAPI
int32_t U_EXPORT2
347 u_parseCodePointRange(const char *s
,
348 uint32_t *pStart
, uint32_t *pEnd
,
349 UErrorCode
*pErrorCode
) {
350 const char *terminator
;
352 u_parseCodePointRangeAnyTerminator(s
, pStart
, pEnd
, &terminator
, pErrorCode
);
353 if(U_SUCCESS(*pErrorCode
)) {
354 terminator
=u_skipWhitespace(terminator
);
355 if(*terminator
!=';' && *terminator
!=0) {
356 *pErrorCode
=U_PARSE_ERROR
;
363 U_CAPI
int32_t U_EXPORT2
364 u_parseUTF8(const char *source
, int32_t sLen
, char *dest
, int32_t destCapacity
, UErrorCode
*status
) {
365 const char *read
= source
;
367 unsigned int value
= 0;
369 sLen
= (int32_t)strlen(source
);
372 while(read
< source
+sLen
) {
373 sscanf(read
, "%2x", &value
);
374 if(i
< destCapacity
) {
375 dest
[i
] = (char)value
;
380 return u_terminateChars(dest
, destCapacity
, i
, status
);