do not use system is<alpha|alnum|space|...>, because of the different implementations...
authorVadim Pisarevsky <no@email>
Tue, 24 May 2011 13:34:25 +0000 (13:34 +0000)
committerVadim Pisarevsky <no@email>
Tue, 24 May 2011 13:34:25 +0000 (13:34 +0000)
modules/core/src/persistence.cpp

index 14c5b39..7fd8fdd 100644 (file)
 #define cv_isprint(c)     ((uchar)(c) >= (uchar)' ')
 #define cv_isprint_or_tab(c)  ((uchar)(c) >= (uchar)' ' || (c) == '\t')
 
+static inline bool cv_isalnum(char c)
+{
+    return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
+}
+
+static inline bool cv_isalpha(char c)
+{
+    return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
+}
+
+static inline bool cv_isdigit(char c)
+{
+    return '0' <= c && c <= '9';
+}
+
+static inline bool cv_isspace(char c)
+{
+    return (9 <= c && c <= 13) || c == ' ';
+}
+
 static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
 {
     const int radix = 10;
@@ -94,14 +114,14 @@ cv::string cv::FileStorage::getDefaultObjectName(const string& _filename)
     char* name = name_buf;
 
     // name must start with letter or '_'
-    if( !isalpha(*ptr) && *ptr!= '_' ){
+    if( !cv_isalpha(*ptr) && *ptr!= '_' ){
         *name++ = '_';
     }
 
     while( ptr < ptr2 )
     {
         char c = *ptr++;
-        if( !isalnum(c) && c != '-' && c != '_' )
+        if( !cv_isalnum(c) && c != '-' && c != '_' )
             c = '_';
         *name++ = c;
     }
@@ -760,7 +780,7 @@ icvDoubleToString( char* buf, double value )
             sprintf( buf, fmt, value );
             if( *ptr == '+' || *ptr == '-' )
                 ptr++;
-            for( ; isdigit(*ptr); ptr++ )
+            for( ; cv_isdigit(*ptr); ptr++ )
                 ;
             if( *ptr == ',' )
                 *ptr = '.';
@@ -799,7 +819,7 @@ icvFloatToString( char* buf, float value )
             sprintf( buf, fmt, value );
             if( *ptr == '+' || *ptr == '-' )
                 ptr++;
-            for( ; isdigit(*ptr); ptr++ )
+            for( ; cv_isdigit(*ptr); ptr++ )
                 ;
             if( *ptr == ',' )
                 *ptr = '.';
@@ -858,7 +878,7 @@ static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
             *endptr = dot_pos;
     }
 
-    if( *endptr == ptr || isalpha(**endptr) )
+    if( *endptr == ptr || cv_isalpha(**endptr) )
         icvProcessSpecialDouble( fs, ptr, &fval, endptr );
 
     return fval;
@@ -1020,20 +1040,20 @@ icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
         }
     }
 
