]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/toolutil/uparse.c
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / uparse.c
index 0a49ecb9a795a3b57293444836d4bed45d82d173..3d098cbfac81b58096c0e6bf2dbdb3442c85ff48 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2000-2004, International Business Machines
+*   Copyright (C) 2000-2011, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 */
 
 #include "unicode/utypes.h"
+#include "unicode/uchar.h"
+#include "unicode/ustring.h"
+#include "unicode/utf16.h"
 #include "cstring.h"
 #include "filestrm.h"
 #include "uparse.h"
-#include "unicode/uchar.h"
-#include "unicode/ustring.h"
 #include "ustr_imp.h"
 
 #include <stdio.h>
 
 U_CAPI const char * U_EXPORT2
 u_skipWhitespace(const char *s) {
-    while(*s==' ' || *s=='\t') {
+    while(U_IS_INV_WHITESPACE(*s)) {
         ++s;
     }
     return s;
 }
 
+U_CAPI char * U_EXPORT2
+u_rtrim(char *s) {
+    char *end=uprv_strchr(s, 0);
+    while(s<end && U_IS_INV_WHITESPACE(*(end-1))) {
+        *--end = 0;
+    }
+    return end;
+}
+
+/*
+ * If the string starts with # @missing: then return the pointer to the
+ * following non-whitespace character.
+ * Otherwise return the original pointer.
+ * Unicode 5.0 adds such lines in some data files to document
+ * default property values.
+ * Poor man's regex for variable amounts of white space.
+ */
+static const char *
+getMissingLimit(const char *s) {
+    const char *s0=s;
+    if(
+        *(s=u_skipWhitespace(s))=='#' &&
+        *(s=u_skipWhitespace(s+1))=='@' &&
+        0==strncmp((s=u_skipWhitespace(s+1)), "missing", 7) &&
+        *(s=u_skipWhitespace(s+7))==':'
+    ) {
+        return u_skipWhitespace(s+1);
+    } else {
+        return s0;
+    }
+}
+
 U_CAPI void U_EXPORT2
 u_parseDelimitedFile(const char *filename, char delimiter,
                      char *fields[][2], int32_t fieldCount,
@@ -46,7 +79,7 @@ u_parseDelimitedFile(const char *filename, char delimiter,
     char *start, *limit;
     int32_t i, length;
 
-    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
+    if(U_FAILURE(*pErrorCode)) {
         return;
     }
 
@@ -67,23 +100,31 @@ u_parseDelimitedFile(const char *filename, char delimiter,
     }
 
     while(T_FileStream_readLine(file, line, sizeof(line))!=NULL) {
-        length=(int32_t)uprv_strlen(line);
-
         /* remove trailing newline characters */
-        while(length>0 && (line[length-1]=='\r' || line[length-1]=='\n')) {
-            line[--length]=0;
+        length=(int32_t)(u_rtrim(line)-line);
+
+        /*
+         * detect a line with # @missing:
+         * start parsing after that, or else from the beginning of the line
+         * set the default warning for @missing lines
+         */
+        start=(char *)getMissingLimit(line);
+        if(start==line) {
+            *pErrorCode=U_ZERO_ERROR;
+        } else {
+            *pErrorCode=U_USING_DEFAULT_WARNING;
         }
 
         /* skip this line if it is empty or a comment */
-        if(line[0]==0 || line[0]=='#') {
+        if(*start==0 || *start=='#') {
             continue;
         }
 
         /* remove in-line comments */
-        limit=uprv_strchr(line, '#');
+        limit=uprv_strchr(start, '#');
         if(limit!=NULL) {
             /* get white space before the pound sign */
-            while(limit>line && (*(limit-1)==' ' || *(limit-1)=='\t')) {
+            while(limit>start && U_IS_INV_WHITESPACE(*(limit-1))) {
                 --limit;
             }
 
@@ -92,12 +133,11 @@ u_parseDelimitedFile(const char *filename, char delimiter,
         }
 
         /* skip lines with only whitespace */
-        if(u_skipWhitespace(line)[0]==0) {
+        if(u_skipWhitespace(start)[0]==0) {
             continue;
         }
 
         /* for each field, call the corresponding field function */
-        start=line;
         for(i=0; i<fieldCount; ++i) {
             /* set the limit pointer of this field */
             limit=start;
@@ -151,11 +191,12 @@ u_parseCodePoints(const char *s,
     uint32_t value;
     int32_t count;
 
-    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
+    if(U_FAILURE(*pErrorCode)) {
         return 0;
     }
     if(s==NULL || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
+        return 0;
     }
 
     count=0;
@@ -167,7 +208,7 @@ u_parseCodePoints(const char *s,
 
         /* read one code point */
         value=(uint32_t)uprv_strtoul(s, &end, 16);
-        if(end<=s || (*end!=' ' && *end!='\t' && *end!=';' && *end!=0) || value>=0x110000) {
+        if(end<=s || (!U_IS_INV_WHITESPACE(*end) && *end!=';' && *end!=0) || value>=0x110000) {
             *pErrorCode=U_PARSE_ERROR;
             return 0;
         }
@@ -199,7 +240,7 @@ u_parseString(const char *s,
     uint32_t value;
     int32_t destLength;
 
-    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
+    if(U_FAILURE(*pErrorCode)) {
         return 0;
     }
     if(s==NULL || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
@@ -226,21 +267,22 @@ u_parseString(const char *s,
 
         /* read one code point */
         value=(uint32_t)uprv_strtoul(s, &end, 16);
-        if(end<=s || (*end!=' ' && *end!='\t' && *end!=';' && *end!=0) || value>=0x110000) {
+        if(end<=s || (!U_IS_INV_WHITESPACE(*end) && *end!=';' && *end!=0) || value>=0x110000) {
             *pErrorCode=U_PARSE_ERROR;
             return 0;
         }
 
         /* store the first code point */
-        if(destLength==0 && pFirst!=NULL) {
+        if(pFirst!=NULL) {
             *pFirst=value;
+            pFirst=NULL;
         }
 
         /* append it to the destination array */
-        if((destLength+UTF_CHAR_LENGTH(value))<=destCapacity) {
-            UTF_APPEND_CHAR_UNSAFE(dest, destLength, value);
+        if((destLength+U16_LENGTH(value))<=destCapacity) {
+            U16_APPEND_UNSAFE(dest, destLength, value);
         } else {
-            destLength+=UTF_CHAR_LENGTH(value);
+            destLength+=U16_LENGTH(value);
         }
 
         /* go to the following characters */
@@ -250,28 +292,25 @@ u_parseString(const char *s,
 
 /* read a range like start or start..end */
 U_CAPI int32_t U_EXPORT2
-u_parseCodePointRange(const char *s,
-                      uint32_t *pStart, uint32_t *pEnd,
-                      UErrorCode *pErrorCode) {
+u_parseCodePointRangeAnyTerminator(const char *s,
+                                   uint32_t *pStart, uint32_t *pEnd,
+                                   const char **terminator,
+                                   UErrorCode *pErrorCode) {
     char *end;
     uint32_t value;
 
-    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
+    if(U_FAILURE(*pErrorCode)) {
         return 0;
     }
     if(s==NULL || pStart==NULL || pEnd==NULL) {
         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
-    }
-
-    s=u_skipWhitespace(s);
-    if(*s==';' || *s==0) {
-        *pErrorCode=U_PARSE_ERROR;
         return 0;
     }
 
     /* read the start code point */
+    s=u_skipWhitespace(s);
     value=(uint32_t)uprv_strtoul(s, &end, 16);
-    if(end<=s || (*end!=' ' && *end!='\t' && *end!='.' && *end!=';') || value>=0x110000) {
+    if(end<=s || value>=0x110000) {
         *pErrorCode=U_PARSE_ERROR;
         return 0;
     }
@@ -279,19 +318,15 @@ u_parseCodePointRange(const char *s,
 
     /* is there a "..end"? */
     s=u_skipWhitespace(end);
-    if(*s==';' || *s==0) {
-        return 1;
-    }
-
     if(*s!='.' || s[1]!='.') {
-        *pErrorCode=U_PARSE_ERROR;
-        return 0;
+        *terminator=end;
+        return 1;
     }
-    s+=2;
+    s=u_skipWhitespace(s+2);
 
     /* read the end code point */
     value=(uint32_t)uprv_strtoul(s, &end, 16);
-    if(end<=s || (*end!=' ' && *end!='\t' && *end!=';') || value>=0x110000) {
+    if(end<=s || value>=0x110000) {
         *pErrorCode=U_PARSE_ERROR;
         return 0;
     }
@@ -303,14 +338,25 @@ u_parseCodePointRange(const char *s,
         return 0;
     }
 
-    /* no garbage after that? */
-    s=u_skipWhitespace(end);
-    if(*s==';' || *s==0) {
-        return value-*pStart+1;
-    } else {
-        *pErrorCode=U_PARSE_ERROR;
-        return 0;
+    *terminator=end;
+    return value-*pStart+1;
+}
+
+U_CAPI int32_t U_EXPORT2
+u_parseCodePointRange(const char *s,
+                      uint32_t *pStart, uint32_t *pEnd,
+                      UErrorCode *pErrorCode) {
+    const char *terminator;
+    int32_t rangeLength=
+        u_parseCodePointRangeAnyTerminator(s, pStart, pEnd, &terminator, pErrorCode);
+    if(U_SUCCESS(*pErrorCode)) {
+        terminator=u_skipWhitespace(terminator);
+        if(*terminator!=';' && *terminator!=0) {
+            *pErrorCode=U_PARSE_ERROR;
+            return 0;
+        }
     }
+    return rangeLength;
 }
 
 U_CAPI int32_t U_EXPORT2