in modules/core/src/persistence.cpp [1822:2060]
int value_type CV_DEFAULT(CV_NODE_NONE))
{
CvFileNode *elem = node;
int have_space = 1, is_simple = 1;
int is_user_type = CV_NODE_IS_USER(value_type);
memset( node, 0, sizeof(*node) );
value_type = CV_NODE_TYPE(value_type);
for(;;)
{
char c = *ptr, d;
char* endptr;
if( cv_isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
{
ptr = icvXMLSkipSpaces( fs, ptr, 0 );
have_space = 1;
c = *ptr;
}
d = ptr[1];
if( c =='<' || c == '\0' )
{
CvStringHashNode *key = 0, *key2 = 0;
CvAttrList* list = 0;
CvTypeInfo* info = 0;
int tag_type = 0;
int is_noname = 0;
const char* type_name = 0;
int elem_type = CV_NODE_NONE;
if( d == '/' || c == '\0' )
break;
ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
if( tag_type == CV_XML_DIRECTIVE_TAG )
CV_PARSE_ERROR( "Directive tags are not allowed here" );
if( tag_type == CV_XML_EMPTY_TAG )
CV_PARSE_ERROR( "Empty tags are not supported" );
assert( tag_type == CV_XML_OPENING_TAG );
type_name = list ? cvAttrValue( list, "type_id" ) : 0;
if( type_name )
{
if( strcmp( type_name, "str" ) == 0 )
elem_type = CV_NODE_STRING;
else if( strcmp( type_name, "map" ) == 0 )
elem_type = CV_NODE_MAP;
else if( strcmp( type_name, "seq" ) == 0 )
elem_type = CV_NODE_SEQ;
else
{
info = cvFindType( type_name );
if( info )
elem_type = CV_NODE_USER;
}
}
is_noname = key->str.len == 1 && key->str.ptr[0] == '_';
if( !CV_NODE_IS_COLLECTION(node->tag) )
{
icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node );
}
else if( is_noname ^ CV_NODE_IS_SEQ(node->tag) )
CV_PARSE_ERROR( is_noname ? "Map element should have a name" :
"Sequence element should not have name (use <_></_>)" );
if( is_noname )
elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
else
elem = cvGetFileNode( fs, node, key, 1 );
ptr = icvXMLParseValue( fs, ptr, elem, elem_type);
if( !is_noname )
elem->tag |= CV_NODE_NAMED;
is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
elem->info = info;
ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type );
if( tag_type != CV_XML_CLOSING_TAG || key2 != key )
CV_PARSE_ERROR( "Mismatched closing tag" );
have_space = 1;
}
else
{
if( !have_space )
CV_PARSE_ERROR( "There should be space between literals" );
elem = node;
if( node->tag != CV_NODE_NONE )
{
if( !CV_NODE_IS_COLLECTION(node->tag) )
icvFSCreateCollection( fs, CV_NODE_SEQ, node );
elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
elem->info = 0;
}
if( value_type != CV_NODE_STRING &&
(cv_isdigit(c) || ((c == '-' || c == '+') &&
(cv_isdigit(d) || d == '.')) || (c == '.' && cv_isalnum(d))) ) // a number
{
double fval;
int ival;
endptr = ptr + (c == '-' || c == '+');
while( cv_isdigit(*endptr) )
endptr++;
if( *endptr == '.' || *endptr == 'e' )
{
fval = icv_strtod( fs, ptr, &endptr );
/*if( endptr == ptr || cv_isalpha(*endptr) )
icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
elem->tag = CV_NODE_REAL;
elem->data.f = fval;
}
else
{
ival = (int)strtol( ptr, &endptr, 0 );
elem->tag = CV_NODE_INT;
elem->data.i = ival;
}
if( endptr == ptr )
CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
ptr = endptr;
}
else
{
// string
char buf[CV_FS_MAX_LEN+16];
int i = 0, len, is_quoted = 0;
elem->tag = CV_NODE_STRING;
if( c == '\"' )
is_quoted = 1;
else
--ptr;
for( ;; )
{
c = *++ptr;
if( !cv_isalnum(c) )
{
if( c == '\"' )
{
if( !is_quoted )
CV_PARSE_ERROR( "Literal \" is not allowed within a string. Use "" );
++ptr;
break;
}
else if( !cv_isprint(c) || c == '<' || (!is_quoted && cv_isspace(c)))
{
if( is_quoted )
CV_PARSE_ERROR( "Closing \" is expected" );
break;
}
else if( c == '\'' || c == '>' )
{
CV_PARSE_ERROR( "Literal \' or > are not allowed. Use ' or >" );
}
else if( c == '&' )
{
if( *++ptr == '#' )
{
int val, base = 10;
ptr++;
if( *ptr == 'x' )
{
base = 16;
ptr++;
}
val = (int)strtol( ptr, &endptr, base );
if( (unsigned)val > (unsigned)255 ||
!endptr || *endptr != ';' )
CV_PARSE_ERROR( "Invalid numeric value in the string" );
c = (char)val;
}
else
{
endptr = ptr;
do c = *++endptr;
while( cv_isalnum(c) );
if( c != ';' )
CV_PARSE_ERROR( "Invalid character in the symbol entity name" );
len = (int)(endptr - ptr);
if( len == 2 && memcmp( ptr, "lt", len ) == 0 )
c = '<';
else if( len == 2 && memcmp( ptr, "gt", len ) == 0 )
c = '>';
else if( len == 3 && memcmp( ptr, "amp", len ) == 0 )
c = '&';
else if( len == 4 && memcmp( ptr, "apos", len ) == 0 )
c = '\'';
else if( len == 4 && memcmp( ptr, "quot", len ) == 0 )
c = '\"';
else
{
memcpy( buf + i, ptr-1, len + 2 );
i += len + 2;
}
}
ptr = endptr;
}
}
buf[i++] = c;
if( i >= CV_FS_MAX_LEN )
CV_PARSE_ERROR( "Too long string literal" );
}
elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i );
}
if( !CV_NODE_IS_COLLECTION(value_type) && value_type != CV_NODE_NONE )
break;
have_space = 0;
}
}
if( (CV_NODE_TYPE(node->tag) == CV_NODE_NONE ||
(CV_NODE_TYPE(node->tag) != value_type &&
!CV_NODE_IS_COLLECTION(node->tag))) &&
CV_NODE_IS_COLLECTION(value_type) )
{
icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ?
CV_NODE_MAP : CV_NODE_SEQ, node );
}
if( value_type != CV_NODE_NONE &&
value_type != CV_NODE_TYPE(node->tag) )
CV_PARSE_ERROR( "The actual type is different from the specified type" );
if( CV_NODE_IS_COLLECTION(node->tag) && is_simple )
node->data.seq->flags |= CV_NODE_SEQ_SIMPLE;
node->tag |= is_user_type ? CV_NODE_USER : 0;
return ptr;
}