-    if( isdigit(c) ||
-        ((c == '-' || c == '+') && (isdigit(d) || d == '.')) ||
-        (c == '.' && isalnum(d))) // a number
+    if( cv_isdigit(c) ||
+        ((c == '-' || c == '+') && (cv_isdigit(d) || d == '.')) ||
+        (c == '.' && cv_isalnum(d))) // a number
     {
         double fval;
         int ival;
         endptr = ptr + (c == '-' || c == '+');
-        while( isdigit(*endptr) )
+        while( cv_isdigit(*endptr) )
             endptr++;
         if( *endptr == '.' || *endptr == 'e' )
         {
 force_real:
             fval = icv_strtod( fs, ptr, &endptr );
-            /*if( endptr == ptr || isalpha(*endptr) )
+            /*if( endptr == ptr || cv_isalpha(*endptr) )
                 icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/
 
             node->tag = CV_NODE_REAL;
@@ -1059,7 +1079,7 @@ force_int:
             for( len = 0; len < CV_FS_MAX_LEN; )
             {
                 c = *++ptr;
-                if( isalnum(c) || (c != '\'' && cv_isprint(c)))
+                if( cv_isalnum(c) || (c != '\'' && cv_isprint(c)))
                     buf[len++] = c;
                 else if( c == '\'' )
                 {
@@ -1075,7 +1095,7 @@ force_int:
             for( len = 0; len < CV_FS_MAX_LEN; )
             {
                 c = *++ptr;
-                if( isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c)))
+                if( cv_isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c)))
                     buf[len++] = c;
                 else if( c == '\"' )
                 {
@@ -1095,7 +1115,7 @@ force_int:
                         buf[len++] = '\r';
                     else if( d == 't' )
                         buf[len++] = '\t';
-                    else if( d == 'x' || (isdigit(d) && d < '8') )
+                    else if( d == 'x' || (cv_isdigit(d) && d < '8') )
                     {
                         int val, is_hex = d == 'x';
                         c = ptr[3];
@@ -1293,7 +1313,7 @@ icvYMLParse( CvFileStorage* fs )
                 else if( is_first )
                     break;
             }
-            else if( isalnum(*ptr) || *ptr=='_')
+            else if( cv_isalnum(*ptr) || *ptr=='_')
             {
                 if( !is_first )
                     CV_PARSE_ERROR( "The YAML streams must start with '---', except the first one" );
@@ -1397,7 +1417,7 @@ icvYMLWrite( CvFileStorage* fs, const char* key, const char* data )
 
     if( key )
     {
-        if( !isalpha(key[0]) && key[0] != '_' )
+        if( !cv_isalpha(key[0]) && key[0] != '_' )
             CV_Error( CV_StsBadArg, "Key must start with a letter or _" );
 
         ptr = icvFSResizeWriteBuffer( fs, ptr, keylen );
@@ -1407,7 +1427,7 @@ icvYMLWrite( CvFileStorage* fs, const char* key, const char* data )
             int c = key[i];
 
             ptr[i] = (char)c;
-            if( !isalnum(c) && c != '-' && c != '_' && c != ' ' )
+            if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' )
                 CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" );
         }
 
@@ -1565,11 +1585,11 @@ icvYMLWriteString( CvFileStorage* fs, const char* key,
         {
             char c = str[i];
 
-            if( !need_quote && !isalnum(c) && c != '_' && c != ' ' && c != '-' &&
+            if( !need_quote && !cv_isalnum(c) && c != '_' && c != ' ' && c != '-' &&
                 c != '(' && c != ')' && c != '/' && c != '+' && c != ';' )
                 need_quote = 1;
 
-            if( !isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') )
+            if( !cv_isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') )
             {
                 *data++ = '\\';
                 if( cv_isprint(c) )
@@ -1589,7 +1609,7 @@ icvYMLWriteString( CvFileStorage* fs, const char* key,
             else
                 *data++ = c;
         }
-        if( !need_quote && (isdigit(str[0]) ||
+        if( !need_quote && (cv_isdigit(str[0]) ||
             str[0] == '+' || str[0] == '-' || str[0] == '.' ))
             need_quote = 1;
 
@@ -1754,7 +1774,7 @@ icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
         char c = *ptr, d;
         char* endptr;
 
-        if( isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
+        if( cv_isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
         {
             ptr = icvXMLSkipSpaces( fs, ptr, 0 );
             have_space = 1;
@@ -1842,18 +1862,18 @@ icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
             }
 
             if( value_type != CV_NODE_STRING &&
-                (isdigit(c) || ((c == '-' || c == '+') &&
-                (isdigit(d) || d == '.')) || (c == '.' && isalnum(d))) ) // a number
+                (cv_isdigit(c) || ((c == '-' || c == '+') &&
+                (cv_isdigit(d) || d == '.')) || (c == '.' && cv_isalnum(d))) ) // a number
             {
                 double fval;
                 int ival;
                 endptr = ptr + (c == '-' || c == '+');
-                while( isdigit(*endptr) )
+                while( cv_isdigit(*endptr) )
                     endptr++;
                 if( *endptr == '.' || *endptr == 'e' )
                 {
                     fval = icv_strtod( fs, ptr, &endptr );
-                    /*if( endptr == ptr || isalpha(*endptr) )
+                    /*if( endptr == ptr || cv_isalpha(*endptr) )
                         icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
                     elem->tag = CV_NODE_REAL;
                     elem->data.f = fval;
@@ -1884,7 +1904,7 @@ icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
                 for( ;; )
                 {
                     c = *++ptr;
-                    if( !isalnum(c) )
+                    if( !cv_isalnum(c) )
                     {
                         if( c == '\"' )
                         {
@@ -1893,7 +1913,7 @@ icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
                             ++ptr;
                             break;
                         }
-                        else if( !cv_isprint(c) || c == '<' || (!is_quoted && isspace(c)))
+                        else if( !cv_isprint(c) || c == '<' || (!is_quoted && cv_isspace(c)))
                         {
                             if( is_quoted )
                                 CV_PARSE_ERROR( "Closing \" is expected" );
@@ -1924,7 +1944,7 @@ icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
                             {
                                 endptr = ptr;
                                 do c = *++endptr;
-                                while( isalnum(c) );
+                                while( cv_isalnum(c) );
                                 if( c != ';' )
                                     CV_PARSE_ERROR( "Invalid character in the symbol entity name" );
                                 len = (int)(endptr - ptr);
@@ -2001,7 +2021,7 @@ icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
         CV_PARSE_ERROR( "Tag should start with \'<\'" );
 
     ptr++;
-    if( isalnum(*ptr) || *ptr == '_' )
+    if( cv_isalnum(*ptr) || *ptr == '_' )
         tag_type = CV_XML_OPENING_TAG;
     else if( *ptr == '/' )
     {
@@ -2026,12 +2046,12 @@ icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
     {
         CvStringHashNode* attrname;
 
-        if( !isalpha(*ptr) && *ptr != '_' )
+        if( !cv_isalpha(*ptr) && *ptr != '_' )
             CV_PARSE_ERROR( "Name should start with a letter or underscore" );
 
         endptr = ptr - 1;
         do c = *++endptr;
-        while( isalnum(c) || c == '_' || c == '-' );
+        while( cv_isalnum(c) || c == '_' || c == '-' );
 
         attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 );
         ptr = endptr;
@@ -2085,7 +2105,7 @@ icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
         }
 
         c = *ptr;
-        have_space = isspace(c) || c == '\0';
+        have_space = cv_isspace(c) || c == '\0';
 
         if( c != '>' )
         {
@@ -2228,14 +2248,14 @@ icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList lis
         *ptr++ = '/';
     }
 
-    if( !isalpha(key[0]) && key[0] != '_' )
+    if( !cv_isalpha(key[0]) && key[0] != '_' )
         CV_Error( CV_StsBadArg, "Key should start with a letter or _" );
 
     ptr = icvFSResizeWriteBuffer( fs, ptr, len );
     for( i = 0; i < len; i++ )
     {
         char c = key[i];
-        if( !isalnum(c) && c != '_' && c != '-' )
+        if( !cv_isalnum(c) && c != '_' && c != '-' )
             CV_Error( CV_StsBadArg, "Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_'" );
         ptr[i] = c;
     }
@@ -2478,7 +2498,7 @@ icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quot
             else
                 *data++ = c;
         }
-        if( !need_quote && (isdigit(str[0]) ||
+        if( !need_quote && (cv_isdigit(str[0]) ||
             str[0] == '+' || str[0] == '-' || str[0] == '.' ))
             need_quote = 1;
 
@@ -2595,7 +2615,7 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags )
     char compression = '\0';
 
     if( dot_pos && dot_pos[1] == 'g' && dot_pos[2] == 'z' &&
-        (dot_pos[3] == '\0' || (isdigit(dot_pos[3]) && dot_pos[4] == '\0')) )
+        (dot_pos[3] == '\0' || (cv_isdigit(dot_pos[3]) && dot_pos[4] == '\0')) )
     {
         if( append )
             CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" );
@@ -2856,10 +2876,10 @@ icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len )
     {
         char c = dt[k];
 
-        if( isdigit(c) )
+        if( cv_isdigit(c) )
         {
             int count = c - '0';
-            if( isdigit(dt[k+1]) )
+            if( cv_isdigit(dt[k+1]) )
             {
                 char* endptr = 0;
                 count = (int)strtol( dt+k, &endptr, 10 );
@@ -4065,7 +4085,7 @@ icvReadSeq( CvFileStorage* fs, CvFileNode* node )
 
     flags = CV_SEQ_MAGIC_VAL;
     
-    if( isdigit(flags_str[0]) )
+    if( cv_isdigit(flags_str[0]) )
     {
         const int OLD_SEQ_ELTYPE_BITS = 9;
         const int OLD_SEQ_ELTYPE_MASK = (1 << OLD_SEQ_ELTYPE_BITS) - 1;
@@ -4495,7 +4515,7 @@ icvReadGraph( CvFileStorage* fs, CvFileNode* node )
             edge_items_per_elem += fmt_pairs[i];
 
         if( edge_dt[2] == 'f' || (edge_dt[2] == '1' && edge_dt[3] == 'f') )
-            dst_edge_dt = edge_dt + 3 + isdigit(edge_dt[2]);
+            dst_edge_dt = edge_dt + 3 + cv_isdigit(edge_dt[2]);
         else
         {
             int val = (int)strtol( edge_dt + 2, &endptr, 10 );
@@ -4671,7 +4691,7 @@ cvRegisterType( const CvTypeInfo* _info )
         "(is_instance, release, read or write) are NULL");
 
     c = _info->type_name[0];
-    if( !isalpha(c) && c != '_' )
+    if( !cv_isalpha(c) && c != '_' )
         CV_Error( CV_StsBadArg, "Type name should start with a letter or _" );
 
     len = (int)strlen(_info->type_name);
@@ -4679,7 +4699,7 @@ cvRegisterType( const CvTypeInfo* _info )
     for( i = 0; i < len; i++ )
     {
         c = _info->type_name[i];
-        if( !isalnum(c) && c != '-' && c != '_' )
+        if( !cv_isalnum(c) && c != '-' && c != '_' )
             CV_Error( CV_StsBadArg,
             "Type name should contain only letters, digits, - and _" );
     }
@@ -4959,7 +4979,7 @@ static void getElemSize( const string& fmt, size_t& elemSize, size_t& cn )
 {
     const char* dt = fmt.c_str();
     cn = 1;
-    if( isdigit(dt[0]) )
+    if( cv_isdigit(dt[0]) )
     {
         cn = dt[0] - '0';
         dt++;
@@ -5045,7 +5065,7 @@ FileStorage& operator << (FileStorage& fs, const string& str)
     }
     else if( fs.state == NAME_EXPECTED + INSIDE_MAP )
     {
-        if( !isalpha(*_str) )
+        if( !cv_isalpha(*_str) )
             CV_Error_( CV_StsError, ("Incorrect element name %s", _str) );
         fs.elname = str;
         fs.state = VALUE_EXPECTED + INSIDE_MAP;