1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
48 /****************************************************************************************\
49 * Common macros and type definitions *
50 \****************************************************************************************/
52 #define cv_isprint(c) ((signed char)(c) >= (signed char)' ')
53 #define cv_isprint_or_tab(c) ((signed char)(c) >= (signed char)' ' || (c) == '\t')
55 static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
58 char* ptr=buffer + 23 /* enough even for 64-bit integers */;
59 unsigned val = abs(_val);
64 unsigned r = val / radix;
65 *--ptr = (char)(val - (r*radix) + '0');
76 cv::string cv::FileStorage::getDefaultObjectName(const string& _filename)
78 static const char* stubname = "unnamed";
79 const char* filename = _filename.c_str();
80 const char* ptr2 = filename + _filename.size();
81 const char* ptr = ptr2 - 1;
82 cv::AutoBuffer<char> name_buf(_filename.size()+1);
84 while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' )
86 if( *ptr == '.' && (!*ptr2 || strncmp(ptr2, ".gz", 3) == 0) )
92 CV_Error( CV_StsBadArg, "Invalid filename" );
94 char* name = name_buf;
96 // name must start with letter or '_'
97 if( !isalpha(*ptr) && *ptr!= '_' ){
104 if( !isalnum(c) && c != '-' && c != '_' )
110 if( strcmp( name, "_" ) == 0 )
111 strcpy( name, stubname );
112 return cv::string(name);
118 string fromUtf16(const WString& str)
120 cv::AutoBuffer<char> _buf(str.size()*4 + 1);
123 size_t sz = wcstombs(buf, str.c_str(), str.size());
124 if( sz == (size_t)-1 )
130 WString toUtf16(const string& str)
132 cv::AutoBuffer<wchar_t> _buf(str.size() + 1);
135 size_t sz = mbstowcs(buf, str.c_str(), str.size());
136 if( sz == (size_t)-1 )
144 typedef struct CvGenericHash
152 typedef CvGenericHash CvStringHash;
154 typedef struct CvFileMapNode
157 const CvStringHashNode* key;
158 struct CvFileMapNode* next;
162 typedef struct CvXMLStackRecord
171 #define CV_XML_OPENING_TAG 1
172 #define CV_XML_CLOSING_TAG 2
173 #define CV_XML_EMPTY_TAG 3
174 #define CV_XML_HEADER_TAG 4
175 #define CV_XML_DIRECTIVE_TAG 5
177 //typedef void (*CvParse)( struct CvFileStorage* fs );
178 typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key,
179 int struct_flags, const char* type_name );
180 typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs );
181 typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value );
182 typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value );
183 typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key,
184 const char* value, int quote );
185 typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment );
186 typedef void (*CvStartNextStream)( struct CvFileStorage* fs );
188 typedef struct CvFileStorage
194 CvMemStorage* memstorage;
195 CvMemStorage* dststorage;
196 CvMemStorage* strstorage;
197 CvStringHash* str_hash;
216 CvStartWriteStruct start_write_struct;
217 CvEndWriteStruct end_write_struct;
218 CvWriteInt write_int;
219 CvWriteReal write_real;
220 CvWriteString write_string;
221 CvWriteComment write_comment;
222 CvStartNextStream start_next_stream;
227 static void icvPuts( CvFileStorage* fs, const char* str )
229 CV_Assert( fs->file || fs->gzfile );
231 fputs( str, fs->file );
233 gzputs( fs->gzfile, str );
236 static char* icvGets( CvFileStorage* fs, char* str, int maxCount )
238 CV_Assert( fs->file || fs->gzfile );
240 return fgets( str, maxCount, fs->file );
241 return gzgets( fs->gzfile, str, maxCount );
244 static int icvEof( CvFileStorage* fs )
246 CV_Assert( fs->file || fs->gzfile );
248 return feof(fs->file);
249 return gzeof(fs->gzfile);
252 static void icvClose( CvFileStorage* fs )
257 gzclose( fs->gzfile );
262 static void icvRewind( CvFileStorage* fs )
264 CV_Assert( fs->file || fs->gzfile );
268 gzrewind(fs->gzfile);
271 #define CV_YML_INDENT 3
272 #define CV_XML_INDENT 2
273 #define CV_YML_INDENT_FLOW 1
274 #define CV_FS_MAX_LEN 4096
276 #define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24))
277 #define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE)
279 #define CV_CHECK_FILE_STORAGE(fs) \
281 if( !CV_IS_FILE_STORAGE(fs) ) \
282 CV_Error( (fs) ? CV_StsBadArg : CV_StsNullPtr, \
283 "Invalid pointer to file storage" ); \
286 #define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \
288 CV_CHECK_FILE_STORAGE(fs); \
289 if( !fs->write_mode ) \
290 CV_Error( CV_StsError, "The file storage is opened for reading" ); \
294 cvAttrValue( const CvAttrList* attr, const char* attr_name )
296 while( attr && attr->attr )
299 for( i = 0; attr->attr[i*2] != 0; i++ )
301 if( strcmp( attr_name, attr->attr[i*2] ) == 0 )
302 return attr->attr[i*2+1];
311 static CvGenericHash*
312 cvCreateMap( int flags, int header_size, int elem_size,
313 CvMemStorage* storage, int start_tab_size )
315 if( header_size < (int)sizeof(CvGenericHash) )
316 CV_Error( CV_StsBadSize, "Too small map header_size" );
318 if( start_tab_size <= 0 )
321 CvGenericHash* map = (CvGenericHash*)cvCreateSet( flags, header_size, elem_size, storage );
323 map->tab_size = start_tab_size;
324 start_tab_size *= sizeof(map->table[0]);
325 map->table = (void**)cvMemStorageAlloc( storage, start_tab_size );
326 memset( map->table, 0, start_tab_size );
332 #define CV_PARSE_ERROR( errmsg ) \
333 icvParseError( fs, __func__, (errmsg), __FILE__, __LINE__ )
335 #define CV_PARSE_ERROR( errmsg ) \
336 icvParseError( fs, "", (errmsg), __FILE__, __LINE__ )
340 icvParseError( CvFileStorage* fs, const char* func_name,
341 const char* err_msg, const char* source_file, int source_line )
344 sprintf( buf, "%s(%d): %s", fs->filename, fs->lineno, err_msg );
345 cvError( CV_StsParseError, func_name, buf, source_file, source_line );
350 icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection )
352 if( CV_NODE_IS_MAP(tag) )
354 if( collection->tag != CV_NODE_NONE )
356 assert( fs->is_xml != 0 );
357 CV_PARSE_ERROR( "Sequence element should not have name (use <_></_>)" );
360 collection->data.map = cvCreateMap( 0, sizeof(CvFileNodeHash),
361 sizeof(CvFileMapNode), fs->memstorage, 16 );
366 seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage );
368 // if <collection> contains some scalar element, add it to the newly created collection
369 if( CV_NODE_TYPE(collection->tag) != CV_NODE_NONE )
370 cvSeqPush( seq, collection );
372 collection->data.seq = seq;
375 collection->tag = tag;
376 cvSetSeqBlockSize( collection->data.seq, 8 );
381 icvFSReleaseCollection( CvSeq* seq )
385 int is_map = CV_IS_SET(seq);
387 int i, total = seq->total;
388 cvStartReadSeq( seq, &reader, 0 );
390 for( i = 0; i < total; i++ )
392 CvFileNode* node = (CvFileNode*)reader.ptr;
394 if( (!is_map || CV_IS_SET_ELEM( node )) && CV_NODE_IS_COLLECTION(node->tag) )
396 if( CV_NODE_IS_USER(node->tag) && node->info && node->data.obj.decoded )
397 cvRelease( (void**)&node->data.obj.decoded );
398 if( !CV_NODE_SEQ_IS_SIMPLE( node->data.seq ))
399 icvFSReleaseCollection( node->data.seq );
401 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
408 icvFSDoResize( CvFileStorage* fs, char* ptr, int len )
411 int written_len = (int)(ptr - fs->buffer_start);
412 int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2);
413 new_size = MAX( written_len + len, new_size );
414 new_ptr = (char*)cvAlloc( new_size + 256 );
415 fs->buffer = new_ptr + (fs->buffer - fs->buffer_start);
416 if( written_len > 0 )
417 memcpy( new_ptr, fs->buffer_start, written_len );
418 fs->buffer_start = new_ptr;
419 fs->buffer_end = fs->buffer_start + new_size;
420 new_ptr += written_len;
425 inline char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len )
427 return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len );
432 icvFSFlush( CvFileStorage* fs )
434 char* ptr = fs->buffer;
437 if( ptr > fs->buffer_start + fs->space )
441 icvPuts( fs, fs->buffer_start );
442 fs->buffer = fs->buffer_start;
445 indent = fs->struct_indent;
447 if( fs->space != indent )
449 if( fs->space < indent )
450 memset( fs->buffer_start + fs->space, ' ', indent - fs->space );
454 ptr = fs->buffer = fs->buffer_start + fs->space;
460 /* closes file storage and deallocates buffers */
462 cvReleaseFileStorage( CvFileStorage** p_fs )
465 CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" );
469 CvFileStorage* fs = *p_fs;
472 if( fs->write_mode && (fs->file || fs->gzfile) )
474 if( fs->write_stack )
476 while( fs->write_stack->total > 0 )
477 cvEndWriteStruct(fs);
481 icvPuts( fs, "</opencv_storage>\n" );
484 //icvFSReleaseCollection( fs->roots ); // delete all the user types recursively
488 cvReleaseMemStorage( &fs->strstorage );
490 cvFree( &fs->buffer_start );
491 cvReleaseMemStorage( &fs->memstorage );
493 memset( fs, 0, sizeof(*fs) );
499 #define CV_HASHVAL_SCALE 33
501 CV_IMPL CvStringHashNode*
502 cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing )
504 CvStringHashNode* node = 0;
505 unsigned hashval = 0;
507 CvStringHash* map = fs->str_hash;
514 for( i = 0; str[i] != '\0'; i++ )
515 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
518 else for( i = 0; i < len; i++ )
519 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
522 tab_size = map->tab_size;
523 if( (tab_size & (tab_size - 1)) == 0 )
524 i = (int)(hashval & (tab_size - 1));
526 i = (int)(hashval % tab_size);
528 for( node = (CvStringHashNode*)(map->table[i]); node != 0; node = node->next )
530 if( node->hashval == hashval &&
531 node->str.len == len &&
532 memcmp( node->str.ptr, str, len ) == 0 )
536 if( !node && create_missing )
538 node = (CvStringHashNode*)cvSetNew( (CvSet*)map );
539 node->hashval = hashval;
540 node->str = cvMemStorageAllocString( map->storage, str, len );
541 node->next = (CvStringHashNode*)(map->table[i]);
542 map->table[i] = node;
550 cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node,
551 const CvStringHashNode* key,
554 CvFileNode* value = 0;
555 int k = 0, attempts = 1;
560 CV_CHECK_FILE_STORAGE(fs);
563 CV_Error( CV_StsNullPtr, "Null key element" );
569 attempts = fs->roots->total;
572 for( k = 0; k < attempts; k++ )
575 CvFileNode* map_node = _map_node;
576 CvFileMapNode* another;
580 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
582 if( !CV_NODE_IS_MAP(map_node->tag) )
584 if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
585 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
586 CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
590 map = map_node->data.map;
591 tab_size = map->tab_size;
593 if( (tab_size & (tab_size - 1)) == 0 )
594 i = (int)(key->hashval & (tab_size - 1));
596 i = (int)(key->hashval % tab_size);
598 for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
599 if( another->key == key )
601 if( !create_missing )
603 value = &another->value;
606 CV_PARSE_ERROR( "Duplicated key" );
609 if( k == attempts - 1 && create_missing )
611 CvFileMapNode* node = (CvFileMapNode*)cvSetNew( (CvSet*)map );
614 node->next = (CvFileMapNode*)(map->table[i]);
615 map->table[i] = node;
616 value = (CvFileNode*)node;
625 cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str )
627 CvFileNode* value = 0;
628 int i, len, tab_size;
629 unsigned hashval = 0;
630 int k = 0, attempts = 1;
635 CV_CHECK_FILE_STORAGE(fs);
638 CV_Error( CV_StsNullPtr, "Null element name" );
640 for( i = 0; str[i] != '\0'; i++ )
641 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
649 attempts = fs->roots->total;
652 for( k = 0; k < attempts; k++ )
655 const CvFileNode* map_node = _map_node;
656 CvFileMapNode* another;
659 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
661 if( !CV_NODE_IS_MAP(map_node->tag) )
663 if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
664 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
665 CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
669 map = map_node->data.map;
670 tab_size = map->tab_size;
672 if( (tab_size & (tab_size - 1)) == 0 )
673 i = (int)(hashval & (tab_size - 1));
675 i = (int)(hashval % tab_size);
677 for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
679 const CvStringHashNode* key = another->key;
681 if( key->hashval == hashval &&
682 key->str.len == len &&
683 memcmp( key->str.ptr, str, len ) == 0 )
685 value = &another->value;
696 cvGetRootFileNode( const CvFileStorage* fs, int stream_index )
698 CV_CHECK_FILE_STORAGE(fs);
700 if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total )
703 return (CvFileNode*)cvGetSeqElem( fs->roots, stream_index );
707 /* returns the sequence element by its index */
708 /*CV_IMPL CvFileNode*
709 cvGetFileNodeFromSeq( CvFileStorage* fs,
710 CvFileNode* seq_node, int index )
712 CvFileNode* value = 0;
717 else if( !CV_NODE_IS_SEQ(seq_node->tag) )
719 if( CV_NODE_IS_MAP(seq_node->tag) )
720 CV_Error( CV_StsError, "The node is map. Use cvGetFileNodeFromMap()." );
721 if( CV_NODE_TYPE(seq_node->tag) == CV_NODE_NONE )
722 CV_Error( CV_StsError, "The node is an empty object (None)." );
723 if( index != 0 && index != -1 )
724 CV_Error( CV_StsOutOfRange, "" );
729 seq = seq_node->data.seq;
732 CV_Error( CV_StsNullPtr, "The file storage is empty" );
734 value = (CvFileNode*)cvGetSeqElem( seq, index, 0 );
743 icvDoubleToString( char* buf, double value )
749 ieee754_hi = (unsigned)(val.u >> 32);
751 if( (ieee754_hi & 0x7ff00000) != 0x7ff00000 )
753 int ivalue = cvRound(value);
754 if( ivalue == value )
755 sprintf( buf, "%d.", ivalue );
758 static const char* fmt[] = {"%.16e", "%.16f"};
759 double avalue = fabs(value);
761 sprintf( buf, fmt[0.01 <= avalue && avalue < 1000], value );
762 if( *ptr == '+' || *ptr == '-' )
764 for( ; isdigit(*ptr); ptr++ )
772 unsigned ieee754_lo = (unsigned)val.u;
773 if( (ieee754_hi & 0x7fffffff) + (ieee754_lo != 0) > 0x7ff00000 )
774 strcpy( buf, ".Nan" );
776 strcpy( buf, (int)ieee754_hi < 0 ? "-.Inf" : ".Inf" );
784 icvFloatToString( char* buf, float value )
791 if( (ieee754 & 0x7f800000) != 0x7f800000 )
793 int ivalue = cvRound(value);
794 if( ivalue == value )
795 sprintf( buf, "%d.", ivalue );
798 static const char* fmt[] = {"%.8e", "%.8f"};
799 double avalue = fabs((double)value);
801 sprintf( buf, fmt[0.01 <= avalue && avalue < 1000], value );
802 if( *ptr == '+' || *ptr == '-' )
804 for( ; isdigit(*ptr); ptr++ )
812 if( (ieee754 & 0x7fffffff) != 0x7f800000 )
813 strcpy( buf, ".Nan" );
815 strcpy( buf, (int)ieee754 < 0 ? "-.Inf" : ".Inf" );
823 icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr )
826 int inf_hi = 0x7ff00000;
828 if( c == '-' || c == '+' )
830 inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000;
835 CV_PARSE_ERROR( "Bad format of floating-point constant" );
837 if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' )
838 *(uint64*)value = ((uint64)inf_hi << 32);
839 else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' )
840 *(uint64*)value = (uint64)-1;
842 CV_PARSE_ERROR( "Bad format of floating-point constant" );
848 static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
850 double fval = strtod( ptr, endptr );
851 if( **endptr == '.' )
853 char* dot_pos = *endptr;
855 double fval2 = strtod( ptr, endptr );
857 if( *endptr > dot_pos )
863 if( *endptr == ptr || isalpha(**endptr) )
864 icvProcessSpecialDouble( fs, ptr, &fval, endptr );
870 /****************************************************************************************\
872 \****************************************************************************************/
875 icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent )
883 if( ptr - fs->buffer_start > max_comment_indent )
887 else if( cv_isprint(*ptr) )
889 if( ptr - fs->buffer_start < min_indent )
890 CV_PARSE_ERROR( "Incorrect indentation" );
893 else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' )
895 int max_size = (int)(fs->buffer_end - fs->buffer_start);
896 ptr = icvGets( fs, fs->buffer_start, max_size );
899 // emulate end of stream
900 ptr = fs->buffer_start;
901 ptr[0] = ptr[1] = ptr[2] = '.';
908 int l = (int)strlen(ptr);
909 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) )
910 CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
916 CV_PARSE_ERROR( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" );
924 icvYMLParseKey( CvFileStorage* fs, char* ptr,
925 CvFileNode* map_node, CvFileNode** value_placeholder )
928 char *endptr = ptr - 1, *saveptr;
929 CvStringHashNode* str_hash_node;
932 CV_PARSE_ERROR( "Key may not start with \'-\'" );
935 while( cv_isprint(c) && c != ':' );
938 CV_PARSE_ERROR( "Missing \':\'" );
940 saveptr = endptr + 1;
946 CV_PARSE_ERROR( "An empty key" );
948 str_hash_node = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 );
949 *value_placeholder = cvGetFileNode( fs, map_node, str_hash_node, 1 );
957 icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
958 int parent_flags, int min_indent )
960 char buf[CV_FS_MAX_LEN + 1024];
962 char c = ptr[0], d = ptr[1];
963 int is_parent_flow = CV_NODE_IS_FLOW(parent_flags);
964 int value_type = CV_NODE_NONE;
967 memset( node, 0, sizeof(*node) );
969 if( c == '!' ) // handle explicit type specification
971 if( d == '!' || d == '^' )
974 value_type |= CV_NODE_USER;
979 while( cv_isprint(d) && d != ' ' );
980 len = (int)(endptr - ptr);
982 CV_PARSE_ERROR( "Empty type name" );
986 if( len == 3 && !CV_NODE_IS_USER(value_type) )
988 if( memcmp( ptr, "str", 3 ) == 0 )
989 value_type = CV_NODE_STRING;
990 else if( memcmp( ptr, "int", 3 ) == 0 )
991 value_type = CV_NODE_INT;
992 else if( memcmp( ptr, "seq", 3 ) == 0 )
993 value_type = CV_NODE_SEQ;
994 else if( memcmp( ptr, "map", 3 ) == 0 )
995 value_type = CV_NODE_MAP;
997 else if( len == 5 && !CV_NODE_IS_USER(value_type) )
999 if( memcmp( ptr, "float", 5 ) == 0 )
1000 value_type = CV_NODE_REAL;
1002 else if( CV_NODE_IS_USER(value_type) )
1004 node->info = cvFindType( ptr );
1006 node->tag &= ~CV_NODE_USER;
1010 ptr = icvYMLSkipSpaces( fs, endptr, min_indent, INT_MAX );
1014 if( !CV_NODE_IS_USER(value_type) )
1016 if( value_type == CV_NODE_STRING && c != '\'' && c != '\"' )
1018 if( value_type == CV_NODE_INT )
1020 if( value_type == CV_NODE_REAL )
1026 ((c == '-' || c == '+') && (isdigit(d) || d == '.')) ||
1027 (c == '.' && isalnum(d))) // a number
1031 endptr = ptr + (c == '-' || c == '+');
1032 while( isdigit(*endptr) )
1034 if( *endptr == '.' || *endptr == 'e' )
1037 fval = icv_strtod( fs, ptr, &endptr );
1038 /*if( endptr == ptr || isalpha(*endptr) )
1039 icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/
1041 node->tag = CV_NODE_REAL;
1042 node->data.f = fval;
1047 ival = (int)strtol( ptr, &endptr, 0 );
1048 node->tag = CV_NODE_INT;
1049 node->data.i = ival;
1052 if( !endptr || endptr == ptr )
1053 CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
1057 else if( c == '\'' || c == '\"' ) // an explicit string
1059 node->tag = CV_NODE_STRING;
1061 for( len = 0; len < CV_FS_MAX_LEN; )
1064 if( isalnum(c) || (c != '\'' && cv_isprint(c)))
1066 else if( c == '\'' )
1074 CV_PARSE_ERROR( "Invalid character" );
1077 for( len = 0; len < CV_FS_MAX_LEN; )
1080 if( isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c)))
1082 else if( c == '\"' )
1087 else if( c == '\\' )
1092 else if( d == '\"' || d == '\\' || d == '\'' )
1100 else if( d == 'x' || (isdigit(d) && d < '8') )
1102 int val, is_hex = d == 'x';
1105 val = strtol( ptr + is_hex, &endptr, is_hex ? 8 : 16 );
1107 if( endptr == ptr + is_hex )
1111 buf[len++] = (char)val;
1117 CV_PARSE_ERROR( "Invalid character" );
1120 if( len >= CV_FS_MAX_LEN )
1121 CV_PARSE_ERROR( "Too long string literal" );
1123 node->data.str = cvMemStorageAllocString( fs->memstorage, buf, len );
1125 else if( c == '[' || c == '{' ) // collection as a flow
1127 int new_min_indent = min_indent + !is_parent_flow;
1128 int struct_flags = CV_NODE_FLOW + (c == '{' ? CV_NODE_MAP : CV_NODE_SEQ);
1131 icvFSCreateCollection( fs, CV_NODE_TYPE(struct_flags) +
1132 (node->info ? CV_NODE_USER : 0), node );
1134 d = c == '[' ? ']' : '}';
1138 CvFileNode* elem = 0;
1140 ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX );
1141 if( *ptr == '}' || *ptr == ']' )
1144 CV_PARSE_ERROR( "The wrong closing bracket" );
1149 if( node->data.seq->total != 0 )
1152 CV_PARSE_ERROR( "Missing , between the elements" );
1153 ptr = icvYMLSkipSpaces( fs, ptr + 1, new_min_indent, INT_MAX );
1156 if( CV_NODE_IS_MAP(struct_flags) )
1158 ptr = icvYMLParseKey( fs, ptr, node, &elem );
1159 ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX );
1165 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1167 ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, new_min_indent );
1168 if( CV_NODE_IS_MAP(struct_flags) )
1169 elem->tag |= CV_NODE_NAMED;
1170 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1172 node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
1176 int indent, struct_flags, is_simple;
1178 if( is_parent_flow || c != '-' )
1180 // implicit (one-line) string or nested block-style collection
1181 if( !is_parent_flow )
1184 CV_PARSE_ERROR( "Complex keys are not supported" );
1185 if( c == '|' || c == '>' )
1186 CV_PARSE_ERROR( "Multi-line text literals are not supported" );
1193 while( cv_isprint(c) &&
1194 (!is_parent_flow || (c != ',' && c != '}' && c != ']')) &&
1195 (is_parent_flow || c != ':' || value_type == CV_NODE_STRING));
1198 CV_PARSE_ERROR( "Invalid character" );
1200 if( is_parent_flow || c != ':' )
1202 char* str_end = endptr;
1203 node->tag = CV_NODE_STRING;
1204 // strip spaces in the end of string
1206 while( str_end > ptr && c == ' ' );
1208 node->data.str = cvMemStorageAllocString( fs->memstorage, ptr, (int)(str_end - ptr) );
1212 struct_flags = CV_NODE_MAP;
1215 struct_flags = CV_NODE_SEQ;
1217 icvFSCreateCollection( fs, struct_flags +
1218 (node->info ? CV_NODE_USER : 0), node );
1220 indent = (int)(ptr - fs->buffer_start);
1225 CvFileNode* elem = 0;
1227 if( CV_NODE_IS_MAP(struct_flags) )
1229 ptr = icvYMLParseKey( fs, ptr, node, &elem );
1235 CV_PARSE_ERROR( "Block sequence elements must be preceded with \'-\'" );
1237 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1240 ptr = icvYMLSkipSpaces( fs, ptr, indent + 1, INT_MAX );
1241 ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, indent + 1 );
1242 if( CV_NODE_IS_MAP(struct_flags) )
1243 elem->tag |= CV_NODE_NAMED;
1244 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1246 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1247 if( ptr - fs->buffer_start != indent )
1249 if( ptr - fs->buffer_start < indent )
1252 CV_PARSE_ERROR( "Incorrect indentation" );
1254 if( memcmp( ptr, "...", 3 ) == 0 )
1258 node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
1266 icvYMLParse( CvFileStorage* fs )
1268 char* ptr = fs->buffer_start;
1273 // 0. skip leading comments and directives and ...
1274 // 1. reach the first item
1277 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1283 if( memcmp( ptr, "%YAML:", 6 ) == 0 &&
1284 memcmp( ptr, "%YAML:1.", 8 ) != 0 )
1285 CV_PARSE_ERROR( "Unsupported YAML version (it must be 1.x)" );
1288 else if( *ptr == '-' )
1290 if( memcmp(ptr, "---", 3) == 0 )
1298 else if( isalnum(*ptr) || *ptr=='_')
1301 CV_PARSE_ERROR( "The YAML streams must start with '---', except the first one" );
1305 CV_PARSE_ERROR( "Invalid or unsupported syntax" );
1308 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1309 if( memcmp( ptr, "...", 3 ) != 0 )
1311 // 2. parse the collection
1312 CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
1314 ptr = icvYMLParseValue( fs, ptr, root_node, CV_NODE_NONE, 0 );
1315 if( !CV_NODE_IS_COLLECTION(root_node->tag) )
1316 CV_PARSE_ERROR( "Only collections as YAML streams are supported by this parser" );
1318 // 3. parse until the end of file or next collection
1319 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1332 /****************************************************************************************\
1334 \****************************************************************************************/
1337 icvYMLWrite( CvFileStorage* fs, const char* key, const char* data )
1344 struct_flags = fs->struct_flags;
1346 if( key && key[0] == '\0' )
1349 if( CV_NODE_IS_COLLECTION(struct_flags) )
1351 if( (CV_NODE_IS_MAP(struct_flags) ^ (key != 0)) )
1352 CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, "
1353 "or add element with key to sequence" );
1358 struct_flags = CV_NODE_EMPTY | (key ? CV_NODE_MAP : CV_NODE_SEQ);
1363 keylen = (int)strlen(key);
1365 CV_Error( CV_StsBadArg, "The key is an empty" );
1367 if( keylen > CV_FS_MAX_LEN )
1368 CV_Error( CV_StsBadArg, "The key is too long" );
1372 datalen = (int)strlen(data);
1374 if( CV_NODE_IS_FLOW(struct_flags) )
1378 if( !CV_NODE_IS_EMPTY(struct_flags) )
1380 new_offset = (int)(ptr - fs->buffer_start) + keylen + datalen;
1381 if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 )
1384 ptr = icvFSFlush(fs);
1391 ptr = icvFSFlush(fs);
1392 if( !CV_NODE_IS_MAP(struct_flags) )
1402 if( !isalpha(key[0]) && key[0] != '_' )
1403 CV_Error( CV_StsBadArg, "Key must start with a letter or _" );
1405 ptr = icvFSResizeWriteBuffer( fs, ptr, keylen );
1407 for( i = 0; i < keylen; i++ )
1412 if( !isalnum(c) && c != '-' && c != '_' && c != ' ' )
1413 CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" );
1418 if( !CV_NODE_IS_FLOW(struct_flags) && data )
1424 ptr = icvFSResizeWriteBuffer( fs, ptr, datalen );
1425 memcpy( ptr, data, datalen );
1430 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
1435 icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
1436 const char* type_name CV_DEFAULT(0))
1439 char buf[CV_FS_MAX_LEN + 1024];
1440 const char* data = 0;
1442 struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
1443 if( !CV_NODE_IS_COLLECTION(struct_flags))
1444 CV_Error( CV_StsBadArg,
1445 "Some collection type - CV_NODE_SEQ or CV_NODE_MAP, must be specified" );
1447 if( CV_NODE_IS_FLOW(struct_flags) )
1449 char c = CV_NODE_IS_MAP(struct_flags) ? '{' : '[';
1450 struct_flags |= CV_NODE_FLOW;
1453 sprintf( buf, "!!%s %c", type_name, c );
1461 else if( type_name )
1463 sprintf( buf, "!!%s", type_name );
1467 icvYMLWrite( fs, key, data );
1469 parent_flags = fs->struct_flags;
1470 cvSeqPush( fs->write_stack, &parent_flags );
1471 fs->struct_flags = struct_flags;
1473 if( !CV_NODE_IS_FLOW(parent_flags) )
1474 fs->struct_indent += CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1479 icvYMLEndWriteStruct( CvFileStorage* fs )
1481 int parent_flags = 0, struct_flags;
1484 struct_flags = fs->struct_flags;
1485 if( fs->write_stack->total == 0 )
1486 CV_Error( CV_StsError, "EndWriteStruct w/o matching StartWriteStruct" );
1488 cvSeqPop( fs->write_stack, &parent_flags );
1490 if( CV_NODE_IS_FLOW(struct_flags) )
1493 if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) )
1495 *ptr++ = CV_NODE_IS_MAP(struct_flags) ? '}' : ']';
1498 else if( CV_NODE_IS_EMPTY(struct_flags) )
1500 ptr = icvFSFlush(fs);
1501 memcpy( ptr, CV_NODE_IS_MAP(struct_flags) ? "{}" : "[]", 2 );
1502 fs->buffer = ptr + 2;
1505 if( !CV_NODE_IS_FLOW(parent_flags) )
1506 fs->struct_indent -= CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1507 assert( fs->struct_indent >= 0 );
1509 fs->struct_flags = parent_flags;
1514 icvYMLStartNextStream( CvFileStorage* fs )
1518 while( fs->write_stack->total > 0 )
1519 icvYMLEndWriteStruct(fs);
1521 fs->struct_indent = 0;
1523 icvPuts( fs, "...\n" );
1524 icvPuts( fs, "---\n" );
1525 fs->buffer = fs->buffer_start;
1531 icvYMLWriteInt( CvFileStorage* fs, const char* key, int value )
1534 icvYMLWrite( fs, key, icv_itoa( value, buf, 10 ));
1539 icvYMLWriteReal( CvFileStorage* fs, const char* key, double value )
1542 icvYMLWrite( fs, key, icvDoubleToString( buf, value ));
1547 icvYMLWriteString( CvFileStorage* fs, const char* key,
1548 const char* str, int quote CV_DEFAULT(0))
1550 char buf[CV_FS_MAX_LEN*4+16];
1551 char* data = (char*)str;
1555 CV_Error( CV_StsNullPtr, "Null string pointer" );
1557 len = (int)strlen(str);
1558 if( len > CV_FS_MAX_LEN )
1559 CV_Error( CV_StsBadArg, "The written string is too long" );
1561 if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') )
1563 int need_quote = quote || len == 0;
1566 for( i = 0; i < len; i++ )
1570 if( !need_quote && !isalnum(c) && c != '_' && c != ' ' && c != '-' &&
1571 c != '(' && c != ')' && c != '/' && c != '+' && c != ';' )
1574 if( !isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') )
1579 else if( c == '\n' )
1581 else if( c == '\r' )
1583 else if( c == '\t' )
1587 sprintf( data, "x%02x", c );
1594 if( !need_quote && (isdigit(str[0]) ||
1595 str[0] == '+' || str[0] == '-' || str[0] == '.' ))
1601 data = buf + !need_quote;
1604 icvYMLWrite( fs, key, data );
1609 icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
1611 int len; //, indent;
1617 CV_Error( CV_StsNullPtr, "Null comment" );
1619 len = (int)strlen(comment);
1620 eol = strchr(comment, '\n');
1621 multiline = eol != 0;
1624 if( !eol_comment || multiline ||
1625 fs->buffer_end - ptr < len || ptr == fs->buffer_start )
1626 ptr = icvFSFlush( fs );
1636 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
1637 memcpy( ptr, comment, eol - comment + 1 );
1638 fs->buffer = ptr + (eol - comment);
1640 eol = strchr( comment, '\n' );
1644 len = (int)strlen(comment);
1645 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
1646 memcpy( ptr, comment, len );
1647 fs->buffer = ptr + len;
1650 ptr = icvFSFlush( fs );
1655 /****************************************************************************************\
1657 \****************************************************************************************/
1659 #define CV_XML_INSIDE_COMMENT 1
1660 #define CV_XML_INSIDE_TAG 2
1661 #define CV_XML_INSIDE_DIRECTIVE 3
1664 icvXMLSkipSpaces( CvFileStorage* fs, char* ptr, int mode )
1673 if( mode == CV_XML_INSIDE_COMMENT )
1676 while( cv_isprint_or_tab(c) && (c != '-' || ptr[1] != '-' || ptr[2] != '>') );
1680 assert( ptr[1] == '-' && ptr[2] == '>' );
1685 else if( mode == CV_XML_INSIDE_DIRECTIVE )
1687 // !!!NOTE!!! This is not quite correct, but should work in most cases
1695 } while( cv_isprint_or_tab(c) );
1700 while( c == ' ' || c == '\t' );
1702 if( c == '<' && ptr[1] == '!' && ptr[2] == '-' && ptr[3] == '-' )
1705 CV_PARSE_ERROR( "Comments are not allowed here" );
1706 mode = CV_XML_INSIDE_COMMENT;
1709 else if( cv_isprint(c) )
1713 if( !cv_isprint(*ptr) )
1715 int max_size = (int)(fs->buffer_end - fs->buffer_start);
1716 if( *ptr != '\0' && *ptr != '\n' && *ptr != '\r' )
1717 CV_PARSE_ERROR( "Invalid character in the stream" );
1718 ptr = icvGets( fs, fs->buffer_start, max_size );
1721 ptr = fs->buffer_start;
1728 int l = (int)strlen(ptr);
1729 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) )
1730 CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
1740 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
1741 CvAttrList** _list, int* _tag_type );
1744 icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
1745 int value_type CV_DEFAULT(CV_NODE_NONE))
1747 CvFileNode *elem = node;
1748 int have_space = 1, is_simple = 1;
1749 int is_user_type = CV_NODE_IS_USER(value_type);
1750 memset( node, 0, sizeof(*node) );
1752 value_type = CV_NODE_TYPE(value_type);
1759 if( isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
1761 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
1770 CvStringHashNode *key = 0, *key2 = 0;
1771 CvAttrList* list = 0;
1772 CvTypeInfo* info = 0;
1775 const char* type_name = 0;
1776 int elem_type = CV_NODE_NONE;
1781 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
1783 if( tag_type == CV_XML_DIRECTIVE_TAG )
1784 CV_PARSE_ERROR( "Directive tags are not allowed here" );
1785 if( tag_type == CV_XML_EMPTY_TAG )
1786 CV_PARSE_ERROR( "Empty tags are not supported" );
1788 assert( tag_type == CV_XML_OPENING_TAG );
1790 type_name = list ? cvAttrValue( list, "type_id" ) : 0;
1793 if( strcmp( type_name, "str" ) == 0 )
1794 elem_type = CV_NODE_STRING;
1795 else if( strcmp( type_name, "map" ) == 0 )
1796 elem_type = CV_NODE_MAP;
1797 else if( strcmp( type_name, "seq" ) == 0 )
1798 elem_type = CV_NODE_SEQ;
1801 info = cvFindType( type_name );
1803 elem_type = CV_NODE_USER;
1807 is_noname = key->str.len == 1 && key->str.ptr[0] == '_';
1808 if( !CV_NODE_IS_COLLECTION(node->tag) )
1810 icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node );
1812 else if( is_noname ^ CV_NODE_IS_SEQ(node->tag) )
1813 CV_PARSE_ERROR( is_noname ? "Map element should have a name" :
1814 "Sequence element should not have name (use <_></_>)" );
1817 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1819 elem = cvGetFileNode( fs, node, key, 1 );
1821 ptr = icvXMLParseValue( fs, ptr, elem, elem_type);
1823 elem->tag |= CV_NODE_NAMED;
1824 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1826 ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type );
1827 if( tag_type != CV_XML_CLOSING_TAG || key2 != key )
1828 CV_PARSE_ERROR( "Mismatched closing tag" );
1834 CV_PARSE_ERROR( "There should be space between literals" );
1837 if( node->tag != CV_NODE_NONE )
1839 if( !CV_NODE_IS_COLLECTION(node->tag) )
1840 icvFSCreateCollection( fs, CV_NODE_SEQ, node );
1842 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1846 if( value_type != CV_NODE_STRING &&
1847 (isdigit(c) || ((c == '-' || c == '+') &&
1848 (isdigit(d) || d == '.')) || (c == '.' && isalnum(d))) ) // a number
1852 endptr = ptr + (c == '-' || c == '+');
1853 while( isdigit(*endptr) )
1855 if( *endptr == '.' || *endptr == 'e' )
1857 fval = icv_strtod( fs, ptr, &endptr );
1858 /*if( endptr == ptr || isalpha(*endptr) )
1859 icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
1860 elem->tag = CV_NODE_REAL;
1861 elem->data.f = fval;
1865 ival = (int)strtol( ptr, &endptr, 0 );
1866 elem->tag = CV_NODE_INT;
1867 elem->data.i = ival;
1871 CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
1878 char buf[CV_FS_MAX_LEN+16];
1879 int i = 0, len, is_quoted = 0;
1880 elem->tag = CV_NODE_STRING;
1894 CV_PARSE_ERROR( "Literal \" is not allowed within a string. Use "" );
1898 else if( !cv_isprint(c) || c == '<' || (!is_quoted && isspace(c)))
1901 CV_PARSE_ERROR( "Closing \" is expected" );
1904 else if( c == '\'' || c == '>' )
1906 CV_PARSE_ERROR( "Literal \' or > are not allowed. Use ' or >" );
1914 val = (int)strtol( ptr, &endptr, 0 );
1915 if( (unsigned)val > (unsigned)255 ||
1916 !endptr || *endptr != ';' )
1917 CV_PARSE_ERROR( "Invalid numeric value in the string" );
1924 while( isalnum(c) );
1926 CV_PARSE_ERROR( "Invalid character in the symbol entity name" );
1927 len = (int)(endptr - ptr);
1928 if( len == 2 && memcmp( ptr, "lt", len ) == 0 )
1930 else if( len == 2 && memcmp( ptr, "gt", len ) == 0 )
1932 else if( len == 3 && memcmp( ptr, "amp", len ) == 0 )
1934 else if( len == 4 && memcmp( ptr, "apos", len ) == 0 )
1936 else if( len == 4 && memcmp( ptr, "quot", len ) == 0 )
1940 memcpy( buf + i, ptr-1, len + 2 );
1948 if( i >= CV_FS_MAX_LEN )
1949 CV_PARSE_ERROR( "Too long string literal" );
1951 elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i );
1954 if( !CV_NODE_IS_COLLECTION(value_type) && value_type != CV_NODE_NONE )
1960 if( (CV_NODE_TYPE(node->tag) == CV_NODE_NONE ||
1961 (CV_NODE_TYPE(node->tag) != value_type &&
1962 !CV_NODE_IS_COLLECTION(node->tag))) &&
1963 CV_NODE_IS_COLLECTION(value_type) )
1965 icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ?
1966 CV_NODE_MAP : CV_NODE_SEQ, node );
1969 if( value_type != CV_NODE_NONE &&
1970 value_type != CV_NODE_TYPE(node->tag) )
1971 CV_PARSE_ERROR( "The actual type is different from the specified type" );
1973 if( CV_NODE_IS_COLLECTION(node->tag) && is_simple )
1974 node->data.seq->flags |= CV_NODE_SEQ_SIMPLE;
1976 node->tag |= is_user_type ? CV_NODE_USER : 0;
1982 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
1983 CvAttrList** _list, int* _tag_type )
1986 CvStringHashNode* tagname = 0;
1987 CvAttrList *first = 0, *last = 0;
1988 int count = 0, max_count = 4;
1989 int attr_buf_size = (max_count*2 + 1)*sizeof(char*) + sizeof(CvAttrList);
1995 CV_PARSE_ERROR( "Tag should start with \'<\'" );
1998 if( isalnum(*ptr) || *ptr == '_' )
1999 tag_type = CV_XML_OPENING_TAG;
2000 else if( *ptr == '/' )
2002 tag_type = CV_XML_CLOSING_TAG;
2005 else if( *ptr == '?' )
2007 tag_type = CV_XML_HEADER_TAG;
2010 else if( *ptr == '!' )
2012 tag_type = CV_XML_DIRECTIVE_TAG;
2013 assert( ptr[1] != '-' || ptr[2] != '-' );
2017 CV_PARSE_ERROR( "Unknown tag type" );
2021 CvStringHashNode* attrname;
2023 if( !isalpha(*ptr) && *ptr != '_' )
2024 CV_PARSE_ERROR( "Name should start with a letter or underscore" );
2028 while( isalnum(c) || c == '_' || c == '-' );
2030 attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 );
2037 if( tag_type == CV_XML_CLOSING_TAG )
2038 CV_PARSE_ERROR( "Closing tag should not contain any attributes" );
2040 if( !last || count >= max_count )
2044 chunk = (CvAttrList*)cvMemStorageAlloc( fs->memstorage, attr_buf_size );
2045 memset( chunk, 0, attr_buf_size );
2046 chunk->attr = (const char**)(chunk + 1);
2049 first = last = chunk;
2051 last = last->next = chunk;
2053 last->attr[count*2] = attrname->str.ptr;
2062 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2064 CV_PARSE_ERROR( "Attribute name should be followed by \'=\'" );
2068 if( c != '\"' && c != '\'' )
2070 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2071 if( *ptr != '\"' && *ptr != '\'' )
2072 CV_PARSE_ERROR( "Attribute value should be put into single or double quotes" );
2075 ptr = icvXMLParseValue( fs, ptr, &stub, CV_NODE_STRING );
2076 assert( stub.tag == CV_NODE_STRING );
2077 last->attr[count*2+1] = stub.data.str.ptr;
2082 have_space = isspace(c) || c == '\0';
2086 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2092 if( tag_type == CV_XML_HEADER_TAG )
2093 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
2097 else if( c == '?' && tag_type == CV_XML_HEADER_TAG )
2100 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
2104 else if( c == '/' && ptr[1] == '>' && tag_type == CV_XML_OPENING_TAG )
2106 tag_type = CV_XML_EMPTY_TAG;
2112 CV_PARSE_ERROR( "There should be space between attributes" );
2116 *_tag_type = tag_type;
2124 icvXMLParse( CvFileStorage* fs )
2126 char* ptr = fs->buffer_start;
2127 CvStringHashNode *key = 0, *key2 = 0;
2128 CvAttrList* list = 0;
2131 // CV_XML_INSIDE_TAG is used to prohibit leading comments
2132 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2134 if( memcmp( ptr, "<?xml", 5 ) != 0 )
2135 CV_PARSE_ERROR( "Valid XML should start with \'<?xml ...?>\'" );
2137 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
2140 const char* version = cvAttrValue( list, "version" );
2141 if( version && strncmp( version, "1.", 2 ) != 0 )
2142 CV_Error( CV_StsParseError, "Unsupported version of XML" );
2145 const char* encoding = cvAttrValue( list, "encoding" );
2146 if( encoding && strcmp( encoding, "ASCII" ) != 0 )
2147 CV_PARSE_ERROR( "Unsupported encoding" );
2150 while( *ptr != '\0' )
2152 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
2156 CvFileNode* root_node;
2157 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
2158 if( tag_type != CV_XML_OPENING_TAG ||
2159 strcmp(key->str.ptr,"opencv_storage") != 0 )
2160 CV_PARSE_ERROR( "<opencv_storage> tag is missing" );
2162 root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
2163 ptr = icvXMLParseValue( fs, ptr, root_node, CV_NODE_NONE );
2164 ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type );
2165 if( tag_type != CV_XML_CLOSING_TAG || key != key2 )
2166 CV_PARSE_ERROR( "</opencv_storage> tag is missing" );
2167 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
2171 assert( fs->dummy_eof != 0 );
2175 /****************************************************************************************\
2177 \****************************************************************************************/
2179 #define icvXMLFlush icvFSFlush
2182 icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList list )
2184 char* ptr = fs->buffer;
2186 int struct_flags = fs->struct_flags;
2188 if( key && key[0] == '\0' )
2191 if( tag_type == CV_XML_OPENING_TAG || tag_type == CV_XML_EMPTY_TAG )
2193 if( CV_NODE_IS_COLLECTION(struct_flags) )
2195 if( CV_NODE_IS_MAP(struct_flags) ^ (key != 0) )
2196 CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, "
2197 "or add element with key to sequence" );
2201 struct_flags = CV_NODE_EMPTY + (key ? CV_NODE_MAP : CV_NODE_SEQ);
2205 if( !CV_NODE_IS_EMPTY(struct_flags) )
2206 ptr = icvXMLFlush(fs);
2211 else if( key[0] == '_' && key[1] == '\0' )
2212 CV_Error( CV_StsBadArg, "A single _ is a reserved tag name" );
2214 len = (int)strlen( key );
2216 if( tag_type == CV_XML_CLOSING_TAG )
2219 CV_Error( CV_StsBadArg, "Closing tag should not include any attributes" );
2223 if( !isalpha(key[0]) && key[0] != '_' )
2224 CV_Error( CV_StsBadArg, "Key should start with a letter or _" );
2226 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2227 for( i = 0; i < len; i++ )
2230 if( !isalnum(c) && c != '_' && c != '-' )
2231 CV_Error( CV_StsBadArg, "Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_'" );
2238 const char** attr = list.attr;
2240 for( ; attr && attr[0] != 0; attr += 2 )
2242 int len0 = (int)strlen(attr[0]);
2243 int len1 = (int)strlen(attr[1]);
2245 ptr = icvFSResizeWriteBuffer( fs, ptr, len0 + len1 + 4 );
2247 memcpy( ptr, attr[0], len0 );
2251 memcpy( ptr, attr[1], len1 );
2260 if( tag_type == CV_XML_EMPTY_TAG )
2264 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
2269 icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2270 const char* type_name CV_DEFAULT(0))
2272 CvXMLStackRecord parent;
2273 const char* attr[10];
2276 struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
2277 if( !CV_NODE_IS_COLLECTION(struct_flags))
2278 CV_Error( CV_StsBadArg,
2279 "Some collection type: CV_NODE_SEQ or CV_NODE_MAP must be specified" );
2283 attr[idx++] = "type_id";
2284 attr[idx++] = type_name;
2288 icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(attr,0) );
2290 parent.struct_flags = fs->struct_flags & ~CV_NODE_EMPTY;
2291 parent.struct_indent = fs->struct_indent;
2292 parent.struct_tag = fs->struct_tag;
2293 cvSaveMemStoragePos( fs->strstorage, &parent.pos );
2294 cvSeqPush( fs->write_stack, &parent );
2296 fs->struct_indent += CV_XML_INDENT;
2297 if( !CV_NODE_IS_FLOW(struct_flags) )
2300 fs->struct_flags = struct_flags;
2303 fs->struct_tag = cvMemStorageAllocString( fs->strstorage, (char*)key, -1 );
2307 fs->struct_tag.ptr = 0;
2308 fs->struct_tag.len = 0;
2314 icvXMLEndWriteStruct( CvFileStorage* fs )
2316 CvXMLStackRecord parent;
2318 if( fs->write_stack->total == 0 )
2319 CV_Error( CV_StsError, "An extra closing tag" );
2321 icvXMLWriteTag( fs, fs->struct_tag.ptr, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
2322 cvSeqPop( fs->write_stack, &parent );
2324 fs->struct_indent = parent.struct_indent;
2325 fs->struct_flags = parent.struct_flags;
2326 fs->struct_tag = parent.struct_tag;
2327 cvRestoreMemStoragePos( fs->strstorage, &parent.pos );
2332 icvXMLStartNextStream( CvFileStorage* fs )
2336 while( fs->write_stack->total > 0 )
2337 icvXMLEndWriteStruct(fs);
2339 fs->struct_indent = 0;
2341 /* XML does not allow multiple top-level elements,
2342 so we just put a comment and continue
2343 the current (and the only) "stream" */
2344 icvPuts( fs, "\n<!-- next stream -->\n" );
2345 /*fputs( "</opencv_storage>\n", fs->file );
2346 fputs( "<opencv_storage>\n", fs->file );*/
2347 fs->buffer = fs->buffer_start;
2353 icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len )
2355 if( CV_NODE_IS_MAP(fs->struct_flags) ||
2356 (!CV_NODE_IS_COLLECTION(fs->struct_flags) && key) )
2358 icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(0,0) );
2359 char* ptr = icvFSResizeWriteBuffer( fs, fs->buffer, len );
2360 memcpy( ptr, data, len );
2361 fs->buffer = ptr + len;
2362 icvXMLWriteTag( fs, key, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
2366 char* ptr = fs->buffer;
2367 int new_offset = (int)(ptr - fs->buffer_start) + len;
2370 CV_Error( CV_StsBadArg, "elements with keys can not be written to sequence" );
2372 fs->struct_flags = CV_NODE_SEQ;
2374 if( (new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10) ||
2375 (ptr > fs->buffer_start && ptr[-1] == '>' && !CV_NODE_IS_EMPTY(fs->struct_flags)) )
2377 ptr = icvXMLFlush(fs);
2379 else if( ptr > fs->buffer_start + fs->struct_indent && ptr[-1] != '>' )
2382 memcpy( ptr, data, len );
2383 fs->buffer = ptr + len;
2389 icvXMLWriteInt( CvFileStorage* fs, const char* key, int value )
2391 char buf[128], *ptr = icv_itoa( value, buf, 10 );
2392 int len = (int)strlen(ptr);
2393 icvXMLWriteScalar( fs, key, ptr, len );
2398 icvXMLWriteReal( CvFileStorage* fs, const char* key, double value )
2401 int len = (int)strlen( icvDoubleToString( buf, value ));
2402 icvXMLWriteScalar( fs, key, buf, len );
2407 icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote )
2409 char buf[CV_FS_MAX_LEN*6+16];
2410 char* data = (char*)str;
2414 CV_Error( CV_StsNullPtr, "Null string pointer" );
2416 len = (int)strlen(str);
2417 if( len > CV_FS_MAX_LEN )
2418 CV_Error( CV_StsBadArg, "The written string is too long" );
2420 if( quote || len == 0 || str[0] != '\"' || str[0] != str[len-1] )
2422 int need_quote = quote || len == 0;
2425 for( i = 0; i < len; i++ )
2429 if( !isalnum(c) && (!cv_isprint(c) || c == '<' || c == '>' ||
2430 c == '&' || c == '\'' || c == '\"') )
2435 memcpy(data, "lt", 2);
2440 memcpy(data, "gt", 2);
2445 memcpy(data, "amp", 3);
2448 else if( c == '\'' )
2450 memcpy(data, "apos", 4);
2453 else if( c == '\"' )
2455 memcpy( data, "quot", 4);
2460 sprintf( data, "#x%02x", c );
2472 if( !need_quote && (isdigit(str[0]) ||
2473 str[0] == '+' || str[0] == '-' || str[0] == '.' ))
2478 len = (int)(data - buf) - !need_quote;
2480 data = buf + !need_quote;
2483 icvXMLWriteScalar( fs, key, data, len );
2488 icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2496 CV_Error( CV_StsNullPtr, "Null comment" );
2498 if( strstr(comment, "--") != 0 )
2499 CV_Error( CV_StsBadArg, "Double hyphen \'--\' is not allowed in the comments" );
2501 len = (int)strlen(comment);
2502 eol = strchr(comment, '\n');
2503 multiline = eol != 0;
2506 if( multiline || !eol_comment || fs->buffer_end - ptr < len + 5 )
2507 ptr = icvXMLFlush( fs );
2508 else if( ptr > fs->buffer_start + fs->struct_indent )
2513 ptr = icvFSResizeWriteBuffer( fs, ptr, len + 9 );
2514 sprintf( ptr, "<!-- %s -->", comment );
2515 len = (int)strlen(ptr);
2519 strcpy( ptr, "<!--" );
2523 fs->buffer = ptr + len;
2524 ptr = icvXMLFlush(fs);
2532 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
2533 memcpy( ptr, comment, eol - comment + 1 );
2534 ptr += eol - comment;
2536 eol = strchr( comment, '\n' );
2540 len = (int)strlen(comment);
2541 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2542 memcpy( ptr, comment, len );
2547 ptr = icvXMLFlush( fs );
2549 sprintf( ptr, "-->" );
2550 fs->buffer = ptr + 3;
2556 /****************************************************************************************\
2557 * Common High-Level Functions *
2558 \****************************************************************************************/
2560 CV_IMPL CvFileStorage*
2561 cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags )
2563 CvFileStorage* fs = 0;
2565 int default_block_size = 1 << 18;
2566 bool append = (flags & 3) == CV_STORAGE_APPEND;
2570 CV_Error( CV_StsNullPtr, "NULL filename" );
2572 fs = (CvFileStorage*)cvAlloc( sizeof(*fs) );
2573 memset( fs, 0, sizeof(*fs));
2575 fs->memstorage = cvCreateMemStorage( default_block_size );
2576 fs->dststorage = dststorage ? dststorage : fs->memstorage;
2578 int fnamelen = (int)strlen(filename);
2580 CV_Error( CV_StsError, "Empty filename" );
2582 fs->filename = (char*)cvMemStorageAlloc( fs->memstorage, fnamelen+1 );
2583 strcpy( fs->filename, filename );
2585 char* dot_pos = strrchr(fs->filename, '.');
2586 char compression = '\0';
2588 if( dot_pos && dot_pos[1] == 'g' && dot_pos[2] == 'z' &&
2589 (dot_pos[3] == '\0' || (isdigit(dot_pos[3]) && dot_pos[4] == '\0')) )
2592 CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" );
2594 compression = dot_pos[3];
2596 dot_pos[3] = '\0', fnamelen--;
2599 fs->flags = CV_FILE_STORAGE;
2600 fs->write_mode = (flags & 3) != 0;
2604 fs->file = fopen(fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" );
2610 char mode[] = { fs->write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' };
2611 fs->gzfile = gzopen(fs->filename, mode);
2617 fs->struct_indent = 0;
2618 fs->struct_flags = 0;
2619 fs->wrap_margin = 71;
2621 if( fs->write_mode )
2623 // we use factor=6 for XML (the longest characters (' and ") are encoded with 6 bytes (' and ")
2624 // and factor=4 for YAML ( as we use 4 bytes for non ASCII characters (e.g. \xAB))
2625 int buf_size = CV_FS_MAX_LEN*(fs->is_xml ? 6 : 4) + 1024;
2627 dot_pos = fs->filename + fnamelen - (isGZ ? 7 : 4);
2628 fs->is_xml = dot_pos > fs->filename && (memcmp( dot_pos, ".xml", 4) == 0 ||
2629 memcmp(dot_pos, ".XML", 4) == 0 || memcmp(dot_pos, ".Xml", 4) == 0);
2632 fseek( fs->file, 0, SEEK_END );
2634 fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->is_xml ?
2635 sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage );
2637 fs->struct_indent = 0;
2638 fs->struct_flags = CV_NODE_EMPTY;
2639 fs->buffer_start = fs->buffer = (char*)cvAlloc( buf_size + 1024 );
2640 fs->buffer_end = fs->buffer_start + buf_size;
2643 int file_size = fs->file ? (int)ftell( fs->file ) : 0;
2644 fs->strstorage = cvCreateChildMemStorage( fs->memstorage );
2645 if( !append || file_size == 0 )
2647 icvPuts( fs, "<?xml version=\"1.0\"?>\n" );
2648 icvPuts( fs, "<opencv_storage>\n" );
2652 int xml_buf_size = 1 << 10;
2653 char substr[] = "</opencv_storage>";
2654 int last_occurence = -1;
2655 xml_buf_size = MIN(xml_buf_size, file_size);
2656 fseek( fs->file, -xml_buf_size, SEEK_END );
2657 xml_buf = (char*)cvAlloc( xml_buf_size+2 );
2658 // find the last occurence of </opencv_storage>
2661 int line_offset = ftell( fs->file );
2662 char* ptr0 = icvGets( fs, xml_buf, xml_buf_size ), *ptr;
2668 ptr = strstr( ptr, substr );
2671 last_occurence = line_offset + (int)(ptr - ptr0);
2672 ptr += strlen(substr);
2675 if( last_occurence < 0 )
2676 CV_Error( CV_StsError, "Could not find </opencv_storage> in the end of file.\n" );
2678 fs->file = fopen( fs->filename, "r+t" );
2679 fseek( fs->file, last_occurence, SEEK_SET );
2680 // replace the last "</opencv_storage>" with " <!-- resumed -->", which has the same length
2681 icvPuts( fs, " <!-- resumed -->" );
2682 fseek( fs->file, 0, SEEK_END );
2683 icvPuts( fs, "\n" );
2685 fs->start_write_struct = icvXMLStartWriteStruct;
2686 fs->end_write_struct = icvXMLEndWriteStruct;
2687 fs->write_int = icvXMLWriteInt;
2688 fs->write_real = icvXMLWriteReal;
2689 fs->write_string = icvXMLWriteString;
2690 fs->write_comment = icvXMLWriteComment;
2691 fs->start_next_stream = icvXMLStartNextStream;
2696 icvPuts( fs, "%YAML:1.0\n" );
2698 icvPuts( fs, "...\n---\n" );
2699 fs->start_write_struct = icvYMLStartWriteStruct;
2700 fs->end_write_struct = icvYMLEndWriteStruct;
2701 fs->write_int = icvYMLWriteInt;
2702 fs->write_real = icvYMLWriteReal;
2703 fs->write_string = icvYMLWriteString;
2704 fs->write_comment = icvYMLWriteComment;
2705 fs->start_next_stream = icvYMLStartNextStream;
2710 int buf_size = 1 << 20;
2711 const char* yaml_signature = "%YAML:";
2713 icvGets( fs, buf, sizeof(buf)-2 );
2714 fs->is_xml = strncmp( buf, yaml_signature, strlen(yaml_signature) ) != 0;
2718 fseek( fs->file, 0, SEEK_END );
2719 buf_size = ftell( fs->file );
2720 buf_size = MIN( buf_size, (1 << 20) );
2721 buf_size = MAX( buf_size, CV_FS_MAX_LEN*2 + 1024 );
2725 fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash),
2726 sizeof(CvStringHashNode), fs->memstorage, 256 );
2728 fs->roots = cvCreateSeq( 0, sizeof(CvSeq),
2729 sizeof(CvFileNode), fs->memstorage );
2731 fs->buffer = fs->buffer_start = (char*)cvAlloc( buf_size + 256 );
2732 fs->buffer_end = fs->buffer_start + buf_size;
2733 fs->buffer[0] = '\n';
2734 fs->buffer[1] = '\0';
2736 //mode = cvGetErrMode();
2737 //cvSetErrMode( CV_ErrModeSilent );
2742 //cvSetErrMode( mode );
2744 // release resources that we do not need anymore
2745 cvFree( &fs->buffer_start );
2746 fs->buffer = fs->buffer_end = 0;
2751 if( cvGetErrStatus() < 0 || (!fs->file && !fs->gzfile) )
2753 cvReleaseFileStorage( &fs );
2755 else if( !fs->write_mode )
2767 cvStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2768 const char* type_name, CvAttrList /*attributes*/ )
2770 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2771 fs->start_write_struct( fs, key, struct_flags, type_name );
2776 cvEndWriteStruct( CvFileStorage* fs )
2778 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2779 fs->end_write_struct( fs );
2784 cvWriteInt( CvFileStorage* fs, const char* key, int value )
2786 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2787 fs->write_int( fs, key, value );
2792 cvWriteReal( CvFileStorage* fs, const char* key, double value )
2794 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2795 fs->write_real( fs, key, value );
2800 cvWriteString( CvFileStorage* fs, const char* key, const char* value, int quote )
2802 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2803 fs->write_string( fs, key, value, quote );
2808 cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2810 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2811 fs->write_comment( fs, comment, eol_comment );
2816 cvStartNextStream( CvFileStorage* fs )
2818 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2819 fs->start_next_stream( fs );
2823 static const char icvTypeSymbol[] = "ucwsifdr";
2824 #define CV_FS_MAX_FMT_PAIRS 128
2827 icvEncodeFormat( int elem_type, char* dt )
2829 sprintf( dt, "%d%c", CV_MAT_CN(elem_type), icvTypeSymbol[CV_MAT_DEPTH(elem_type)] );
2830 return dt + ( dt[2] == '\0' && dt[0] == '1' );
2834 icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len )
2836 int fmt_pair_count = 0;
2837 int i = 0, k = 0, len = dt ? (int)strlen(dt) : 0;
2842 assert( fmt_pairs != 0 && max_len > 0 );
2846 for( ; k < len; k++ )
2852 int count = c - '0';
2853 if( isdigit(dt[k+1]) )
2856 count = (int)strtol( dt+k, &endptr, 10 );
2857 k = (int)(endptr - dt) - 1;
2861 CV_Error( CV_StsBadArg, "Invalid data type specification" );
2863 fmt_pairs[i] = count;
2867 const char* pos = strchr( icvTypeSymbol, c );
2869 CV_Error( CV_StsBadArg, "Invalid data type specification" );
2870 if( fmt_pairs[i] == 0 )
2872 fmt_pairs[i+1] = (int)(pos - icvTypeSymbol);
2873 if( i > 0 && fmt_pairs[i+1] == fmt_pairs[i-1] )
2874 fmt_pairs[i-2] += fmt_pairs[i];
2879 CV_Error( CV_StsBadArg, "Too long data type specification" );
2885 fmt_pair_count = i/2;
2886 return fmt_pair_count;
2891 icvCalcElemSize( const char* dt, int initial_size )
2894 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
2897 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
2898 fmt_pair_count *= 2;
2899 for( i = 0, size = initial_size; i < fmt_pair_count; i += 2 )
2901 comp_size = CV_ELEM_SIZE(fmt_pairs[i+1]);
2902 size = cvAlign( size, comp_size );
2903 size += comp_size * fmt_pairs[i];
2905 if( initial_size == 0 )
2907 comp_size = CV_ELEM_SIZE(fmt_pairs[1]);
2908 size = cvAlign( size, comp_size );
2915 icvDecodeSimpleFormat( const char* dt )
2918 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
2920 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
2921 if( fmt_pair_count != 1 || fmt_pairs[0] > 4 )
2922 CV_Error( CV_StsError, "Too complex format for the matrix" );
2924 elem_type = CV_MAKETYPE( fmt_pairs[1], fmt_pairs[0] );
2931 cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
2933 const char* data0 = (const char*)_data;
2935 int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k, fmt_pair_count;
2938 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
2941 CV_Error( CV_StsNullPtr, "Null data pointer" );
2944 CV_Error( CV_StsOutOfRange, "Negative number of elements" );
2946 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
2951 if( fmt_pair_count == 1 )
2953 fmt_pairs[0] *= len;
2959 for( k = 0; k < fmt_pair_count; k++ )
2961 int i, count = fmt_pairs[k*2];
2962 int elem_type = fmt_pairs[k*2+1];
2963 int elem_size = CV_ELEM_SIZE(elem_type);
2964 const char* data, *ptr;
2966 offset = cvAlign( offset, elem_size );
2967 data = data0 + offset;
2969 for( i = 0; i < count; i++ )
2974 ptr = icv_itoa( *(uchar*)data, buf, 10 );
2978 ptr = icv_itoa( *(char*)data, buf, 10 );
2982 ptr = icv_itoa( *(ushort*)data, buf, 10 );
2983 data += sizeof(ushort);
2986 ptr = icv_itoa( *(short*)data, buf, 10 );
2987 data += sizeof(short);
2990 ptr = icv_itoa( *(int*)data, buf, 10 );
2991 data += sizeof(int);
2994 ptr = icvFloatToString( buf, *(float*)data );
2995 data += sizeof(float);
2998 ptr = icvDoubleToString( buf, *(double*)data );
2999 data += sizeof(double);
3001 case CV_USRTYPE1: /* reference */
3002 ptr = icv_itoa( (int)*(size_t*)data, buf, 10 );
3003 data += sizeof(size_t);
3012 int buf_len = (int)strlen(ptr);
3013 icvXMLWriteScalar( fs, 0, ptr, buf_len );
3016 icvYMLWrite( fs, 0, ptr );
3019 offset = (int)(data - data0);
3026 cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader )
3029 CV_CHECK_FILE_STORAGE( fs );
3031 if( !src || !reader )
3032 CV_Error( CV_StsNullPtr, "Null pointer to source file node or reader" );
3034 node_type = CV_NODE_TYPE(src->tag);
3035 if( node_type == CV_NODE_INT || node_type == CV_NODE_REAL )
3037 // emulate reading from 1-element sequence
3038 reader->ptr = (schar*)src;
3039 reader->block_max = reader->ptr + sizeof(*src)*2;
3040 reader->block_min = reader->ptr;
3043 else if( node_type == CV_NODE_SEQ )
3045 cvStartReadSeq( src->data.seq, reader, 0 );
3047 else if( node_type == CV_NODE_NONE )
3049 memset( reader, 0, sizeof(*reader) );
3052 CV_Error( CV_StsBadArg, "The file node should be a numerical scalar or a sequence" );
3057 cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,
3058 int len, void* _data, const char* dt )
3060 char* data0 = (char*)_data;
3061 int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k = 0, fmt_pair_count;
3062 int i = 0, offset = 0, count = 0;
3064 CV_CHECK_FILE_STORAGE( fs );
3066 if( !reader || !data0 )
3067 CV_Error( CV_StsNullPtr, "Null pointer to reader or destination array" );
3069 if( !reader->seq && len != 1 )
3070 CV_Error( CV_StsBadSize, "The readed sequence is a scalar, thus len must be 1" );
3072 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
3076 for( k = 0; k < fmt_pair_count; k++ )
3078 int elem_type = fmt_pairs[k*2+1];
3079 int elem_size = CV_ELEM_SIZE(elem_type);
3082 count = fmt_pairs[k*2];
3083 offset = cvAlign( offset, elem_size );
3084 data = data0 + offset;
3086 for( i = 0; i < count; i++ )
3088 CvFileNode* node = (CvFileNode*)reader->ptr;
3089 if( CV_NODE_IS_INT(node->tag) )
3091 int ival = node->data.i;
3096 *(uchar*)data = CV_CAST_8U(ival);
3100 *(char*)data = CV_CAST_8S(ival);
3104 *(ushort*)data = CV_CAST_16U(ival);
3105 data += sizeof(ushort);
3108 *(short*)data = CV_CAST_16S(ival);
3109 data += sizeof(short);
3113 data += sizeof(int);
3116 *(float*)data = (float)ival;
3117 data += sizeof(float);
3120 *(double*)data = (double)ival;
3121 data += sizeof(double);
3123 case CV_USRTYPE1: /* reference */
3124 *(size_t*)data = ival;
3125 data += sizeof(size_t);
3132 else if( CV_NODE_IS_REAL(node->tag) )
3134 double fval = node->data.f;
3140 ival = cvRound(fval);
3141 *(uchar*)data = CV_CAST_8U(ival);
3145 ival = cvRound(fval);
3146 *(char*)data = CV_CAST_8S(ival);
3150 ival = cvRound(fval);
3151 *(ushort*)data = CV_CAST_16U(ival);
3152 data += sizeof(ushort);
3155 ival = cvRound(fval);
3156 *(short*)data = CV_CAST_16S(ival);
3157 data += sizeof(short);
3160 ival = cvRound(fval);
3162 data += sizeof(int);
3165 *(float*)data = (float)fval;
3166 data += sizeof(float);
3169 *(double*)data = fval;
3170 data += sizeof(double);
3172 case CV_USRTYPE1: /* reference */
3173 ival = cvRound(fval);
3174 *(size_t*)data = ival;
3175 data += sizeof(size_t);
3183 CV_Error( CV_StsError,
3184 "The sequence element is not a numerical scalar" );
3186 CV_NEXT_SEQ_ELEM( sizeof(CvFileNode), *reader );
3191 offset = (int)(data - data0);
3196 if( i != count - 1 || k != fmt_pair_count - 1 )
3197 CV_Error( CV_StsBadSize,
3198 "The sequence slice does not fit an integer number of records" );
3201 reader->ptr -= sizeof(CvFileNode);
3206 cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,
3207 void* data, const char* dt )
3212 CV_Error( CV_StsNullPtr, "Null pointers to source file node or destination array" );
3214 cvStartReadRawData( fs, src, &reader );
3215 cvReadRawDataSlice( fs, &reader, CV_NODE_IS_SEQ(src->tag) ?
3216 src->data.seq->total : 1, data, dt );
3221 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node );
3224 icvWriteCollection( CvFileStorage* fs, const CvFileNode* node )
3226 int i, total = node->data.seq->total;
3227 int elem_size = node->data.seq->elem_size;
3228 int is_map = CV_NODE_IS_MAP(node->tag);
3231 cvStartReadSeq( node->data.seq, &reader, 0 );
3233 for( i = 0; i < total; i++ )
3235 CvFileMapNode* elem = (CvFileMapNode*)reader.ptr;
3236 if( !is_map || CV_IS_SET_ELEM(elem) )
3238 const char* name = is_map ? elem->key->str.ptr : 0;
3239 icvWriteFileNode( fs, name, &elem->value );
3241 CV_NEXT_SEQ_ELEM( elem_size, reader );
3246 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node )
3248 switch( CV_NODE_TYPE(node->tag) )
3251 fs->write_int( fs, name, node->data.i );
3254 fs->write_real( fs, name, node->data.f );
3257 fs->write_string( fs, name, node->data.str.ptr, 0 );
3261 fs->start_write_struct( fs, name, CV_NODE_TYPE(node->tag) +
3262 (CV_NODE_SEQ_IS_SIMPLE(node->data.seq) ? CV_NODE_FLOW : 0),
3263 node->info ? node->info->type_name : 0 );
3264 icvWriteCollection( fs, node );
3265 fs->end_write_struct( fs );
3268 fs->start_write_struct( fs, name, CV_NODE_SEQ, 0 );
3269 fs->end_write_struct( fs );
3272 CV_Error( CV_StsBadFlag, "Unknown type of file node" );
3278 cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,
3279 const CvFileNode* node, int embed )
3281 CvFileStorage* dst = 0;
3282 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
3287 if( CV_NODE_IS_COLLECTION(node->tag) && embed )
3289 icvWriteCollection( fs, node );
3293 icvWriteFileNode( fs, new_node_name, node );
3296 int i, stream_count;
3297 stream_count = fs->roots->total;
3298 for( i = 0; i < stream_count; i++ )
3300 CvFileNode* node = (CvFileNode*)cvGetSeqElem( fs->roots, i, 0 );
3301 icvDumpCollection( dst, node );
3302 if( i < stream_count - 1 )
3303 dst->start_next_stream( dst );
3305 cvReleaseFileStorage( &dst );
3310 cvGetFileNodeName( const CvFileNode* file_node )
3312 return file_node && CV_NODE_HAS_NAME(file_node->tag) ?
3313 ((CvFileMapNode*)file_node)->key->str.ptr : 0;
3316 /****************************************************************************************\
3317 * Reading/Writing etc. for standard types *
3318 \****************************************************************************************/
3320 /*#define CV_TYPE_NAME_MAT "opencv-matrix"
3321 #define CV_TYPE_NAME_MATND "opencv-nd-matrix"
3322 #define CV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix"
3323 #define CV_TYPE_NAME_IMAGE "opencv-image"
3324 #define CV_TYPE_NAME_SEQ "opencv-sequence"
3325 #define CV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree"
3326 #define CV_TYPE_NAME_GRAPH "opencv-graph"*/
3328 /******************************* CvMat ******************************/
3331 icvIsMat( const void* ptr )
3333 return CV_IS_MAT_HDR(ptr);
3337 icvWriteMat( CvFileStorage* fs, const char* name,
3338 const void* struct_ptr, CvAttrList /*attr*/ )
3340 const CvMat* mat = (const CvMat*)struct_ptr;
3345 assert( CV_IS_MAT(mat) );
3347 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT );
3348 cvWriteInt( fs, "rows", mat->rows );
3349 cvWriteInt( fs, "cols", mat->cols );
3350 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3351 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3353 size = cvGetSize(mat);
3354 if( CV_IS_MAT_CONT(mat->type) )
3356 size.width *= size.height;
3360 for( y = 0; y < size.height; y++ )
3361 cvWriteRawData( fs, mat->data.ptr + y*mat->step, size.width, dt );
3362 cvEndWriteStruct( fs );
3363 cvEndWriteStruct( fs );
3368 icvFileNodeSeqLen( CvFileNode* node )
3370 return CV_NODE_IS_COLLECTION(node->tag) ? node->data.seq->total :
3371 CV_NODE_TYPE(node->tag) != CV_NODE_NONE;
3376 icvReadMat( CvFileStorage* fs, CvFileNode* node )
3382 int rows, cols, elem_type;
3384 rows = cvReadIntByName( fs, node, "rows", 0 );
3385 cols = cvReadIntByName( fs, node, "cols", 0 );
3386 dt = cvReadStringByName( fs, node, "dt", 0 );
3388 if( rows == 0 || cols == 0 || dt == 0 )
3389 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3391 elem_type = icvDecodeSimpleFormat( dt );
3393 data = cvGetFileNodeByName( fs, node, "data" );
3395 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3397 if( icvFileNodeSeqLen( data ) != rows*cols*CV_MAT_CN(elem_type) )
3398 CV_Error( CV_StsUnmatchedSizes,
3399 "The matrix size does not match to the number of stored elements" );
3401 mat = cvCreateMat( rows, cols, elem_type );
3402 cvReadRawData( fs, data, mat->data.ptr, dt );
3409 /******************************* CvMatND ******************************/
3412 icvIsMatND( const void* ptr )
3414 return CV_IS_MATND(ptr);
3419 icvWriteMatND( CvFileStorage* fs, const char* name,
3420 const void* struct_ptr, CvAttrList /*attr*/ )
3422 void* mat = (void*)struct_ptr;
3424 CvNArrayIterator iterator;
3425 int dims, sizes[CV_MAX_DIM];
3428 assert( CV_IS_MATND(mat) );
3430 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MATND );
3431 dims = cvGetDims( mat, sizes );
3432 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3433 cvWriteRawData( fs, sizes, dims, "i" );
3434 cvEndWriteStruct( fs );
3435 cvWriteString( fs, "dt", icvEncodeFormat( cvGetElemType(mat), dt ), 0 );
3436 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3438 cvInitNArrayIterator( 1, &mat, 0, &stub, &iterator );
3441 cvWriteRawData( fs, iterator.ptr[0], iterator.size.width, dt );
3442 while( cvNextNArraySlice( &iterator ));
3443 cvEndWriteStruct( fs );
3444 cvEndWriteStruct( fs );
3449 icvReadMatND( CvFileStorage* fs, CvFileNode* node )
3455 CvFileNode* sizes_node;
3456 int sizes[CV_MAX_DIM], dims, elem_type;
3459 sizes_node = cvGetFileNodeByName( fs, node, "sizes" );
3460 dt = cvReadStringByName( fs, node, "dt", 0 );
3462 if( !sizes_node || !dt )
3463 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3465 dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
3466 CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
3468 if( dims <= 0 || dims > CV_MAX_DIM )
3469 CV_Error( CV_StsParseError, "Could not determine the matrix dimensionality" );
3471 cvReadRawData( fs, sizes_node, sizes, "i" );
3472 elem_type = icvDecodeSimpleFormat( dt );
3474 data = cvGetFileNodeByName( fs, node, "data" );
3476 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3478 for( total_size = CV_MAT_CN(elem_type), i = 0; i < dims; i++ )
3479 total_size *= sizes[i];
3481 if( icvFileNodeSeqLen( data ) != total_size )
3482 CV_Error( CV_StsUnmatchedSizes,
3483 "The matrix size does not match to the number of stored elements" );
3485 mat = cvCreateMatND( dims, sizes, elem_type );
3486 cvReadRawData( fs, data, mat->data.ptr, dt );
3493 /******************************* CvSparseMat ******************************/
3496 icvIsSparseMat( const void* ptr )
3498 return CV_IS_SPARSE_MAT(ptr);
3503 icvSortIdxCmpFunc( const void* _a, const void* _b, void* userdata )
3505 int i, dims = *(int*)userdata;
3506 const int* a = *(const int**)_a;
3507 const int* b = *(const int**)_b;
3509 for( i = 0; i < dims; i++ )
3511 int delta = a[i] - b[i];
3521 icvWriteSparseMat( CvFileStorage* fs, const char* name,
3522 const void* struct_ptr, CvAttrList /*attr*/ )
3524 CvMemStorage* memstorage = 0;
3525 const CvSparseMat* mat = (const CvSparseMat*)struct_ptr;
3526 CvSparseMatIterator iterator;
3534 assert( CV_IS_SPARSE_MAT(mat) );
3536 memstorage = cvCreateMemStorage();
3538 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SPARSE_MAT );
3539 dims = cvGetDims( mat, 0 );
3541 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3542 cvWriteRawData( fs, mat->size, dims, "i" );
3543 cvEndWriteStruct( fs );
3544 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3545 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3547 elements = cvCreateSeq( CV_SEQ_ELTYPE_PTR, sizeof(CvSeq), sizeof(int*), memstorage );
3549 node = cvInitSparseMatIterator( mat, &iterator );
3552 int* idx = CV_NODE_IDX( mat, node );
3553 cvSeqPush( elements, &idx );
3554 node = cvGetNextSparseNode( &iterator );
3557 cvSeqSort( elements, icvSortIdxCmpFunc, &dims );
3558 cvStartReadSeq( elements, &reader, 0 );
3560 for( i = 0; i < elements->total; i++ )
3566 CV_READ_SEQ_ELEM( idx, reader );
3569 for( ; idx[k] == prev_idx[k]; k++ )
3572 fs->write_int( fs, 0, k - dims + 1 );
3574 for( ; k < dims; k++ )
3575 fs->write_int( fs, 0, idx[k] );
3578 node = (CvSparseNode*)((uchar*)idx - mat->idxoffset );
3579 val = CV_NODE_VAL( mat, node );
3581 cvWriteRawData( fs, val, 1, dt );
3584 cvEndWriteStruct( fs );
3585 cvEndWriteStruct( fs );
3586 cvReleaseMemStorage( &memstorage );
3591 icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
3597 CvFileNode* sizes_node;
3601 int* sizes = 0, dims, elem_type, cn;
3604 sizes_node = cvGetFileNodeByName( fs, node, "sizes" );
3605 dt = cvReadStringByName( fs, node, "dt", 0 );
3607 if( !sizes_node || !dt )
3608 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3610 dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
3611 CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
3613 if( dims <= 0 || dims > CV_MAX_DIM_HEAP )
3614 CV_Error( CV_StsParseError, "Could not determine sparse matrix dimensionality" );
3616 sizes = (int*)cvStackAlloc( dims*sizeof(sizes[0]));
3617 cvReadRawData( fs, sizes_node, sizes, "i" );
3618 elem_type = icvDecodeSimpleFormat( dt );
3620 data = cvGetFileNodeByName( fs, node, "data" );
3621 if( !data || !CV_NODE_IS_SEQ(data->tag) )
3622 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3624 mat = cvCreateSparseMat( dims, sizes, elem_type );
3626 cn = CV_MAT_CN(elem_type);
3627 idx = (int*)alloca( dims*sizeof(idx[0]) );
3628 elements = data->data.seq;
3629 cvStartReadRawData( fs, data, &reader );
3631 for( i = 0; i < elements->total; )
3633 CvFileNode* elem = (CvFileNode*)reader.ptr;
3636 if( !CV_NODE_IS_INT(elem->tag ))
3637 CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" );
3639 if( i > 0 && k >= 0 )
3647 for( ; k < dims; k++ )
3649 CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
3651 elem = (CvFileNode*)reader.ptr;
3652 if( !CV_NODE_IS_INT(elem->tag ) || elem->data.i < 0 )
3653 CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" );
3654 idx[k] = elem->data.i;
3657 CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
3659 val = cvPtrND( mat, idx, 0, 1, 0 );
3660 cvReadRawDataSlice( fs, &reader, cn, val, dt );
3669 /******************************* IplImage ******************************/
3672 icvIsImage( const void* ptr )
3674 return CV_IS_IMAGE_HDR(ptr);
3678 icvWriteImage( CvFileStorage* fs, const char* name,
3679 const void* struct_ptr, CvAttrList /*attr*/ )
3681 const IplImage* image = (const IplImage*)struct_ptr;
3682 char dt_buf[16], *dt;
3686 assert( CV_IS_IMAGE(image) );
3688 if( image->dataOrder == IPL_DATA_ORDER_PLANE )
3689 CV_Error( CV_StsUnsupportedFormat,
3690 "Images with planar data layout are not supported" );
3692 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_IMAGE );
3693 cvWriteInt( fs, "width", image->width );
3694 cvWriteInt( fs, "height", image->height );
3695 cvWriteString( fs, "origin", image->origin == IPL_ORIGIN_TL
3696 ? "top-left" : "bottom-left", 0 );
3697 cvWriteString( fs, "layout", image->dataOrder == IPL_DATA_ORDER_PLANE
3698 ? "planar" : "interleaved", 0 );
3701 cvStartWriteStruct( fs, "roi", CV_NODE_MAP + CV_NODE_FLOW );
3702 cvWriteInt( fs, "x", image->roi->xOffset );
3703 cvWriteInt( fs, "y", image->roi->yOffset );
3704 cvWriteInt( fs, "width", image->roi->width );
3705 cvWriteInt( fs, "height", image->roi->height );
3706 cvWriteInt( fs, "coi", image->roi->coi );
3707 cvEndWriteStruct( fs );
3710 depth = IplToCvDepth(image->depth);
3711 sprintf( dt_buf, "%d%c", image->nChannels, icvTypeSymbol[depth] );
3712 dt = dt_buf + (dt_buf[2] == '\0' && dt_buf[0] == '1');
3713 cvWriteString( fs, "dt", dt, 0 );
3715 size = cvSize(image->width, image->height);
3716 if( size.width*image->nChannels*CV_ELEM_SIZE(depth) == image->widthStep )
3718 size.width *= size.height;
3722 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3723 for( y = 0; y < size.height; y++ )
3724 cvWriteRawData( fs, image->imageData + y*image->widthStep, size.width, dt );
3725 cvEndWriteStruct( fs );
3726 cvEndWriteStruct( fs );
3731 icvReadImage( CvFileStorage* fs, CvFileNode* node )
3737 CvFileNode* roi_node;
3740 int y, width, height, elem_type, coi, depth;
3741 const char* origin, *data_order;
3743 width = cvReadIntByName( fs, node, "width", 0 );
3744 height = cvReadIntByName( fs, node, "height", 0 );
3745 dt = cvReadStringByName( fs, node, "dt", 0 );
3746 origin = cvReadStringByName( fs, node, "origin", 0 );
3748 if( width == 0 || height == 0 || dt == 0 || origin == 0 )
3749 CV_Error( CV_StsError, "Some of essential image attributes are absent" );
3751 elem_type = icvDecodeSimpleFormat( dt );
3752 data_order = cvReadStringByName( fs, node, "layout", "interleaved" );
3753 if( strcmp( data_order, "interleaved" ) != 0 )
3754 CV_Error( CV_StsError, "Only interleaved images can be read" );
3756 data = cvGetFileNodeByName( fs, node, "data" );
3758 CV_Error( CV_StsError, "The image data is not found in file storage" );
3760 if( icvFileNodeSeqLen( data ) != width*height*CV_MAT_CN(elem_type) )
3761 CV_Error( CV_StsUnmatchedSizes,
3762 "The matrix size does not match to the number of stored elements" );
3764 depth = cvIplDepth(elem_type);
3765 image = cvCreateImage( cvSize(width,height), depth, CV_MAT_CN(elem_type) );
3767 roi_node = cvGetFileNodeByName( fs, node, "roi" );
3770 roi.x = cvReadIntByName( fs, roi_node, "x", 0 );
3771 roi.y = cvReadIntByName( fs, roi_node, "y", 0 );
3772 roi.width = cvReadIntByName( fs, roi_node, "width", 0 );
3773 roi.height = cvReadIntByName( fs, roi_node, "height", 0 );
3774 coi = cvReadIntByName( fs, roi_node, "coi", 0 );
3776 cvSetImageROI( image, roi );
3777 cvSetImageCOI( image, coi );
3780 if( width*CV_ELEM_SIZE(elem_type) == image->widthStep )
3786 width *= CV_MAT_CN(elem_type);
3787 cvStartReadRawData( fs, data, &reader );
3788 for( y = 0; y < height; y++ )
3790 cvReadRawDataSlice( fs, &reader, width,
3791 image->imageData + y*image->widthStep, dt );
3799 /******************************* CvSeq ******************************/
3802 icvIsSeq( const void* ptr )
3804 return CV_IS_SEQ(ptr);
3809 icvReleaseSeq( void** ptr )
3812 CV_Error( CV_StsNullPtr, "NULL double pointer" );
3813 *ptr = 0; // it's impossible now to release seq, so just clear the pointer
3818 icvCloneSeq( const void* ptr )
3820 return cvSeqSlice( (CvSeq*)ptr, CV_WHOLE_SEQ,
3821 0 /* use the same storage as for the original sequence */, 1 );
3826 icvWriteHeaderData( CvFileStorage* fs, const CvSeq* seq,
3827 CvAttrList* attr, int initial_header_size )
3829 char header_dt_buf[128];
3830 const char* header_dt = cvAttrValue( attr, "header_dt" );
3835 dt_header_size = icvCalcElemSize( header_dt, initial_header_size );
3836 if( dt_header_size > seq->header_size )
3837 CV_Error( CV_StsUnmatchedSizes,
3838 "The size of header calculated from \"header_dt\" is greater than header_size" );
3840 else if( seq->header_size > initial_header_size )
3842 if( CV_IS_SEQ(seq) && CV_IS_SEQ_POINT_SET(seq) &&
3843 seq->header_size == sizeof(CvPoint2DSeq) &&
3844 seq->elem_size == sizeof(int)*2 )
3846 CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
3848 cvStartWriteStruct( fs, "rect", CV_NODE_MAP + CV_NODE_FLOW );
3849 cvWriteInt( fs, "x", point_seq->rect.x );
3850 cvWriteInt( fs, "y", point_seq->rect.y );
3851 cvWriteInt( fs, "width", point_seq->rect.width );
3852 cvWriteInt( fs, "height", point_seq->rect.height );
3853 cvEndWriteStruct( fs );
3854 cvWriteInt( fs, "color", point_seq->color );
3856 else if( CV_IS_SEQ(seq) && CV_IS_SEQ_CHAIN(seq) &&
3857 CV_MAT_TYPE(seq->flags) == CV_8UC1 )
3859 CvChain* chain = (CvChain*)seq;
3861 cvStartWriteStruct( fs, "origin", CV_NODE_MAP + CV_NODE_FLOW );
3862 cvWriteInt( fs, "x", chain->origin.x );
3863 cvWriteInt( fs, "y", chain->origin.y );
3864 cvEndWriteStruct( fs );
3868 unsigned extra_size = seq->header_size - initial_header_size;
3869 // a heuristic to provide nice defaults for sequences of int's & float's
3870 if( extra_size % sizeof(int) == 0 )
3871 sprintf( header_dt_buf, "%ui", (unsigned)(extra_size/sizeof(int)) );
3873 sprintf( header_dt_buf, "%uu", extra_size );
3874 header_dt = header_dt_buf;
3880 cvWriteString( fs, "header_dt", header_dt, 0 );
3881 cvStartWriteStruct( fs, "header_user_data", CV_NODE_SEQ + CV_NODE_FLOW );
3882 cvWriteRawData( fs, (uchar*)seq + sizeof(CvSeq), 1, header_dt );
3883 cvEndWriteStruct( fs );
3889 icvGetFormat( const CvSeq* seq, const char* dt_key, CvAttrList* attr,
3890 int initial_elem_size, char* dt_buf )
3893 dt = (char*)cvAttrValue( attr, dt_key );
3898 dt_elem_size = icvCalcElemSize( dt, initial_elem_size );
3899 if( dt_elem_size != seq->elem_size )
3900 CV_Error( CV_StsUnmatchedSizes,
3901 "The size of element calculated from \"dt\" and "
3902 "the elem_size do not match" );
3904 else if( CV_MAT_TYPE(seq->flags) != 0 || seq->elem_size == 1 )
3906 int align = CV_MAT_DEPTH(seq->flags) == CV_64F ? sizeof(double) : sizeof(size_t);
3907 int full_elem_size = cvAlign(CV_ELEM_SIZE(seq->flags) + initial_elem_size, align);
3908 if( seq->elem_size != full_elem_size )
3909 CV_Error( CV_StsUnmatchedSizes,
3910 "Size of sequence element (elem_size) is inconsistent with seq->flags" );
3911 dt = icvEncodeFormat( CV_MAT_TYPE(seq->flags), dt_buf );
3913 else if( seq->elem_size > initial_elem_size )
3915 unsigned extra_elem_size = seq->elem_size - initial_elem_size;
3916 // a heuristic to provide nice defaults for sequences of int's & float's
3917 if( extra_elem_size % sizeof(int) == 0 )
3918 sprintf( dt_buf, "%ui", (unsigned)(extra_elem_size/sizeof(int)) );
3920 sprintf( dt_buf, "%uu", extra_elem_size );
3929 icvWriteSeq( CvFileStorage* fs, const char* name,
3930 const void* struct_ptr,
3931 CvAttrList attr, int level )
3933 const CvSeq* seq = (CvSeq*)struct_ptr;
3936 char dt_buf[128], *dt;
3938 assert( CV_IS_SEQ( seq ));
3939 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ );
3942 cvWriteInt( fs, "level", level );
3944 sprintf( buf, "%08x", seq->flags );
3945 cvWriteString( fs, "flags", buf, 1 );
3946 cvWriteInt( fs, "count", seq->total );
3947 dt = icvGetFormat( seq, "dt", &attr, 0, dt_buf );
3948 cvWriteString( fs, "dt", dt, 0 );
3950 icvWriteHeaderData( fs, seq, &attr, sizeof(CvSeq) );
3951 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3953 for( block = seq->first; block; block = block->next )
3955 cvWriteRawData( fs, block->data, block->count, dt );
3956 if( block == seq->first->prev )
3959 cvEndWriteStruct( fs );
3960 cvEndWriteStruct( fs );
3965 icvWriteSeqTree( CvFileStorage* fs, const char* name,
3966 const void* struct_ptr, CvAttrList attr )
3968 const CvSeq* seq = (CvSeq*)struct_ptr;
3969 const char* recursive_value = cvAttrValue( &attr, "recursive" );
3970 int is_recursive = recursive_value &&
3971 strcmp(recursive_value,"0") != 0 &&
3972 strcmp(recursive_value,"false") != 0 &&
3973 strcmp(recursive_value,"False") != 0 &&
3974 strcmp(recursive_value,"FALSE") != 0;
3976 assert( CV_IS_SEQ( seq ));
3980 icvWriteSeq( fs, name, seq, attr, -1 );
3984 CvTreeNodeIterator tree_iterator;
3986 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ_TREE );
3987 cvStartWriteStruct( fs, "sequences", CV_NODE_SEQ );
3988 cvInitTreeNodeIterator( &tree_iterator, seq, INT_MAX );
3992 if( !tree_iterator.node )
3994 icvWriteSeq( fs, 0, tree_iterator.node, attr, tree_iterator.level );
3995 cvNextTreeNode( &tree_iterator );
3998 cvEndWriteStruct( fs );
3999 cvEndWriteStruct( fs );
4005 icvReadSeq( CvFileStorage* fs, CvFileNode* node )
4010 CvFileNode *data, *header_node, *rect_node, *origin_node;
4013 int elem_size, header_size = sizeof(CvSeq);
4014 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
4015 int items_per_elem = 0;
4016 const char* flags_str;
4017 const char* header_dt;
4021 flags_str = cvReadStringByName( fs, node, "flags", 0 );
4022 total = cvReadIntByName( fs, node, "count", -1 );
4023 dt = cvReadStringByName( fs, node, "dt", 0 );
4025 if( !flags_str || total == -1 || !dt )
4026 CV_Error( CV_StsError, "Some of essential sequence attributes are absent" );
4028 flags = (int)strtol( flags_str, &endptr, 16 );
4029 if( endptr == flags_str || (flags & CV_MAGIC_MASK) != CV_SEQ_MAGIC_VAL )
4030 CV_Error( CV_StsError, "The sequence flags are invalid" );
4032 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4033 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4035 if( (header_dt != 0) ^ (header_node != 0) )
4036 CV_Error( CV_StsError,
4037 "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
4039 rect_node = cvGetFileNodeByName( fs, node, "rect" );
4040 origin_node = cvGetFileNodeByName( fs, node, "origin" );
4042 if( (header_node != 0) + (rect_node != 0) + (origin_node != 0) > 1 )
4043 CV_Error( CV_StsError, "Only one of \"header_user_data\", \"rect\" and \"origin\" tags may occur" );
4047 header_size = icvCalcElemSize( header_dt, header_size );
4049 else if( rect_node )
4050 header_size = sizeof(CvPoint2DSeq);
4051 else if( origin_node )
4052 header_size = sizeof(CvChain);
4054 elem_size = icvCalcElemSize( dt, 0 );
4055 seq = cvCreateSeq( flags, header_size, elem_size, fs->dststorage );
4059 cvReadRawData( fs, header_node, (char*)seq + sizeof(CvSeq), header_dt );
4061 else if( rect_node )
4063 CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
4064 point_seq->rect.x = cvReadIntByName( fs, rect_node, "x", 0 );
4065 point_seq->rect.y = cvReadIntByName( fs, rect_node, "y", 0 );
4066 point_seq->rect.width = cvReadIntByName( fs, rect_node, "width", 0 );
4067 point_seq->rect.height = cvReadIntByName( fs, rect_node, "height", 0 );
4068 point_seq->color = cvReadIntByName( fs, node, "color", 0 );
4070 else if( origin_node )
4072 CvChain* chain = (CvChain*)seq;
4073 chain->origin.x = cvReadIntByName( fs, origin_node, "x", 0 );
4074 chain->origin.y = cvReadIntByName( fs, origin_node, "y", 0 );
4077 cvSeqPushMulti( seq, 0, total, 0 );
4078 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4079 fmt_pair_count *= 2;
4080 for( i = 0; i < fmt_pair_count; i += 2 )
4081 items_per_elem += fmt_pairs[i];
4083 data = cvGetFileNodeByName( fs, node, "data" );
4085 CV_Error( CV_StsError, "The image data is not found in file storage" );
4087 if( icvFileNodeSeqLen( data ) != total*items_per_elem )
4088 CV_Error( CV_StsError, "The number of stored elements does not match to \"count\"" );
4090 cvStartReadRawData( fs, data, &reader );
4091 for( block = seq->first; block; block = block->next )
4093 int delta = block->count*items_per_elem;
4094 cvReadRawDataSlice( fs, &reader, delta, block->data, dt );
4095 if( block == seq->first->prev )
4105 icvReadSeqTree( CvFileStorage* fs, CvFileNode* node )
4108 CvFileNode *sequences_node = cvGetFileNodeByName( fs, node, "sequences" );
4112 CvSeq* prev_seq = 0;
4117 if( !sequences_node || !CV_NODE_IS_SEQ(sequences_node->tag) )
4118 CV_Error( CV_StsParseError,
4119 "opencv-sequence-tree instance should contain a field \"sequences\" that should be a sequence" );
4121 sequences = sequences_node->data.seq;
4122 total = sequences->total;
4124 cvStartReadSeq( sequences, &reader, 0 );
4125 for( i = 0; i < total; i++ )
4127 CvFileNode* elem = (CvFileNode*)reader.ptr;
4130 seq = (CvSeq*)cvRead( fs, elem );
4131 level = cvReadIntByName( fs, elem, "level", -1 );
4133 CV_Error( CV_StsParseError, "All the sequence tree nodes should contain \"level\" field" );
4136 if( level > prev_level )
4138 assert( level == prev_level + 1 );
4142 parent->v_next = seq;
4144 else if( level < prev_level )
4146 for( ; prev_level > level; prev_level-- )
4147 prev_seq = prev_seq->v_prev;
4148 parent = prev_seq->v_prev;
4150 seq->h_prev = prev_seq;
4152 prev_seq->h_next = seq;
4153 seq->v_prev = parent;
4156 CV_NEXT_SEQ_ELEM( sequences->elem_size, reader );
4163 /******************************* CvGraph ******************************/
4166 icvIsGraph( const void* ptr )
4168 return CV_IS_GRAPH(ptr);
4173 icvReleaseGraph( void** ptr )
4176 CV_Error( CV_StsNullPtr, "NULL double pointer" );
4178 *ptr = 0; // it's impossible now to release graph, so just clear the pointer
4183 icvCloneGraph( const void* ptr )
4185 return cvCloneGraph( (const CvGraph*)ptr, 0 );
4190 icvWriteGraph( CvFileStorage* fs, const char* name,
4191 const void* struct_ptr, CvAttrList attr )
4194 char* write_buf = 0;
4195 const CvGraph* graph = (const CvGraph*)struct_ptr;
4198 int i, k, vtx_count, edge_count;
4199 char vtx_dt_buf[128], *vtx_dt;
4200 char edge_dt_buf[128], *edge_dt;
4203 assert( CV_IS_GRAPH(graph) );
4204 vtx_count = cvGraphGetVtxCount( graph );
4205 edge_count = cvGraphGetEdgeCount( graph );
4206 flag_buf = (int*)cvAlloc( vtx_count*sizeof(flag_buf[0]));
4209 cvStartReadSeq( (CvSeq*)graph, &reader );
4210 for( i = 0, k = 0; i < graph->total; i++ )
4212 if( CV_IS_SET_ELEM( reader.ptr ))
4214 CvGraphVtx* vtx = (CvGraphVtx*)reader.ptr;
4215 flag_buf[k] = vtx->flags;
4218 CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
4222 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH );
4224 sprintf( buf, "%08x", graph->flags );
4225 cvWriteString( fs, "flags", buf, 1 );
4227 cvWriteInt( fs, "vertex_count", vtx_count );
4228 vtx_dt = icvGetFormat( (CvSeq*)graph, "vertex_dt",
4229 &attr, sizeof(CvGraphVtx), vtx_dt_buf );
4231 cvWriteString( fs, "vertex_dt", vtx_dt, 0 );
4233 cvWriteInt( fs, "edge_count", edge_count );
4234 edge_dt = icvGetFormat( (CvSeq*)graph->edges, "edge_dt",
4235 &attr, sizeof(CvGraphEdge), buf );
4236 sprintf( edge_dt_buf, "2if%s", edge_dt ? edge_dt : "" );
4237 edge_dt = edge_dt_buf;
4238 cvWriteString( fs, "edge_dt", edge_dt, 0 );
4240 icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) );
4242 write_buf_size = MAX( 3*graph->elem_size, 1 << 16 );
4243 write_buf_size = MAX( 3*graph->edges->elem_size, write_buf_size );
4244 write_buf = (char*)cvAlloc( write_buf_size );
4246 // as vertices and edges are written in similar way,
4247 // do it as a parametrized 2-iteration loop
4248 for( k = 0; k < 2; k++ )
4250 const char* dt = k == 0 ? vtx_dt : edge_dt;
4253 CvSet* data = k == 0 ? (CvSet*)graph : graph->edges;
4254 int elem_size = data->elem_size;
4255 int write_elem_size = icvCalcElemSize( dt, 0 );
4256 char* src_ptr = write_buf;
4257 int write_max = write_buf_size / write_elem_size, write_count = 0;
4259 // alignment of user part of the edge data following 2if
4260 int edge_user_align = sizeof(float);
4264 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
4265 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4266 if( fmt_pair_count > 2 || CV_ELEM_SIZE(fmt_pairs[2*2+1]) >= (int)sizeof(double))
4267 edge_user_align = sizeof(double);
4270 cvStartWriteStruct( fs, k == 0 ? "vertices" : "edges",
4271 CV_NODE_SEQ + CV_NODE_FLOW );
4272 cvStartReadSeq( (CvSeq*)data, &reader );
4273 for( i = 0; i < data->total; i++ )
4275 if( CV_IS_SET_ELEM( reader.ptr ))
4277 if( k == 0 ) // vertices
4278 memcpy( src_ptr, reader.ptr + sizeof(CvGraphVtx), write_elem_size );
4281 CvGraphEdge* edge = (CvGraphEdge*)reader.ptr;
4282 src_ptr = (char*)cvAlignPtr( src_ptr, sizeof(int) );
4283 ((int*)src_ptr)[0] = edge->vtx[0]->flags;
4284 ((int*)src_ptr)[1] = edge->vtx[1]->flags;
4285 *(float*)(src_ptr + sizeof(int)*2) = edge->weight;
4286 if( elem_size > (int)sizeof(CvGraphEdge) )
4288 char* src_ptr2 = (char*)cvAlignPtr( src_ptr + 2*sizeof(int)
4289 + sizeof(float), edge_user_align );
4290 memcpy( src_ptr2, edge + 1, elem_size - sizeof(CvGraphEdge) );
4293 src_ptr += write_elem_size;
4294 if( ++write_count >= write_max )
4296 cvWriteRawData( fs, write_buf, write_count, dt );
4298 src_ptr = write_buf;
4301 CV_NEXT_SEQ_ELEM( data->elem_size, reader );
4304 if( write_count > 0 )
4305 cvWriteRawData( fs, write_buf, write_count, dt );
4306 cvEndWriteStruct( fs );
4310 cvEndWriteStruct( fs );
4312 // final stage. restore the graph flags
4313 cvStartReadSeq( (CvSeq*)graph, &reader );
4315 for( i = 0; i < graph->total; i++ )
4317 if( CV_IS_SET_ELEM( reader.ptr ))
4318 ((CvGraphVtx*)reader.ptr)->flags = flag_buf[vtx_count++];
4319 CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
4322 cvFree( &write_buf );
4323 cvFree( &flag_buf );
4328 icvReadGraph( CvFileStorage* fs, CvFileNode* node )
4332 CvGraphVtx** vtx_buf = 0;
4334 CvFileNode *header_node, *vtx_node, *edge_node;
4335 int flags, vtx_count, edge_count;
4336 int vtx_size = sizeof(CvGraphVtx), edge_size, header_size = sizeof(CvGraph);
4337 int src_vtx_size = 0, src_edge_size;
4338 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
4339 int vtx_items_per_elem = 0, edge_items_per_elem = 0;
4340 int edge_user_align = sizeof(float);
4343 const char* flags_str;
4344 const char* header_dt;
4346 const char* edge_dt;
4349 flags_str = cvReadStringByName( fs, node, "flags", 0 );
4350 vtx_dt = cvReadStringByName( fs, node, "vertex_dt", 0 );
4351 edge_dt = cvReadStringByName( fs, node, "edge_dt", 0 );
4352 vtx_count = cvReadIntByName( fs, node, "vertex_count", -1 );
4353 edge_count = cvReadIntByName( fs, node, "edge_count", -1 );
4355 if( !flags_str || vtx_count == -1 || edge_count == -1 || !edge_dt )
4356 CV_Error( CV_StsError, "Some of essential sequence attributes are absent" );
4358 flags = (int)strtol( flags_str, &endptr, 16 );
4359 if( endptr == flags_str ||
4360 (flags & (CV_SEQ_KIND_MASK|CV_MAGIC_MASK)) != (CV_GRAPH|CV_SET_MAGIC_VAL))
4361 CV_Error( CV_StsError, "Invalid graph signature" );
4363 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4364 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4366 if( (header_dt != 0) ^ (header_node != 0) )
4367 CV_Error( CV_StsError,
4368 "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
4371 header_size = icvCalcElemSize( header_dt, header_size );
4375 src_vtx_size = icvCalcElemSize( vtx_dt, 0 );
4376 vtx_size = icvCalcElemSize( vtx_dt, vtx_size );
4377 fmt_pair_count = icvDecodeFormat( edge_dt,
4378 fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4379 fmt_pair_count *= 2;
4380 for( i = 0; i < fmt_pair_count; i += 2 )
4381 vtx_items_per_elem += fmt_pairs[i];
4385 char dst_edge_dt_buf[128];
4386 const char* dst_edge_dt = 0;
4388 fmt_pair_count = icvDecodeFormat( edge_dt,
4389 fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4390 if( fmt_pair_count < 2 ||
4391 fmt_pairs[0] != 2 || fmt_pairs[1] != CV_32S ||
4392 fmt_pairs[2] < 1 || fmt_pairs[3] != CV_32F )
4393 CV_Error( CV_StsBadArg,
4394 "Graph edges should start with 2 integers and a float" );
4396 // alignment of user part of the edge data following 2if
4397 if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[5]) >= (int)sizeof(double))
4398 edge_user_align = sizeof(double);
4400 fmt_pair_count *= 2;
4401 for( i = 0; i < fmt_pair_count; i += 2 )
4402 edge_items_per_elem += fmt_pairs[i];
4404 if( edge_dt[2] == 'f' || (edge_dt[2] == '1' && edge_dt[3] == 'f') )
4405 dst_edge_dt = edge_dt + 3 + isdigit(edge_dt[2]);
4408 int val = (int)strtol( edge_dt + 2, &endptr, 10 );
4409 sprintf( dst_edge_dt_buf, "%df%s", val-1, endptr );
4410 dst_edge_dt = dst_edge_dt_buf;
4413 edge_size = icvCalcElemSize( dst_edge_dt, sizeof(CvGraphEdge) );
4414 src_edge_size = icvCalcElemSize( edge_dt, 0 );
4417 graph = cvCreateGraph( flags, header_size, vtx_size, edge_size, fs->dststorage );
4420 cvReadRawData( fs, header_node, (char*)graph + sizeof(CvGraph), header_dt );
4422 read_buf_size = MAX( src_vtx_size*3, 1 << 16 );
4423 read_buf_size = MAX( src_edge_size*3, read_buf_size );
4424 read_buf = (char*)cvAlloc( read_buf_size );
4425 vtx_buf = (CvGraphVtx**)cvAlloc( vtx_count * sizeof(vtx_buf[0]) );
4427 vtx_node = cvGetFileNodeByName( fs, node, "vertices" );
4428 edge_node = cvGetFileNodeByName( fs, node, "edges" );
4430 CV_Error( CV_StsBadArg, "No edges data" );
4431 if( vtx_dt && !vtx_node )
4432 CV_Error( CV_StsBadArg, "No vertices data" );
4434 // as vertices and edges are read in similar way,
4435 // do it as a parametrized 2-iteration loop
4436 for( k = 0; k < 2; k++ )
4438 const char* dt = k == 0 ? vtx_dt : edge_dt;
4439 int elem_size = k == 0 ? vtx_size : edge_size;
4440 int src_elem_size = k == 0 ? src_vtx_size : src_edge_size;
4441 int items_per_elem = k == 0 ? vtx_items_per_elem : edge_items_per_elem;
4442 int elem_count = k == 0 ? vtx_count : edge_count;
4443 char* dst_ptr = read_buf;
4444 int read_max = read_buf_size /MAX(src_elem_size, 1), read_count = 0;
4446 cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
4448 for( i = 0; i < elem_count; i++ )
4450 if( read_count == 0 && dt )
4452 int count = MIN( elem_count - i, read_max )*items_per_elem;
4453 cvReadRawDataSlice( fs, &reader, count, read_buf, dt );
4461 cvGraphAddVtx( graph, 0, &vtx );
4464 memcpy( vtx + 1, dst_ptr, src_elem_size );
4468 CvGraphEdge* edge = 0;
4469 int vtx1 = ((int*)dst_ptr)[0];
4470 int vtx2 = ((int*)dst_ptr)[1];
4473 if( (unsigned)vtx1 >= (unsigned)vtx_count ||
4474 (unsigned)vtx2 >= (unsigned)vtx_count )
4475 CV_Error( CV_StsOutOfRange,
4476 "Some of stored vertex indices are out of range" );
4478 result = cvGraphAddEdgeByPtr( graph,
4479 vtx_buf[vtx1], vtx_buf[vtx2], 0, &edge );
4482 CV_Error( CV_StsBadArg, "Duplicated edge has occured" );
4484 edge->weight = *(float*)(dst_ptr + sizeof(int)*2);
4485 if( elem_size > (int)sizeof(CvGraphEdge) )
4487 char* dst_ptr2 = (char*)cvAlignPtr( dst_ptr + sizeof(int)*2 +
4488 sizeof(float), edge_user_align );
4489 memcpy( edge + 1, dst_ptr2, elem_size - sizeof(CvGraphEdge) );
4493 dst_ptr += src_elem_size;
4499 cvFree( &read_buf );
4505 /****************************************************************************************\
4507 \****************************************************************************************/
4509 CvTypeInfo *CvType::first = 0, *CvType::last = 0;
4511 CvType::CvType( const char* type_name,
4512 CvIsInstanceFunc is_instance, CvReleaseFunc release,
4513 CvReadFunc read, CvWriteFunc write, CvCloneFunc clone )
4517 _info.header_size = sizeof(_info);
4518 _info.type_name = type_name;
4519 _info.prev = _info.next = 0;
4520 _info.is_instance = is_instance;
4521 _info.release = release;
4522 _info.clone = clone;
4524 _info.write = write;
4526 cvRegisterType( &_info );
4533 cvUnregisterType( info->type_name );
4537 CvType seq_type( CV_TYPE_NAME_SEQ, icvIsSeq, icvReleaseSeq, icvReadSeq,
4538 icvWriteSeqTree /* this is the entry point for
4539 writing a single sequence too */, icvCloneSeq );
4541 CvType seq_tree_type( CV_TYPE_NAME_SEQ_TREE, icvIsSeq, icvReleaseSeq,
4542 icvReadSeqTree, icvWriteSeqTree, icvCloneSeq );
4544 CvType seq_graph_type( CV_TYPE_NAME_GRAPH, icvIsGraph, icvReleaseGraph,
4545 icvReadGraph, icvWriteGraph, icvCloneGraph );
4547 CvType sparse_mat_type( CV_TYPE_NAME_SPARSE_MAT, icvIsSparseMat,
4548 (CvReleaseFunc)cvReleaseSparseMat, icvReadSparseMat,
4549 icvWriteSparseMat, (CvCloneFunc)cvCloneSparseMat );
4551 CvType image_type( CV_TYPE_NAME_IMAGE, icvIsImage, (CvReleaseFunc)cvReleaseImage,
4552 icvReadImage, icvWriteImage, (CvCloneFunc)cvCloneImage );
4554 CvType mat_type( CV_TYPE_NAME_MAT, icvIsMat, (CvReleaseFunc)cvReleaseMat,
4555 icvReadMat, icvWriteMat, (CvCloneFunc)cvCloneMat );
4557 CvType matnd_type( CV_TYPE_NAME_MATND, icvIsMatND, (CvReleaseFunc)cvReleaseMatND,
4558 icvReadMatND, icvWriteMatND, (CvCloneFunc)cvCloneMatND );
4561 cvRegisterType( const CvTypeInfo* _info )
4563 CvTypeInfo* info = 0;
4567 //if( !CvType::first )
4568 // icvCreateStandardTypes();
4570 if( !_info || _info->header_size != sizeof(CvTypeInfo) )
4571 CV_Error( CV_StsBadSize, "Invalid type info" );
4573 if( !_info->is_instance || !_info->release ||
4574 !_info->read || !_info->write )
4575 CV_Error( CV_StsNullPtr,
4576 "Some of required function pointers "
4577 "(is_instance, release, read or write) are NULL");
4579 c = _info->type_name[0];
4580 if( !isalpha(c) && c != '_' )
4581 CV_Error( CV_StsBadArg, "Type name should start with a letter or _" );
4583 len = (int)strlen(_info->type_name);
4585 for( i = 0; i < len; i++ )
4587 c = _info->type_name[i];
4588 if( !isalnum(c) && c != '-' && c != '_' )
4589 CV_Error( CV_StsBadArg,
4590 "Type name should contain only letters, digits, - and _" );
4593 info = (CvTypeInfo*)malloc( sizeof(*info) + len + 1 );
4596 info->type_name = (char*)(info + 1);
4597 memcpy( (char*)info->type_name, _info->type_name, len + 1 );
4600 info->next = CvType::first;
4603 CvType::first->prev = info;
4605 CvType::last = info;
4606 CvType::first = info;
4611 cvUnregisterType( const char* type_name )
4615 info = cvFindType( type_name );
4619 info->prev->next = info->next;
4621 CvType::first = info->next;
4624 info->next->prev = info->prev;
4626 CvType::last = info->prev;
4628 if( !CvType::first || !CvType::last )
4629 CvType::first = CvType::last = 0;
4639 return CvType::first;
4644 cvFindType( const char* type_name )
4646 CvTypeInfo* info = 0;
4648 for( info = CvType::first; info != 0; info = info->next )
4649 if( strcmp( info->type_name, type_name ) == 0 )
4657 cvTypeOf( const void* struct_ptr )
4659 CvTypeInfo* info = 0;
4661 for( info = CvType::first; info != 0; info = info->next )
4662 if( info->is_instance( struct_ptr ))
4669 /* universal functions */
4671 cvRelease( void** struct_ptr )
4676 CV_Error( CV_StsNullPtr, "NULL double pointer" );
4680 info = cvTypeOf( *struct_ptr );
4682 CV_Error( CV_StsError, "Unknown object type" );
4683 if( !info->release )
4684 CV_Error( CV_StsError, "release function pointer is NULL" );
4686 info->release( struct_ptr );
4692 void* cvClone( const void* struct_ptr )
4694 void* struct_copy = 0;
4698 CV_Error( CV_StsNullPtr, "NULL structure pointer" );
4700 info = cvTypeOf( struct_ptr );
4702 CV_Error( CV_StsError, "Unknown object type" );
4704 CV_Error( CV_StsError, "clone function pointer is NULL" );
4706 struct_copy = info->clone( struct_ptr );
4711 /* reads matrix, image, sequence, graph etc. */
4713 cvRead( CvFileStorage* fs, CvFileNode* node, CvAttrList* list )
4716 CV_CHECK_FILE_STORAGE( fs );
4721 if( !CV_NODE_IS_USER(node->tag) || !node->info )
4722 CV_Error( CV_StsError, "The node does not represent a user object (unknown type?)" );
4724 obj = node->info->read( fs, node );
4726 *list = cvAttrList(0,0);
4732 /* writes matrix, image, sequence, graph etc. */
4734 cvWrite( CvFileStorage* fs, const char* name,
4735 const void* ptr, CvAttrList attributes )
4739 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
4742 CV_Error( CV_StsNullPtr, "Null pointer to the written object" );
4744 info = cvTypeOf( ptr );
4746 CV_Error( CV_StsBadArg, "Unknown object" );
4749 CV_Error( CV_StsBadArg, "The object does not have write function" );
4751 info->write( fs, name, ptr, attributes );
4755 /* simple API for reading/writing data */
4757 cvSave( const char* filename, const void* struct_ptr,
4758 const char* _name, const char* comment, CvAttrList attributes )
4760 CvFileStorage* fs = 0;
4763 CV_Error( CV_StsNullPtr, "NULL object pointer" );
4765 fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE );
4767 CV_Error( CV_StsError, "Could not open the file storage. Check the path and permissions" );
4769 cv::string name = _name ? cv::string(_name) : cv::FileStorage::getDefaultObjectName(filename);
4772 cvWriteComment( fs, comment, 0 );
4773 cvWrite( fs, name.c_str(), struct_ptr, attributes );
4774 cvReleaseFileStorage( &fs );
4778 cvLoad( const char* filename, CvMemStorage* memstorage,
4779 const char* name, const char** _real_name )
4782 const char* real_name = 0;
4783 CvFileStorage* fs = 0;
4785 CvFileNode* node = 0;
4786 fs = cvOpenFileStorage( filename, memstorage, CV_STORAGE_READ );
4793 node = cvGetFileNodeByName( fs, 0, name );
4798 for( k = 0; k < fs->roots->total; k++ )
4803 node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
4804 if( !CV_NODE_IS_MAP( node->tag ))
4806 seq = node->data.seq;
4809 cvStartReadSeq( seq, &reader, 0 );
4811 // find the first element in the map
4812 for( i = 0; i < seq->total; i++ )
4814 if( CV_IS_SET_ELEM( reader.ptr ))
4816 node = (CvFileNode*)reader.ptr;
4819 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
4828 CV_Error( CV_StsObjectNotFound, "Could not find the/an object in file storage" );
4830 real_name = cvGetFileNodeName( node );
4831 ptr = cvRead( fs, node, 0 );
4834 if( !memstorage && (CV_IS_SEQ( ptr ) || CV_IS_SET( ptr )) )
4835 CV_Error( CV_StsNullPtr,
4836 "NULL memory storage is passed - the loaded dynamic structure can not be stored" );
4838 if( cvGetErrStatus() < 0 )
4840 cvRelease( (void**)&ptr );
4848 *_real_name = (const char*)cvAlloc(strlen(real_name));
4849 memcpy((void*)*_real_name, real_name, strlen(real_name));
4855 cvReleaseFileStorage( &fs );
4861 ///////////////////////// new C++ interface for CvFileStorage ///////////////////////////
4866 static void getElemSize( const string& fmt, size_t& elemSize, size_t& cn )
4868 const char* dt = fmt.c_str();
4870 if( isdigit(dt[0]) )
4876 elemSize = cn*(c == 'u' || c == 'c' ? sizeof(uchar) : c == 'w' || c == 's' ? sizeof(ushort) :
4877 c == 'i' ? sizeof(int) : c == 'f' ? sizeof(float) : c == 'd' ? sizeof(double) :
4878 c == 'r' ? sizeof(void*) : (size_t)0);
4881 FileStorage::FileStorage()
4886 FileStorage::FileStorage(const string& filename, int flags)
4889 open( filename, flags );
4892 FileStorage::FileStorage(CvFileStorage* _fs)
4894 fs = Ptr<CvFileStorage>(_fs);
4895 state = _fs ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED;
4898 FileStorage::~FileStorage()
4900 while( structs.size() > 0 )
4902 cvEndWriteStruct(fs);
4907 bool FileStorage::open(const string& filename, int flags)
4910 fs = Ptr<CvFileStorage>(cvOpenFileStorage( filename.c_str(), 0, flags ));
4911 bool ok = isOpened();
4912 state = ok ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED;
4916 bool FileStorage::isOpened() const
4921 void FileStorage::release()
4928 FileNode FileStorage::root(int streamidx) const
4930 return isOpened() ? FileNode(fs, cvGetRootFileNode(fs, streamidx)) : FileNode();
4933 FileStorage& operator << (FileStorage& fs, const string& str)
4935 enum { NAME_EXPECTED = FileStorage::NAME_EXPECTED,
4936 VALUE_EXPECTED = FileStorage::VALUE_EXPECTED,
4937 INSIDE_MAP = FileStorage::INSIDE_MAP };
4938 const char* _str = str.c_str();
4939 if( !fs.isOpened() || !_str )
4941 if( *_str == '}' || *_str == ']' )
4943 if( fs.structs.empty() )
4944 CV_Error_( CV_StsError, ("Extra closing '%c'", *_str) );
4945 if( (*_str == ']' ? '[' : '{') != fs.structs.back() )
4946 CV_Error_( CV_StsError,
4947 ("The closing '%c' does not match the opening '%c'", *_str, fs.structs.back()));
4948 fs.structs.pop_back();
4949 fs.state = fs.structs.empty() || fs.structs.back() == '{' ?
4950 INSIDE_MAP + NAME_EXPECTED : VALUE_EXPECTED;
4951 cvEndWriteStruct( *fs );
4952 fs.elname = string();
4954 else if( fs.state == NAME_EXPECTED + INSIDE_MAP )
4956 if( !isalpha(*_str) )
4957 CV_Error_( CV_StsError, ("Incorrect element name %s", _str) );
4959 fs.state = VALUE_EXPECTED + INSIDE_MAP;
4961 else if( (fs.state & 3) == VALUE_EXPECTED )
4963 if( *_str == '{' || *_str == '[' )
4965 fs.structs.push_back(*_str);
4966 int flags = *_str++ == '{' ? CV_NODE_MAP : CV_NODE_SEQ;
4967 fs.state = flags == CV_NODE_MAP ? INSIDE_MAP +
4968 NAME_EXPECTED : VALUE_EXPECTED;
4971 flags |= CV_NODE_FLOW;
4974 cvStartWriteStruct( *fs, fs.elname.size() > 0 ? fs.elname.c_str() : 0,
4975 flags, *_str ? _str : 0 );
4976 fs.elname = string();
4980 write( fs, fs.elname, (_str[0] == '\\' && (_str[1] == '{' || _str[1] == '}' ||
4981 _str[1] == '[' || _str[1] == ']')) ? string(_str+1) : str );
4982 if( fs.state == INSIDE_MAP + VALUE_EXPECTED )
4983 fs.state = INSIDE_MAP + NAME_EXPECTED;
4987 CV_Error( CV_StsError, "Invalid fs.state" );
4992 void FileStorage::writeRaw( const string& fmt, const uchar* vec, size_t len )
4996 size_t elemSize, cn;
4997 getElemSize( fmt, elemSize, cn );
4998 CV_Assert( len % elemSize == 0 );
4999 cvWriteRawData( fs, vec, (int)(len/elemSize), fmt.c_str());
5003 void FileStorage::writeObj( const string& name, const void* obj )
5007 cvWrite( fs, name.size() > 0 ? name.c_str() : 0, obj );
5011 void* FileNode::readObj() const
5015 return cvRead( (CvFileStorage*)fs, (CvFileNode*)node );
5019 FileNodeIterator::FileNodeIterator()
5027 FileNodeIterator::FileNodeIterator(const CvFileStorage* _fs,
5028 const CvFileNode* _node, size_t _ofs)
5032 int node_type = _node->tag & FileNode::TYPE_MASK;
5035 if( node_type == FileNode::SEQ || node_type == FileNode::MAP )
5037 cvStartReadSeq( _node->data.seq, &reader );
5038 remaining = FileNode(_fs, _node).size();
5042 reader.ptr = (schar*)_node;
5046 (*this) += (int)_ofs;
5057 FileNodeIterator::FileNodeIterator(const FileNodeIterator& it)
5060 container = it.container;
5062 remaining = it.remaining;
5065 FileNodeIterator& FileNodeIterator::operator ++()
5070 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
5076 FileNodeIterator FileNodeIterator::operator ++(int)
5078 FileNodeIterator it = *this;
5083 FileNodeIterator& FileNodeIterator::operator --()
5085 if( remaining < FileNode(fs, container).size() )
5088 CV_PREV_SEQ_ELEM( reader.seq->elem_size, reader );
5094 FileNodeIterator FileNodeIterator::operator --(int)
5096 FileNodeIterator it = *this;
5101 FileNodeIterator& FileNodeIterator::operator += (int ofs)
5106 ofs = std::min(ofs, (int)remaining);
5109 size_t count = FileNode(fs, container).size();
5110 ofs = (int)(remaining - std::min(remaining - ofs, count));
5114 cvSetSeqReaderPos( &reader, ofs, 1 );
5118 FileNodeIterator& FileNodeIterator::operator -= (int ofs)
5120 return operator += (-ofs);
5124 FileNodeIterator& FileNodeIterator::readRaw( const string& fmt, uchar* vec, size_t maxCount )
5126 if( fs && container && remaining > 0 )
5128 size_t elem_size, cn;
5129 getElemSize( fmt, elem_size, cn );
5130 CV_Assert( elem_size > 0 );
5131 size_t count = std::min(remaining, maxCount);
5135 cvReadRawDataSlice( fs, &reader, (int)count, vec, fmt.c_str() );
5136 remaining -= count*cn;
5140 cvReadRawData( fs, container, vec, fmt.c_str() );
5147 void write( FileStorage& fs, const string& name, const Mat& value )
5150 cvWrite( *fs, name.size() ? name.c_str() : 0, &mat );
5153 void write( FileStorage& fs, const string& name, const MatND& value )
5155 CvMatND mat = value;
5156 cvWrite( *fs, name.size() ? name.c_str() : 0, &mat );
5159 // TODO: the 4 functions below need to be implemented more efficiently
5160 void write( FileStorage& fs, const string& name, const SparseMat& value )
5162 Ptr<CvSparseMat> mat = (CvSparseMat*)value;
5163 cvWrite( *fs, name.size() ? name.c_str() : 0, mat );
5167 void read( const FileNode& node, Mat& mat, const Mat& default_mat )
5171 default_mat.copyTo(mat);
5174 Ptr<CvMat> m = (CvMat*)cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node);
5175 CV_Assert(CV_IS_MAT(m));
5179 void read( const FileNode& node, MatND& mat, const MatND& default_mat )
5183 default_mat.copyTo(mat);
5186 Ptr<CvMatND> m = (CvMatND*)cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node);
5187 CV_Assert(CV_IS_MATND(m));
5188 MatND(m).copyTo(mat);
5191 void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat )
5195 default_mat.copyTo(mat);
5198 Ptr<CvSparseMat> m = (CvSparseMat*)cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node);
5199 CV_Assert(CV_IS_SPARSE_MAT(m));
5200 SparseMat(m).copyTo(mat);