2 *******************************************************************************
4 * Copyright (C) 2000-2011, 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
;
256 s
=u_skipWhitespace(s
);
257 if(*s
==';' || *s
==0) {
258 if(destLength
<destCapacity
) {
260 } else if(destLength
==destCapacity
) {
261 *pErrorCode
=U_STRING_NOT_TERMINATED_WARNING
;
263 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
268 /* read one code point */
269 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
270 if(end
<=s
|| (!U_IS_INV_WHITESPACE(*end
) && *end
!=';' && *end
!=0) || value
>=0x110000) {
271 *pErrorCode
=U_PARSE_ERROR
;
275 /* store the first code point */
281 /* append it to the destination array */
282 if((destLength
+U16_LENGTH(value
))<=destCapacity
) {
283 U16_APPEND_UNSAFE(dest
, destLength
, value
);
285 destLength
+=U16_LENGTH(value
);
288 /* go to the following characters */
293 /* read a range like start or start..end */
294 U_CAPI
int32_t U_EXPORT2
295 u_parseCodePointRangeAnyTerminator(const char *s
,
296 uint32_t *pStart
, uint32_t *pEnd
,
297 const char **terminator
,
298 UErrorCode
*pErrorCode
) {
302 if(U_FAILURE(*pErrorCode
)) {
305 if(s
==NULL
|| pStart
==NULL
|| pEnd
==NULL
) {
306 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
310 /* read the start code point */
311 s
=u_skipWhitespace(s
);
312 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
313 if(end
<=s
|| value
>=0x110000) {
314 *pErrorCode
=U_PARSE_ERROR
;
319 /* is there a "..end"? */
320 s
=u_skipWhitespace(end
);
321 if(*s
!='.' || s
[1]!='.') {
325 s
=u_skipWhitespace(s
+2);
327 /* read the end code point */
328 value
=(uint32_t)uprv_strtoul(s
, &end
, 16);
329 if(end
<=s
|| value
>=0x110000) {
330 *pErrorCode
=U_PARSE_ERROR
;
335 /* is this a valid range? */
337 *pErrorCode
=U_PARSE_ERROR
;
342 return value
-*pStart
+1;
345 U_CAPI
int32_t U_EXPORT2
346 u_parseCodePointRange(const char *s
,
347 uint32_t *pStart
, uint32_t *pEnd
,
348 UErrorCode
*pErrorCode
) {
349 const char *terminator
;
351 u_parseCodePointRangeAnyTerminator(s
, pStart
, pEnd
, &terminator
, pErrorCode
);
352 if(U_SUCCESS(*pErrorCode
)) {
353 terminator
=u_skipWhitespace(terminator
);
354 if(*terminator
!=';' && *terminator
!=0) {
355 *pErrorCode
=U_PARSE_ERROR
;
362 U_CAPI
int32_t U_EXPORT2
363 u_parseUTF8(const char *source
, int32_t sLen
, char *dest
, int32_t destCapacity
, UErrorCode
*status
) {
364 const char *read
= source
;
366 unsigned int value
= 0;
368 sLen
= (int32_t)strlen(source
);
371 while(read
< source
+sLen
) {
372 sscanf(read
, "%2x", &value
);
373 if(i
< destCapacity
) {
374 dest
[i
] = (char)value
;
379 return u_terminateChars(dest
, destCapacity
, i
, status
);