Home | History | Annotate | Download | only in src
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                        Intel License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
     14 // Third party copyrights are property of their respective owners.
     15 //
     16 // Redistribution and use in source and binary forms, with or without modification,
     17 // are permitted provided that the following conditions are met:
     18 //
     19 //   * Redistribution's of source code must retain the above copyright notice,
     20 //     this list of conditions and the following disclaimer.
     21 //
     22 //   * Redistribution's in binary form must reproduce the above copyright notice,
     23 //     this list of conditions and the following disclaimer in the documentation
     24 //     and/or other materials provided with the distribution.
     25 //
     26 //   * The name of Intel Corporation may not be used to endorse or promote products
     27 //     derived from this software without specific prior written permission.
     28 //
     29 // This software is provided by the copyright holders and contributors "as is" and
     30 // any express or implied warranties, including, but not limited to, the implied
     31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     32 // In no event shall the Intel Corporation or contributors be liable for any direct,
     33 // indirect, incidental, special, exemplary, or consequential damages
     34 // (including, but not limited to, procurement of substitute goods or services;
     35 // loss of use, data, or profits; or business interruption) however caused
     36 // and on any theory of liability, whether in contract, strict liability,
     37 // or tort (including negligence or otherwise) arising in any way out of
     38 // the use of this software, even if advised of the possibility of such damage.
     39 //
     40 //M*/
     41 
     42 #include "_cxcore.h"
     43 #include <ctype.h>
     44 
     45 /****************************************************************************************\
     46 *                            Common macros and type definitions                          *
     47 \****************************************************************************************/
     48 
     49 #define cv_isprint(c)     ((signed char)(c) >= (signed char)' ')
     50 #define cv_isprint_or_tab(c)  ((signed char)(c) >= (signed char)' ' || (c) == '\t')
     51 
     52 static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
     53 {
     54     const int radix = 10;
     55     char* ptr=buffer + 23 /* enough even for 64-bit integers */;
     56     unsigned val = abs(_val);
     57 
     58     *ptr = '\0';
     59     do
     60     {
     61         unsigned r = val / radix;
     62         *--ptr = (char)(val - (r*radix) + '0');
     63         val = r;
     64     }
     65     while( val != 0 );
     66 
     67     if( _val < 0 )
     68         *--ptr = '-';
     69 
     70     return ptr;
     71 }
     72 
     73 
     74 typedef struct CvGenericHash
     75 {
     76     CV_SET_FIELDS()
     77     int tab_size;
     78     void** table;
     79 }
     80 CvGenericHash;
     81 
     82 typedef CvGenericHash CvStringHash;
     83 
     84 typedef struct CvFileMapNode
     85 {
     86     CvFileNode value;
     87     const CvStringHashNode* key;
     88     struct CvFileMapNode* next;
     89 }
     90 CvFileMapNode;
     91 
     92 typedef struct CvXMLStackRecord
     93 {
     94     CvMemStoragePos pos;
     95     CvString struct_tag;
     96     int struct_indent;
     97     int struct_flags;
     98 }
     99 CvXMLStackRecord;
    100 
    101 #define CV_XML_OPENING_TAG 1
    102 #define CV_XML_CLOSING_TAG 2
    103 #define CV_XML_EMPTY_TAG 3
    104 #define CV_XML_HEADER_TAG 4
    105 #define CV_XML_DIRECTIVE_TAG 5
    106 
    107 //typedef void (*CvParse)( struct CvFileStorage* fs );
    108 typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key,
    109                                     int struct_flags, const char* type_name );
    110 typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs );
    111 typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value );
    112 typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value );
    113 typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key,
    114                                const char* value, int quote );
    115 typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment );
    116 typedef void (*CvStartNextStream)( struct CvFileStorage* fs );
    117 
    118 typedef struct CvFileStorage
    119 {
    120     int flags;
    121     int is_xml;
    122     int write_mode;
    123     int is_first;
    124     CvMemStorage* memstorage;
    125     CvMemStorage* dststorage;
    126     CvMemStorage* strstorage;
    127     CvStringHash* str_hash;
    128     CvSeq* roots;
    129     CvSeq* write_stack;
    130     int struct_indent;
    131     int struct_flags;
    132     CvString struct_tag;
    133     int space;
    134     char* filename;
    135     FILE* file;
    136     char* buffer;
    137     char* buffer_start;
    138     char* buffer_end;
    139     int wrap_margin;
    140     int lineno;
    141     int dummy_eof;
    142     const char* errmsg;
    143     char errmsgbuf[128];
    144 
    145     CvStartWriteStruct start_write_struct;
    146     CvEndWriteStruct end_write_struct;
    147     CvWriteInt write_int;
    148     CvWriteReal write_real;
    149     CvWriteString write_string;
    150     CvWriteComment write_comment;
    151     CvStartNextStream start_next_stream;
    152     //CvParse parse;
    153 }
    154 CvFileStorage;
    155 
    156 
    157 #define CV_YML_INDENT  3
    158 #define CV_XML_INDENT  2
    159 #define CV_YML_INDENT_FLOW  1
    160 #define CV_FS_MAX_LEN 4096
    161 
    162 #define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24))
    163 #define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE)
    164 
    165 #define CV_CHECK_FILE_STORAGE(fs)                       \
    166 {                                                       \
    167     if( !CV_IS_FILE_STORAGE(fs) )                       \
    168         CV_ERROR( (fs) ? CV_StsBadArg : CV_StsNullPtr,  \
    169                   "Invalid pointer to file storage" );  \
    170 }
    171 
    172 #define CV_CHECK_OUTPUT_FILE_STORAGE(fs)                \
    173 {                                                       \
    174     CV_CHECK_FILE_STORAGE(fs);                          \
    175     if( !fs->write_mode )                               \
    176         CV_ERROR( CV_StsError, "The file storage is opened for reading" ); \
    177 }
    178 
    179 CV_IMPL const char*
    180 cvAttrValue( const CvAttrList* attr, const char* attr_name )
    181 {
    182     while( attr && attr->attr )
    183     {
    184         int i;
    185         for( i = 0; attr->attr[i*2] != 0; i++ )
    186         {
    187             if( strcmp( attr_name, attr->attr[i*2] ) == 0 )
    188                 return attr->attr[i*2+1];
    189         }
    190         attr = attr->next;
    191     }
    192 
    193     return 0;
    194 }
    195 
    196 
    197 static CvGenericHash*
    198 cvCreateMap( int flags, int header_size, int elem_size,
    199              CvMemStorage* storage, int start_tab_size )
    200 {
    201     CvGenericHash* map = 0;
    202 
    203     CV_FUNCNAME( "cvCreateMap" );
    204 
    205     __BEGIN__;
    206 
    207     if( header_size < (int)sizeof(CvGenericHash) )
    208         CV_ERROR( CV_StsBadSize, "Too small map header_size" );
    209 
    210     if( start_tab_size <= 0 )
    211         start_tab_size = 16;
    212 
    213     CV_CALL( map = (CvGenericHash*)cvCreateSet( flags, header_size, elem_size, storage ));
    214 
    215     map->tab_size = start_tab_size;
    216     start_tab_size *= sizeof(map->table[0]);
    217     CV_CALL( map->table = (void**)cvMemStorageAlloc( storage, start_tab_size ));
    218     memset( map->table, 0, start_tab_size );
    219 
    220     __END__;
    221 
    222     if( cvGetErrStatus() < 0 )
    223         map = 0;
    224 
    225     return map;
    226 }
    227 
    228 
    229 #define CV_PARSE_ERROR( errmsg )                                    \
    230 {                                                                   \
    231     icvParseError( fs, cvFuncName, (errmsg), __FILE__, __LINE__ );  \
    232     EXIT;                                                           \
    233 }
    234 
    235 
    236 static void
    237 icvParseError( CvFileStorage* fs, const char* func_name,
    238                const char* err_msg, const char* source_file, int source_line )
    239 {
    240     char buf[1<<10];
    241     sprintf( buf, "%s(%d): %s", fs->filename, fs->lineno, err_msg );
    242     cvError( CV_StsParseError, func_name, buf, source_file, source_line );
    243 }
    244 
    245 
    246 static void
    247 icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection )
    248 {
    249     CV_FUNCNAME( "icvFSCreateCollection" );
    250 
    251     __BEGIN__;
    252 
    253     if( CV_NODE_IS_MAP(tag) )
    254     {
    255         if( collection->tag != CV_NODE_NONE )
    256         {
    257             assert( fs->is_xml != 0 );
    258             CV_PARSE_ERROR( "Sequence element should not have name (use <_></_>)" );
    259         }
    260 
    261         CV_CALL( collection->data.map = cvCreateMap( 0, sizeof(CvFileNodeHash),
    262                             sizeof(CvFileMapNode), fs->memstorage, 16 ));
    263     }
    264     else
    265     {
    266         CvSeq* seq;
    267         CV_CALL( seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage ));
    268 
    269         // if <collection> contains some scalar element, add it to the newly created collection
    270         if( CV_NODE_TYPE(collection->tag) != CV_NODE_NONE )
    271             cvSeqPush( seq, collection );
    272 
    273         collection->data.seq = seq;
    274     }
    275 
    276     collection->tag = tag;
    277     cvSetSeqBlockSize( collection->data.seq, 8 );
    278 
    279     __END__;
    280 }
    281 
    282 
    283 /*static void
    284 icvFSReleaseCollection( CvSeq* seq )
    285 {
    286     if( seq )
    287     {
    288         int is_map = CV_IS_SET(seq);
    289         CvSeqReader reader;
    290         int i, total = seq->total;
    291         cvStartReadSeq( seq, &reader, 0 );
    292 
    293         for( i = 0; i < total; i++ )
    294         {
    295             CvFileNode* node = (CvFileNode*)reader.ptr;
    296 
    297             if( (!is_map || CV_IS_SET_ELEM( node )) && CV_NODE_IS_COLLECTION(node->tag) )
    298             {
    299                 if( CV_NODE_IS_USER(node->tag) && node->info && node->data.obj.decoded )
    300                     cvRelease( (void**)&node->data.obj.decoded );
    301                 if( !CV_NODE_SEQ_IS_SIMPLE( node->data.seq ))
    302                     icvFSReleaseCollection( node->data.seq );
    303             }
    304             CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
    305         }
    306     }
    307 }*/
    308 
    309 
    310 static char*
    311 icvFSDoResize( CvFileStorage* fs, char* ptr, int len )
    312 {
    313     char* new_ptr = 0;
    314     CV_FUNCNAME( "icvFSDoResize" );
    315 
    316     __BEGIN__;
    317 
    318     int written_len = (int)(ptr - fs->buffer_start);
    319     int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2);
    320     new_size = MAX( written_len + len, new_size );
    321     CV_CALL( new_ptr = (char*)cvAlloc( new_size + 256 ));
    322     fs->buffer = new_ptr + (fs->buffer - fs->buffer_start);
    323     if( written_len > 0 )
    324         memcpy( new_ptr, fs->buffer_start, written_len );
    325     fs->buffer_start = new_ptr;
    326     fs->buffer_end = fs->buffer_start + new_size;
    327     new_ptr += written_len;
    328 
    329     __END__;
    330 
    331     return new_ptr;
    332 }
    333 
    334 
    335 inline char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len )
    336 {
    337     return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len );
    338 }
    339 
    340 
    341 static char*
    342 icvFSFlush( CvFileStorage* fs )
    343 {
    344     char* ptr = fs->buffer;
    345     int indent;
    346 
    347     if( ptr > fs->buffer_start + fs->space )
    348     {
    349         ptr[0] = '\n';
    350         ptr[1] = '\0';
    351         fputs( fs->buffer_start, fs->file );
    352         fs->buffer = fs->buffer_start;
    353     }
    354 
    355     indent = fs->struct_indent;
    356 
    357     if( fs->space != indent )
    358     {
    359         if( fs->space < indent )
    360             memset( fs->buffer_start + fs->space, ' ', indent - fs->space );
    361         fs->space = indent;
    362     }
    363 
    364     ptr = fs->buffer = fs->buffer_start + fs->space;
    365 
    366     return ptr;
    367 }
    368 
    369 
    370 /* closes file storage and deallocates buffers */
    371 CV_IMPL  void
    372 cvReleaseFileStorage( CvFileStorage** p_fs )
    373 {
    374     CV_FUNCNAME("cvReleaseFileStorage" );
    375 
    376     __BEGIN__;
    377 
    378     if( !p_fs )
    379         CV_ERROR( CV_StsNullPtr, "NULL double pointer to file storage" );
    380 
    381     if( *p_fs )
    382     {
    383         CvFileStorage* fs = *p_fs;
    384         *p_fs = 0;
    385 
    386         if( fs->write_mode && fs->file )
    387         {
    388             if( fs->write_stack )
    389             {
    390                 while( fs->write_stack->total > 0 )
    391                     cvEndWriteStruct(fs);
    392             }
    393             icvFSFlush(fs);
    394             if( fs->is_xml )
    395                 fputs("</opencv_storage>\n", fs->file );
    396         }
    397 
    398         //icvFSReleaseCollection( fs->roots ); // delete all the user types recursively
    399 
    400         if( fs->file )
    401         {
    402             fclose( fs->file );
    403             fs->file = 0;
    404         }
    405 
    406         cvReleaseMemStorage( &fs->strstorage );
    407 
    408         cvFree( &fs->buffer_start );
    409         cvReleaseMemStorage( &fs->memstorage );
    410 
    411         memset( fs, 0, sizeof(*fs) );
    412         cvFree( &fs );
    413     }
    414 
    415     __END__;
    416 }
    417 
    418 
    419 #define CV_HASHVAL_SCALE 33
    420 
    421 CV_IMPL CvStringHashNode*
    422 cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing )
    423 {
    424     CvStringHashNode* node = 0;
    425     CV_FUNCNAME( "cvGetHashedKey" );
    426 
    427     __BEGIN__;
    428 
    429     unsigned hashval = 0;
    430     int i, tab_size;
    431     CvStringHash* map = fs->str_hash;
    432 
    433     if( !fs )
    434         EXIT;
    435 
    436     if( len < 0 )
    437     {
    438         for( i = 0; str[i] != '\0'; i++ )
    439             hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
    440         len = i;
    441     }
    442     else for( i = 0; i < len; i++ )
    443         hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
    444 
    445     hashval &= INT_MAX;
    446     tab_size = map->tab_size;
    447     if( (tab_size & (tab_size - 1)) == 0 )
    448         i = (int)(hashval & (tab_size - 1));
    449     else
    450         i = (int)(hashval % tab_size);
    451 
    452     for( node = (CvStringHashNode*)(map->table[i]); node != 0; node = node->next )
    453     {
    454         if( node->hashval == hashval &&
    455             node->str.len == len &&
    456             memcmp( node->str.ptr, str, len ) == 0 )
    457             break;
    458     }
    459 
    460     if( !node && create_missing )
    461     {
    462         node = (CvStringHashNode*)cvSetNew( (CvSet*)map );
    463         node->hashval = hashval;
    464         CV_CALL( node->str = cvMemStorageAllocString( map->storage, str, len ));
    465         node->next = (CvStringHashNode*)(map->table[i]);
    466         map->table[i] = node;
    467     }
    468 
    469     __END__;
    470 
    471     return node;
    472 }
    473 
    474 
    475 CV_IMPL CvFileNode*
    476 cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node,
    477                const CvStringHashNode* key,
    478                int create_missing )
    479 {
    480     CvFileNode* value = 0;
    481 
    482     CV_FUNCNAME( "cvGetFileNode" );
    483 
    484     __BEGIN__;
    485 
    486     int k = 0, attempts = 1;
    487 
    488     if( !fs )
    489         EXIT;
    490 
    491     CV_CHECK_FILE_STORAGE(fs);
    492 
    493     if( !key )
    494         CV_ERROR( CV_StsNullPtr, "Null key element" );
    495 
    496     if( _map_node )
    497     {
    498         if( !fs->roots )
    499             EXIT;
    500         attempts = fs->roots->total;
    501     }
    502 
    503     for( k = 0; k < attempts; k++ )
    504     {
    505         int i, tab_size;
    506         CvFileNode* map_node = _map_node;
    507         CvFileMapNode* another;
    508         CvFileNodeHash* map;
    509 
    510         if( !map_node )
    511             map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
    512 
    513         if( !CV_NODE_IS_MAP(map_node->tag) )
    514         {
    515             if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
    516                 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
    517                 CV_ERROR( CV_StsError, "The node is neither a map nor an empty collection" );
    518             EXIT;
    519         }
    520 
    521         map = map_node->data.map;
    522         tab_size = map->tab_size;
    523 
    524         if( (tab_size & (tab_size - 1)) == 0 )
    525             i = (int)(key->hashval & (tab_size - 1));
    526         else
    527             i = (int)(key->hashval % tab_size);
    528 
    529         for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
    530             if( another->key == key )
    531             {
    532                 if( !create_missing )
    533                 {
    534                     value = &another->value;
    535                     EXIT;
    536                 }
    537                 CV_PARSE_ERROR( "Duplicated key" );
    538             }
    539 
    540         if( k == attempts - 1 && create_missing )
    541         {
    542             CvFileMapNode* node = (CvFileMapNode*)cvSetNew( (CvSet*)map );
    543             node->key = key;
    544 
    545             node->next = (CvFileMapNode*)(map->table[i]);
    546             map->table[i] = node;
    547             value = (CvFileNode*)node;
    548         }
    549     }
    550 
    551     __END__;
    552 
    553     return value;
    554 }
    555 
    556 
    557 CV_IMPL CvFileNode*
    558 cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str )
    559 {
    560     CvFileNode* value = 0;
    561     CV_FUNCNAME( "cvGetFileNodeByName" );
    562 
    563     __BEGIN__;
    564 
    565     int i, len, tab_size;
    566     unsigned hashval = 0;
    567     int k = 0, attempts = 1;
    568 
    569     if( !fs )
    570         EXIT;
    571 
    572     CV_CHECK_FILE_STORAGE(fs);
    573 
    574     if( !str )
    575         CV_ERROR( CV_StsNullPtr, "Null element name" );
    576 
    577     for( i = 0; str[i] != '\0'; i++ )
    578         hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
    579     hashval &= INT_MAX;
    580     len = i;
    581 
    582     if( !_map_node )
    583     {
    584         if( !fs->roots )
    585             EXIT;
    586         attempts = fs->roots->total;
    587     }
    588 
    589     for( k = 0; k < attempts; k++ )
    590     {
    591         CvFileNodeHash* map;
    592         const CvFileNode* map_node = _map_node;
    593         CvFileMapNode* another;
    594 
    595         if( !map_node )
    596             map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
    597 
    598         if( !CV_NODE_IS_MAP(map_node->tag) )
    599         {
    600             if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
    601                 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
    602                 CV_ERROR( CV_StsError, "The node is neither a map nor an empty collection" );
    603             EXIT;
    604         }
    605 
    606         map = map_node->data.map;
    607         tab_size = map->tab_size;
    608 
    609         if( (tab_size & (tab_size - 1)) == 0 )
    610             i = (int)(hashval & (tab_size - 1));
    611         else
    612             i = (int)(hashval % tab_size);
    613 
    614         for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
    615         {
    616             const CvStringHashNode* key = another->key;
    617 
    618             if( key->hashval == hashval &&
    619                 key->str.len == len &&
    620                 memcmp( key->str.ptr, str, len ) == 0 )
    621             {
    622                 value = &another->value;
    623                 EXIT;
    624             }
    625         }
    626     }
    627 
    628     __END__;
    629 
    630     return value;
    631 }
    632 
    633 
    634 CV_IMPL CvFileNode*
    635 cvGetRootFileNode( const CvFileStorage* fs, int stream_index )
    636 {
    637     CvFileNode* value = 0;
    638     CV_FUNCNAME( "cvGetRootFileNode" );
    639 
    640     __BEGIN__;
    641 
    642     CV_CHECK_FILE_STORAGE(fs);
    643 
    644     if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total )
    645         EXIT;
    646 
    647     value = (CvFileNode*)cvGetSeqElem( fs->roots, stream_index );
    648 
    649     __END__;
    650 
    651     return value;
    652 }
    653 
    654 
    655 /* returns the sequence element by its index */
    656 /*CV_IMPL CvFileNode*
    657 cvGetFileNodeFromSeq( CvFileStorage* fs,
    658                       CvFileNode* seq_node, int index )
    659 {
    660     CvFileNode* value = 0;
    661 
    662     CV_FUNCNAME( "cvGetFileNodeFromSeq" );
    663 
    664     __BEGIN__;
    665 
    666     CvSeq* seq;
    667 
    668     if( !seq_node )
    669         seq = fs->roots;
    670     else if( !CV_NODE_IS_SEQ(seq_node->tag) )
    671     {
    672         if( CV_NODE_IS_MAP(seq_node->tag) )
    673             CV_ERROR( CV_StsError, "The node is map. Use cvGetFileNodeFromMap()." );
    674         if( CV_NODE_TYPE(seq_node->tag) == CV_NODE_NONE )
    675             CV_ERROR( CV_StsError, "The node is an empty object (None)." );
    676         if( index != 0 && index != -1 )
    677             CV_ERROR( CV_StsOutOfRange, "" );
    678         value = seq_node;
    679         EXIT;
    680     }
    681     else
    682         seq = seq_node->data.seq;
    683 
    684     if( !seq )
    685         CV_ERROR( CV_StsNullPtr, "The file storage is empty" );
    686 
    687     value = (CvFileNode*)cvGetSeqElem( seq, index, 0 );
    688 
    689     __END__;
    690 
    691     return value;
    692 }*/
    693 
    694 
    695 static char*
    696 icvDoubleToString( char* buf, double value )
    697 {
    698     Cv64suf val;
    699     unsigned ieee754_hi;
    700 
    701     val.f = value;
    702     ieee754_hi = (unsigned)(val.u >> 32);
    703 
    704     if( (ieee754_hi & 0x7ff00000) != 0x7ff00000 )
    705     {
    706         int ivalue = cvRound(value);
    707         if( ivalue == value )
    708             sprintf( buf, "%d.", ivalue );
    709         else
    710         {
    711             static const char* fmt[] = {"%.16e", "%.16f"};
    712             double avalue = fabs(value);
    713             char* ptr = buf;
    714             sprintf( buf, fmt[0.01 <= avalue && avalue < 1000], value );
    715             if( *ptr == '+' || *ptr == '-' )
    716                 ptr++;
    717             for( ; isdigit(*ptr); ptr++ )
    718                 ;
    719             if( *ptr == ',' )
    720                 *ptr = '.';
    721         }
    722     }
    723     else
    724     {
    725         unsigned ieee754_lo = (unsigned)val.u;
    726         if( (ieee754_hi & 0x7fffffff) + (ieee754_lo != 0) > 0x7ff00000 )
    727             strcpy( buf, ".Nan" );
    728         else
    729             strcpy( buf, (int)ieee754_hi < 0 ? "-.Inf" : ".Inf" );
    730     }
    731 
    732     return buf;
    733 }
    734 
    735 
    736 static char*
    737 icvFloatToString( char* buf, float value )
    738 {
    739     Cv32suf val;
    740     unsigned ieee754;
    741     val.f = value;
    742     ieee754 = val.u;
    743 
    744     if( (ieee754 & 0x7f800000) != 0x7f800000 )
    745     {
    746         int ivalue = cvRound(value);
    747         if( ivalue == value )
    748             sprintf( buf, "%d.", ivalue );
    749         else
    750         {
    751             static const char* fmt[] = {"%.8e", "%.8f"};
    752             double avalue = fabs((double)value);
    753             char* ptr = buf;
    754             sprintf( buf, fmt[0.01 <= avalue && avalue < 1000], value );
    755             if( *ptr == '+' || *ptr == '-' )
    756                 ptr++;
    757             for( ; isdigit(*ptr); ptr++ )
    758                 ;
    759             if( *ptr == ',' )
    760                 *ptr = '.';
    761         }
    762     }
    763     else
    764     {
    765         if( (ieee754 & 0x7fffffff) != 0x7f800000 )
    766             strcpy( buf, ".Nan" );
    767         else
    768             strcpy( buf, (int)ieee754 < 0 ? "-.Inf" : ".Inf" );
    769     }
    770 
    771     return buf;
    772 }
    773 
    774 
    775 static void
    776 icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr )
    777 {
    778     CV_FUNCNAME( "icvProcessSpecialDouble" );
    779 
    780     __BEGIN__;
    781 
    782     char c = buf[0];
    783     int inf_hi = 0x7ff00000;
    784 
    785     if( c == '-' || c == '+' )
    786     {
    787         inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000;
    788         c = *++buf;
    789     }
    790 
    791     if( c != '.' )
    792         CV_PARSE_ERROR( "Bad format of floating-point constant" );
    793 
    794     if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' )
    795         *(uint64*)value = ((uint64)inf_hi << 32);
    796     else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' )
    797         *(uint64*)value = (uint64)-1;
    798     else
    799         CV_PARSE_ERROR( "Bad format of floating-point constant" );
    800 
    801     *endptr = buf + 4;
    802 
    803     __END__;
    804 }
    805 
    806 
    807 static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
    808 {
    809     double fval = strtod( ptr, endptr );
    810     if( **endptr == '.' )
    811     {
    812         char* dot_pos = *endptr;
    813         *dot_pos = ',';
    814         double fval2 = strtod( ptr, endptr );
    815         *dot_pos = '.';
    816         if( *endptr > dot_pos )
    817             fval = fval2;
    818         else
    819             *endptr = dot_pos;
    820     }
    821 
    822     if( *endptr == ptr || isalpha(**endptr) )
    823         icvProcessSpecialDouble( fs, ptr, &fval, endptr );
    824 
    825     return fval;
    826 }
    827 
    828 
    829 /****************************************************************************************\
    830 *                                       YAML Parser                                      *
    831 \****************************************************************************************/
    832 
    833 static char*
    834 icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent )
    835 {
    836     CV_FUNCNAME( "icvYMLSkipSpaces" );
    837 
    838     __BEGIN__;
    839 
    840     for(;;)
    841     {
    842         while( *ptr == ' ' )
    843             ptr++;
    844         if( *ptr == '#' )
    845         {
    846             if( ptr - fs->buffer_start > max_comment_indent )
    847                 EXIT;
    848             *ptr = '\0';
    849         }
    850         else if( cv_isprint(*ptr) )
    851         {
    852             if( ptr - fs->buffer_start < min_indent )
    853                 CV_PARSE_ERROR( "Incorrect indentation" );
    854             break;
    855         }
    856         else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' )
    857         {
    858             int max_size = (int)(fs->buffer_end - fs->buffer_start);
    859             ptr = fgets( fs->buffer_start, max_size, fs->file );
    860             if( !ptr )
    861             {
    862                 // emulate end of stream
    863                 ptr = fs->buffer_start;
    864                 ptr[0] = ptr[1] = ptr[2] = '.';
    865                 ptr[3] = '\0';
    866                 fs->dummy_eof = 1;
    867                 break;
    868             }
    869             else
    870             {
    871                 int l = (int)strlen(ptr);
    872                 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !feof(fs->file) )
    873                     CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
    874             }
    875 
    876             fs->lineno++;
    877         }
    878         else
    879             CV_PARSE_ERROR( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" );
    880     }
    881 
    882     __END__;
    883 
    884     return ptr;
    885 }
    886 
    887 
    888 static char*
    889 icvYMLParseKey( CvFileStorage* fs, char* ptr,
    890                 CvFileNode* map_node, CvFileNode** value_placeholder )
    891 {
    892     CV_FUNCNAME( "icvYMLParseKey" );
    893 
    894     __BEGIN__;
    895 
    896     char c;
    897     char *endptr = ptr - 1, *saveptr;
    898     CvStringHashNode* str_hash_node;
    899 
    900     if( *ptr == '-' )
    901         CV_PARSE_ERROR( "Key may not start with \'-\'" );
    902 
    903     do c = *++endptr;
    904     while( cv_isprint(c) && c != ':' );
    905 
    906     if( c != ':' )
    907         CV_PARSE_ERROR( "Missing \':\'" );
    908 
    909     saveptr = endptr + 1;
    910     do c = *--endptr;
    911     while( c == ' ' );
    912 
    913     ++endptr;
    914     if( endptr == ptr )
    915         CV_PARSE_ERROR( "An empty key" );
    916 
    917     CV_CALL( str_hash_node = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ));
    918     CV_CALL( *value_placeholder = cvGetFileNode( fs, map_node, str_hash_node, 1 ));
    919     ptr = saveptr;
    920 
    921     __END__;
    922 
    923     return ptr;
    924 }
    925 
    926 
    927 static char*
    928 icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
    929                   int parent_flags, int min_indent )
    930 {
    931     CV_FUNCNAME( "icvYMLParseValue" );
    932 
    933     __BEGIN__;
    934 
    935     char buf[CV_FS_MAX_LEN + 1024];
    936     char* endptr = 0;
    937     char c = ptr[0], d = ptr[1];
    938     int is_parent_flow = CV_NODE_IS_FLOW(parent_flags);
    939     int value_type = CV_NODE_NONE;
    940     int len;
    941 
    942     memset( node, 0, sizeof(*node) );
    943 
    944     if( c == '!' ) // handle explicit type specification
    945     {
    946         if( d == '!' || d == '^' )
    947         {
    948             ptr++;
    949             value_type |= CV_NODE_USER;
    950         }
    951 
    952         endptr = ptr++;
    953         do d = *++endptr;
    954         while( cv_isprint(d) && d != ' ' );
    955         len = (int)(endptr - ptr);
    956         if( len == 0 )
    957             CV_PARSE_ERROR( "Empty type name" );
    958         d = *endptr;
    959         *endptr = '\0';
    960 
    961         if( len == 3 && !CV_NODE_IS_USER(value_type) )
    962         {
    963             if( memcmp( ptr, "str", 3 ) == 0 )
    964                 value_type = CV_NODE_STRING;
    965             else if( memcmp( ptr, "int", 3 ) == 0 )
    966                 value_type = CV_NODE_INT;
    967             else if( memcmp( ptr, "seq", 3 ) == 0 )
    968                 value_type = CV_NODE_SEQ;
    969             else if( memcmp( ptr, "map", 3 ) == 0 )
    970                 value_type = CV_NODE_MAP;
    971         }
    972         else if( len == 5 && !CV_NODE_IS_USER(value_type) )
    973         {
    974             if( memcmp( ptr, "float", 5 ) == 0 )
    975                 value_type = CV_NODE_REAL;
    976         }
    977         else if( CV_NODE_IS_USER(value_type) )
    978         {
    979             CV_CALL( node->info = cvFindType( ptr ));
    980             if( !node->info )
    981                 node->tag &= ~CV_NODE_USER;
    982         }
    983 
    984         *endptr = d;
    985         CV_CALL( ptr = icvYMLSkipSpaces( fs, endptr, min_indent, INT_MAX ));
    986 
    987         c = *ptr;
    988 
    989         if( !CV_NODE_IS_USER(value_type) )
    990         {
    991             if( value_type == CV_NODE_STRING && c != '\'' && c != '\"' )
    992                 goto force_string;
    993             if( value_type == CV_NODE_INT )
    994                 goto force_int;
    995             if( value_type == CV_NODE_REAL )
    996                 goto force_real;
    997         }
    998     }
    999 
   1000     if( isdigit(c) ||
   1001         ((c == '-' || c == '+') && (isdigit(d) || d == '.')) ||
   1002         (c == '.' && isalnum(d))) // a number
   1003     {
   1004         double fval;
   1005         int ival;
   1006         endptr = ptr + (c == '-' || c == '+');
   1007         while( isdigit(*endptr) )
   1008             endptr++;
   1009         if( *endptr == '.' || *endptr == 'e' )
   1010         {
   1011 force_real:
   1012             fval = icv_strtod( fs, ptr, &endptr );
   1013             /*if( endptr == ptr || isalpha(*endptr) )
   1014                 CV_CALL( icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/
   1015 
   1016             node->tag = CV_NODE_REAL;
   1017             node->data.f = fval;
   1018         }
   1019         else
   1020         {
   1021 force_int:
   1022             ival = (int)strtol( ptr, &endptr, 0 );
   1023             node->tag = CV_NODE_INT;
   1024             node->data.i = ival;
   1025         }
   1026 
   1027         if( !endptr || endptr == ptr )
   1028             CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
   1029 
   1030         ptr = endptr;
   1031     }
   1032     else if( c == '\'' || c == '\"' ) // an explicit string
   1033     {
   1034         node->tag = CV_NODE_STRING;
   1035         if( c == '\'' )
   1036             for( len = 0; len < CV_FS_MAX_LEN; )
   1037             {
   1038                 c = *++ptr;
   1039                 if( isalnum(c) || (c != '\'' && cv_isprint(c)))
   1040                     buf[len++] = c;
   1041                 else if( c == '\'' )
   1042                 {
   1043                     c = *++ptr;
   1044                     if( c != '\'' )
   1045                         break;
   1046                     buf[len++] = c;
   1047                 }
   1048                 else
   1049                     CV_PARSE_ERROR( "Invalid character" );
   1050             }
   1051         else
   1052             for( len = 0; len < CV_FS_MAX_LEN; )
   1053             {
   1054                 c = *++ptr;
   1055                 if( isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c)))
   1056                     buf[len++] = c;
   1057                 else if( c == '\"' )
   1058                 {
   1059                     ++ptr;
   1060                     break;
   1061                 }
   1062                 else if( c == '\\' )
   1063                 {
   1064                     d = *++ptr;
   1065                     if( d == '\'' )
   1066                         buf[len++] = d;
   1067                     else if( d == '\"' || d == '\\' || d == '\'' )
   1068                         buf[len++] = d;
   1069                     else if( d == 'n' )
   1070                         buf[len++] = '\n';
   1071                     else if( d == 'r' )
   1072                         buf[len++] = '\r';
   1073                     else if( d == 't' )
   1074                         buf[len++] = '\t';
   1075                     else if( d == 'x' || (isdigit(d) && d < '8') )
   1076                     {
   1077                         int val, is_hex = d == 'x';
   1078                         c = ptr[3];
   1079                         ptr[3] = '\0';
   1080                         val = strtol( ptr + is_hex, &endptr, is_hex ? 8 : 16 );
   1081                         ptr[3] = c;
   1082                         if( endptr == ptr + is_hex )
   1083                             buf[len++] = 'x';
   1084                         else
   1085                         {
   1086                             buf[len++] = (char)val;
   1087                             ptr = endptr;
   1088                         }
   1089                     }
   1090                 }
   1091                 else
   1092                     CV_PARSE_ERROR( "Invalid character" );
   1093             }
   1094 
   1095         if( len >= CV_FS_MAX_LEN )
   1096             CV_PARSE_ERROR( "Too long string literal" );
   1097 
   1098         CV_CALL( node->data.str = cvMemStorageAllocString( fs->memstorage, buf, len ));
   1099     }
   1100     else if( c == '[' || c == '{' ) // collection as a flow
   1101     {
   1102         int new_min_indent = min_indent + !is_parent_flow;
   1103         int struct_flags = CV_NODE_FLOW + (c == '{' ? CV_NODE_MAP : CV_NODE_SEQ);
   1104         int is_simple = 1;
   1105 
   1106         CV_CALL( icvFSCreateCollection( fs, CV_NODE_TYPE(struct_flags) +
   1107                                         (node->info ? CV_NODE_USER : 0), node ));
   1108 
   1109         d = c == '[' ? ']' : '}';
   1110 
   1111         for( ++ptr ;;)
   1112         {
   1113             CvFileNode* elem = 0;
   1114 
   1115             CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ));
   1116             if( *ptr == '}' || *ptr == ']' )
   1117             {
   1118                 if( *ptr != d )
   1119                     CV_PARSE_ERROR( "The wrong closing bracket" );
   1120                 ptr++;
   1121                 break;
   1122             }
   1123 
   1124             if( node->data.seq->total != 0 )
   1125             {
   1126                 if( *ptr != ',' )
   1127                     CV_PARSE_ERROR( "Missing , between the elements" );
   1128                 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr + 1, new_min_indent, INT_MAX ));
   1129             }
   1130 
   1131             if( CV_NODE_IS_MAP(struct_flags) )
   1132             {
   1133                 CV_CALL( ptr = icvYMLParseKey( fs, ptr, node, &elem ));
   1134                 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ));
   1135             }
   1136             else
   1137             {
   1138                 if( *ptr == ']' )
   1139                     break;
   1140                 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
   1141             }
   1142             CV_CALL( ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, new_min_indent ));
   1143             if( CV_NODE_IS_MAP(struct_flags) )
   1144                 elem->tag |= CV_NODE_NAMED;
   1145             is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
   1146         }
   1147         node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
   1148     }
   1149     else
   1150     {
   1151         int indent, struct_flags, is_simple;
   1152 
   1153         if( is_parent_flow || c != '-' )
   1154         {
   1155             // implicit (one-line) string or nested block-style collection
   1156             if( !is_parent_flow )
   1157             {
   1158                 if( c == '?' )
   1159                     CV_PARSE_ERROR( "Complex keys are not supported" );
   1160                 if( c == '|' || c == '>' )
   1161                     CV_PARSE_ERROR( "Multi-line text literals are not supported" );
   1162             }
   1163 
   1164 force_string:
   1165             endptr = ptr - 1;
   1166 
   1167             do c = *++endptr;
   1168             while( cv_isprint(c) &&
   1169                    (!is_parent_flow || (c != ',' && c != '}' && c != ']')) &&
   1170                    (is_parent_flow || c != ':' || value_type == CV_NODE_STRING));
   1171 
   1172             if( endptr == ptr )
   1173                 CV_PARSE_ERROR( "Invalid character" );
   1174 
   1175             if( is_parent_flow || c != ':' )
   1176             {
   1177                 char* str_end = endptr;
   1178                 node->tag = CV_NODE_STRING;
   1179                 // strip spaces in the end of string
   1180                 do c = *--str_end;
   1181                 while( str_end > ptr && c == ' ' );
   1182                 str_end++;
   1183                 CV_CALL( node->data.str = cvMemStorageAllocString( fs->memstorage, ptr, (int)(str_end - ptr) ));
   1184                 ptr = endptr;
   1185                 EXIT;
   1186             }
   1187             struct_flags = CV_NODE_MAP;
   1188         }
   1189         else
   1190             struct_flags = CV_NODE_SEQ;
   1191 
   1192         CV_CALL( icvFSCreateCollection( fs, struct_flags +
   1193                     (node->info ? CV_NODE_USER : 0), node ));
   1194 
   1195         indent = (int)(ptr - fs->buffer_start);
   1196         is_simple = 1;
   1197 
   1198         for(;;)
   1199         {
   1200             CvFileNode* elem = 0;
   1201 
   1202             if( CV_NODE_IS_MAP(struct_flags) )
   1203             {
   1204                 CV_CALL( ptr = icvYMLParseKey( fs, ptr, node, &elem ));
   1205             }
   1206             else
   1207             {
   1208                 c = *ptr++;
   1209                 if( c != '-' )
   1210                     CV_PARSE_ERROR( "Block sequence elements must be preceded with \'-\'" );
   1211 
   1212                 CV_CALL( elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 ));
   1213             }
   1214 
   1215             CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, indent + 1, INT_MAX ));
   1216             CV_CALL( ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, indent + 1 ));
   1217             if( CV_NODE_IS_MAP(struct_flags) )
   1218                 elem->tag |= CV_NODE_NAMED;
   1219             is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
   1220 
   1221             CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
   1222             if( ptr - fs->buffer_start != indent )
   1223             {
   1224                 if( ptr - fs->buffer_start < indent )
   1225                     break;
   1226                 else
   1227                     CV_PARSE_ERROR( "Incorrect indentation" );
   1228             }
   1229             if( memcmp( ptr, "...", 3 ) == 0 )
   1230                 break;
   1231         }
   1232 
   1233         node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
   1234     }
   1235 
   1236     __END__;
   1237 
   1238     return ptr;
   1239 }
   1240 
   1241 
   1242 static void
   1243 icvYMLParse( CvFileStorage* fs )
   1244 {
   1245     CV_FUNCNAME( "icvYMLParse" );
   1246 
   1247     __BEGIN__;
   1248 
   1249     char* ptr = fs->buffer_start;
   1250     int is_first = 1;
   1251 
   1252     for(;;)
   1253     {
   1254         // 0. skip leading comments and directives  and ...
   1255         // 1. reach the first item
   1256         for(;;)
   1257         {
   1258             CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
   1259             if( !ptr )
   1260                 EXIT;
   1261 
   1262             if( *ptr == '%' )
   1263             {
   1264                 if( memcmp( ptr, "%YAML:", 6 ) == 0 &&
   1265                     memcmp( ptr, "%YAML:1.", 8 ) != 0 )
   1266                     CV_PARSE_ERROR( "Unsupported YAML version (it must be 1.x)" );
   1267                 *ptr = '\0';
   1268             }
   1269             else if( *ptr == '-' )
   1270             {
   1271                 if( memcmp(ptr, "---", 3) == 0 )
   1272                 {
   1273                     ptr += 3;
   1274                     break;
   1275                 }
   1276                 else if( is_first )
   1277                     break;
   1278             }
   1279             else if( isalnum(*ptr) || *ptr=='_')
   1280             {
   1281                 if( !is_first )
   1282                     CV_PARSE_ERROR( "The YAML streams must start with '---', except the first one" );
   1283                 break;
   1284             }
   1285             else
   1286                 CV_PARSE_ERROR( "Invalid or unsupported syntax" );
   1287         }
   1288 
   1289         CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
   1290         if( memcmp( ptr, "...", 3 ) != 0 )
   1291         {
   1292             // 2. parse the collection
   1293             CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
   1294 
   1295             CV_CALL( ptr = icvYMLParseValue( fs, ptr, root_node, CV_NODE_NONE, 0 ));
   1296             if( !CV_NODE_IS_COLLECTION(root_node->tag) )
   1297                 CV_PARSE_ERROR( "Only collections as YAML streams are supported by this parser" );
   1298 
   1299             // 3. parse until the end of file or next collection
   1300             CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
   1301             if( !ptr )
   1302                 EXIT;
   1303         }
   1304 
   1305         if( fs->dummy_eof )
   1306             break;
   1307         ptr += 3;
   1308         is_first = 0;
   1309     }
   1310 
   1311     __END__;
   1312 }
   1313 
   1314 
   1315 /****************************************************************************************\
   1316 *                                       YAML Emitter                                     *
   1317 \****************************************************************************************/
   1318 
   1319 static void
   1320 icvYMLWrite( CvFileStorage* fs, const char* key, const char* data, const char* cvFuncName )
   1321 {
   1322     //CV_FUNCNAME( "icvYMLWrite" );
   1323 
   1324     __BEGIN__;
   1325 
   1326     int i, keylen = 0;
   1327     int datalen = 0;
   1328     int struct_flags;
   1329     char* ptr;
   1330 
   1331     struct_flags = fs->struct_flags;
   1332 
   1333     if( key && key[0] == '\0' )
   1334         key = 0;
   1335 
   1336     if( CV_NODE_IS_COLLECTION(struct_flags) )
   1337     {
   1338         if( (CV_NODE_IS_MAP(struct_flags) ^ (key != 0)) )
   1339             CV_ERROR( CV_StsBadArg, "An attempt to add element without a key to a map, "
   1340                                     "or add element with key to sequence" );
   1341     }
   1342     else
   1343     {
   1344         fs->is_first = 0;
   1345         struct_flags = CV_NODE_EMPTY | (key ? CV_NODE_MAP : CV_NODE_SEQ);
   1346     }
   1347 
   1348     if( key )
   1349     {
   1350         keylen = (int)strlen(key);
   1351         if( keylen == 0 )
   1352             CV_ERROR( CV_StsBadArg, "The key is an empty" );
   1353 
   1354         if( keylen > CV_FS_MAX_LEN )
   1355             CV_ERROR( CV_StsBadArg, "The key is too long" );
   1356     }
   1357 
   1358     if( data )
   1359         datalen = (int)strlen(data);
   1360 
   1361     if( CV_NODE_IS_FLOW(struct_flags) )
   1362     {
   1363         int new_offset;
   1364         ptr = fs->buffer;
   1365         if( !CV_NODE_IS_EMPTY(struct_flags) )
   1366             *ptr++ = ',';
   1367         new_offset = (int)(ptr - fs->buffer_start) + keylen + datalen;
   1368         if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 )
   1369         {
   1370             fs->buffer = ptr;
   1371             ptr = icvFSFlush(fs);
   1372         }
   1373         else
   1374             *ptr++ = ' ';
   1375     }
   1376     else
   1377     {
   1378         ptr = icvFSFlush(fs);
   1379         if( !CV_NODE_IS_MAP(struct_flags) )
   1380         {
   1381             *ptr++ = '-';
   1382             if( data )
   1383                 *ptr++ = ' ';
   1384         }
   1385     }
   1386 
   1387     if( key )
   1388     {
   1389         if( !isalpha(key[0]) && key[0] != '_' )
   1390             CV_ERROR( CV_StsBadArg, "Key must start with a letter or _" );
   1391 
   1392         ptr = icvFSResizeWriteBuffer( fs, ptr, keylen );
   1393 
   1394         for( i = 0; i < keylen; i++ )
   1395         {
   1396             int c = key[i];
   1397 
   1398             ptr[i] = (char)c;
   1399             if( !isalnum(c) && c != '-' && c != '_' && c != ' ' )
   1400                 CV_ERROR( CV_StsBadArg, "Invalid character occurs in the key" );
   1401         }
   1402 
   1403         ptr += keylen;
   1404         *ptr++ = ':';
   1405         if( !CV_NODE_IS_FLOW(struct_flags) && data )
   1406             *ptr++ = ' ';
   1407     }
   1408 
   1409     if( data )
   1410     {
   1411         ptr = icvFSResizeWriteBuffer( fs, ptr, datalen );
   1412         memcpy( ptr, data, datalen );
   1413         ptr += datalen;
   1414     }
   1415 
   1416     fs->buffer = ptr;
   1417     fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
   1418 
   1419     __END__;
   1420 }
   1421 
   1422 
   1423 static void
   1424 icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
   1425                         const char* type_name CV_DEFAULT(0))
   1426 {
   1427     CV_FUNCNAME( "icvYMLStartWriteStruct" );
   1428 
   1429     __BEGIN__;
   1430 
   1431     int parent_flags;
   1432     char buf[CV_FS_MAX_LEN + 1024];
   1433     const char* data = 0;
   1434 
   1435     struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
   1436     if( !CV_NODE_IS_COLLECTION(struct_flags))
   1437         CV_ERROR( CV_StsBadArg,
   1438         "Some collection type - CV_NODE_SEQ or CV_NODE_MAP, must be specified" );
   1439 
   1440     if( CV_NODE_IS_FLOW(struct_flags) )
   1441     {
   1442         char c = CV_NODE_IS_MAP(struct_flags) ? '{' : '[';
   1443         struct_flags |= CV_NODE_FLOW;
   1444 
   1445         if( type_name )
   1446             sprintf( buf, "!!%s %c", type_name, c );
   1447         else
   1448         {
   1449             buf[0] = c;
   1450             buf[1] = '\0';
   1451         }
   1452         data = buf;
   1453     }
   1454     else if( type_name )
   1455     {
   1456         sprintf( buf, "!!%s", type_name );
   1457         data = buf;
   1458     }
   1459 
   1460     CV_CALL( icvYMLWrite( fs, key, data, cvFuncName ));
   1461 
   1462     parent_flags = fs->struct_flags;
   1463     cvSeqPush( fs->write_stack, &parent_flags );
   1464     fs->struct_flags = struct_flags;
   1465 
   1466     if( !CV_NODE_IS_FLOW(parent_flags) )
   1467         fs->struct_indent += CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
   1468 
   1469     __END__;
   1470 }
   1471 
   1472 
   1473 static void
   1474 icvYMLEndWriteStruct( CvFileStorage* fs )
   1475 {
   1476     CV_FUNCNAME( "icvYMLEndWriteStruct" );
   1477 
   1478     __BEGIN__;
   1479 
   1480     int parent_flags = 0, struct_flags;
   1481     char* ptr;
   1482 
   1483     struct_flags = fs->struct_flags;
   1484     if( fs->write_stack->total == 0 )
   1485         CV_ERROR( CV_StsError, "EndWriteStruct w/o matching StartWriteStruct" );
   1486 
   1487     cvSeqPop( fs->write_stack, &parent_flags );
   1488 
   1489     if( CV_NODE_IS_FLOW(struct_flags) )
   1490     {
   1491         ptr = fs->buffer;
   1492         if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) )
   1493             *ptr++ = ' ';
   1494         *ptr++ = CV_NODE_IS_MAP(struct_flags) ? '}' : ']';
   1495         fs->buffer = ptr;
   1496     }
   1497     else if( CV_NODE_IS_EMPTY(struct_flags) )
   1498     {
   1499         ptr = icvFSFlush(fs);
   1500         memcpy( ptr, CV_NODE_IS_MAP(struct_flags) ? "{}" : "[]", 2 );
   1501         fs->buffer = ptr + 2;
   1502     }
   1503 
   1504     if( !CV_NODE_IS_FLOW(parent_flags) )
   1505         fs->struct_indent -= CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
   1506     assert( fs->struct_indent >= 0 );
   1507 
   1508     fs->struct_flags = parent_flags;
   1509 
   1510     __END__;
   1511 }
   1512 
   1513 
   1514 static void
   1515 icvYMLStartNextStream( CvFileStorage* fs )
   1516 {
   1517     //CV_FUNCNAME( "icvYMLStartNextStream" );
   1518 
   1519     __BEGIN__;
   1520 
   1521     if( !fs->is_first )
   1522     {
   1523         while( fs->write_stack->total > 0 )
   1524             icvYMLEndWriteStruct(fs);
   1525 
   1526         fs->struct_indent = 0;
   1527         icvFSFlush(fs);
   1528         fputs( "...\n", fs->file );
   1529         fputs( "---\n", fs->file );
   1530         fs->buffer = fs->buffer_start;
   1531     }
   1532 
   1533     __END__;
   1534 }
   1535 
   1536 
   1537 static void
   1538 icvYMLWriteInt( CvFileStorage* fs, const char* key, int value )
   1539 {
   1540     CV_FUNCNAME( "icvYMLWriteInt" );
   1541 
   1542     __BEGIN__;
   1543 
   1544     char buf[128];
   1545     CV_CALL( icvYMLWrite( fs, key, icv_itoa( value, buf, 10 ), cvFuncName ));
   1546 
   1547     __END__;
   1548 }
   1549 
   1550 
   1551 static void
   1552 icvYMLWriteReal( CvFileStorage* fs, const char* key, double value )
   1553 {
   1554     CV_FUNCNAME( "icvYMLWriteReal" );
   1555 
   1556     __BEGIN__;
   1557 
   1558     char buf[128];
   1559     CV_CALL( icvYMLWrite( fs, key, icvDoubleToString( buf, value ), cvFuncName ));
   1560 
   1561     __END__;
   1562 }
   1563 
   1564 
   1565 static void
   1566 icvYMLWriteString( CvFileStorage* fs, const char* key,
   1567                    const char* str, int quote CV_DEFAULT(0))
   1568 {
   1569     CV_FUNCNAME( "icvYMLWriteString" );
   1570 
   1571     __BEGIN__;
   1572 
   1573     char buf[CV_FS_MAX_LEN*4+16];
   1574     char* data = (char*)str;
   1575     int i, len;
   1576 
   1577     if( !str )
   1578         CV_ERROR( CV_StsNullPtr, "Null string pointer" );
   1579 
   1580     len = (int)strlen(str);
   1581     if( len > CV_FS_MAX_LEN )
   1582         CV_ERROR( CV_StsBadArg, "The written string is too long" );
   1583 
   1584     if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') )
   1585     {
   1586         int need_quote = quote || len == 0;
   1587         data = buf;
   1588         *data++ = '\"';
   1589         for( i = 0; i < len; i++ )
   1590         {
   1591             char c = str[i];
   1592 
   1593             if( !need_quote && !isalnum(c) && c != '_' && c != ' ' && c != '-' &&
   1594                 c != '(' && c != ')' && c != '/' && c != '+' && c != ';' )
   1595                 need_quote = 1;
   1596 
   1597             if( !isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') )
   1598             {
   1599                 *data++ = '\\';
   1600                 if( cv_isprint(c) )
   1601                     *data++ = c;
   1602                 else if( c == '\n' )
   1603                     *data++ = 'n';
   1604                 else if( c == '\r' )
   1605                     *data++ = 'r';
   1606                 else if( c == '\t' )
   1607                     *data++ = 't';
   1608                 else
   1609                 {
   1610                     sprintf( data, "x%02x", c );
   1611                     data += 3;
   1612                 }
   1613             }
   1614             else
   1615                 *data++ = c;
   1616         }
   1617         if( !need_quote && (isdigit(str[0]) ||
   1618             str[0] == '+' || str[0] == '-' || str[0] == '.' ))
   1619             need_quote = 1;
   1620 
   1621         if( need_quote )
   1622             *data++ = '\"';
   1623         *data++ = '\0';
   1624         data = buf + !need_quote;
   1625     }
   1626 
   1627     CV_CALL( icvYMLWrite( fs, key, data, cvFuncName ));
   1628 
   1629     __END__;
   1630 }
   1631 
   1632 
   1633 static void
   1634 icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
   1635 {
   1636     CV_FUNCNAME( "icvYMLWriteComment" );
   1637 
   1638     __BEGIN__;
   1639 
   1640     int len; //, indent;
   1641     int multiline;
   1642     const char* eol;
   1643     char* ptr;
   1644 
   1645     if( !comment )
   1646         CV_ERROR( CV_StsNullPtr, "Null comment" );
   1647 
   1648     len = (int)strlen(comment);
   1649     eol = strchr(comment, '\n');
   1650     multiline = eol != 0;
   1651     ptr = fs->buffer;
   1652 
   1653     if( !eol_comment || multiline ||
   1654         fs->buffer_end - ptr < len || ptr == fs->buffer_start )
   1655         ptr = icvFSFlush( fs );
   1656     else
   1657         *ptr++ = ' ';
   1658 
   1659     while( comment )
   1660     {
   1661         *ptr++ = '#';
   1662         *ptr++ = ' ';
   1663         if( eol )
   1664         {
   1665             ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
   1666             memcpy( ptr, comment, eol - comment + 1 );
   1667             fs->buffer = ptr + (eol - comment);
   1668             comment = eol + 1;
   1669             eol = strchr( comment, '\n' );
   1670         }
   1671         else
   1672         {
   1673             len = (int)strlen(comment);
   1674             ptr = icvFSResizeWriteBuffer( fs, ptr, len );
   1675             memcpy( ptr, comment, len );
   1676             fs->buffer = ptr + len;
   1677             comment = 0;
   1678         }
   1679         ptr = icvFSFlush( fs );
   1680     }
   1681 
   1682     __END__;
   1683 }
   1684 
   1685 
   1686 /****************************************************************************************\
   1687 *                                       XML Parser                                       *
   1688 \****************************************************************************************/
   1689 
   1690 #define CV_XML_INSIDE_COMMENT 1
   1691 #define CV_XML_INSIDE_TAG 2
   1692 #define CV_XML_INSIDE_DIRECTIVE 3
   1693 
   1694 static char*
   1695 icvXMLSkipSpaces( CvFileStorage* fs, char* ptr, int mode )
   1696 {
   1697     CV_FUNCNAME( "icvXMLSkipSpaces" );
   1698 
   1699     __BEGIN__;
   1700 
   1701     int level = 0;
   1702 
   1703     for(;;)
   1704     {
   1705         char c;
   1706         ptr--;
   1707 
   1708         if( mode == CV_XML_INSIDE_COMMENT )
   1709         {
   1710             do c = *++ptr;
   1711             while( cv_isprint_or_tab(c) && (c != '-' || ptr[1] != '-' || ptr[2] != '>') );
   1712 
   1713             if( c == '-' )
   1714             {
   1715                 assert( ptr[1] == '-' && ptr[2] == '>' );
   1716                 mode = 0;
   1717                 ptr += 3;
   1718             }
   1719         }
   1720         else if( mode == CV_XML_INSIDE_DIRECTIVE )
   1721         {
   1722             // !!!NOTE!!! This is not quite correct, but should work in most cases
   1723             do
   1724             {
   1725                 c = *++ptr;
   1726                 level += c == '<';
   1727                 level -= c == '>';
   1728                 if( level < 0 )
   1729                     EXIT;
   1730             } while( cv_isprint_or_tab(c) );
   1731         }
   1732         else
   1733         {
   1734             do c = *++ptr;
   1735             while( c == ' ' || c == '\t' );
   1736 
   1737             if( c == '<' && ptr[1] == '!' && ptr[2] == '-' && ptr[3] == '-' )
   1738             {
   1739                 if( mode != 0 )
   1740                     CV_PARSE_ERROR( "Comments are not allowed here" );
   1741                 mode = CV_XML_INSIDE_COMMENT;
   1742                 ptr += 4;
   1743             }
   1744             else if( cv_isprint(c) )
   1745                 break;
   1746         }
   1747 
   1748         if( !cv_isprint(*ptr) )
   1749         {
   1750             int max_size = (int)(fs->buffer_end - fs->buffer_start);
   1751             if( *ptr != '\0' && *ptr != '\n' && *ptr != '\r' )
   1752                 CV_PARSE_ERROR( "Invalid character in the stream" );
   1753             ptr = fgets( fs->buffer_start, max_size, fs->file );
   1754             if( !ptr )
   1755             {
   1756                 ptr = fs->buffer_start;
   1757                 *ptr = '\0';
   1758                 fs->dummy_eof = 1;
   1759                 break;
   1760             }
   1761             else
   1762             {
   1763                 int l = (int)strlen(ptr);
   1764                 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !feof(fs->file) )
   1765                     CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
   1766             }
   1767             fs->lineno++;
   1768         }
   1769     }
   1770 
   1771     __END__;
   1772 
   1773     return ptr;
   1774 }
   1775 
   1776 
   1777 static char*
   1778 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
   1779                 CvAttrList** _list, int* _tag_type );
   1780 
   1781 static char*
   1782 icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
   1783                   int value_type CV_DEFAULT(CV_NODE_NONE))
   1784 {
   1785     CV_FUNCNAME( "icvXMLParseValue" );
   1786 
   1787     __BEGIN__;
   1788 
   1789     CvFileNode *elem = node;
   1790     int have_space = 1, is_simple = 1;
   1791     int is_user_type = CV_NODE_IS_USER(value_type);
   1792     memset( node, 0, sizeof(*node) );
   1793 
   1794     value_type = CV_NODE_TYPE(value_type);
   1795 
   1796     for(;;)
   1797     {
   1798         char c = *ptr, d;
   1799         char* endptr;
   1800 
   1801         if( isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
   1802         {
   1803             CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
   1804             have_space = 1;
   1805             c = *ptr;
   1806         }
   1807 
   1808         d = ptr[1];
   1809 
   1810         if( c =='<' )
   1811         {
   1812             CvStringHashNode *key = 0, *key2 = 0;
   1813             CvAttrList* list = 0;
   1814             CvTypeInfo* info = 0;
   1815             int tag_type = 0;
   1816             int is_noname = 0;
   1817             const char* type_name = 0;
   1818             int elem_type = CV_NODE_NONE;
   1819 
   1820             if( d == '/' )
   1821                 break;
   1822 
   1823             CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
   1824 
   1825             if( tag_type == CV_XML_DIRECTIVE_TAG )
   1826                 CV_PARSE_ERROR( "Directive tags are not allowed here" );
   1827             if( tag_type == CV_XML_EMPTY_TAG )
   1828                 CV_PARSE_ERROR( "Empty tags are not supported" );
   1829 
   1830             assert( tag_type == CV_XML_OPENING_TAG );
   1831 
   1832             type_name = list ? cvAttrValue( list, "type_id" ) : 0;
   1833             if( type_name )
   1834             {
   1835                 if( strcmp( type_name, "str" ) == 0 )
   1836                     elem_type = CV_NODE_STRING;
   1837                 else if( strcmp( type_name, "map" ) == 0 )
   1838                     elem_type = CV_NODE_MAP;
   1839                 else if( strcmp( type_name, "seq" ) == 0 )
   1840                     elem_type = CV_NODE_MAP;
   1841                 else
   1842                 {
   1843                     CV_CALL( info = cvFindType( type_name ));
   1844                     if( info )
   1845                         elem_type = CV_NODE_USER;
   1846                 }
   1847             }
   1848 
   1849             is_noname = key->str.len == 1 && key->str.ptr[0] == '_';
   1850             if( !CV_NODE_IS_COLLECTION(node->tag) )
   1851             {
   1852                 CV_CALL( icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node ));
   1853             }
   1854             else if( is_noname ^ CV_NODE_IS_SEQ(node->tag) )
   1855                 CV_PARSE_ERROR( is_noname ? "Map element should have a name" :
   1856                               "Sequence element should not have name (use <_></_>)" );
   1857 
   1858             if( is_noname )
   1859                 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
   1860             else
   1861                 CV_CALL( elem = cvGetFileNode( fs, node, key, 1 ));
   1862 
   1863             CV_CALL( ptr = icvXMLParseValue( fs, ptr, elem, elem_type));
   1864             if( !is_noname )
   1865                 elem->tag |= CV_NODE_NAMED;
   1866             is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
   1867             elem->info = info;
   1868             CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type ));
   1869             if( tag_type != CV_XML_CLOSING_TAG || key2 != key )
   1870                 CV_PARSE_ERROR( "Mismatched closing tag" );
   1871             have_space = 1;
   1872         }
   1873         else
   1874         {
   1875             if( !have_space )
   1876                 CV_PARSE_ERROR( "There should be space between literals" );
   1877 
   1878             elem = node;
   1879             if( node->tag != CV_NODE_NONE )
   1880             {
   1881                 if( !CV_NODE_IS_COLLECTION(node->tag) )
   1882                     CV_CALL( icvFSCreateCollection( fs, CV_NODE_SEQ, node ));
   1883 
   1884                 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
   1885                 elem->info = 0;
   1886             }
   1887 
   1888             if( value_type != CV_NODE_STRING &&
   1889                 (isdigit(c) || ((c == '-' || c == '+') &&
   1890                 (isdigit(d) || d == '.')) || (c == '.' && isalnum(d))) ) // a number
   1891             {
   1892                 double fval;
   1893                 int ival;
   1894                 endptr = ptr + (c == '-' || c == '+');
   1895                 while( isdigit(*endptr) )
   1896                     endptr++;
   1897                 if( *endptr == '.' || *endptr == 'e' )
   1898                 {
   1899                     fval = icv_strtod( fs, ptr, &endptr );
   1900                     /*if( endptr == ptr || isalpha(*endptr) )
   1901                         CV_CALL( icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
   1902                     elem->tag = CV_NODE_REAL;
   1903                     elem->data.f = fval;
   1904                 }
   1905                 else
   1906                 {
   1907                     ival = (int)strtol( ptr, &endptr, 0 );
   1908                     elem->tag = CV_NODE_INT;
   1909                     elem->data.i = ival;
   1910                 }
   1911 
   1912                 if( endptr == ptr )
   1913                     CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
   1914 
   1915                 ptr = endptr;
   1916             }
   1917             else
   1918             {
   1919                 // string
   1920                 char buf[CV_FS_MAX_LEN+16];
   1921                 int i = 0, len, is_quoted = 0;
   1922                 elem->tag = CV_NODE_STRING;
   1923                 if( c == '\"' )
   1924                     is_quoted = 1;
   1925                 else
   1926                     --ptr;
   1927 
   1928                 for( ;; )
   1929                 {
   1930                     c = *++ptr;
   1931                     if( !isalnum(c) )
   1932                     {
   1933                         if( c == '\"' )
   1934                         {
   1935                             if( !is_quoted )
   1936                                 CV_PARSE_ERROR( "Literal \" is not allowed within a string. Use &quot;" );
   1937                             ++ptr;
   1938                             break;
   1939                         }
   1940                         else if( !cv_isprint(c) || c == '<' || (!is_quoted && isspace(c)))
   1941                         {
   1942                             if( is_quoted )
   1943                                 CV_PARSE_ERROR( "Closing \" is expected" );
   1944                             break;
   1945                         }
   1946                         else if( c == '\'' || c == '>' )
   1947                         {
   1948                             CV_PARSE_ERROR( "Literal \' or > are not allowed. Use &apos; or &gt;" );
   1949                         }
   1950                         else if( c == '&' )
   1951                         {
   1952                             if( *ptr == '#' )
   1953                             {
   1954                                 int val;
   1955                                 ptr++;
   1956                                 val = (int)strtol( ptr, &endptr, 0 );
   1957                                 if( (unsigned)val > (unsigned)255 ||
   1958                                     !endptr || *endptr != ';' )
   1959                                     CV_PARSE_ERROR( "Invalid numeric value in the string" );
   1960                                 c = (char)val;
   1961                             }
   1962                             else
   1963                             {
   1964                                 endptr = ptr++;
   1965                                 do c = *++endptr;
   1966                                 while( isalnum(c) );
   1967                                 if( c != ';' )
   1968                                     CV_PARSE_ERROR( "Invalid character in the symbol entity name" );
   1969                                 len = (int)(endptr - ptr);
   1970                                 if( len == 2 && memcmp( ptr, "lt", len ) == 0 )
   1971                                     c = '<';
   1972                                 else if( len == 2 && memcmp( ptr, "gt", len ) == 0 )
   1973                                     c = '>';
   1974                                 else if( len == 3 && memcmp( ptr, "amp", len ) == 0 )
   1975                                     c = '&';
   1976                                 else if( len == 4 && memcmp( ptr, "apos", len ) == 0 )
   1977                                     c = '\'';
   1978                                 else if( len == 4 && memcmp( ptr, "quot", len ) == 0 )
   1979                                     c = '\"';
   1980                                 else
   1981                                 {
   1982                                     memcpy( buf + i, ptr-1, len + 2 );
   1983                                     i += len + 2;
   1984                                 }
   1985                             }
   1986                             ptr = endptr;
   1987                         }
   1988                     }
   1989                     buf[i++] = c;
   1990                     if( i >= CV_FS_MAX_LEN )
   1991                         CV_PARSE_ERROR( "Too long string literal" );
   1992                 }
   1993                 CV_CALL( elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i ));
   1994             }
   1995 
   1996             if( !CV_NODE_IS_COLLECTION(value_type) && value_type != CV_NODE_NONE )
   1997                 break;
   1998             have_space = 0;
   1999         }
   2000     }
   2001 
   2002     if( (CV_NODE_TYPE(node->tag) == CV_NODE_NONE ||
   2003         (CV_NODE_TYPE(node->tag) != value_type &&
   2004         !CV_NODE_IS_COLLECTION(node->tag))) &&
   2005         CV_NODE_IS_COLLECTION(value_type) )
   2006     {
   2007         CV_CALL( icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ?
   2008                                         CV_NODE_MAP : CV_NODE_SEQ, node ));
   2009     }
   2010 
   2011     if( value_type != CV_NODE_NONE &&
   2012         value_type != CV_NODE_TYPE(node->tag) )
   2013         CV_PARSE_ERROR( "The actual type is different from the specified type" );
   2014 
   2015     if( CV_NODE_IS_COLLECTION(node->tag) && is_simple )
   2016             node->data.seq->flags |= CV_NODE_SEQ_SIMPLE;
   2017 
   2018     node->tag |= is_user_type ? CV_NODE_USER : 0;
   2019 
   2020     __END__;
   2021 
   2022     return ptr;
   2023 }
   2024 
   2025 
   2026 static char*
   2027 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
   2028                 CvAttrList** _list, int* _tag_type )
   2029 {
   2030     int tag_type = 0;
   2031     CvStringHashNode* tagname = 0;
   2032     CvAttrList *first = 0, *last = 0;
   2033     int count = 0, max_count = 4;
   2034     int attr_buf_size = (max_count*2 + 1)*sizeof(char*) + sizeof(CvAttrList);
   2035 
   2036     CV_FUNCNAME( "icvXMLParseTag" );
   2037 
   2038     __BEGIN__;
   2039 
   2040     char* endptr;
   2041     char c;
   2042     int have_space;
   2043 
   2044     if( *ptr != '<' )
   2045         CV_PARSE_ERROR( "Tag should start with \'<\'" );
   2046 
   2047     ptr++;
   2048     if( isalnum(*ptr) || *ptr == '_' )
   2049         tag_type = CV_XML_OPENING_TAG;
   2050     else if( *ptr == '/' )
   2051     {
   2052         tag_type = CV_XML_CLOSING_TAG;
   2053         ptr++;
   2054     }
   2055     else if( *ptr == '?' )
   2056     {
   2057         tag_type = CV_XML_HEADER_TAG;
   2058         ptr++;
   2059     }
   2060     else if( *ptr == '!' )
   2061     {
   2062         tag_type = CV_XML_DIRECTIVE_TAG;
   2063         assert( ptr[1] != '-' || ptr[2] != '-' );
   2064         ptr++;
   2065     }
   2066     else
   2067         CV_PARSE_ERROR( "Unknown tag type" );
   2068 
   2069     for(;;)
   2070     {
   2071         CvStringHashNode* attrname;
   2072 
   2073         if( !isalpha(*ptr) && *ptr != '_' )
   2074             CV_PARSE_ERROR( "Name should start with a letter or underscore" );
   2075 
   2076         endptr = ptr - 1;
   2077         do c = *++endptr;
   2078         while( isalnum(c) || c == '_' || c == '-' );
   2079 
   2080         CV_CALL( attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ));
   2081         ptr = endptr;
   2082 
   2083         if( !tagname )
   2084             tagname = attrname;
   2085         else
   2086         {
   2087             if( tag_type == CV_XML_CLOSING_TAG )
   2088                 CV_PARSE_ERROR( "Closing tag should not contain any attributes" );
   2089 
   2090             if( !last || count >= max_count )
   2091             {
   2092                 CvAttrList* chunk;
   2093 
   2094                 CV_CALL( chunk = (CvAttrList*)cvMemStorageAlloc( fs->memstorage, attr_buf_size ));
   2095                 memset( chunk, 0, attr_buf_size );
   2096                 chunk->attr = (const char**)(chunk + 1);
   2097                 count = 0;
   2098                 if( !last )
   2099                     first = last = chunk;
   2100                 else
   2101                     last = last->next = chunk;
   2102             }
   2103             last->attr[count*2] = attrname->str.ptr;
   2104         }
   2105 
   2106         if( last )
   2107         {
   2108             CvFileNode stub;
   2109 
   2110             if( *ptr != '=' )
   2111             {
   2112                 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
   2113                 if( *ptr != '=' )
   2114                     CV_PARSE_ERROR( "Attribute name should be followed by \'=\'" );
   2115             }
   2116 
   2117             c = *++ptr;
   2118             if( c != '\"' && c != '\'' )
   2119             {
   2120                 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
   2121                 if( *ptr != '\"' && *ptr != '\'' )
   2122                     CV_PARSE_ERROR( "Attribute value should be put into single or double quotes" );
   2123             }
   2124 
   2125             ptr = icvXMLParseValue( fs, ptr, &stub, CV_NODE_STRING );
   2126             assert( stub.tag == CV_NODE_STRING );
   2127             last->attr[count*2+1] = stub.data.str.ptr;
   2128             count++;
   2129         }
   2130 
   2131         c = *ptr;
   2132         have_space = isspace(c) || c == '\0';
   2133 
   2134         if( c != '>' )
   2135         {
   2136             CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
   2137             c = *ptr;
   2138         }
   2139 
   2140         if( c == '>' )
   2141         {
   2142             if( tag_type == CV_XML_HEADER_TAG )
   2143                 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
   2144             ptr++;
   2145             break;
   2146         }
   2147         else if( c == '?' && tag_type == CV_XML_HEADER_TAG )
   2148         {
   2149             if( ptr[1] != '>'  )
   2150                 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
   2151             ptr += 2;
   2152             break;
   2153         }
   2154         else if( c == '/' && ptr[1] == '>' && tag_type == CV_XML_OPENING_TAG )
   2155         {
   2156             tag_type = CV_XML_EMPTY_TAG;
   2157             ptr += 2;
   2158             break;
   2159         }
   2160 
   2161         if( !have_space )
   2162             CV_PARSE_ERROR( "There should be space between attributes" );
   2163     }
   2164 
   2165     __END__;
   2166 
   2167     *_tag = tagname;
   2168     *_tag_type = tag_type;
   2169     *_list = first;
   2170 
   2171     return ptr;
   2172 }
   2173 
   2174 
   2175 static void
   2176 icvXMLParse( CvFileStorage* fs )
   2177 {
   2178     CV_FUNCNAME( "icvXMLParse" );
   2179 
   2180     __BEGIN__;
   2181 
   2182     char* ptr = fs->buffer_start;
   2183     CvStringHashNode *key = 0, *key2 = 0;
   2184     CvAttrList* list = 0;
   2185     int tag_type = 0;
   2186 
   2187     // CV_XML_INSIDE_TAG is used to prohibit leading comments
   2188     CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
   2189 
   2190     if( memcmp( ptr, "<?xml", 5 ) != 0 )
   2191         CV_PARSE_ERROR( "Valid XML should start with \'<?xml ...?>\'" );
   2192 
   2193     CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
   2194 
   2195     /*{
   2196         const char* version = cvAttrValue( list, "version" );
   2197         if( version && strncmp( version, "1.", 2 ) != 0 )
   2198             CV_ERROR( CV_StsParseError, "Unsupported version of XML" );
   2199     }*/
   2200     {
   2201         const char* encoding = cvAttrValue( list, "encoding" );
   2202         if( encoding && strcmp( encoding, "ASCII" ) != 0 )
   2203             CV_PARSE_ERROR( "Unsupported encoding" );
   2204     }
   2205 
   2206     while( *ptr != '\0' )
   2207     {
   2208         CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
   2209 
   2210         if( *ptr != '\0' )
   2211         {
   2212             CvFileNode* root_node;
   2213             CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
   2214             if( tag_type != CV_XML_OPENING_TAG ||
   2215                 strcmp(key->str.ptr,"opencv_storage") != 0 )
   2216                 CV_PARSE_ERROR( "<opencv_storage> tag is missing" );
   2217 
   2218             root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
   2219             CV_CALL( ptr = icvXMLParseValue( fs, ptr, root_node, CV_NODE_NONE ));
   2220             CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type ));
   2221             if( tag_type != CV_XML_CLOSING_TAG || key != key2 )
   2222                 CV_PARSE_ERROR( "</opencv_storage> tag is missing" );
   2223             CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
   2224         }
   2225     }
   2226 
   2227     assert( fs->dummy_eof != 0 );
   2228 
   2229     __END__;
   2230 }
   2231 
   2232 
   2233 /****************************************************************************************\
   2234 *                                       XML Emitter                                      *
   2235 \****************************************************************************************/
   2236 
   2237 #define icvXMLFlush icvFSFlush
   2238 
   2239 static void
   2240 icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList list )
   2241 {
   2242     CV_FUNCNAME( "icvXMLWriteTag" );
   2243 
   2244     __BEGIN__;
   2245 
   2246     char* ptr = fs->buffer;
   2247     int i, len = 0;
   2248     int struct_flags = fs->struct_flags;
   2249 
   2250     if( key && key[0] == '\0' )
   2251         key = 0;
   2252 
   2253     if( tag_type == CV_XML_OPENING_TAG || tag_type == CV_XML_EMPTY_TAG )
   2254     {
   2255         if( CV_NODE_IS_COLLECTION(struct_flags) )
   2256         {
   2257             if( CV_NODE_IS_MAP(struct_flags) ^ (key != 0) )
   2258                 CV_ERROR( CV_StsBadArg, "An attempt to add element without a key to a map, "
   2259                                         "or add element with key to sequence" );
   2260         }
   2261         else
   2262         {
   2263             struct_flags = CV_NODE_EMPTY + (key ? CV_NODE_MAP : CV_NODE_SEQ);
   2264             fs->is_first = 0;
   2265         }
   2266 
   2267         if( !CV_NODE_IS_EMPTY(struct_flags) )
   2268             ptr = icvXMLFlush(fs);
   2269     }
   2270 
   2271     if( !key )
   2272         key = "_";
   2273     else if( key[0] == '_' && key[1] == '\0' )
   2274         CV_ERROR( CV_StsBadArg, "A single _ is a reserved tag name" );
   2275 
   2276     len = (int)strlen( key );
   2277     *ptr++ = '<';
   2278     if( tag_type == CV_XML_CLOSING_TAG )
   2279     {
   2280         if( list.attr )
   2281             CV_ERROR( CV_StsBadArg, "Closing tag should not include any attributes" );
   2282         *ptr++ = '/';
   2283     }
   2284 
   2285     if( !isalpha(key[0]) && key[0] != '_' )
   2286         CV_ERROR( CV_StsBadArg, "Key should start with a letter or _" );
   2287 
   2288     ptr = icvFSResizeWriteBuffer( fs, ptr, len );
   2289     for( i = 0; i < len; i++ )
   2290     {
   2291         char c = key[i];
   2292         if( !isalnum(c) && c != '_' && c != '-' )
   2293             CV_ERROR( CV_StsBadArg, "Invalid character in the key" );
   2294         ptr[i] = c;
   2295     }
   2296     ptr += len;
   2297 
   2298     for(;;)
   2299     {
   2300         const char** attr = list.attr;
   2301 
   2302         for( ; attr && attr[0] != 0; attr += 2 )
   2303         {
   2304             int len0 = (int)strlen(attr[0]);
   2305             int len1 = (int)strlen(attr[1]);
   2306 
   2307             ptr = icvFSResizeWriteBuffer( fs, ptr, len0 + len1 + 4 );
   2308             *ptr++ = ' ';
   2309             memcpy( ptr, attr[0], len0 );
   2310             ptr += len0;
   2311             *ptr++ = '=';
   2312             *ptr++ = '\"';
   2313             memcpy( ptr, attr[1], len1 );
   2314             ptr += len1;
   2315             *ptr++ = '\"';
   2316         }
   2317         if( !list.next )
   2318             break;
   2319         list = *list.next;
   2320     }
   2321 
   2322     if( tag_type == CV_XML_EMPTY_TAG )
   2323         *ptr++ = '/';
   2324     *ptr++ = '>';
   2325     fs->buffer = ptr;
   2326     fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
   2327 
   2328     __END__;
   2329 }
   2330 
   2331 
   2332 static void
   2333 icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
   2334                         const char* type_name CV_DEFAULT(0))
   2335 {
   2336     CV_FUNCNAME( "icvXMLStartWriteStruct" );
   2337 
   2338     __BEGIN__;
   2339 
   2340     CvXMLStackRecord parent;
   2341     const char* attr[10];
   2342     int idx = 0;
   2343 
   2344     struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
   2345     if( !CV_NODE_IS_COLLECTION(struct_flags))
   2346         CV_ERROR( CV_StsBadArg,
   2347         "Some collection type: CV_NODE_SEQ or CV_NODE_MAP must be specified" );
   2348 
   2349     if( type_name )
   2350     {
   2351         attr[idx++] = "type_id";
   2352         attr[idx++] = type_name;
   2353     }
   2354     attr[idx++] = 0;
   2355 
   2356     CV_CALL( icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(attr,0) ));
   2357 
   2358     parent.struct_flags = fs->struct_flags & ~CV_NODE_EMPTY;
   2359     parent.struct_indent = fs->struct_indent;
   2360     parent.struct_tag = fs->struct_tag;
   2361     cvSaveMemStoragePos( fs->strstorage, &parent.pos );
   2362     cvSeqPush( fs->write_stack, &parent );
   2363 
   2364     fs->struct_indent += CV_XML_INDENT;
   2365     if( !CV_NODE_IS_FLOW(struct_flags) )
   2366         icvXMLFlush( fs );
   2367 
   2368     fs->struct_flags = struct_flags;
   2369     if( key )
   2370     {
   2371         CV_CALL( fs->struct_tag = cvMemStorageAllocString( fs->strstorage, (char*)key, -1 ));
   2372     }
   2373     else
   2374     {
   2375         fs->struct_tag.ptr = 0;
   2376         fs->struct_tag.len = 0;
   2377     }
   2378 
   2379     __END__;
   2380 }
   2381 
   2382 
   2383 static void
   2384 icvXMLEndWriteStruct( CvFileStorage* fs )
   2385 {
   2386     CV_FUNCNAME( "icvXMLStartWriteStruct" );
   2387 
   2388     __BEGIN__;
   2389 
   2390     CvXMLStackRecord parent;
   2391 
   2392     if( fs->write_stack->total == 0 )
   2393         CV_ERROR( CV_StsError, "An extra closing tag" );
   2394 
   2395     CV_CALL( icvXMLWriteTag( fs, fs->struct_tag.ptr, CV_XML_CLOSING_TAG, cvAttrList(0,0) ));
   2396     cvSeqPop( fs->write_stack, &parent );
   2397 
   2398     fs->struct_indent = parent.struct_indent;
   2399     fs->struct_flags = parent.struct_flags;
   2400     fs->struct_tag = parent.struct_tag;
   2401     cvRestoreMemStoragePos( fs->strstorage, &parent.pos );
   2402 
   2403     __END__;
   2404 }
   2405 
   2406 
   2407 static void
   2408 icvXMLStartNextStream( CvFileStorage* fs )
   2409 {
   2410     //CV_FUNCNAME( "icvXMLStartNextStream" );
   2411 
   2412     __BEGIN__;
   2413 
   2414     if( !fs->is_first )
   2415     {
   2416         while( fs->write_stack->total > 0 )
   2417             icvXMLEndWriteStruct(fs);
   2418 
   2419         fs->struct_indent = 0;
   2420         icvXMLFlush(fs);
   2421         /* XML does not allow multiple top-level elements,
   2422            so we just put a comment and continue
   2423            the current (and the only) "stream" */
   2424         fputs( "\n<!-- next stream -->\n", fs->file );
   2425         /*fputs( "</opencv_storage>\n", fs->file );
   2426         fputs( "<opencv_storage>\n", fs->file );*/
   2427         fs->buffer = fs->buffer_start;
   2428     }
   2429 
   2430     __END__;
   2431 }
   2432 
   2433 
   2434 static void
   2435 icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len )
   2436 {
   2437     CV_FUNCNAME( "icvXMLWriteScalar" );
   2438 
   2439     __BEGIN__;
   2440 
   2441     if( CV_NODE_IS_MAP(fs->struct_flags) ||
   2442         (!CV_NODE_IS_COLLECTION(fs->struct_flags) && key) )
   2443     {
   2444         icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(0,0) );
   2445         char* ptr = icvFSResizeWriteBuffer( fs, fs->buffer, len );
   2446         memcpy( ptr, data, len );
   2447         fs->buffer = ptr + len;
   2448         icvXMLWriteTag( fs, key, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
   2449     }
   2450     else
   2451     {
   2452         char* ptr = fs->buffer;
   2453         int new_offset = (int)(ptr - fs->buffer_start) + len;
   2454 
   2455         if( key )
   2456             CV_ERROR( CV_StsBadArg, "elements with keys can not be written to sequence" );
   2457 
   2458         fs->struct_flags = CV_NODE_SEQ;
   2459 
   2460         if( (new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10) ||
   2461             (ptr > fs->buffer_start && ptr[-1] == '>' && !CV_NODE_IS_EMPTY(fs->struct_flags)) )
   2462         {
   2463             ptr = icvXMLFlush(fs);
   2464         }
   2465         else if( ptr > fs->buffer_start + fs->struct_indent && ptr[-1] != '>' )
   2466             *ptr++ = ' ';
   2467 
   2468         memcpy( ptr, data, len );
   2469         fs->buffer = ptr + len;
   2470     }
   2471 
   2472     __END__;
   2473 }
   2474 
   2475 
   2476 static void
   2477 icvXMLWriteInt( CvFileStorage* fs, const char* key, int value )
   2478 {
   2479     //CV_FUNCNAME( "cvXMLWriteInt" );
   2480 
   2481     __BEGIN__;
   2482 
   2483     char buf[128], *ptr = icv_itoa( value, buf, 10 );
   2484     int len = (int)strlen(ptr);
   2485     icvXMLWriteScalar( fs, key, ptr, len );
   2486 
   2487     __END__;
   2488 }
   2489 
   2490 
   2491 static void
   2492 icvXMLWriteReal( CvFileStorage* fs, const char* key, double value )
   2493 {
   2494     //CV_FUNCNAME( "cvXMLWriteReal" );
   2495 
   2496     __BEGIN__;
   2497 
   2498     char buf[128];
   2499     int len = (int)strlen( icvDoubleToString( buf, value ));
   2500     icvXMLWriteScalar( fs, key, buf, len );
   2501 
   2502     __END__;
   2503 }
   2504 
   2505 
   2506 static void
   2507 icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote )
   2508 {
   2509     CV_FUNCNAME( "cvXMLWriteString" );
   2510 
   2511     __BEGIN__;
   2512 
   2513     char buf[CV_FS_MAX_LEN*6+16];
   2514     char* data = (char*)str;
   2515     int i, len;
   2516 
   2517     if( !str )
   2518         CV_ERROR( CV_StsNullPtr, "Null string pointer" );
   2519 
   2520     len = (int)strlen(str);
   2521     if( len > CV_FS_MAX_LEN )
   2522         CV_ERROR( CV_StsBadArg, "The written string is too long" );
   2523 
   2524     if( quote || len == 0 || str[0] != '\"' || str[0] != str[len-1] )
   2525     {
   2526         int need_quote = quote || len == 0;
   2527         data = buf;
   2528         *data++ = '\"';
   2529         for( i = 0; i < len; i++ )
   2530         {
   2531             char c = str[i];
   2532 
   2533             if( !isalnum(c) && (!cv_isprint(c) || c == '<' || c == '>' ||
   2534                 c == '&' || c == '\'' || c == '\"') )
   2535             {
   2536                 *data++ = '&';
   2537                 if( c == '<' )
   2538                 {
   2539                     memcpy(data, "lt", 2);
   2540                     data += 2;
   2541                 }
   2542                 else if( c == '>' )
   2543                 {
   2544                     memcpy(data, "gt", 2);
   2545                     data += 2;
   2546                 }
   2547                 else if( c == '&' )
   2548                 {
   2549                     memcpy(data, "amp", 3);
   2550                     data += 3;
   2551                 }
   2552                 else if( c == '\'' )
   2553                 {
   2554                     memcpy(data, "apos", 4);
   2555                     data += 4;
   2556                 }
   2557                 else if( c == '\"' )
   2558                 {
   2559                     memcpy( data, "quot", 4);
   2560                     data += 4;
   2561                 }
   2562                 else
   2563                 {
   2564                     sprintf( data, "#x%02x", c );
   2565                     data += 4;
   2566                 }
   2567                 *data++ = ';';
   2568             }
   2569             else
   2570             {
   2571                 if( c == ' ' )
   2572                     need_quote = 1;
   2573                 *data++ = c;
   2574             }
   2575         }
   2576         if( !need_quote && (isdigit(str[0]) ||
   2577             str[0] == '+' || str[0] == '-' || str[0] == '.' ))
   2578             need_quote = 1;
   2579 
   2580         if( need_quote )
   2581             *data++ = '\"';
   2582         len = (int)(data - buf) - !need_quote;
   2583         *data++ = '\0';
   2584         data = buf + !need_quote;
   2585     }
   2586 
   2587     icvXMLWriteScalar( fs, key, data, len );
   2588 
   2589     __END__;
   2590 }
   2591 
   2592 
   2593 static void
   2594 icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
   2595 {
   2596     CV_FUNCNAME( "cvXMLWriteComment" );
   2597 
   2598     __BEGIN__;
   2599 
   2600     int len;
   2601     int multiline;
   2602     const char* eol;
   2603     char* ptr;
   2604 
   2605     if( !comment )
   2606         CV_ERROR( CV_StsNullPtr, "Null comment" );
   2607 
   2608     if( strstr(comment, "--") != 0 )
   2609         CV_ERROR( CV_StsBadArg, "Double hyphen \'--\' is not allowed in the comments" );
   2610 
   2611     len = (int)strlen(comment);
   2612     eol = strchr(comment, '\n');
   2613     multiline = eol != 0;
   2614     ptr = fs->buffer;
   2615 
   2616     if( multiline || !eol_comment || fs->buffer_end - ptr < len + 5 )
   2617         ptr = icvXMLFlush( fs );
   2618     else if( ptr > fs->buffer_start + fs->struct_indent )
   2619         *ptr++ = ' ';
   2620 
   2621     if( !multiline )
   2622     {
   2623         ptr = icvFSResizeWriteBuffer( fs, ptr, len + 9 );
   2624         sprintf( ptr, "<!-- %s -->", comment );
   2625         len = (int)strlen(ptr);
   2626     }
   2627     else
   2628     {
   2629         strcpy( ptr, "<!--" );
   2630         len = 4;
   2631     }
   2632 
   2633     fs->buffer = ptr + len;
   2634     ptr = icvXMLFlush(fs);
   2635 
   2636     if( multiline )
   2637     {
   2638         while( comment )
   2639         {
   2640             if( eol )
   2641             {
   2642                 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
   2643                 memcpy( ptr, comment, eol - comment + 1 );
   2644                 ptr += eol - comment;
   2645                 comment = eol + 1;
   2646                 eol = strchr( comment, '\n' );
   2647             }
   2648             else
   2649             {
   2650                 len = (int)strlen(comment);
   2651                 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
   2652                 memcpy( ptr, comment, len );
   2653                 ptr += len;
   2654                 comment = 0;
   2655             }
   2656             fs->buffer = ptr;
   2657             ptr = icvXMLFlush( fs );
   2658         }
   2659         sprintf( ptr, "-->" );
   2660         fs->buffer = ptr + 3;
   2661         icvXMLFlush( fs );
   2662     }
   2663 
   2664     __END__;
   2665 }
   2666 
   2667 
   2668 /****************************************************************************************\
   2669 *                              Common High-Level Functions                               *
   2670 \****************************************************************************************/
   2671 
   2672 CV_IMPL CvFileStorage*
   2673 cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags )
   2674 {
   2675     CvFileStorage* fs = 0;
   2676     char* xml_buf = 0;
   2677 
   2678     CV_FUNCNAME("cvOpenFileStorage" );
   2679 
   2680     __BEGIN__;
   2681 
   2682     int default_block_size = 1 << 18;
   2683     bool append = (flags & 3) == CV_STORAGE_APPEND;
   2684 
   2685     if( !filename )
   2686         CV_ERROR( CV_StsNullPtr, "NULL filename" );
   2687 
   2688     CV_CALL( fs = (CvFileStorage*)cvAlloc( sizeof(*fs) ));
   2689     memset( fs, 0, sizeof(*fs));
   2690 
   2691     CV_CALL( fs->memstorage = cvCreateMemStorage( default_block_size ));
   2692     fs->dststorage = dststorage ? dststorage : fs->memstorage;
   2693 
   2694     CV_CALL( fs->filename = (char*)cvMemStorageAlloc( fs->memstorage, strlen(filename)+1 ));
   2695     strcpy( fs->filename, filename );
   2696 
   2697     fs->flags = CV_FILE_STORAGE;
   2698     fs->write_mode = (flags & 3) != 0;
   2699     fs->file = fopen( fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" );
   2700     if( !fs->file )
   2701         EXIT;
   2702 
   2703     fs->roots = 0;
   2704     fs->struct_indent = 0;
   2705     fs->struct_flags = 0;
   2706     fs->wrap_margin = 71;
   2707 
   2708     if( fs->write_mode )
   2709     {
   2710         // we use factor=6 for XML (the longest characters (' and ") are encoded with 6 bytes (&apos; and &quot;)
   2711         // and factor=4 for YAML ( as we use 4 bytes for non ASCII characters (e.g. \xAB))
   2712         int buf_size = CV_FS_MAX_LEN*(fs->is_xml ? 6 : 4) + 1024;
   2713 
   2714         char* dot_pos = strrchr( fs->filename, '.' );
   2715         fs->is_xml = dot_pos && (strcmp( dot_pos, ".xml" ) == 0 ||
   2716                       strcmp( dot_pos, ".XML" ) == 0 || strcmp( dot_pos, ".Xml" ) == 0);
   2717 
   2718         if( append )
   2719             fseek( fs->file, 0, SEEK_END );
   2720 
   2721         fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->is_xml ?
   2722                 sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage );
   2723         fs->is_first = 1;
   2724         fs->struct_indent = 0;
   2725         fs->struct_flags = CV_NODE_EMPTY;
   2726         CV_CALL( fs->buffer_start = fs->buffer = (char*)cvAlloc( buf_size + 1024 ));
   2727         fs->buffer_end = fs->buffer_start + buf_size;
   2728         if( fs->is_xml )
   2729         {
   2730             int file_size = (int)ftell( fs->file );
   2731             CV_CALL( fs->strstorage = cvCreateChildMemStorage( fs->memstorage ));
   2732             if( !append || file_size == 0 )
   2733             {
   2734                 fputs( "<?xml version=\"1.0\"?>\n", fs->file );
   2735                 fputs( "<opencv_storage>\n", fs->file );
   2736             }
   2737             else
   2738             {
   2739                 int xml_buf_size = 1 << 10;
   2740                 char substr[] = "</opencv_storage>";
   2741                 int last_occurence = -1;
   2742                 xml_buf_size = MIN(xml_buf_size, file_size);
   2743                 fseek( fs->file, -xml_buf_size, SEEK_END );
   2744                 CV_CALL(xml_buf = (char*)cvAlloc( xml_buf_size+2 ));
   2745                 // find the last occurence of </opencv_storage>
   2746                 for(;;)
   2747                 {
   2748                     int line_offset = ftell( fs->file );
   2749                     char* ptr0 = fgets( xml_buf, xml_buf_size, fs->file ), *ptr;
   2750                     if( !ptr0 )
   2751                         break;
   2752                     ptr = ptr0;
   2753                     for(;;)
   2754                     {
   2755                         ptr = strstr( ptr, substr );
   2756                         if( !ptr )
   2757                             break;
   2758                         last_occurence = line_offset + (int)(ptr - ptr0);
   2759                         ptr += strlen(substr);
   2760                     }
   2761                 }
   2762                 if( last_occurence < 0 )
   2763                     CV_ERROR( CV_StsError, "Could not find </opencv_storage> in the end of file.\n" );
   2764                 fclose( fs->file );
   2765                 fs->file = fopen( fs->filename, "r+t" );
   2766                 fseek( fs->file, last_occurence, SEEK_SET );
   2767                 // replace the last "</opencv_storage>" with " <!-- resumed -->", which has the same length
   2768                 fputs( " <!-- resumed -->", fs->file );
   2769                 fseek( fs->file, 0, SEEK_END );
   2770                 fputs( "\n", fs->file );
   2771             }
   2772             fs->start_write_struct = icvXMLStartWriteStruct;
   2773             fs->end_write_struct = icvXMLEndWriteStruct;
   2774             fs->write_int = icvXMLWriteInt;
   2775             fs->write_real = icvXMLWriteReal;
   2776             fs->write_string = icvXMLWriteString;
   2777             fs->write_comment = icvXMLWriteComment;
   2778             fs->start_next_stream = icvXMLStartNextStream;
   2779         }
   2780         else
   2781         {
   2782             if( !append )
   2783                 fputs( "%YAML:1.0\n", fs->file );
   2784             else
   2785                 fputs( "...\n---\n", fs->file );
   2786             fs->start_write_struct = icvYMLStartWriteStruct;
   2787             fs->end_write_struct = icvYMLEndWriteStruct;
   2788             fs->write_int = icvYMLWriteInt;
   2789             fs->write_real = icvYMLWriteReal;
   2790             fs->write_string = icvYMLWriteString;
   2791             fs->write_comment = icvYMLWriteComment;
   2792             fs->start_next_stream = icvYMLStartNextStream;
   2793         }
   2794     }
   2795     else
   2796     {
   2797         int buf_size;
   2798         const char* yaml_signature = "%YAML:";
   2799         char buf[16];
   2800         fgets( buf, sizeof(buf)-2, fs->file );
   2801         fs->is_xml = strncmp( buf, yaml_signature, strlen(yaml_signature) ) != 0;
   2802 
   2803         fseek( fs->file, 0, SEEK_END );
   2804         buf_size = ftell( fs->file );
   2805         fseek( fs->file, 0, SEEK_SET );
   2806 
   2807         buf_size = MIN( buf_size, (1 << 20) );
   2808         buf_size = MAX( buf_size, CV_FS_MAX_LEN*2 + 1024 );
   2809 
   2810         CV_CALL( fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash),
   2811                         sizeof(CvStringHashNode), fs->memstorage, 256 ));
   2812 
   2813         CV_CALL( fs->roots = cvCreateSeq( 0, sizeof(CvSeq),
   2814                         sizeof(CvFileNode), fs->memstorage ));
   2815 
   2816         CV_CALL( fs->buffer = fs->buffer_start = (char*)cvAlloc( buf_size + 256 ));
   2817         fs->buffer_end = fs->buffer_start + buf_size;
   2818         fs->buffer[0] = '\n';
   2819         fs->buffer[1] = '\0';
   2820 
   2821         //mode = cvGetErrMode();
   2822         //cvSetErrMode( CV_ErrModeSilent );
   2823         if( fs->is_xml )
   2824             icvXMLParse( fs );
   2825         else
   2826             icvYMLParse( fs );
   2827         //cvSetErrMode( mode );
   2828 
   2829         // release resources that we do not need anymore
   2830         cvFree( &fs->buffer_start );
   2831         fs->buffer = fs->buffer_end = 0;
   2832     }
   2833 
   2834     __END__;
   2835 
   2836     if( fs )
   2837     {
   2838         if( cvGetErrStatus() < 0 || !fs->file )
   2839         {
   2840             cvReleaseFileStorage( &fs );
   2841         }
   2842         else if( !fs->write_mode )
   2843         {
   2844             fclose( fs->file );
   2845             fs->file = 0;
   2846         }
   2847     }
   2848 
   2849     cvFree( &xml_buf );
   2850 
   2851     return  fs;
   2852 }
   2853 
   2854 
   2855 CV_IMPL void
   2856 cvStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
   2857                     const char* type_name, CvAttrList /*attributes*/ )
   2858 {
   2859     CV_FUNCNAME( "cvStartWriteStruct" );
   2860 
   2861     __BEGIN__;
   2862 
   2863     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2864     CV_CALL( fs->start_write_struct( fs, key, struct_flags, type_name ));
   2865 
   2866     __END__;
   2867 }
   2868 
   2869 
   2870 CV_IMPL void
   2871 cvEndWriteStruct( CvFileStorage* fs )
   2872 {
   2873     CV_FUNCNAME( "cvEndWriteStruct" );
   2874 
   2875     __BEGIN__;
   2876 
   2877     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2878     CV_CALL( fs->end_write_struct( fs ));
   2879 
   2880     __END__;
   2881 }
   2882 
   2883 
   2884 CV_IMPL void
   2885 cvWriteInt( CvFileStorage* fs, const char* key, int value )
   2886 {
   2887     CV_FUNCNAME( "cvWriteInt" );
   2888 
   2889     __BEGIN__;
   2890 
   2891     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2892     CV_CALL( fs->write_int( fs, key, value ));
   2893 
   2894     __END__;
   2895 }
   2896 
   2897 
   2898 CV_IMPL void
   2899 cvWriteReal( CvFileStorage* fs, const char* key, double value )
   2900 {
   2901     CV_FUNCNAME( "cvWriteReal" );
   2902 
   2903     __BEGIN__;
   2904 
   2905     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2906     CV_CALL( fs->write_real( fs, key, value ));
   2907 
   2908     __END__;
   2909 }
   2910 
   2911 
   2912 CV_IMPL void
   2913 cvWriteString( CvFileStorage* fs, const char* key, const char* value, int quote )
   2914 {
   2915     CV_FUNCNAME( "cvWriteString" );
   2916 
   2917     __BEGIN__;
   2918 
   2919     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2920     CV_CALL( fs->write_string( fs, key, value, quote ));
   2921 
   2922     __END__;
   2923 }
   2924 
   2925 
   2926 CV_IMPL void
   2927 cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
   2928 {
   2929     CV_FUNCNAME( "cvWriteComment" );
   2930 
   2931     __BEGIN__;
   2932 
   2933     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2934     CV_CALL( fs->write_comment( fs, comment, eol_comment ));
   2935 
   2936     __END__;
   2937 }
   2938 
   2939 
   2940 CV_IMPL void
   2941 cvStartNextStream( CvFileStorage* fs )
   2942 {
   2943     CV_FUNCNAME( "cvStartNextStream" );
   2944 
   2945     __BEGIN__;
   2946 
   2947     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   2948     CV_CALL( fs->start_next_stream( fs ));
   2949 
   2950     __END__;
   2951 }
   2952 
   2953 
   2954 static const char icvTypeSymbol[] = "ucwsifdr";
   2955 #define CV_FS_MAX_FMT_PAIRS  128
   2956 
   2957 static char*
   2958 icvEncodeFormat( int elem_type, char* dt )
   2959 {
   2960     sprintf( dt, "%d%c", CV_MAT_CN(elem_type), icvTypeSymbol[CV_MAT_DEPTH(elem_type)] );
   2961     return dt + ( dt[2] == '\0' && dt[0] == '1' );
   2962 }
   2963 
   2964 static int
   2965 icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len )
   2966 {
   2967     int fmt_pair_count = 0;
   2968     CV_FUNCNAME( "icvDecodeFormat" );
   2969 
   2970     __BEGIN__;
   2971 
   2972     int i = 0, k = 0, len = dt ? (int)strlen(dt) : 0;
   2973 
   2974     if( !dt || !len )
   2975         EXIT;
   2976 
   2977     assert( fmt_pairs != 0 && max_len > 0 );
   2978     fmt_pairs[0] = 0;
   2979     max_len *= 2;
   2980 
   2981     for( ; k < len; k++ )
   2982     {
   2983         char c = dt[k];
   2984 
   2985         if( isdigit(c) )
   2986         {
   2987             int count = c - '0';
   2988             if( isdigit(dt[k+1]) )
   2989             {
   2990                 char* endptr = 0;
   2991                 count = (int)strtol( dt+k, &endptr, 10 );
   2992                 k = (int)(endptr - dt) - 1;
   2993             }
   2994 
   2995             if( count <= 0 )
   2996                 CV_ERROR( CV_StsBadArg, "Invalid data type specification" );
   2997 
   2998             fmt_pairs[i] = count;
   2999         }
   3000         else
   3001         {
   3002             const char* pos = strchr( icvTypeSymbol, c );
   3003             if( !pos )
   3004                 CV_ERROR( CV_StsBadArg, "Invalid data type specification" );
   3005             if( fmt_pairs[i] == 0 )
   3006                 fmt_pairs[i] = 1;
   3007             fmt_pairs[i+1] = (int)(pos - icvTypeSymbol);
   3008             if( i > 0 && fmt_pairs[i+1] == fmt_pairs[i-1] )
   3009                 fmt_pairs[i-2] += fmt_pairs[i];
   3010             else
   3011             {
   3012                 i += 2;
   3013                 if( i >= max_len )
   3014                     CV_ERROR( CV_StsBadArg, "Too long data type specification" );
   3015             }
   3016             fmt_pairs[i] = 0;
   3017         }
   3018     }
   3019 
   3020     fmt_pair_count = i/2;
   3021 
   3022     __END__;
   3023 
   3024     return fmt_pair_count;
   3025 }
   3026 
   3027 
   3028 static int
   3029 icvCalcElemSize( const char* dt, int initial_size )
   3030 {
   3031     int size = 0;
   3032     CV_FUNCNAME( "icvCalcElemSize" );
   3033 
   3034     __BEGIN__;
   3035 
   3036     int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
   3037     int comp_size;
   3038 
   3039     CV_CALL( fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   3040     fmt_pair_count *= 2;
   3041     for( i = 0, size = initial_size; i < fmt_pair_count; i += 2 )
   3042     {
   3043         comp_size = CV_ELEM_SIZE(fmt_pairs[i+1]);
   3044         size = cvAlign( size, comp_size );
   3045         size += comp_size * fmt_pairs[i];
   3046     }
   3047     if( initial_size == 0 )
   3048     {
   3049         comp_size = CV_ELEM_SIZE(fmt_pairs[1]);
   3050         size = cvAlign( size, comp_size );
   3051     }
   3052 
   3053     __END__;
   3054 
   3055     return size;
   3056 }
   3057 
   3058 
   3059 static int
   3060 icvDecodeSimpleFormat( const char* dt )
   3061 {
   3062     int elem_type = -1;
   3063 
   3064     CV_FUNCNAME( "icvDecodeSimpleFormat" );
   3065 
   3066     __BEGIN__;
   3067 
   3068     int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
   3069 
   3070     CV_CALL( fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   3071     if( fmt_pair_count != 1 || fmt_pairs[0] > 4 )
   3072         CV_ERROR( CV_StsError, "Too complex format for the matrix" );
   3073 
   3074     elem_type = CV_MAKETYPE( fmt_pairs[1], fmt_pairs[0] );
   3075 
   3076     __END__;
   3077 
   3078     return elem_type;
   3079 }
   3080 
   3081 
   3082 CV_IMPL void
   3083 cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
   3084 {
   3085     const char* data0 = (const char*)_data;
   3086 
   3087     CV_FUNCNAME( "cvWriteRawData" );
   3088 
   3089     __BEGIN__;
   3090 
   3091     int offset = 0;
   3092     int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k, fmt_pair_count;
   3093     char buf[256] = "";
   3094 
   3095     CV_CHECK_OUTPUT_FILE_STORAGE( fs );
   3096 
   3097     if( !data0 )
   3098         CV_ERROR( CV_StsNullPtr, "Null data pointer" );
   3099 
   3100     if( len < 0 )
   3101         CV_ERROR( CV_StsOutOfRange, "Negative number of elements" );
   3102 
   3103     CV_CALL( fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   3104 
   3105     if( !len )
   3106         EXIT;
   3107 
   3108     if( fmt_pair_count == 1 )
   3109     {
   3110         fmt_pairs[0] *= len;
   3111         len = 1;
   3112     }
   3113 
   3114     for(;len--;)
   3115     {
   3116         for( k = 0; k < fmt_pair_count; k++ )
   3117         {
   3118             int i, count = fmt_pairs[k*2];
   3119             int elem_type = fmt_pairs[k*2+1];
   3120             int elem_size = CV_ELEM_SIZE(elem_type);
   3121             const char* data, *ptr;
   3122 
   3123             offset = cvAlign( offset, elem_size );
   3124             data = data0 + offset;
   3125 
   3126             for( i = 0; i < count; i++ )
   3127             {
   3128                 switch( elem_type )
   3129                 {
   3130                 case CV_8U:
   3131                     ptr = icv_itoa( *(uchar*)data, buf, 10 );
   3132                     data++;
   3133                     break;
   3134                 case CV_8S:
   3135                     ptr = icv_itoa( *(char*)data, buf, 10 );
   3136                     data++;
   3137                     break;
   3138                 case CV_16U:
   3139                     ptr = icv_itoa( *(ushort*)data, buf, 10 );
   3140                     data += sizeof(ushort);
   3141                     break;
   3142                 case CV_16S:
   3143                     ptr = icv_itoa( *(short*)data, buf, 10 );
   3144                     data += sizeof(short);
   3145                     break;
   3146                 case CV_32S:
   3147                     ptr = icv_itoa( *(int*)data, buf, 10 );
   3148                     data += sizeof(int);
   3149                     break;
   3150                 case CV_32F:
   3151                     ptr = icvFloatToString( buf, *(float*)data );
   3152                     data += sizeof(float);
   3153                     break;
   3154                 case CV_64F:
   3155                     ptr = icvDoubleToString( buf, *(double*)data );
   3156                     data += sizeof(double);
   3157                     break;
   3158                 case CV_USRTYPE1: /* reference */
   3159                     ptr = icv_itoa( (int)*(size_t*)data, buf, 10 );
   3160                     data += sizeof(size_t);
   3161                     break;
   3162                 default:
   3163                     assert(0);
   3164                     EXIT;
   3165                 }
   3166 
   3167                 if( fs->is_xml )
   3168                 {
   3169                     int buf_len = (int)strlen(ptr);
   3170                     CV_CALL( icvXMLWriteScalar( fs, 0, ptr, buf_len ));
   3171                 }
   3172                 else
   3173                     CV_CALL( icvYMLWrite( fs, 0, ptr, cvFuncName ));
   3174             }
   3175 
   3176             offset = (int)(data - data0);
   3177         }
   3178     }
   3179 
   3180     __END__;
   3181 }
   3182 
   3183 
   3184 CV_IMPL void
   3185 cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader )
   3186 {
   3187     CV_FUNCNAME( "cvStartReadRawData" );
   3188 
   3189     __BEGIN__;
   3190 
   3191     int node_type;
   3192     CV_CHECK_FILE_STORAGE( fs );
   3193 
   3194     if( !src || !reader )
   3195         CV_ERROR( CV_StsNullPtr, "Null pointer to source file node or reader" );
   3196 
   3197     node_type = CV_NODE_TYPE(src->tag);
   3198     if( node_type == CV_NODE_INT || node_type == CV_NODE_REAL )
   3199     {
   3200         // emulate reading from 1-element sequence
   3201         reader->ptr = (schar*)src;
   3202         reader->block_max = reader->ptr + sizeof(*src)*2;
   3203         reader->block_min = reader->ptr;
   3204         reader->seq = 0;
   3205     }
   3206     else if( node_type == CV_NODE_SEQ )
   3207     {
   3208         CV_CALL( cvStartReadSeq( src->data.seq, reader, 0 ));
   3209     }
   3210     else if( node_type == CV_NODE_NONE )
   3211     {
   3212         memset( reader, 0, sizeof(*reader) );
   3213     }
   3214     else
   3215         CV_ERROR( CV_StsBadArg, "The file node should be a numerical scalar or a sequence" );
   3216 
   3217     __END__;
   3218 }
   3219 
   3220 
   3221 CV_IMPL void
   3222 cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,
   3223                     int len, void* _data, const char* dt )
   3224 {
   3225     char* data0 = (char*)_data;
   3226     CV_FUNCNAME( "cvReadRawDataSlice" );
   3227 
   3228     __BEGIN__;
   3229 
   3230     int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k = 0, fmt_pair_count;
   3231     int i = 0, offset = 0, count = 0;
   3232 
   3233     CV_CHECK_FILE_STORAGE( fs );
   3234 
   3235     if( !reader || !data0 )
   3236         CV_ERROR( CV_StsNullPtr, "Null pointer to reader or destination array" );
   3237 
   3238     if( !reader->seq && len != 1 )
   3239         CV_ERROR( CV_StsBadSize, "The readed sequence is a scalar, thus len must be 1" );
   3240 
   3241     CV_CALL( fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   3242 
   3243     for(;;)
   3244     {
   3245         for( k = 0; k < fmt_pair_count; k++ )
   3246         {
   3247             int elem_type = fmt_pairs[k*2+1];
   3248             int elem_size = CV_ELEM_SIZE(elem_type);
   3249             char* data;
   3250 
   3251             count = fmt_pairs[k*2];
   3252             offset = cvAlign( offset, elem_size );
   3253             data = data0 + offset;
   3254 
   3255             for( i = 0; i < count; i++ )
   3256             {
   3257                 CvFileNode* node = (CvFileNode*)reader->ptr;
   3258                 if( CV_NODE_IS_INT(node->tag) )
   3259                 {
   3260                     int ival = node->data.i;
   3261 
   3262                     switch( elem_type )
   3263                     {
   3264                     case CV_8U:
   3265                         *(uchar*)data = CV_CAST_8U(ival);
   3266                         data++;
   3267                         break;
   3268                     case CV_8S:
   3269                         *(char*)data = CV_CAST_8S(ival);
   3270                         break;
   3271                     case CV_16U:
   3272                         *(ushort*)data = CV_CAST_16U(ival);
   3273                         data += sizeof(ushort);
   3274                         break;
   3275                     case CV_16S:
   3276                         *(short*)data = CV_CAST_16S(ival);
   3277                         data += sizeof(short);
   3278                         break;
   3279                     case CV_32S:
   3280                         *(int*)data = ival;
   3281                         data += sizeof(int);
   3282                         break;
   3283                     case CV_32F:
   3284                         *(float*)data = (float)ival;
   3285                         data += sizeof(float);
   3286                         break;
   3287                     case CV_64F:
   3288                         *(double*)data = (double)ival;
   3289                         data += sizeof(double);
   3290                         break;
   3291                     case CV_USRTYPE1: /* reference */
   3292                         *(size_t*)data = ival;
   3293                         data += sizeof(size_t);
   3294                         break;
   3295                     default:
   3296                         assert(0);
   3297                         EXIT;
   3298                     }
   3299                 }
   3300                 else if( CV_NODE_IS_REAL(node->tag) )
   3301                 {
   3302                     double fval = node->data.f;
   3303                     int ival;
   3304 
   3305                     switch( elem_type )
   3306                     {
   3307                     case CV_8U:
   3308                         ival = cvRound(fval);
   3309                         *(uchar*)data = CV_CAST_8U(ival);
   3310                         data++;
   3311                         break;
   3312                     case CV_8S:
   3313                         ival = cvRound(fval);
   3314                         *(char*)data = CV_CAST_8S(ival);
   3315                         break;
   3316                     case CV_16U:
   3317                         ival = cvRound(fval);
   3318                         *(ushort*)data = CV_CAST_16U(ival);
   3319                         data += sizeof(ushort);
   3320                         break;
   3321                     case CV_16S:
   3322                         ival = cvRound(fval);
   3323                         *(short*)data = CV_CAST_16S(ival);
   3324                         data += sizeof(short);
   3325                         break;
   3326                     case CV_32S:
   3327                         ival = cvRound(fval);
   3328                         *(int*)data = ival;
   3329                         data += sizeof(int);
   3330                         break;
   3331                     case CV_32F:
   3332                         *(float*)data = (float)fval;
   3333                         data += sizeof(float);
   3334                         break;
   3335                     case CV_64F:
   3336                         *(double*)data = fval;
   3337                         data += sizeof(double);
   3338                         break;
   3339                     case CV_USRTYPE1: /* reference */
   3340                         ival = cvRound(fval);
   3341                         *(size_t*)data = ival;
   3342                         data += sizeof(size_t);
   3343                         break;
   3344                     default:
   3345                         assert(0);
   3346                         EXIT;
   3347                     }
   3348                 }
   3349                 else
   3350                     CV_ERROR( CV_StsError,
   3351                     "The sequence element is not a numerical scalar" );
   3352 
   3353                 CV_NEXT_SEQ_ELEM( sizeof(CvFileNode), *reader );
   3354                 if( !--len )
   3355                     goto end_loop;
   3356             }
   3357 
   3358             offset = (int)(data - data0);
   3359         }
   3360     }
   3361 
   3362 end_loop:
   3363     if( i != count - 1 || k != fmt_pair_count - 1 )
   3364         CV_ERROR( CV_StsBadSize,
   3365         "The sequence slice does not fit an integer number of records" );
   3366 
   3367     if( !reader->seq )
   3368         reader->ptr -= sizeof(CvFileNode);
   3369 
   3370     __END__;
   3371 }
   3372 
   3373 
   3374 CV_IMPL void
   3375 cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,
   3376                void* data, const char* dt )
   3377 {
   3378     CV_FUNCNAME( "cvReadRawData" );
   3379 
   3380     __BEGIN__;
   3381 
   3382     CvSeqReader reader;
   3383 
   3384     if( !src || !data )
   3385         CV_ERROR( CV_StsNullPtr, "Null pointers to source file node or destination array" );
   3386 
   3387     CV_CALL( cvStartReadRawData( fs, src, &reader ));
   3388     cvReadRawDataSlice( fs, &reader, CV_NODE_IS_SEQ(src->tag) ?
   3389                         src->data.seq->total : 1, data, dt );
   3390 
   3391     __END__;
   3392 }
   3393 
   3394 
   3395 static void
   3396 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node );
   3397 
   3398 static void
   3399 icvWriteCollection( CvFileStorage* fs, const CvFileNode* node )
   3400 {
   3401     int i, total = node->data.seq->total;
   3402     int elem_size = node->data.seq->elem_size;
   3403     int is_map = CV_NODE_IS_MAP(node->tag);
   3404     CvSeqReader reader;
   3405 
   3406     cvStartReadSeq( node->data.seq, &reader, 0 );
   3407 
   3408     for( i = 0; i < total; i++ )
   3409     {
   3410         CvFileMapNode* elem = (CvFileMapNode*)reader.ptr;
   3411         if( !is_map || CV_IS_SET_ELEM(elem) )
   3412         {
   3413             const char* name = is_map ? elem->key->str.ptr : 0;
   3414             icvWriteFileNode( fs, name, &elem->value );
   3415         }
   3416         CV_NEXT_SEQ_ELEM( elem_size, reader );
   3417     }
   3418 }
   3419 
   3420 static void
   3421 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node )
   3422 {
   3423     CV_FUNCNAME( "icvWriteFileNode" );
   3424 
   3425     __BEGIN__;
   3426 
   3427     switch( CV_NODE_TYPE(node->tag) )
   3428     {
   3429     case CV_NODE_INT:
   3430         fs->write_int( fs, name, node->data.i );
   3431         break;
   3432     case CV_NODE_REAL:
   3433         fs->write_real( fs, name, node->data.f );
   3434         break;
   3435     case CV_NODE_STR:
   3436         fs->write_string( fs, name, node->data.str.ptr, 0 );
   3437         break;
   3438     case CV_NODE_SEQ:
   3439     case CV_NODE_MAP:
   3440         fs->start_write_struct( fs, name, CV_NODE_TYPE(node->tag) +
   3441                 (CV_NODE_SEQ_IS_SIMPLE(node->data.seq) ? CV_NODE_FLOW : 0),
   3442                 node->info ? node->info->type_name : 0 );
   3443         icvWriteCollection( fs, node );
   3444         fs->end_write_struct( fs );
   3445         break;
   3446     case CV_NODE_NONE:
   3447         fs->start_write_struct( fs, name, CV_NODE_SEQ, 0 );
   3448         fs->end_write_struct( fs );
   3449         break;
   3450     default:
   3451         CV_ERROR( CV_StsBadFlag, "Unknown type of file node" );
   3452     }
   3453 
   3454     __END__;
   3455 }
   3456 
   3457 
   3458 CV_IMPL void
   3459 cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,
   3460                  const CvFileNode* node, int embed )
   3461 {
   3462     CvFileStorage* dst = 0;
   3463 
   3464     CV_FUNCNAME( "cvWriteFileNode" );
   3465 
   3466     __BEGIN__;
   3467 
   3468     CV_CHECK_OUTPUT_FILE_STORAGE(fs);
   3469 
   3470     if( !node )
   3471         EXIT;
   3472 
   3473     if( CV_NODE_IS_COLLECTION(node->tag) && embed )
   3474     {
   3475         CV_CALL( icvWriteCollection( fs, node ));
   3476     }
   3477     else
   3478     {
   3479         CV_CALL( icvWriteFileNode( fs, new_node_name, node ));
   3480     }
   3481     /*
   3482     int i, stream_count;
   3483     stream_count = fs->roots->total;
   3484     for( i = 0; i < stream_count; i++ )
   3485     {
   3486         CvFileNode* node = (CvFileNode*)cvGetSeqElem( fs->roots, i, 0 );
   3487         icvDumpCollection( dst, node );
   3488         if( i < stream_count - 1 )
   3489             dst->start_next_stream( dst );
   3490     }*/
   3491 
   3492     __END__;
   3493 
   3494     cvReleaseFileStorage( &dst );
   3495 }
   3496 
   3497 
   3498 CV_IMPL const char*
   3499 cvGetFileNodeName( const CvFileNode* file_node )
   3500 {
   3501     return file_node && CV_NODE_HAS_NAME(file_node->tag) ?
   3502         ((CvFileMapNode*)file_node)->key->str.ptr : 0;
   3503 }
   3504 
   3505 /****************************************************************************************\
   3506 *                          Reading/Writing etc. for standard types                       *
   3507 \****************************************************************************************/
   3508 
   3509 /*#define CV_TYPE_NAME_MAT "opencv-matrix"
   3510 #define CV_TYPE_NAME_MATND "opencv-nd-matrix"
   3511 #define CV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix"
   3512 #define CV_TYPE_NAME_IMAGE "opencv-image"
   3513 #define CV_TYPE_NAME_SEQ "opencv-sequence"
   3514 #define CV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree"
   3515 #define CV_TYPE_NAME_GRAPH "opencv-graph"*/
   3516 
   3517 /******************************* CvMat ******************************/
   3518 
   3519 static int
   3520 icvIsMat( const void* ptr )
   3521 {
   3522     return CV_IS_MAT_HDR(ptr);
   3523 }
   3524 
   3525 static void
   3526 icvWriteMat( CvFileStorage* fs, const char* name,
   3527              const void* struct_ptr, CvAttrList /*attr*/ )
   3528 {
   3529     CV_FUNCNAME( "icvWriteMat" );
   3530 
   3531     __BEGIN__;
   3532 
   3533     const CvMat* mat = (const CvMat*)struct_ptr;
   3534     char dt[16];
   3535     CvSize size;
   3536     int y;
   3537 
   3538     assert( CV_IS_MAT(mat) );
   3539 
   3540     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT ));
   3541     cvWriteInt( fs, "rows", mat->rows );
   3542     cvWriteInt( fs, "cols", mat->cols );
   3543     cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
   3544     cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
   3545 
   3546     size = cvGetSize(mat);
   3547     if( CV_IS_MAT_CONT(mat->type) )
   3548     {
   3549         size.width *= size.height;
   3550         size.height = 1;
   3551     }
   3552 
   3553     for( y = 0; y < size.height; y++ )
   3554         cvWriteRawData( fs, mat->data.ptr + y*mat->step, size.width, dt );
   3555     cvEndWriteStruct( fs );
   3556     cvEndWriteStruct( fs );
   3557 
   3558     __END__;
   3559 }
   3560 
   3561 
   3562 static int
   3563 icvFileNodeSeqLen( CvFileNode* node )
   3564 {
   3565     return CV_NODE_IS_COLLECTION(node->tag) ? node->data.seq->total :
   3566            CV_NODE_TYPE(node->tag) != CV_NODE_NONE;
   3567 }
   3568 
   3569 
   3570 static void*
   3571 icvReadMat( CvFileStorage* fs, CvFileNode* node )
   3572 {
   3573     void* ptr = 0;
   3574     CV_FUNCNAME( "icvReadMat" );
   3575 
   3576     __BEGIN__;
   3577 
   3578     CvMat* mat;
   3579     const char* dt;
   3580     CvFileNode* data;
   3581     int rows, cols, elem_type;
   3582 
   3583     CV_CALL( rows = cvReadIntByName( fs, node, "rows", 0 ));
   3584     cols = cvReadIntByName( fs, node, "cols", 0 );
   3585     dt = cvReadStringByName( fs, node, "dt", 0 );
   3586 
   3587     if( rows == 0 || cols == 0 || dt == 0 )
   3588         CV_ERROR( CV_StsError, "Some of essential matrix attributes are absent" );
   3589 
   3590     CV_CALL( elem_type = icvDecodeSimpleFormat( dt ));
   3591 
   3592     data = cvGetFileNodeByName( fs, node, "data" );
   3593     if( !data )
   3594         CV_ERROR( CV_StsError, "The matrix data is not found in file storage" );
   3595 
   3596     if( icvFileNodeSeqLen( data ) != rows*cols*CV_MAT_CN(elem_type) )
   3597         CV_ERROR( CV_StsUnmatchedSizes,
   3598         "The matrix size does not match to the number of stored elements" );
   3599 
   3600     CV_CALL( mat = cvCreateMat( rows, cols, elem_type ));
   3601     CV_CALL( cvReadRawData( fs, data, mat->data.ptr, dt ));
   3602 
   3603     ptr = mat;
   3604 
   3605     __END__;
   3606 
   3607     return ptr;
   3608 }
   3609 
   3610 
   3611 /******************************* CvMatND ******************************/
   3612 
   3613 static int
   3614 icvIsMatND( const void* ptr )
   3615 {
   3616     return CV_IS_MATND(ptr);
   3617 }
   3618 
   3619 
   3620 static void
   3621 icvWriteMatND( CvFileStorage* fs, const char* name,
   3622                const void* struct_ptr, CvAttrList /*attr*/ )
   3623 {
   3624     CV_FUNCNAME( "icvWriteMatND" );
   3625 
   3626     __BEGIN__;
   3627 
   3628     void* mat = (void*)struct_ptr;
   3629     CvMatND stub;
   3630     CvNArrayIterator iterator;
   3631     int dims, sizes[CV_MAX_DIM];
   3632     char dt[16];
   3633 
   3634     assert( CV_IS_MATND(mat) );
   3635 
   3636     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MATND ));
   3637     dims = cvGetDims( mat, sizes );
   3638     cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
   3639     cvWriteRawData( fs, sizes, dims, "i" );
   3640     cvEndWriteStruct( fs );
   3641     cvWriteString( fs, "dt", icvEncodeFormat( cvGetElemType(mat), dt ), 0 );
   3642     cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
   3643 
   3644     CV_CALL( cvInitNArrayIterator( 1, &mat, 0, &stub, &iterator ));
   3645 
   3646     do
   3647         cvWriteRawData( fs, iterator.ptr[0], iterator.size.width, dt );
   3648     while( cvNextNArraySlice( &iterator ));
   3649     cvEndWriteStruct( fs );
   3650     cvEndWriteStruct( fs );
   3651 
   3652     __END__;
   3653 }
   3654 
   3655 
   3656 static void*
   3657 icvReadMatND( CvFileStorage* fs, CvFileNode* node )
   3658 {
   3659     void* ptr = 0;
   3660     CV_FUNCNAME( "icvReadMatND" );
   3661 
   3662     __BEGIN__;
   3663 
   3664     CvMatND* mat;
   3665     const char* dt;
   3666     CvFileNode* data;
   3667     CvFileNode* sizes_node;
   3668     int sizes[CV_MAX_DIM], dims, elem_type;
   3669     int i, total_size;
   3670 
   3671     CV_CALL( sizes_node = cvGetFileNodeByName( fs, node, "sizes" ));
   3672     dt = cvReadStringByName( fs, node, "dt", 0 );
   3673 
   3674     if( !sizes_node || !dt )
   3675         CV_ERROR( CV_StsError, "Some of essential matrix attributes are absent" );
   3676 
   3677     dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
   3678            CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
   3679 
   3680     if( dims <= 0 || dims > CV_MAX_DIM )
   3681         CV_ERROR( CV_StsParseError, "Could not determine the matrix dimensionality" );
   3682 
   3683     CV_CALL( cvReadRawData( fs, sizes_node, sizes, "i" ));
   3684     CV_CALL( elem_type = icvDecodeSimpleFormat( dt ));
   3685 
   3686     data = cvGetFileNodeByName( fs, node, "data" );
   3687     if( !data )
   3688         CV_ERROR( CV_StsError, "The matrix data is not found in file storage" );
   3689 
   3690     for( total_size = CV_MAT_CN(elem_type), i = 0; i < dims; i++ )
   3691         total_size *= sizes[i];
   3692 
   3693     if( icvFileNodeSeqLen( data ) != total_size )
   3694         CV_ERROR( CV_StsUnmatchedSizes,
   3695         "The matrix size does not match to the number of stored elements" );
   3696 
   3697     CV_CALL( mat = cvCreateMatND( dims, sizes, elem_type ));
   3698     CV_CALL( cvReadRawData( fs, data, mat->data.ptr, dt ));
   3699 
   3700     ptr = mat;
   3701 
   3702     __END__;
   3703 
   3704     return ptr;
   3705 }
   3706 
   3707 
   3708 /******************************* CvSparseMat ******************************/
   3709 
   3710 static int
   3711 icvIsSparseMat( const void* ptr )
   3712 {
   3713     return CV_IS_SPARSE_MAT(ptr);
   3714 }
   3715 
   3716 
   3717 static int
   3718 icvSortIdxCmpFunc( const void* _a, const void* _b, void* userdata )
   3719 {
   3720     int i, dims = *(int*)userdata;
   3721     const int* a = *(const int**)_a;
   3722     const int* b = *(const int**)_b;
   3723 
   3724     for( i = 0; i < dims; i++ )
   3725     {
   3726         int delta = a[i] - b[i];
   3727         if( delta )
   3728             return delta;
   3729     }
   3730 
   3731     return 0;
   3732 }
   3733 
   3734 
   3735 static void
   3736 icvWriteSparseMat( CvFileStorage* fs, const char* name,
   3737                    const void* struct_ptr, CvAttrList /*attr*/ )
   3738 {
   3739     CvMemStorage* memstorage = 0;
   3740 
   3741     CV_FUNCNAME( "icvWriteSparseMat" );
   3742 
   3743     __BEGIN__;
   3744 
   3745     const CvSparseMat* mat = (const CvSparseMat*)struct_ptr;
   3746     CvSparseMatIterator iterator;
   3747     CvSparseNode* node;
   3748     CvSeq* elements;
   3749     CvSeqReader reader;
   3750     int i, dims;
   3751     int *prev_idx = 0;
   3752     char dt[16];
   3753 
   3754     assert( CV_IS_SPARSE_MAT(mat) );
   3755 
   3756     CV_CALL( memstorage = cvCreateMemStorage());
   3757 
   3758     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SPARSE_MAT ));
   3759     dims = cvGetDims( mat, 0 );
   3760 
   3761     cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
   3762     cvWriteRawData( fs, mat->size, dims, "i