do not use system is<alpha|alnum|space|...>, because of the different implementations (and in OpenCV we assume english names of the identifiers etc)

pull/13383/head
Vadim Pisarevsky 14 years ago
parent 76e4c2007b
commit 309bb171b2
  1. 106
      modules/core/src/persistence.cpp

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

Loading…
Cancel
Save