1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2000-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 2000apr18
16 * created by: Markus W. Scherer
18 * This file provides a parser for files that are delimited by one single
19 * character like ';' or TAB. Example: the Unicode Character Properties files
20 * like UnicodeData.txt are semicolon-delimited.
23 #include "unicode/utypes.h"
24 #include "unicode/uchar.h"
25 #include "unicode/ustring.h"
26 #include "unicode/utf16.h"
34 U_CAPI const char * U_EXPORT2
35 u_skipWhitespace(const char *s) {
36 while(U_IS_INV_WHITESPACE(*s)) {
42 U_CAPI char * U_EXPORT2
44 char *end=uprv_strchr(s, 0);
45 while(s<end && U_IS_INV_WHITESPACE(*(end-1))) {
52 * If the string starts with # @missing: then return the pointer to the
53 * following non-whitespace character.
54 * Otherwise return the original pointer.
55 * Unicode 5.0 adds such lines in some data files to document
56 * default property values.
57 * Poor man's regex for variable amounts of white space.
60 getMissingLimit(const char *s) {
63 *(s=u_skipWhitespace(s))=='#' &&
64 *(s=u_skipWhitespace(s+1))=='@' &&
65 0==strncmp((s=u_skipWhitespace(s+1)), "missing", 7) &&
66 *(s=u_skipWhitespace(s+7))==':'
68 return u_skipWhitespace(s+1);
75 u_parseDelimitedFile(const char *filename, char delimiter,
76 char *fields[][2], int32_t fieldCount,
77 UParseLineFn *lineFn, void *context,
78 UErrorCode *pErrorCode) {
84 if(U_FAILURE(*pErrorCode)) {
88 if(fields==NULL || lineFn==NULL || fieldCount<=0) {
89 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
93 if(filename==NULL || *filename==0 || (*filename=='-' && filename[1]==0)) {
95 file=T_FileStream_stdin();
97 file=T_FileStream_open(filename, "r");
100 *pErrorCode=U_FILE_ACCESS_ERROR;
104 while(T_FileStream_readLine(file, line, sizeof(line))!=NULL) {
105 /* remove trailing newline characters */
106 length=(int32_t)(u_rtrim(line)-line);
109 * detect a line with # @missing:
110 * start parsing after that, or else from the beginning of the line
111 * set the default warning for @missing lines
113 start=(char *)getMissingLimit(line);
115 *pErrorCode=U_ZERO_ERROR;
117 *pErrorCode=U_USING_DEFAULT_WARNING;
120 /* skip this line if it is empty or a comment */
121 if(*start==0 || *start=='#') {
125 /* remove in-line comments */
126 limit=uprv_strchr(start, '#');
128 /* get white space before the pound sign */
129 while(limit>start && U_IS_INV_WHITESPACE(*(limit-1))) {
133 /* truncate the line */
137 /* skip lines with only whitespace */
138 if(u_skipWhitespace(start)[0]==0) {
142 /* for each field, call the corresponding field function */
143 for(i=0; i<fieldCount; ++i) {
144 /* set the limit pointer of this field */
146 while(*limit!=delimiter && *limit!=0) {
150 /* set the field start and limit in the fields array */
154 /* set start to the beginning of the next field, if any */
158 } else if(i+1<fieldCount) {
159 *pErrorCode=U_PARSE_ERROR;
166 /* error in a field function? */
167 if(U_FAILURE(*pErrorCode)) {
171 /* call the field function */
172 lineFn(context, fields, fieldCount, pErrorCode);
173 if(U_FAILURE(*pErrorCode)) {
179 T_FileStream_close(file);
184 * parse a list of code points
185 * store them as a UTF-32 string in dest[destCapacity]
186 * return the number of code points
188 U_CAPI int32_t U_EXPORT2
189 u_parseCodePoints(const char *s,
190 uint32_t *dest, int32_t destCapacity,
191 UErrorCode *pErrorCode) {
196 if(U_FAILURE(*pErrorCode)) {
199 if(s==NULL || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
200 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
206 s=u_skipWhitespace(s);
207 if(*s==';' || *s==0) {
211 /* read one code point */
212 value=(uint32_t)uprv_strtoul(s, &end, 16);
213 if(end<=s || (!U_IS_INV_WHITESPACE(*end) && *end!=';' && *end!=0) || value>=0x110000) {
214 *pErrorCode=U_PARSE_ERROR;
218 /* append it to the destination array */
219 if(count<destCapacity) {
222 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
225 /* go to the following characters */
231 * parse a list of code points
232 * store them as a string in dest[destCapacity]
233 * set the first code point in *pFirst
234 * @return The length of the string in numbers of UChars.
236 U_CAPI int32_t U_EXPORT2
237 u_parseString(const char *s,
238 UChar *dest, int32_t destCapacity,
240 UErrorCode *pErrorCode) {
245 if(U_FAILURE(*pErrorCode)) {
248 if(s==NULL || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
249 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
259 s=u_skipWhitespace(s);
260 if(*s==';' || *s==0) {
261 if(destLength<destCapacity) {
263 } else if(destLength==destCapacity) {
264 *pErrorCode=U_STRING_NOT_TERMINATED_WARNING;
266 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
271 /* read one code point */
272 value=(uint32_t)uprv_strtoul(s, &end, 16);
273 if(end<=s || (!U_IS_INV_WHITESPACE(*end) && *end!=';' && *end!=0) || value>=0x110000) {
274 *pErrorCode=U_PARSE_ERROR;
278 /* store the first code point */
284 /* append it to the destination array */
285 if((destLength+U16_LENGTH(value))<=destCapacity) {
286 U16_APPEND_UNSAFE(dest, destLength, value);
288 destLength+=U16_LENGTH(value);
291 /* go to the following characters */
296 /* read a range like start or start..end */
297 U_CAPI int32_t U_EXPORT2
298 u_parseCodePointRangeAnyTerminator(const char *s,
299 uint32_t *pStart, uint32_t *pEnd,
300 const char **terminator,
301 UErrorCode *pErrorCode) {
305 if(U_FAILURE(*pErrorCode)) {
308 if(s==NULL || pStart==NULL || pEnd==NULL) {
309 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
313 /* read the start code point */
314 s=u_skipWhitespace(s);
315 value=(uint32_t)uprv_strtoul(s, &end, 16);
316 if(end<=s || value>=0x110000) {
317 *pErrorCode=U_PARSE_ERROR;
322 /* is there a "..end"? */
323 s=u_skipWhitespace(end);
324 if(*s!='.' || s[1]!='.') {
328 s=u_skipWhitespace(s+2);
330 /* read the end code point */
331 value=(uint32_t)uprv_strtoul(s, &end, 16);
332 if(end<=s || value>=0x110000) {
333 *pErrorCode=U_PARSE_ERROR;
338 /* is this a valid range? */
340 *pErrorCode=U_PARSE_ERROR;
345 return value-*pStart+1;
348 U_CAPI int32_t U_EXPORT2
349 u_parseCodePointRange(const char *s,
350 uint32_t *pStart, uint32_t *pEnd,
351 UErrorCode *pErrorCode) {
352 const char *terminator;
354 u_parseCodePointRangeAnyTerminator(s, pStart, pEnd, &terminator, pErrorCode);
355 if(U_SUCCESS(*pErrorCode)) {
356 terminator=u_skipWhitespace(terminator);
357 if(*terminator!=';' && *terminator!=0) {
358 *pErrorCode=U_PARSE_ERROR;
365 U_CAPI int32_t U_EXPORT2
366 u_parseUTF8(const char *source, int32_t sLen, char *dest, int32_t destCapacity, UErrorCode *status) {
367 const char *read = source;
369 unsigned int value = 0;
371 sLen = (int32_t)strlen(source);
374 while(read < source+sLen) {
375 sscanf(read, "%2x", &value);
376 if(i < destCapacity) {
377 dest[i] = (char)value;
382 return u_terminateChars(dest, destCapacity, i, status);