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" );
   3763     cvEndWriteStruct( fs );
   3764     cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
   3765     cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
   3766 
   3767     elements = cvCreateSeq( CV_SEQ_ELTYPE_PTR, sizeof(CvSeq), sizeof(int*), memstorage );
   3768 
   3769     node = cvInitSparseMatIterator( mat, &iterator );
   3770     while( node )
   3771     {
   3772         int* idx = CV_NODE_IDX( mat, node );
   3773         cvSeqPush( elements, &idx );
   3774         node = cvGetNextSparseNode( &iterator );
   3775     }
   3776 
   3777     cvSeqSort( elements, icvSortIdxCmpFunc, &dims );
   3778     cvStartReadSeq( elements, &reader, 0 );
   3779 
   3780     for( i = 0; i < elements->total; i++ )
   3781     {
   3782         int* idx;
   3783         void* val;
   3784         int k = 0;
   3785 
   3786         CV_READ_SEQ_ELEM( idx, reader );
   3787         if( i > 0 )
   3788         {
   3789             for( ; idx[k] == prev_idx[k]; k++ )
   3790                 assert( k < dims );
   3791             if( k < dims - 1 )
   3792                 fs->write_int( fs, 0, k - dims + 1 );
   3793         }
   3794         for( ; k < dims; k++ )
   3795             fs->write_int( fs, 0, idx[k] );
   3796         prev_idx = idx;
   3797 
   3798         node = (CvSparseNode*)((uchar*)idx - mat->idxoffset );
   3799         val = CV_NODE_VAL( mat, node );
   3800 
   3801         cvWriteRawData( fs, val, 1, dt );
   3802     }
   3803 
   3804     cvEndWriteStruct( fs );
   3805     cvEndWriteStruct( fs );
   3806 
   3807     __END__;
   3808 
   3809     cvReleaseMemStorage( &memstorage );
   3810 }
   3811 
   3812 
   3813 static void*
   3814 icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
   3815 {
   3816     void* ptr = 0;
   3817     CV_FUNCNAME( "icvReadSparseMat" );
   3818 
   3819     __BEGIN__;
   3820 
   3821     CvSparseMat* mat;
   3822     const char* dt;
   3823     CvFileNode* data;
   3824     CvFileNode* sizes_node;
   3825     CvSeqReader reader;
   3826     CvSeq* elements;
   3827     int* idx;
   3828     int* sizes = 0, dims, elem_type, cn;
   3829     int i;
   3830 
   3831     CV_CALL( sizes_node = cvGetFileNodeByName( fs, node, "sizes" ));
   3832     dt = cvReadStringByName( fs, node, "dt", 0 );
   3833 
   3834     if( !sizes_node || !dt )
   3835         CV_ERROR( CV_StsError, "Some of essential matrix attributes are absent" );
   3836 
   3837     dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
   3838            CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
   3839 
   3840     if( dims <= 0 || dims > CV_MAX_DIM_HEAP )
   3841         CV_ERROR( CV_StsParseError, "Could not determine sparse matrix dimensionality" );
   3842 
   3843     sizes = (int*)alloca( dims*sizeof(sizes[0]));
   3844     CV_CALL( cvReadRawData( fs, sizes_node, sizes, "i" ));
   3845     CV_CALL( elem_type = icvDecodeSimpleFormat( dt ));
   3846 
   3847     data = cvGetFileNodeByName( fs, node, "data" );
   3848     if( !data || !CV_NODE_IS_SEQ(data->tag) )
   3849         CV_ERROR( CV_StsError, "The matrix data is not found in file storage" );
   3850 
   3851     CV_CALL( mat = cvCreateSparseMat( dims, sizes, elem_type ));
   3852 
   3853     cn = CV_MAT_CN(elem_type);
   3854     idx = (int*)alloca( dims*sizeof(idx[0]) );
   3855     elements = data->data.seq;
   3856     cvStartReadRawData( fs, data, &reader );
   3857 
   3858     for( i = 0; i < elements->total; )
   3859     {
   3860         CvFileNode* elem = (CvFileNode*)reader.ptr;
   3861         uchar* val;
   3862         int k;
   3863         if( !CV_NODE_IS_INT(elem->tag ))
   3864             CV_ERROR( CV_StsParseError, "Sparse matrix data is corrupted" );
   3865         k = elem->data.i;
   3866         if( i > 0 && k >= 0 )
   3867             idx[dims-1] = k;
   3868         else
   3869         {
   3870             if( i > 0 )
   3871                 k = dims + k - 1;
   3872             else
   3873                 idx[0] = k, k = 1;
   3874             for( ; k < dims; k++ )
   3875             {
   3876                 CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
   3877                 i++;
   3878                 elem = (CvFileNode*)reader.ptr;
   3879                 if( !CV_NODE_IS_INT(elem->tag ) || elem->data.i < 0 )
   3880                     CV_ERROR( CV_StsParseError, "Sparse matrix data is corrupted" );
   3881                 idx[k] = elem->data.i;
   3882             }
   3883         }
   3884         CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
   3885         i++;
   3886         CV_CALL( val = cvPtrND( mat, idx, 0, 1, 0 ));
   3887         CV_CALL( cvReadRawDataSlice( fs, &reader, cn, val, dt ));
   3888         i += cn;
   3889     }
   3890 
   3891     ptr = mat;
   3892 
   3893     __END__;
   3894 
   3895     return ptr;
   3896 }
   3897 
   3898 
   3899 /******************************* IplImage ******************************/
   3900 
   3901 static int
   3902 icvIsImage( const void* ptr )
   3903 {
   3904     return CV_IS_IMAGE_HDR(ptr);
   3905 }
   3906 
   3907 static void
   3908 icvWriteImage( CvFileStorage* fs, const char* name,
   3909                const void* struct_ptr, CvAttrList /*attr*/ )
   3910 {
   3911     CV_FUNCNAME( "icvWriteImage" );
   3912 
   3913     __BEGIN__;
   3914 
   3915     const IplImage* image = (const IplImage*)struct_ptr;
   3916     char dt_buf[16], *dt;
   3917     CvSize size;
   3918     int y, depth;
   3919 
   3920     assert( CV_IS_IMAGE(image) );
   3921 
   3922     if( image->dataOrder == IPL_DATA_ORDER_PLANE )
   3923         CV_ERROR( CV_StsUnsupportedFormat,
   3924         "Images with planar data layout are not supported" );
   3925 
   3926     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_IMAGE ));
   3927     cvWriteInt( fs, "width", image->width );
   3928     cvWriteInt( fs, "height", image->height );
   3929     cvWriteString( fs, "origin", image->origin == IPL_ORIGIN_TL
   3930                    ? "top-left" : "bottom-left", 0 );
   3931     cvWriteString( fs, "layout", image->dataOrder == IPL_DATA_ORDER_PLANE
   3932                    ? "planar" : "interleaved", 0 );
   3933     if( image->roi )
   3934     {
   3935         cvStartWriteStruct( fs, "roi", CV_NODE_MAP + CV_NODE_FLOW );
   3936         cvWriteInt( fs, "x", image->roi->xOffset );
   3937         cvWriteInt( fs, "y", image->roi->yOffset );
   3938         cvWriteInt( fs, "width", image->roi->width );
   3939         cvWriteInt( fs, "height", image->roi->height );
   3940         cvWriteInt( fs, "coi", image->roi->coi );
   3941         cvEndWriteStruct( fs );
   3942     }
   3943 
   3944     depth = icvIplToCvDepth(image->depth);
   3945     sprintf( dt_buf, "%d%c", image->nChannels, icvTypeSymbol[depth] );
   3946     dt = dt_buf + (dt_buf[2] == '\0' && dt_buf[0] == '1');
   3947     cvWriteString( fs, "dt", dt, 0 );
   3948 
   3949     size = cvSize(image->width, image->height);
   3950     if( size.width*image->nChannels*CV_ELEM_SIZE(depth) == image->widthStep )
   3951     {
   3952         size.width *= size.height;
   3953         size.height = 1;
   3954     }
   3955 
   3956     cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
   3957     for( y = 0; y < size.height; y++ )
   3958         cvWriteRawData( fs, image->imageData + y*image->widthStep, size.width, dt );
   3959     cvEndWriteStruct( fs );
   3960     cvEndWriteStruct( fs );
   3961 
   3962     __END__;
   3963 }
   3964 
   3965 
   3966 static void*
   3967 icvReadImage( CvFileStorage* fs, CvFileNode* node )
   3968 {
   3969     void* ptr = 0;
   3970     CV_FUNCNAME( "icvReadImage" );
   3971 
   3972     __BEGIN__;
   3973 
   3974     IplImage* image;
   3975     const char* dt;
   3976     CvFileNode* data;
   3977     CvFileNode* roi_node;
   3978     CvSeqReader reader;
   3979     CvRect roi;
   3980     int y, width, height, elem_type, coi, depth;
   3981     const char* origin, *data_order;
   3982 
   3983     CV_CALL( width = cvReadIntByName( fs, node, "width", 0 ));
   3984     height = cvReadIntByName( fs, node, "height", 0 );
   3985     dt = cvReadStringByName( fs, node, "dt", 0 );
   3986     origin = cvReadStringByName( fs, node, "origin", 0 );
   3987 
   3988     if( width == 0 || height == 0 || dt == 0 || origin == 0 )
   3989         CV_ERROR( CV_StsError, "Some of essential image attributes are absent" );
   3990 
   3991     CV_CALL( elem_type = icvDecodeSimpleFormat( dt ));
   3992     data_order = cvReadStringByName( fs, node, "layout", "interleaved" );
   3993     if( strcmp( data_order, "interleaved" ) != 0 )
   3994         CV_ERROR( CV_StsError, "Only interleaved images can be read" );
   3995 
   3996     data = cvGetFileNodeByName( fs, node, "data" );
   3997     if( !data )
   3998         CV_ERROR( CV_StsError, "The image data is not found in file storage" );
   3999 
   4000     if( icvFileNodeSeqLen( data ) != width*height*CV_MAT_CN(elem_type) )
   4001         CV_ERROR( CV_StsUnmatchedSizes,
   4002         "The matrix size does not match to the number of stored elements" );
   4003 
   4004     depth = cvCvToIplDepth(elem_type);
   4005     CV_CALL( image = cvCreateImage( cvSize(width,height), depth, CV_MAT_CN(elem_type) ));
   4006 
   4007     roi_node = cvGetFileNodeByName( fs, node, "roi" );
   4008     if( roi_node )
   4009     {
   4010         roi.x = cvReadIntByName( fs, roi_node, "x", 0 );
   4011         roi.y = cvReadIntByName( fs, roi_node, "y", 0 );
   4012         roi.width = cvReadIntByName( fs, roi_node, "width", 0 );
   4013         roi.height = cvReadIntByName( fs, roi_node, "height", 0 );
   4014         coi = cvReadIntByName( fs, roi_node, "coi", 0 );
   4015 
   4016         cvSetImageROI( image, roi );
   4017         cvSetImageCOI( image, coi );
   4018     }
   4019 
   4020     if( width*CV_ELEM_SIZE(elem_type) == image->widthStep )
   4021     {
   4022         width *= height;
   4023         height = 1;
   4024     }
   4025 
   4026     width *= CV_MAT_CN(elem_type);
   4027     cvStartReadRawData( fs, data, &reader );
   4028     for( y = 0; y < height; y++ )
   4029     {
   4030         CV_CALL( cvReadRawDataSlice( fs, &reader, width,
   4031             image->imageData + y*image->widthStep, dt ));
   4032     }
   4033 
   4034     ptr = image;
   4035 
   4036     __END__;
   4037 
   4038     return ptr;
   4039 }
   4040 
   4041 
   4042 /******************************* CvSeq ******************************/
   4043 
   4044 static int
   4045 icvIsSeq( const void* ptr )
   4046 {
   4047     return CV_IS_SEQ(ptr);
   4048 }
   4049 
   4050 
   4051 static void
   4052 icvReleaseSeq( void** ptr )
   4053 {
   4054     CV_FUNCNAME( "icvReleaseSeq" );
   4055 
   4056     __BEGIN__;
   4057 
   4058     if( !ptr )
   4059         CV_ERROR( CV_StsNullPtr, "NULL double pointer" );
   4060 
   4061     *ptr = 0; // it's impossible now to release seq, so just clear the pointer
   4062 
   4063     __END__;
   4064 }
   4065 
   4066 
   4067 static void*
   4068 icvCloneSeq( const void* ptr )
   4069 {
   4070     return cvSeqSlice( (CvSeq*)ptr, CV_WHOLE_SEQ,
   4071                        0 /* use the same storage as for the original sequence */, 1 );
   4072 }
   4073 
   4074 
   4075 static void
   4076 icvWriteHeaderData( CvFileStorage* fs, const CvSeq* seq,
   4077                     CvAttrList* attr, int initial_header_size )
   4078 {
   4079     CV_FUNCNAME( "icvWriteHeaderData" );
   4080 
   4081     __BEGIN__;
   4082 
   4083     char header_dt_buf[128];
   4084     const char* header_dt = cvAttrValue( attr, "header_dt" );
   4085 
   4086     if( header_dt )
   4087     {
   4088         int dt_header_size;
   4089         CV_CALL( dt_header_size = icvCalcElemSize( header_dt, initial_header_size ));
   4090         if( dt_header_size > seq->header_size )
   4091             CV_ERROR( CV_StsUnmatchedSizes,
   4092             "The size of header calculated from \"header_dt\" is greater than header_size" );
   4093     }
   4094     else if( seq->header_size > initial_header_size )
   4095     {
   4096         if( CV_IS_SEQ(seq) && CV_IS_SEQ_POINT_SET(seq) &&
   4097             seq->header_size == sizeof(CvPoint2DSeq) &&
   4098             seq->elem_size == sizeof(int)*2 )
   4099         {
   4100             CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
   4101 
   4102             cvStartWriteStruct( fs, "rect", CV_NODE_MAP + CV_NODE_FLOW );
   4103             cvWriteInt( fs, "x", point_seq->rect.x );
   4104             cvWriteInt( fs, "y", point_seq->rect.y );
   4105             cvWriteInt( fs, "width", point_seq->rect.width );
   4106             cvWriteInt( fs, "height", point_seq->rect.height );
   4107             cvEndWriteStruct( fs );
   4108             cvWriteInt( fs, "color", point_seq->color );
   4109         }
   4110         else if( CV_IS_SEQ(seq) && CV_IS_SEQ_CHAIN(seq) &&
   4111                  CV_MAT_TYPE(seq->flags) == CV_8UC1 )
   4112         {
   4113             CvChain* chain = (CvChain*)seq;
   4114 
   4115             cvStartWriteStruct( fs, "origin", CV_NODE_MAP + CV_NODE_FLOW );
   4116             cvWriteInt( fs, "x", chain->origin.x );
   4117             cvWriteInt( fs, "y", chain->origin.y );
   4118             cvEndWriteStruct( fs );
   4119         }
   4120         else
   4121         {
   4122             unsigned extra_size = seq->header_size - initial_header_size;
   4123             // a heuristic to provide nice defaults for sequences of int's & float's
   4124             if( extra_size % sizeof(int) == 0 )
   4125                 sprintf( header_dt_buf, "%ui", (unsigned)(extra_size/sizeof(int)) );
   4126             else
   4127                 sprintf( header_dt_buf, "%uu", extra_size );
   4128             header_dt = header_dt_buf;
   4129         }
   4130     }
   4131 
   4132     if( header_dt )
   4133     {
   4134         cvWriteString( fs, "header_dt", header_dt, 0 );
   4135         cvStartWriteStruct( fs, "header_user_data", CV_NODE_SEQ + CV_NODE_FLOW );
   4136         cvWriteRawData( fs, (uchar*)seq + sizeof(CvSeq), 1, header_dt );
   4137         cvEndWriteStruct( fs );
   4138     }
   4139 
   4140     __END__;
   4141 }
   4142 
   4143 
   4144 static char*
   4145 icvGetFormat( const CvSeq* seq, const char* dt_key, CvAttrList* attr,
   4146               int initial_elem_size, char* dt_buf )
   4147 {
   4148     char* dt = 0;
   4149 
   4150     CV_FUNCNAME( "icvWriteFormat" );
   4151 
   4152     __BEGIN__;
   4153 
   4154     dt = (char*)cvAttrValue( attr, dt_key );
   4155 
   4156     if( dt )
   4157     {
   4158         int dt_elem_size;
   4159         CV_CALL( dt_elem_size = icvCalcElemSize( dt, initial_elem_size ));
   4160         if( dt_elem_size != seq->elem_size )
   4161             CV_ERROR( CV_StsUnmatchedSizes,
   4162             "The size of element calculated from \"dt\" and "
   4163             "the elem_size do not match" );
   4164     }
   4165     else if( CV_MAT_TYPE(seq->flags) != 0 || seq->elem_size == 1 )
   4166     {
   4167         int align = CV_MAT_DEPTH(seq->flags) == CV_64F ? sizeof(double) : sizeof(size_t);
   4168         int full_elem_size = cvAlign(CV_ELEM_SIZE(seq->flags) + initial_elem_size, align);
   4169         if( seq->elem_size != full_elem_size )
   4170             CV_ERROR( CV_StsUnmatchedSizes,
   4171             "Size of sequence element (elem_size) is inconsistent with seq->flags" );
   4172         dt = icvEncodeFormat( CV_MAT_TYPE(seq->flags), dt_buf );
   4173     }
   4174     else if( seq->elem_size > initial_elem_size )
   4175     {
   4176         unsigned extra_elem_size = seq->elem_size - initial_elem_size;
   4177         // a heuristic to provide nice defaults for sequences of int's & float's
   4178         if( extra_elem_size % sizeof(int) == 0 )
   4179             sprintf( dt_buf, "%ui", (unsigned)(extra_elem_size/sizeof(int)) );
   4180         else
   4181             sprintf( dt_buf, "%uu", extra_elem_size );
   4182         dt = dt_buf;
   4183     }
   4184 
   4185     __END__;
   4186 
   4187     return dt;
   4188 }
   4189 
   4190 
   4191 static void
   4192 icvWriteSeq( CvFileStorage* fs, const char* name,
   4193              const void* struct_ptr,
   4194              CvAttrList attr, int level )
   4195 {
   4196     CV_FUNCNAME( "icvWriteSeq" );
   4197 
   4198     __BEGIN__;
   4199 
   4200     const CvSeq* seq = (CvSeq*)struct_ptr;
   4201     CvSeqBlock* block;
   4202     char buf[128];
   4203     char dt_buf[128], *dt;
   4204 
   4205     assert( CV_IS_SEQ( seq ));
   4206     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ ));
   4207 
   4208     if( level >= 0 )
   4209         cvWriteInt( fs, "level", level );
   4210 
   4211     sprintf( buf, "%08x", seq->flags );
   4212     cvWriteString( fs, "flags", buf, 1 );
   4213     cvWriteInt( fs, "count", seq->total );
   4214     CV_CALL( dt = icvGetFormat( seq, "dt", &attr, 0, dt_buf ));
   4215     cvWriteString( fs, "dt", dt, 0 );
   4216 
   4217     CV_CALL( icvWriteHeaderData( fs, seq, &attr, sizeof(CvSeq) ));
   4218     cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
   4219 
   4220     for( block = seq->first; block; block = block->next )
   4221     {
   4222         cvWriteRawData( fs, block->data, block->count, dt );
   4223         if( block == seq->first->prev )
   4224             break;
   4225     }
   4226     cvEndWriteStruct( fs );
   4227     cvEndWriteStruct( fs );
   4228 
   4229     __END__;
   4230 }
   4231 
   4232 
   4233 static void
   4234 icvWriteSeqTree( CvFileStorage* fs, const char* name,
   4235                  const void* struct_ptr, CvAttrList attr )
   4236 {
   4237     CV_FUNCNAME( "icvWriteSeqTree" );
   4238 
   4239     __BEGIN__;
   4240 
   4241     const CvSeq* seq = (CvSeq*)struct_ptr;
   4242     const char* recursive_value = cvAttrValue( &attr, "recursive" );
   4243     int is_recursive = recursive_value &&
   4244                        strcmp(recursive_value,"0") != 0 &&
   4245                        strcmp(recursive_value,"false") != 0 &&
   4246                        strcmp(recursive_value,"False") != 0 &&
   4247                        strcmp(recursive_value,"FALSE") != 0;
   4248 
   4249     assert( CV_IS_SEQ( seq ));
   4250 
   4251     if( !is_recursive )
   4252     {
   4253         CV_CALL( icvWriteSeq( fs, name, seq, attr, -1 ));
   4254     }
   4255     else
   4256     {
   4257         CvTreeNodeIterator tree_iterator;
   4258 
   4259         CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ_TREE ));
   4260         CV_CALL( cvStartWriteStruct( fs, "sequences", CV_NODE_SEQ ));
   4261         cvInitTreeNodeIterator( &tree_iterator, seq, INT_MAX );
   4262 
   4263         for(;;)
   4264         {
   4265             if( !tree_iterator.node )
   4266                 break;
   4267             CV_CALL( icvWriteSeq( fs, 0, tree_iterator.node, attr, tree_iterator.level ));
   4268             cvNextTreeNode( &tree_iterator );
   4269         }
   4270 
   4271         cvEndWriteStruct( fs );
   4272         cvEndWriteStruct( fs );
   4273     }
   4274 
   4275     __END__;
   4276 }
   4277 
   4278 
   4279 static void*
   4280 icvReadSeq( CvFileStorage* fs, CvFileNode* node )
   4281 {
   4282     void* ptr = 0;
   4283     CV_FUNCNAME( "icvReadSeq" );
   4284 
   4285     __BEGIN__;
   4286 
   4287     CvSeq* seq;
   4288     CvSeqBlock* block;
   4289     CvFileNode *data, *header_node, *rect_node, *origin_node;
   4290     CvSeqReader reader;
   4291     int total, flags;
   4292     int elem_size, header_size = sizeof(CvSeq);
   4293     int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
   4294     int items_per_elem = 0;
   4295     const char* flags_str;
   4296     const char* header_dt;
   4297     const char* dt;
   4298     char* endptr = 0;
   4299 
   4300     CV_CALL( flags_str = cvReadStringByName( fs, node, "flags", 0 ));
   4301     total = cvReadIntByName( fs, node, "count", -1 );
   4302     dt = cvReadStringByName( fs, node, "dt", 0 );
   4303 
   4304     if( !flags_str || total == -1 || !dt )
   4305         CV_ERROR( CV_StsError, "Some of essential sequence attributes are absent" );
   4306 
   4307     flags = (int)strtol( flags_str, &endptr, 16 );
   4308     if( endptr == flags_str || (flags & CV_MAGIC_MASK) != CV_SEQ_MAGIC_VAL )
   4309         CV_ERROR( CV_StsError, "The sequence flags are invalid" );
   4310 
   4311     header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
   4312     header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
   4313 
   4314     if( (header_dt != 0) ^ (header_node != 0) )
   4315         CV_ERROR( CV_StsError,
   4316         "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
   4317 
   4318     rect_node = cvGetFileNodeByName( fs, node, "rect" );
   4319     origin_node = cvGetFileNodeByName( fs, node, "origin" );
   4320 
   4321     if( (header_node != 0) + (rect_node != 0) + (origin_node != 0) > 1 )
   4322         CV_ERROR( CV_StsError, "Only one of \"header_user_data\", \"rect\" and \"origin\" tags may occur" );
   4323 
   4324     if( header_dt )
   4325     {
   4326         CV_CALL( header_size = icvCalcElemSize( header_dt, header_size ));
   4327     }
   4328     else if( rect_node )
   4329         header_size = sizeof(CvPoint2DSeq);
   4330     else if( origin_node )
   4331         header_size = sizeof(CvChain);
   4332 
   4333     CV_CALL( elem_size = icvCalcElemSize( dt, 0 ));
   4334     CV_CALL( seq = cvCreateSeq( flags, header_size, elem_size, fs->dststorage ));
   4335 
   4336     if( header_node )
   4337     {
   4338         CV_CALL( cvReadRawData( fs, header_node, (char*)seq + sizeof(CvSeq), header_dt ));
   4339     }
   4340     else if( rect_node )
   4341     {
   4342         CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
   4343         point_seq->rect.x = cvReadIntByName( fs, rect_node, "x", 0 );
   4344         point_seq->rect.y = cvReadIntByName( fs, rect_node, "y", 0 );
   4345         point_seq->rect.width = cvReadIntByName( fs, rect_node, "width", 0 );
   4346         point_seq->rect.height = cvReadIntByName( fs, rect_node, "height", 0 );
   4347         point_seq->color = cvReadIntByName( fs, node, "color", 0 );
   4348     }
   4349     else if( origin_node )
   4350     {
   4351         CvChain* chain = (CvChain*)seq;
   4352         chain->origin.x = cvReadIntByName( fs, origin_node, "x", 0 );
   4353         chain->origin.y = cvReadIntByName( fs, origin_node, "y", 0 );
   4354     }
   4355 
   4356     cvSeqPushMulti( seq, 0, total, 0 );
   4357     CV_CALL( fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   4358     fmt_pair_count *= 2;
   4359     for( i = 0; i < fmt_pair_count; i += 2 )
   4360         items_per_elem += fmt_pairs[i];
   4361 
   4362     data = cvGetFileNodeByName( fs, node, "data" );
   4363     if( !data )
   4364         CV_ERROR( CV_StsError, "The image data is not found in file storage" );
   4365 
   4366     if( icvFileNodeSeqLen( data ) != total*items_per_elem )
   4367         CV_ERROR( CV_StsError, "The number of stored elements does not match to \"count\"" );
   4368 
   4369     cvStartReadRawData( fs, data, &reader );
   4370     for( block = seq->first; block; block = block->next )
   4371     {
   4372         int delta = block->count*items_per_elem;
   4373         cvReadRawDataSlice( fs, &reader, delta, block->data, dt );
   4374         if( block == seq->first->prev )
   4375             break;
   4376     }
   4377 
   4378     ptr = seq;
   4379 
   4380     __END__;
   4381 
   4382     return ptr;
   4383 }
   4384 
   4385 
   4386 static void*
   4387 icvReadSeqTree( CvFileStorage* fs, CvFileNode* node )
   4388 {
   4389     void* ptr = 0;
   4390     CV_FUNCNAME( "icvReadSeqTree" );
   4391 
   4392     __BEGIN__;
   4393 
   4394     CvFileNode *sequences_node = cvGetFileNodeByName( fs, node, "sequences" );
   4395     CvSeq* sequences;
   4396     CvSeq* root = 0;
   4397     CvSeq* parent = 0;
   4398     CvSeq* prev_seq = 0;
   4399     CvSeqReader reader;
   4400     int i, total;
   4401     int prev_level = 0;
   4402 
   4403     if( !sequences_node || !CV_NODE_IS_SEQ(sequences_node->tag) )
   4404         CV_ERROR( CV_StsParseError,
   4405         "opencv-sequence-tree instance should contain a field \"sequences\" that should be a sequence" );
   4406 
   4407     sequences = sequences_node->data.seq;
   4408     total = sequences->total;
   4409 
   4410     cvStartReadSeq( sequences, &reader, 0 );
   4411     for( i = 0; i < total; i++ )
   4412     {
   4413         CvFileNode* elem = (CvFileNode*)reader.ptr;
   4414         CvSeq* seq;
   4415         int level;
   4416         CV_CALL( seq = (CvSeq*)cvRead( fs, elem ));
   4417         CV_CALL( level = cvReadIntByName( fs, elem, "level", -1 ));
   4418         if( level < 0 )
   4419             CV_ERROR( CV_StsParseError, "All the sequence tree nodes should contain \"level\" field" );
   4420         if( !root )
   4421             root = seq;
   4422         if( level > prev_level )
   4423         {
   4424             assert( level == prev_level + 1 );
   4425             parent = prev_seq;
   4426             prev_seq = 0;
   4427             if( parent )
   4428                 parent->v_next = seq;
   4429         }
   4430         else if( level < prev_level )
   4431         {
   4432             for( ; prev_level > level; prev_level-- )
   4433                 prev_seq = prev_seq->v_prev;
   4434             parent = prev_seq->v_prev;
   4435         }
   4436         seq->h_prev = prev_seq;
   4437         if( prev_seq )
   4438             prev_seq->h_next = seq;
   4439         seq->v_prev = parent;
   4440         prev_seq = seq;
   4441         prev_level = level;
   4442         CV_NEXT_SEQ_ELEM( sequences->elem_size, reader );
   4443     }
   4444 
   4445     ptr = root;
   4446 
   4447     __END__;
   4448 
   4449     return ptr;
   4450 }
   4451 
   4452 /******************************* CvGraph ******************************/
   4453 
   4454 static int
   4455 icvIsGraph( const void* ptr )
   4456 {
   4457     return CV_IS_GRAPH(ptr);
   4458 }
   4459 
   4460 
   4461 static void
   4462 icvReleaseGraph( void** ptr )
   4463 {
   4464     CV_FUNCNAME( "icvReleaseGraph" );
   4465 
   4466     __BEGIN__;
   4467 
   4468     if( !ptr )
   4469         CV_ERROR( CV_StsNullPtr, "NULL double pointer" );
   4470 
   4471     *ptr = 0; // it's impossible now to release graph, so just clear the pointer
   4472 
   4473     __END__;
   4474 }
   4475 
   4476 
   4477 static void*
   4478 icvCloneGraph( const void* ptr )
   4479 {
   4480     return cvCloneGraph( (const CvGraph*)ptr, 0 );
   4481 }
   4482 
   4483 
   4484 static void
   4485 icvWriteGraph( CvFileStorage* fs, const char* name,
   4486                const void* struct_ptr, CvAttrList attr )
   4487 {
   4488     int* flag_buf = 0;
   4489     char* write_buf = 0;
   4490     CV_FUNCNAME( "icvWriteGraph" );
   4491 
   4492     __BEGIN__;
   4493 
   4494     const CvGraph* graph = (const CvGraph*)struct_ptr;
   4495     CvSeqReader reader;
   4496     char buf[128];
   4497     int i, k, vtx_count, edge_count;
   4498     char vtx_dt_buf[128], *vtx_dt;
   4499     char edge_dt_buf[128], *edge_dt;
   4500     int write_buf_size;
   4501 
   4502     assert( CV_IS_GRAPH(graph) );
   4503     vtx_count = cvGraphGetVtxCount( graph );
   4504     edge_count = cvGraphGetEdgeCount( graph );
   4505     CV_CALL( flag_buf = (int*)cvAlloc( vtx_count*sizeof(flag_buf[0])));
   4506 
   4507     // count vertices
   4508     cvStartReadSeq( (CvSeq*)graph, &reader );
   4509     for( i = 0, k = 0; i < graph->total; i++ )
   4510     {
   4511         if( CV_IS_SET_ELEM( reader.ptr ))
   4512         {
   4513             CvGraphVtx* vtx = (CvGraphVtx*)reader.ptr;
   4514             flag_buf[k] = vtx->flags;
   4515             vtx->flags = k++;
   4516         }
   4517         CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
   4518     }
   4519 
   4520     // write header
   4521     CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH ));
   4522 
   4523     sprintf( buf, "%08x", graph->flags );
   4524     cvWriteString( fs, "flags", buf, 1 );
   4525 
   4526     cvWriteInt( fs, "vertex_count", vtx_count );
   4527     CV_CALL( vtx_dt = icvGetFormat( (CvSeq*)graph, "vertex_dt",
   4528                     &attr, sizeof(CvGraphVtx), vtx_dt_buf ));
   4529     if( vtx_dt )
   4530         cvWriteString( fs, "vertex_dt", vtx_dt, 0 );
   4531 
   4532     cvWriteInt( fs, "edge_count", edge_count );
   4533     CV_CALL( edge_dt = icvGetFormat( (CvSeq*)graph->edges, "edge_dt",
   4534                                 &attr, sizeof(CvGraphEdge), buf ));
   4535     sprintf( edge_dt_buf, "2if%s", edge_dt ? edge_dt : "" );
   4536     edge_dt = edge_dt_buf;
   4537     cvWriteString( fs, "edge_dt", edge_dt, 0 );
   4538 
   4539     CV_CALL( icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) ));
   4540 
   4541     write_buf_size = MAX( 3*graph->elem_size, 1 << 16 );
   4542     write_buf_size = MAX( 3*graph->edges->elem_size, write_buf_size );
   4543     CV_CALL( write_buf = (char*)cvAlloc( write_buf_size ));
   4544 
   4545     // as vertices and edges are written in similar way,
   4546     // do it as a parametrized 2-iteration loop
   4547     for( k = 0; k < 2; k++ )
   4548     {
   4549         const char* dt = k == 0 ? vtx_dt : edge_dt;
   4550         if( dt )
   4551         {
   4552             CvSet* data = k == 0 ? (CvSet*)graph : graph->edges;
   4553             int elem_size = data->elem_size;
   4554             int write_elem_size = icvCalcElemSize( dt, 0 );
   4555             char* src_ptr = write_buf;
   4556             int write_max = write_buf_size / write_elem_size, write_count = 0;
   4557 
   4558             // alignment of user part of the edge data following 2if
   4559             int edge_user_align = sizeof(float);
   4560 
   4561             if( k == 1 )
   4562             {
   4563                 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
   4564                 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
   4565                 if( fmt_pair_count > 2 || CV_ELEM_SIZE(fmt_pairs[2*2+1]) >= (int)sizeof(double))
   4566                     edge_user_align = sizeof(double);
   4567             }
   4568 
   4569             cvStartWriteStruct( fs, k == 0 ? "vertices" : "edges",
   4570                                 CV_NODE_SEQ + CV_NODE_FLOW );
   4571             cvStartReadSeq( (CvSeq*)data, &reader );
   4572             for( i = 0; i < data->total; i++ )
   4573             {
   4574                 if( CV_IS_SET_ELEM( reader.ptr ))
   4575                 {
   4576                     if( k == 0 ) // vertices
   4577                         memcpy( src_ptr, reader.ptr + sizeof(CvGraphVtx), write_elem_size );
   4578                     else
   4579                     {
   4580                         CvGraphEdge* edge = (CvGraphEdge*)reader.ptr;
   4581                         src_ptr = (char*)cvAlignPtr( src_ptr, sizeof(int) );
   4582                         ((int*)src_ptr)[0] = edge->vtx[0]->flags;
   4583                         ((int*)src_ptr)[1] = edge->vtx[1]->flags;
   4584                         *(float*)(src_ptr + sizeof(int)*2) = edge->weight;
   4585                         if( elem_size > (int)sizeof(CvGraphEdge) )
   4586                         {
   4587                             char* src_ptr2 = (char*)cvAlignPtr( src_ptr + 2*sizeof(int)
   4588                                                 + sizeof(float), edge_user_align );
   4589                             memcpy( src_ptr2, edge + 1, elem_size - sizeof(CvGraphEdge) );
   4590                         }
   4591                     }
   4592                     src_ptr += write_elem_size;
   4593                     if( ++write_count >= write_max )
   4594                     {
   4595                         cvWriteRawData( fs, write_buf, write_count, dt );
   4596                         write_count = 0;
   4597                         src_ptr = write_buf;
   4598                     }
   4599                 }
   4600                 CV_NEXT_SEQ_ELEM( data->elem_size, reader );
   4601             }
   4602 
   4603             if( write_count > 0 )
   4604                 cvWriteRawData( fs, write_buf, write_count, dt );
   4605             cvEndWriteStruct( fs );
   4606         }
   4607     }
   4608 
   4609     cvEndWriteStruct( fs );
   4610 
   4611     // final stage. restore the graph flags
   4612     cvStartReadSeq( (CvSeq*)graph, &reader );
   4613     vtx_count = 0;
   4614     for( i = 0; i < graph->total; i++ )
   4615     {
   4616         if( CV_IS_SET_ELEM( reader.ptr ))
   4617             ((CvGraphVtx*)reader.ptr)->flags = flag_buf[vtx_count++];
   4618         CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
   4619     }
   4620 
   4621     __END__;
   4622 
   4623     cvFree( &write_buf );
   4624     cvFree( &flag_buf );
   4625 }
   4626 
   4627 
   4628 static void*
   4629 icvReadGraph( CvFileStorage* fs, CvFileNode* node )
   4630 {
   4631     void* ptr = 0;
   4632     char* read_buf = 0;
   4633     CvGraphVtx** vtx_buf = 0;
   4634     CV_FUNCNAME( "icvReadGraph" );
   4635 
   4636     __BEGIN__;
   4637 
   4638     CvGraph* graph;
   4639     CvFileNode *header_node, *vtx_node, *edge_node;
   4640     int flags, vtx_count, edge_count;
   4641     int vtx_size = sizeof(CvGraphVtx), edge_size, header_size = sizeof(CvGraph);
   4642     int src_vtx_size = 0, src_edge_size;
   4643     int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
   4644     int vtx_items_per_elem = 0, edge_items_per_elem = 0;
   4645     int edge_user_align = sizeof(float);
   4646     int read_buf_size;
   4647     int i, k;
   4648     const char* flags_str;
   4649     const char* header_dt;
   4650     const char* vtx_dt;
   4651     const char* edge_dt;
   4652     char* endptr = 0;
   4653 
   4654     CV_CALL( flags_str = cvReadStringByName( fs, node, "flags", 0 ));
   4655     vtx_dt = cvReadStringByName( fs, node, "vertex_dt", 0 );
   4656     edge_dt = cvReadStringByName( fs, node, "edge_dt", 0 );
   4657     vtx_count = cvReadIntByName( fs, node, "vertex_count", -1 );
   4658     edge_count = cvReadIntByName( fs, node, "edge_count", -1 );
   4659 
   4660     if( !flags_str || vtx_count == -1 || edge_count == -1 || !edge_dt )
   4661         CV_ERROR( CV_StsError, "Some of essential sequence attributes are absent" );
   4662 
   4663     flags = (int)strtol( flags_str, &endptr, 16 );
   4664     if( endptr == flags_str ||
   4665         (flags & (CV_SEQ_KIND_MASK|CV_MAGIC_MASK)) != (CV_GRAPH|CV_SET_MAGIC_VAL))
   4666         CV_ERROR( CV_StsError, "Invalid graph signature" );
   4667 
   4668     header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
   4669     header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
   4670 
   4671     if( (header_dt != 0) ^ (header_node != 0) )
   4672         CV_ERROR( CV_StsError,
   4673         "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
   4674 
   4675     if( header_dt )
   4676         CV_CALL( header_size = icvCalcElemSize( header_dt, header_size ));
   4677 
   4678     if( vtx_dt )
   4679     {
   4680         CV_CALL( src_vtx_size = icvCalcElemSize( vtx_dt, 0 ));
   4681         CV_CALL( vtx_size = icvCalcElemSize( vtx_dt, vtx_size ));
   4682         CV_CALL( fmt_pair_count = icvDecodeFormat( edge_dt,
   4683                             fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   4684         fmt_pair_count *= 2;
   4685         for( i = 0; i < fmt_pair_count; i += 2 )
   4686             vtx_items_per_elem += fmt_pairs[i];
   4687     }
   4688 
   4689     {
   4690         char dst_edge_dt_buf[128];
   4691         const char* dst_edge_dt = 0;
   4692 
   4693         CV_CALL( fmt_pair_count = icvDecodeFormat( edge_dt,
   4694                             fmt_pairs, CV_FS_MAX_FMT_PAIRS ));
   4695         if( fmt_pair_count < 2 ||
   4696             fmt_pairs[0] != 2 || fmt_pairs[1] != CV_32S ||
   4697             fmt_pairs[2] < 1 || fmt_pairs[3] != CV_32F )
   4698             CV_ERROR( CV_StsBadArg,
   4699             "Graph edges should start with 2 integers and a float" );
   4700 
   4701         // alignment of user part of the edge data following 2if
   4702         if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[5]) >= (int)sizeof(double))
   4703             edge_user_align = sizeof(double);
   4704 
   4705         fmt_pair_count *= 2;
   4706         for( i = 0; i < fmt_pair_count; i += 2 )
   4707             edge_items_per_elem += fmt_pairs[i];
   4708 
   4709         if( edge_dt[2] == 'f' || (edge_dt[2] == '1' && edge_dt[3] == 'f') )
   4710             dst_edge_dt = edge_dt + 3 + isdigit(edge_dt[2]);
   4711         else
   4712         {
   4713             int val = (int)strtol( edge_dt + 2, &endptr, 10 );
   4714             sprintf( dst_edge_dt_buf, "%df%s", val-1, endptr );
   4715             dst_edge_dt = dst_edge_dt_buf;
   4716         }
   4717 
   4718         CV_CALL( edge_size = icvCalcElemSize( dst_edge_dt, sizeof(CvGraphEdge) ));
   4719         CV_CALL( src_edge_size = icvCalcElemSize( edge_dt, 0 ));
   4720     }
   4721 
   4722     CV_CALL( graph = cvCreateGraph( flags, header_size, vtx_size, edge_size, fs->dststorage ));
   4723 
   4724     if( header_node )
   4725         CV_CALL( cvReadRawData( fs, header_node, (char*)graph + sizeof(CvGraph), header_dt ));
   4726 
   4727     read_buf_size = MAX( src_vtx_size*3, 1 << 16 );
   4728     read_buf_size = MAX( src_edge_size*3, read_buf_size );
   4729     CV_CALL( read_buf = (char*)cvAlloc( read_buf_size ));
   4730     CV_CALL( vtx_buf = (CvGraphVtx**)cvAlloc( vtx_count * sizeof(vtx_buf[0]) ));
   4731 
   4732     vtx_node = cvGetFileNodeByName( fs, node, "vertices" );
   4733     edge_node = cvGetFileNodeByName( fs, node, "edges" );
   4734     if( !edge_node )
   4735         CV_ERROR( CV_StsBadArg, "No edges data" );
   4736     if( vtx_dt && !vtx_node )
   4737         CV_ERROR( CV_StsBadArg, "No vertices data" );
   4738 
   4739     // as vertices and edges are read in similar way,
   4740     // do it as a parametrized 2-iteration loop
   4741     for( k = 0; k < 2; k++ )
   4742     {
   4743         const char* dt = k == 0 ? vtx_dt : edge_dt;
   4744         int elem_size = k == 0 ? vtx_size : edge_size;
   4745         int src_elem_size = k == 0 ? src_vtx_size : src_edge_size;
   4746         int items_per_elem = k == 0 ? vtx_items_per_elem : edge_items_per_elem;
   4747         int elem_count = k == 0 ? vtx_count : edge_count;
   4748         char* dst_ptr = read_buf;
   4749         int read_max = read_buf_size /MAX(src_elem_size, 1), read_count = 0;
   4750         CvSeqReader reader;
   4751         cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
   4752 
   4753         for( i = 0; i < elem_count; i++ )
   4754         {
   4755             if( read_count == 0 && dt )
   4756             {
   4757                 int count = MIN( elem_count - i, read_max )*items_per_elem;
   4758                 cvReadRawDataSlice( fs, &reader, count, read_buf, dt );
   4759                 read_count = count;
   4760                 dst_ptr = read_buf;
   4761             }
   4762 
   4763             if( k == 0 )
   4764             {
   4765                 CvGraphVtx* vtx;
   4766                 cvGraphAddVtx( graph, 0, &vtx );
   4767                 vtx_buf[i] = vtx;
   4768                 if( dt )
   4769                     memcpy( vtx + 1, dst_ptr, src_elem_size );
   4770             }
   4771             else
   4772             {
   4773                 CvGraphEdge* edge = 0;
   4774                 int vtx1 = ((int*)dst_ptr)[0];
   4775                 int vtx2 = ((int*)dst_ptr)[1];
   4776                 int result;
   4777 
   4778                 if( (unsigned)vtx1 >= (unsigned)vtx_count ||
   4779                     (unsigned)vtx2 >= (unsigned)vtx_count )
   4780                     CV_ERROR( CV_StsOutOfRange,
   4781                     "Some of stored vertex indices are out of range" );
   4782 
   4783                 CV_CALL( result = cvGraphAddEdgeByPtr( graph,
   4784                     vtx_buf[vtx1], vtx_buf[vtx2], 0, &edge ));
   4785 
   4786                 if( result == 0 )
   4787                     CV_ERROR( CV_StsBadArg, "Duplicated edge has occured" );
   4788 
   4789                 edge->weight = *(float*)(dst_ptr + sizeof(int)*2);
   4790                 if( elem_size > (int)sizeof(CvGraphEdge) )
   4791                 {
   4792                     char* dst_ptr2 = (char*)cvAlignPtr( dst_ptr + sizeof(int)*2 +
   4793                                                 sizeof(float), edge_user_align );
   4794                     memcpy( edge + 1, dst_ptr2, elem_size - sizeof(CvGraphEdge) );
   4795                 }
   4796             }
   4797 
   4798             dst_ptr += src_elem_size;
   4799             read_count--;
   4800         }
   4801     }
   4802 
   4803     ptr = graph;
   4804 
   4805     __END__;
   4806 
   4807     cvFree( &read_buf );
   4808     cvFree( &vtx_buf );
   4809 
   4810     return ptr;
   4811 }
   4812 
   4813 /****************************************************************************************\
   4814 *                                    RTTI Functions                                      *
   4815 \****************************************************************************************/
   4816 
   4817 CvTypeInfo *CvType::first = 0, *CvType::last = 0;
   4818 
   4819 CvType::CvType( const char* type_name,
   4820                 CvIsInstanceFunc is_instance, CvReleaseFunc release,
   4821                 CvReadFunc read, CvWriteFunc write, CvCloneFunc clone )
   4822 {
   4823     CvTypeInfo _info;
   4824     _info.flags = 0;
   4825     _info.header_size = sizeof(_info);
   4826     _info.type_name = type_name;
   4827     _info.prev = _info.next = 0;
   4828     _info.is_instance = is_instance;
   4829     _info.release = release;
   4830     _info.clone = clone;
   4831     _info.read = read;
   4832     _info.write = write;
   4833 
   4834     cvRegisterType( &_info );
   4835     info = first;
   4836 }
   4837 
   4838 
   4839 CvType::~CvType()
   4840 {
   4841     cvUnregisterType( info->type_name );
   4842 }
   4843 
   4844 
   4845 CvType seq_type( CV_TYPE_NAME_SEQ, icvIsSeq, icvReleaseSeq, icvReadSeq,
   4846                  icvWriteSeqTree /* this is the entry point for
   4847                  writing a single sequence too */, icvCloneSeq );
   4848 
   4849 CvType seq_tree_type( CV_TYPE_NAME_SEQ_TREE, icvIsSeq, icvReleaseSeq,
   4850                       icvReadSeqTree, icvWriteSeqTree, icvCloneSeq );
   4851 
   4852 CvType seq_graph_type( CV_TYPE_NAME_GRAPH, icvIsGraph, icvReleaseGraph,
   4853                        icvReadGraph, icvWriteGraph, icvCloneGraph );
   4854 
   4855 CvType sparse_mat_type( CV_TYPE_NAME_SPARSE_MAT, icvIsSparseMat,
   4856                         (CvReleaseFunc)cvReleaseSparseMat, icvReadSparseMat,
   4857                         icvWriteSparseMat, (CvCloneFunc)cvCloneSparseMat );
   4858 
   4859 CvType image_type( CV_TYPE_NAME_IMAGE, icvIsImage, (CvReleaseFunc)cvReleaseImage,
   4860                    icvReadImage, icvWriteImage, (CvCloneFunc)cvCloneImage );
   4861 
   4862 CvType mat_type( CV_TYPE_NAME_MAT, icvIsMat, (CvReleaseFunc)cvReleaseMat,
   4863                  icvReadMat, icvWriteMat, (CvCloneFunc)cvCloneMat );
   4864 
   4865 CvType matnd_type( CV_TYPE_NAME_MATND, icvIsMatND, (CvReleaseFunc)cvReleaseMatND,
   4866                    icvReadMatND, icvWriteMatND, (CvCloneFunc)cvCloneMatND );
   4867 
   4868 CV_IMPL  void
   4869 cvRegisterType( const CvTypeInfo* _info )
   4870 {
   4871     CV_FUNCNAME("cvRegisterType" );
   4872 
   4873     __BEGIN__;
   4874 
   4875     CvTypeInfo* info = 0;
   4876     int i, len;
   4877     char c;
   4878 
   4879     //if( !CvType::first )
   4880     //    icvCreateStandardTypes();
   4881 
   4882     if( !_info || _info->header_size != sizeof(CvTypeInfo) )
   4883         CV_ERROR( CV_StsBadSize, "Invalid type info" );
   4884 
   4885     if( !_info->is_instance || !_info->release ||
   4886         !_info->read || !_info->write )
   4887         CV_ERROR( CV_StsNullPtr,
   4888         "Some of required function pointers "
   4889         "(is_instance, release, read or write) are NULL");
   4890 
   4891     c = _info->type_name[0];
   4892     if( !isalpha(c) && c != '_' )
   4893         CV_ERROR( CV_StsBadArg, "Type name should start with a letter or _" );
   4894 
   4895     len = (int)strlen(_info->type_name);
   4896 
   4897     for( i = 0; i < len; i++ )
   4898     {
   4899         c = _info->type_name[i];
   4900         if( !isalnum(c) && c != '-' && c != '_' )
   4901             CV_ERROR( CV_StsBadArg,
   4902             "Type name should contain only letters, digits, - and _" );
   4903     }
   4904 
   4905     CV_CALL( info = (CvTypeInfo*)cvAlloc( sizeof(*info) + len + 1 ));
   4906 
   4907     *info = *_info;
   4908     info->type_name = (char*)(info + 1);
   4909     memcpy( (char*)info->type_name, _info->type_name, len + 1 );
   4910 
   4911     info->flags = 0;
   4912     info->next = CvType::first;
   4913     info->prev = 0;
   4914     if( CvType::first )
   4915         CvType::first->prev = info;
   4916     else
   4917         CvType::last = info;
   4918     CvType::first = info;
   4919 
   4920     __END__;
   4921 }
   4922 
   4923 
   4924 CV_IMPL void
   4925 cvUnregisterType( const char* type_name )
   4926 {
   4927     CV_FUNCNAME("cvUnregisterType" );
   4928 
   4929     __BEGIN__;
   4930 
   4931     CvTypeInfo* info;
   4932 
   4933     CV_CALL( info = cvFindType( type_name ));
   4934     if( info )
   4935     {
   4936         if( info->prev )
   4937             info->prev->next = info->next;
   4938         else
   4939             CvType::first = info->next;
   4940 
   4941         if( info->next )
   4942             info->next->prev = info->prev;
   4943         else
   4944             CvType::last = info->prev;
   4945 
   4946         if( !CvType::first || !CvType::last )
   4947             CvType::first = CvType::last = 0;
   4948 
   4949         cvFree( &info );
   4950     }
   4951 
   4952     __END__;
   4953 }
   4954 
   4955 
   4956 CV_IMPL CvTypeInfo*
   4957 cvFirstType( void )
   4958 {
   4959     return CvType::first;
   4960 }
   4961 
   4962 
   4963 CV_IMPL CvTypeInfo*
   4964 cvFindType( const char* type_name )
   4965 {
   4966     CvTypeInfo* info = 0;
   4967 
   4968     for( info = CvType::first; info != 0; info = info->next )
   4969         if( strcmp( info->type_name, type_name ) == 0 )
   4970             break;
   4971 
   4972     return info;
   4973 }
   4974 
   4975 
   4976 CV_IMPL CvTypeInfo*
   4977 cvTypeOf( const void* struct_ptr )
   4978 {
   4979     CvTypeInfo* info = 0;
   4980 
   4981     for( info = CvType::first; info != 0; info = info->next )
   4982         if( info->is_instance( struct_ptr ))
   4983             break;
   4984 
   4985     return info;
   4986 }
   4987 
   4988 
   4989 /* universal functions */
   4990 CV_IMPL void
   4991 cvRelease( void** struct_ptr )
   4992 {
   4993     CV_FUNCNAME("cvRelease" );
   4994 
   4995     __BEGIN__;
   4996 
   4997     CvTypeInfo* info;
   4998 
   4999     if( !struct_ptr )
   5000         CV_ERROR( CV_StsNullPtr, "NULL double pointer" );
   5001 
   5002     if( *struct_ptr )
   5003     {
   5004         CV_CALL( info = cvTypeOf( *struct_ptr ));
   5005         if( !info )
   5006             CV_ERROR( CV_StsError, "Unknown object type" );
   5007         if( !info->release )
   5008             CV_ERROR( CV_StsError, "release function pointer is NULL" );
   5009 
   5010         CV_CALL( info->release( struct_ptr ));
   5011         *struct_ptr = 0;
   5012     }
   5013 
   5014     __END__;
   5015 }
   5016 
   5017 
   5018 void* cvClone( const void* struct_ptr )
   5019 {
   5020     void* struct_copy = 0;
   5021 
   5022     CV_FUNCNAME("cvClone" );
   5023 
   5024     __BEGIN__;
   5025 
   5026     CvTypeInfo* info;
   5027 
   5028     if( !struct_ptr )
   5029         CV_ERROR( CV_StsNullPtr, "NULL structure pointer" );
   5030 
   5031     CV_CALL( info = cvTypeOf( struct_ptr ));
   5032     if( !info )
   5033         CV_ERROR( CV_StsError, "Unknown object type" );
   5034     if( !info->clone )
   5035         CV_ERROR( CV_StsError, "clone function pointer is NULL" );
   5036 
   5037     CV_CALL( struct_copy = info->clone( struct_ptr ));
   5038 
   5039     __END__;
   5040 
   5041     return struct_copy;
   5042 }
   5043 
   5044 
   5045 /* reads matrix, image, sequence, graph etc. */
   5046 CV_IMPL void*
   5047 cvRead( CvFileStorage* fs, CvFileNode* node, CvAttrList* list )
   5048 {
   5049     void* obj = 0;
   5050 
   5051     CV_FUNCNAME( "cvRead" );
   5052 
   5053     __BEGIN__;
   5054 
   5055     CV_CHECK_FILE_STORAGE( fs );
   5056 
   5057     if( !node )
   5058         EXIT;
   5059 
   5060     if( !CV_NODE_IS_USER(node->tag) || !node->info )
   5061         CV_ERROR( CV_StsError, "The node does not represent a user object (unknown type?)" );
   5062 
   5063     CV_CALL( obj = node->info->read( fs, node ));
   5064 
   5065     __END__;
   5066 
   5067     if( list )
   5068         *list = cvAttrList(0,0);
   5069 
   5070     return obj;
   5071 }
   5072 
   5073 
   5074 /* writes matrix, image, sequence, graph etc. */
   5075 CV_IMPL void
   5076 cvWrite( CvFileStorage* fs, const char* name,
   5077          const void* ptr, CvAttrList attributes )
   5078 {
   5079     CV_FUNCNAME( "cvWrite" );
   5080 
   5081     __BEGIN__;
   5082 
   5083     CvTypeInfo* info;
   5084 
   5085     CV_CHECK_OUTPUT_FILE_STORAGE( fs );
   5086 
   5087     if( !ptr )
   5088         CV_ERROR( CV_StsNullPtr, "Null pointer to the written object" );
   5089 
   5090     CV_CALL( info = cvTypeOf( ptr ));
   5091     if( !info )
   5092         CV_ERROR( CV_StsBadArg, "Unknown object" );
   5093 
   5094     if( !info->write )
   5095         CV_ERROR( CV_StsBadArg, "The object does not have write function" );
   5096 
   5097     CV_CALL( info->write( fs, name, ptr, attributes ));
   5098 
   5099     __END__;
   5100 }
   5101 
   5102 
   5103 /* simple API for reading/writing data */
   5104 CV_IMPL void
   5105 cvSave( const char* filename, const void* struct_ptr,
   5106         const char* _name, const char* comment, CvAttrList attributes )
   5107 {
   5108     CvFileStorage* fs = 0;
   5109 
   5110     CV_FUNCNAME( "cvSave" );
   5111 
   5112     __BEGIN__;
   5113 
   5114     char name_buf[CV_FS_MAX_LEN + 256];
   5115     char* name = (char*)_name;
   5116 
   5117     if( !struct_ptr )
   5118         CV_ERROR( CV_StsNullPtr, "NULL object pointer" );
   5119 
   5120     CV_CALL( fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE ));
   5121     if( !fs )
   5122         CV_ERROR( CV_StsError, "Could not open the file storage. Check the path and permissions" );
   5123 
   5124     if( !name )
   5125     {
   5126         static const char* stubname = "unnamed";
   5127         const char* ptr2 = filename + strlen( filename );
   5128         const char* ptr = ptr2 - 1;
   5129 
   5130         while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' )
   5131         {
   5132             if( *ptr == '.' && !*ptr2 )
   5133                 ptr2 = ptr;
   5134             ptr--;
   5135         }
   5136         ptr++;
   5137         if( ptr == ptr2 )
   5138             CV_ERROR( CV_StsBadArg, "Invalid filename" );
   5139 
   5140         name=name_buf;
   5141 
   5142         // name must start with letter or '_'
   5143         if( !isalpha(*ptr) && *ptr!= '_' ){
   5144             *name++ = '_';
   5145         }
   5146 
   5147         while( ptr < ptr2 )
   5148         {
   5149             char c = *ptr++;
   5150             if( !isalnum(c) && c != '-' && c != '_' )
   5151                 c = '_';
   5152             *name++ = c;
   5153         }
   5154         *name = '\0';
   5155         name = name_buf;
   5156         if( strcmp( name, "_" ) == 0 )
   5157             strcpy( name, stubname );
   5158     }
   5159 
   5160     if( comment )
   5161         CV_CALL( cvWriteComment( fs, comment, 0 ));
   5162     CV_CALL( cvWrite( fs, name, struct_ptr, attributes ));
   5163 
   5164     __END__;
   5165 
   5166     cvReleaseFileStorage( &fs );
   5167 }
   5168 
   5169 
   5170 CV_IMPL void*
   5171 cvLoad( const char* filename, CvMemStorage* memstorage,
   5172         const char* name, const char** _real_name )
   5173 {
   5174     void* ptr = 0;
   5175     const char* real_name = 0;
   5176     CvFileStorage* fs = 0;
   5177 
   5178     CV_FUNCNAME( "cvLoad" );
   5179 
   5180     __BEGIN__;
   5181 
   5182     CvFileNode* node = 0;
   5183     CV_CALL( fs = cvOpenFileStorage( filename, memstorage, CV_STORAGE_READ ));
   5184 
   5185     if( !fs )
   5186         EXIT;
   5187 
   5188     if( name )
   5189     {
   5190         CV_CALL( node = cvGetFileNodeByName( fs, 0, name ));
   5191     }
   5192     else
   5193     {
   5194         int i, k;
   5195         for( k = 0; k < fs->roots->total; k++ )
   5196         {
   5197             CvSeq* seq;
   5198             CvSeqReader reader;
   5199 
   5200             node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
   5201             if( !CV_NODE_IS_MAP( node->tag ))
   5202                 EXIT;
   5203             seq = node->data.seq;
   5204             node = 0;
   5205 
   5206             cvStartReadSeq( seq, &reader, 0 );
   5207 
   5208             // find the first element in the map
   5209             for( i = 0; i < seq->total; i++ )
   5210             {
   5211                 if( CV_IS_SET_ELEM( reader.ptr ))
   5212                 {
   5213                     node = (CvFileNode*)reader.ptr;
   5214                     goto stop_search;
   5215                 }
   5216                 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
   5217             }
   5218         }
   5219 
   5220 stop_search:
   5221         ;
   5222     }
   5223 
   5224     if( !node )
   5225         CV_ERROR( CV_StsObjectNotFound, "Could not find the/an object in file storage" );
   5226 
   5227     real_name = cvGetFileNodeName( node );
   5228     CV_CALL( ptr = cvRead( fs, node, 0 ));
   5229 
   5230     // sanity check
   5231     if( !memstorage && (CV_IS_SEQ( ptr ) || CV_IS_SET( ptr )) )
   5232         CV_ERROR( CV_StsNullPtr,
   5233         "NULL memory storage is passed - the loaded dynamic structure can not be stored" );
   5234 
   5235     __END__;
   5236 
   5237     cvReleaseFileStorage( &fs );
   5238     if( cvGetErrStatus() < 0 )
   5239     {
   5240         cvRelease( (void**)&ptr );
   5241         real_name = 0;
   5242     }
   5243 
   5244     if( _real_name )
   5245         *_real_name = real_name;
   5246 
   5247     return ptr;
   5248 }
   5249 
   5250 /* End of file. */
   5251