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.
43 #include "precomp.hpp"
53 # include "TargetConditionals.h"
54 # if (defined TARGET_OS_IPHONE && TARGET_OS_IPHONE) || (defined TARGET_IPHONE_SIMULATOR && TARGET_IPHONE_SIMULATOR)
62 # ifndef _LFS64_LARGEFILE
63 # define _LFS64_LARGEFILE 0
65 # ifndef _FILE_OFFSET_BITS
66 # define _FILE_OFFSET_BITS 0
71 /****************************************************************************************\
72 * Common macros and type definitions *
73 \****************************************************************************************/
75 #define cv_isprint(c) ((uchar)(c) >= (uchar)' ')
76 #define cv_isprint_or_tab(c) ((uchar)(c) >= (uchar)' ' || (c) == '\t')
78 static inline bool cv_isalnum(char c)
80 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
83 static inline bool cv_isalpha(char c)
85 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
88 static inline bool cv_isdigit(char c)
90 return '0' <= c && c <= '9';
93 static inline bool cv_isspace(char c)
95 return (9 <= c && c <= 13) || c == ' ';
98 static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
100 const int radix = 10;
101 char* ptr=buffer + 23 /* enough even for 64-bit integers */;
102 unsigned val = abs(_val);
107 unsigned r = val / radix;
108 *--ptr = (char)(val - (r*radix) + '0');
119 cv::string cv::FileStorage::getDefaultObjectName(const string& _filename)
121 static const char* stubname = "unnamed";
122 const char* filename = _filename.c_str();
123 const char* ptr2 = filename + _filename.size();
124 const char* ptr = ptr2 - 1;
125 cv::AutoBuffer<char> name_buf(_filename.size()+1);
127 while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' )
129 if( *ptr == '.' && (!*ptr2 || strncmp(ptr2, ".gz", 3) == 0) )
135 CV_Error( CV_StsBadArg, "Invalid filename" );
137 char* name = name_buf;
139 // name must start with letter or '_'
140 if( !cv_isalpha(*ptr) && *ptr!= '_' ){
147 if( !cv_isalnum(c) && c != '-' && c != '_' )
153 if( strcmp( name, "_" ) == 0 )
154 strcpy( name, stubname );
155 return cv::string(name);
160 #if !defined(ANDROID) || (defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_WCHAR_T)
161 string fromUtf16(const WString& str)
163 cv::AutoBuffer<char> _buf(str.size()*4 + 1);
166 size_t sz = wcstombs(buf, str.c_str(), str.size());
167 if( sz == (size_t)-1 )
173 WString toUtf16(const string& str)
175 cv::AutoBuffer<wchar_t> _buf(str.size() + 1);
178 size_t sz = mbstowcs(buf, str.c_str(), str.size());
179 if( sz == (size_t)-1 )
187 typedef struct CvGenericHash
195 typedef CvGenericHash CvStringHash;
197 typedef struct CvFileMapNode
200 const CvStringHashNode* key;
201 struct CvFileMapNode* next;
205 typedef struct CvXMLStackRecord
214 #define CV_XML_OPENING_TAG 1
215 #define CV_XML_CLOSING_TAG 2
216 #define CV_XML_EMPTY_TAG 3
217 #define CV_XML_HEADER_TAG 4
218 #define CV_XML_DIRECTIVE_TAG 5
220 //typedef void (*CvParse)( struct CvFileStorage* fs );
221 typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key,
222 int struct_flags, const char* type_name );
223 typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs );
224 typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value );
225 typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value );
226 typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key,
227 const char* value, int quote );
228 typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment );
229 typedef void (*CvStartNextStream)( struct CvFileStorage* fs );
231 typedef struct CvFileStorage
237 CvMemStorage* memstorage;
238 CvMemStorage* dststorage;
239 CvMemStorage* strstorage;
240 CvStringHash* str_hash;
259 CvStartWriteStruct start_write_struct;
260 CvEndWriteStruct end_write_struct;
261 CvWriteInt write_int;
262 CvWriteReal write_real;
263 CvWriteString write_string;
264 CvWriteComment write_comment;
265 CvStartNextStream start_next_stream;
268 size_t strbufsize, strbufpos;
269 std::deque<char>* outbuf;
275 static void icvPuts( CvFileStorage* fs, const char* str )
278 std::copy(str, str + strlen(str), std::back_inserter(*fs->outbuf));
280 fputs( str, fs->file );
282 else if( fs->gzfile )
283 gzputs( fs->gzfile, str );
286 CV_Error( CV_StsError, "The storage is not opened" );
289 static char* icvGets( CvFileStorage* fs, char* str, int maxCount )
293 size_t i = fs->strbufpos, len = fs->strbufsize;
295 const char* instr = fs->strbuf;
296 while( i < len && j < maxCount-1 )
307 return j > 1 ? str : 0;
310 return fgets( str, maxCount, fs->file );
313 return gzgets( fs->gzfile, str, maxCount );
315 CV_Error( CV_StsError, "The storage is not opened" );
319 static int icvEof( CvFileStorage* fs )
322 return fs->strbufpos >= fs->strbufsize;
324 return feof(fs->file);
327 return gzeof(fs->gzfile);
332 static void icvCloseFile( CvFileStorage* fs )
337 else if( fs->gzfile )
338 gzclose( fs->gzfile );
344 fs->is_opened = false;
347 static void icvRewind( CvFileStorage* fs )
352 else if( fs->gzfile )
353 gzrewind(fs->gzfile);
358 #define CV_YML_INDENT 3
359 #define CV_XML_INDENT 2
360 #define CV_YML_INDENT_FLOW 1
361 #define CV_FS_MAX_LEN 4096
363 #define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24))
364 #define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE)
366 #define CV_CHECK_FILE_STORAGE(fs) \
368 if( !CV_IS_FILE_STORAGE(fs) ) \
369 CV_Error( (fs) ? CV_StsBadArg : CV_StsNullPtr, \
370 "Invalid pointer to file storage" ); \
373 #define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \
375 CV_CHECK_FILE_STORAGE(fs); \
376 if( !fs->write_mode ) \
377 CV_Error( CV_StsError, "The file storage is opened for reading" ); \
381 cvAttrValue( const CvAttrList* attr, const char* attr_name )
383 while( attr && attr->attr )
386 for( i = 0; attr->attr[i*2] != 0; i++ )
388 if( strcmp( attr_name, attr->attr[i*2] ) == 0 )
389 return attr->attr[i*2+1];
398 static CvGenericHash*
399 cvCreateMap( int flags, int header_size, int elem_size,
400 CvMemStorage* storage, int start_tab_size )
402 if( header_size < (int)sizeof(CvGenericHash) )
403 CV_Error( CV_StsBadSize, "Too small map header_size" );
405 if( start_tab_size <= 0 )
408 CvGenericHash* map = (CvGenericHash*)cvCreateSet( flags, header_size, elem_size, storage );
410 map->tab_size = start_tab_size;
411 start_tab_size *= sizeof(map->table[0]);
412 map->table = (void**)cvMemStorageAlloc( storage, start_tab_size );
413 memset( map->table, 0, start_tab_size );
419 #define CV_PARSE_ERROR( errmsg ) \
420 icvParseError( fs, __func__, (errmsg), __FILE__, __LINE__ )
422 #define CV_PARSE_ERROR( errmsg ) \
423 icvParseError( fs, "", (errmsg), __FILE__, __LINE__ )
427 icvParseError( CvFileStorage* fs, const char* func_name,
428 const char* err_msg, const char* source_file, int source_line )
431 sprintf( buf, "%s(%d): %s", fs->filename, fs->lineno, err_msg );
432 cvError( CV_StsParseError, func_name, buf, source_file, source_line );
437 icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection )
439 if( CV_NODE_IS_MAP(tag) )
441 if( collection->tag != CV_NODE_NONE )
443 assert( fs->fmt == CV_STORAGE_FORMAT_XML );
444 CV_PARSE_ERROR( "Sequence element should not have name (use <_></_>)" );
447 collection->data.map = cvCreateMap( 0, sizeof(CvFileNodeHash),
448 sizeof(CvFileMapNode), fs->memstorage, 16 );
453 seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage );
455 // if <collection> contains some scalar element, add it to the newly created collection
456 if( CV_NODE_TYPE(collection->tag) != CV_NODE_NONE )
457 cvSeqPush( seq, collection );
459 collection->data.seq = seq;
462 collection->tag = tag;
463 cvSetSeqBlockSize( collection->data.seq, 8 );
468 icvFSReleaseCollection( CvSeq* seq )
472 int is_map = CV_IS_SET(seq);
474 int i, total = seq->total;
475 cvStartReadSeq( seq, &reader, 0 );
477 for( i = 0; i < total; i++ )
479 CvFileNode* node = (CvFileNode*)reader.ptr;
481 if( (!is_map || CV_IS_SET_ELEM( node )) && CV_NODE_IS_COLLECTION(node->tag) )
483 if( CV_NODE_IS_USER(node->tag) && node->info && node->data.obj.decoded )
484 cvRelease( (void**)&node->data.obj.decoded );
485 if( !CV_NODE_SEQ_IS_SIMPLE( node->data.seq ))
486 icvFSReleaseCollection( node->data.seq );
488 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
495 icvFSDoResize( CvFileStorage* fs, char* ptr, int len )
498 int written_len = (int)(ptr - fs->buffer_start);
499 int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2);
500 new_size = MAX( written_len + len, new_size );
501 new_ptr = (char*)cvAlloc( new_size + 256 );
502 fs->buffer = new_ptr + (fs->buffer - fs->buffer_start);
503 if( written_len > 0 )
504 memcpy( new_ptr, fs->buffer_start, written_len );
505 fs->buffer_start = new_ptr;
506 fs->buffer_end = fs->buffer_start + new_size;
507 new_ptr += written_len;
512 inline char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len )
514 return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len );
519 icvFSFlush( CvFileStorage* fs )
521 char* ptr = fs->buffer;
524 if( ptr > fs->buffer_start + fs->space )
528 icvPuts( fs, fs->buffer_start );
529 fs->buffer = fs->buffer_start;
532 indent = fs->struct_indent;
534 if( fs->space != indent )
536 if( fs->space < indent )
537 memset( fs->buffer_start + fs->space, ' ', indent - fs->space );
541 ptr = fs->buffer = fs->buffer_start + fs->space;
548 icvClose( CvFileStorage* fs, std::string* out )
554 CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" );
558 if( fs->write_mode && (fs->file || fs->gzfile || fs->outbuf) )
560 if( fs->write_stack )
562 while( fs->write_stack->total > 0 )
563 cvEndWriteStruct(fs);
566 if( fs->fmt == CV_STORAGE_FORMAT_XML )
567 icvPuts( fs, "</opencv_storage>\n" );
573 if( fs->outbuf && out )
575 out->resize(fs->outbuf->size());
576 std::copy(fs->outbuf->begin(), fs->outbuf->end(), out->begin());
581 /* closes file storage and deallocates buffers */
583 cvReleaseFileStorage( CvFileStorage** p_fs )
586 CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" );
590 CvFileStorage* fs = *p_fs;
595 cvReleaseMemStorage( &fs->strstorage );
596 cvFree( &fs->buffer_start );
597 cvReleaseMemStorage( &fs->memstorage );
602 memset( fs, 0, sizeof(*fs) );
608 #define CV_HASHVAL_SCALE 33
610 CV_IMPL CvStringHashNode*
611 cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing )
613 CvStringHashNode* node = 0;
614 unsigned hashval = 0;
620 CvStringHash* map = fs->str_hash;
624 for( i = 0; str[i] != '\0'; i++ )
625 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
628 else for( i = 0; i < len; i++ )
629 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
632 tab_size = map->tab_size;
633 if( (tab_size & (tab_size - 1)) == 0 )
634 i = (int)(hashval & (tab_size - 1));
636 i = (int)(hashval % tab_size);
638 for( node = (CvStringHashNode*)(map->table[i]); node != 0; node = node->next )
640 if( node->hashval == hashval &&
641 node->str.len == len &&
642 memcmp( node->str.ptr, str, len ) == 0 )
646 if( !node && create_missing )
648 node = (CvStringHashNode*)cvSetNew( (CvSet*)map );
649 node->hashval = hashval;
650 node->str = cvMemStorageAllocString( map->storage, str, len );
651 node->next = (CvStringHashNode*)(map->table[i]);
652 map->table[i] = node;
660 cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node,
661 const CvStringHashNode* key,
664 CvFileNode* value = 0;
665 int k = 0, attempts = 1;
670 CV_CHECK_FILE_STORAGE(fs);
673 CV_Error( CV_StsNullPtr, "Null key element" );
679 attempts = fs->roots->total;
682 for( k = 0; k < attempts; k++ )
685 CvFileNode* map_node = _map_node;
686 CvFileMapNode* another;
690 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
692 if( !CV_NODE_IS_MAP(map_node->tag) )
694 if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
695 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
696 CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
700 map = map_node->data.map;
701 tab_size = map->tab_size;
703 if( (tab_size & (tab_size - 1)) == 0 )
704 i = (int)(key->hashval & (tab_size - 1));
706 i = (int)(key->hashval % tab_size);
708 for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
709 if( another->key == key )
711 if( !create_missing )
713 value = &another->value;
716 CV_PARSE_ERROR( "Duplicated key" );
719 if( k == attempts - 1 && create_missing )
721 CvFileMapNode* node = (CvFileMapNode*)cvSetNew( (CvSet*)map );
724 node->next = (CvFileMapNode*)(map->table[i]);
725 map->table[i] = node;
726 value = (CvFileNode*)node;
735 cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str )
737 CvFileNode* value = 0;
738 int i, len, tab_size;
739 unsigned hashval = 0;
740 int k = 0, attempts = 1;
745 CV_CHECK_FILE_STORAGE(fs);
748 CV_Error( CV_StsNullPtr, "Null element name" );
750 for( i = 0; str[i] != '\0'; i++ )
751 hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
759 attempts = fs->roots->total;
762 for( k = 0; k < attempts; k++ )
765 const CvFileNode* map_node = _map_node;
766 CvFileMapNode* another;
769 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
771 if( !CV_NODE_IS_MAP(map_node->tag) )
773 if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
774 CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
775 CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
779 map = map_node->data.map;
780 tab_size = map->tab_size;
782 if( (tab_size & (tab_size - 1)) == 0 )
783 i = (int)(hashval & (tab_size - 1));
785 i = (int)(hashval % tab_size);
787 for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
789 const CvStringHashNode* key = another->key;
791 if( key->hashval == hashval &&
792 key->str.len == len &&
793 memcmp( key->str.ptr, str, len ) == 0 )
795 value = &another->value;
806 cvGetRootFileNode( const CvFileStorage* fs, int stream_index )
808 CV_CHECK_FILE_STORAGE(fs);
810 if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total )
813 return (CvFileNode*)cvGetSeqElem( fs->roots, stream_index );
817 /* returns the sequence element by its index */
818 /*CV_IMPL CvFileNode*
819 cvGetFileNodeFromSeq( CvFileStorage* fs,
820 CvFileNode* seq_node, int index )
822 CvFileNode* value = 0;
827 else if( !CV_NODE_IS_SEQ(seq_node->tag) )
829 if( CV_NODE_IS_MAP(seq_node->tag) )
830 CV_Error( CV_StsError, "The node is map. Use cvGetFileNodeFromMap()." );
831 if( CV_NODE_TYPE(seq_node->tag) == CV_NODE_NONE )
832 CV_Error( CV_StsError, "The node is an empty object (None)." );
833 if( index != 0 && index != -1 )
834 CV_Error( CV_StsOutOfRange, "" );
839 seq = seq_node->data.seq;
842 CV_Error( CV_StsNullPtr, "The file storage is empty" );
844 value = (CvFileNode*)cvGetSeqElem( seq, index, 0 );
853 icvDoubleToString( char* buf, double value )
859 ieee754_hi = (unsigned)(val.u >> 32);
861 if( (ieee754_hi & 0x7ff00000) != 0x7ff00000 )
863 int ivalue = cvRound(value);
864 if( ivalue == value )
865 sprintf( buf, "%d.", ivalue );
868 static const char* fmt = "%.16e";
870 sprintf( buf, fmt, value );
871 if( *ptr == '+' || *ptr == '-' )
873 for( ; cv_isdigit(*ptr); ptr++ )
881 unsigned ieee754_lo = (unsigned)val.u;
882 if( (ieee754_hi & 0x7fffffff) + (ieee754_lo != 0) > 0x7ff00000 )
883 strcpy( buf, ".Nan" );
885 strcpy( buf, (int)ieee754_hi < 0 ? "-.Inf" : ".Inf" );
893 icvFloatToString( char* buf, float value )
900 if( (ieee754 & 0x7f800000) != 0x7f800000 )
902 int ivalue = cvRound(value);
903 if( ivalue == value )
904 sprintf( buf, "%d.", ivalue );
907 static const char* fmt = "%.8e";
909 sprintf( buf, fmt, value );
910 if( *ptr == '+' || *ptr == '-' )
912 for( ; cv_isdigit(*ptr); ptr++ )
920 if( (ieee754 & 0x7fffffff) != 0x7f800000 )
921 strcpy( buf, ".Nan" );
923 strcpy( buf, (int)ieee754 < 0 ? "-.Inf" : ".Inf" );
931 icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr )
934 int inf_hi = 0x7ff00000;
936 if( c == '-' || c == '+' )
938 inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000;
943 CV_PARSE_ERROR( "Bad format of floating-point constant" );
945 union{double d; uint64 i;} v;
947 if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' )
948 v.i = (uint64)inf_hi << 32;
949 else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' )
952 CV_PARSE_ERROR( "Bad format of floating-point constant" );
959 static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
961 double fval = strtod( ptr, endptr );
962 if( **endptr == '.' )
964 char* dot_pos = *endptr;
966 double fval2 = strtod( ptr, endptr );
968 if( *endptr > dot_pos )
974 if( *endptr == ptr || cv_isalpha(**endptr) )
975 icvProcessSpecialDouble( fs, ptr, &fval, endptr );
981 /****************************************************************************************\
983 \****************************************************************************************/
986 icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent )
994 if( ptr - fs->buffer_start > max_comment_indent )
998 else if( cv_isprint(*ptr) )
1000 if( ptr - fs->buffer_start < min_indent )
1001 CV_PARSE_ERROR( "Incorrect indentation" );
1004 else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' )
1006 int max_size = (int)(fs->buffer_end - fs->buffer_start);
1007 ptr = icvGets( fs, fs->buffer_start, max_size );
1010 // emulate end of stream
1011 ptr = fs->buffer_start;
1012 ptr[0] = ptr[1] = ptr[2] = '.';
1019 int l = (int)strlen(ptr);
1020 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) )
1021 CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
1027 CV_PARSE_ERROR( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" );
1035 icvYMLParseKey( CvFileStorage* fs, char* ptr,
1036 CvFileNode* map_node, CvFileNode** value_placeholder )
1039 char *endptr = ptr - 1, *saveptr;
1040 CvStringHashNode* str_hash_node;
1043 CV_PARSE_ERROR( "Key may not start with \'-\'" );
1046 while( cv_isprint(c) && c != ':' );
1049 CV_PARSE_ERROR( "Missing \':\'" );
1051 saveptr = endptr + 1;
1057 CV_PARSE_ERROR( "An empty key" );
1059 str_hash_node = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 );
1060 *value_placeholder = cvGetFileNode( fs, map_node, str_hash_node, 1 );
1068 icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
1069 int parent_flags, int min_indent )
1071 char buf[CV_FS_MAX_LEN + 1024];
1073 char c = ptr[0], d = ptr[1];
1074 int is_parent_flow = CV_NODE_IS_FLOW(parent_flags);
1075 int value_type = CV_NODE_NONE;
1078 memset( node, 0, sizeof(*node) );
1080 if( c == '!' ) // handle explicit type specification
1082 if( d == '!' || d == '^' )
1085 value_type |= CV_NODE_USER;
1090 while( cv_isprint(d) && d != ' ' );
1091 len = (int)(endptr - ptr);
1093 CV_PARSE_ERROR( "Empty type name" );
1097 if( len == 3 && !CV_NODE_IS_USER(value_type) )
1099 if( memcmp( ptr, "str", 3 ) == 0 )
1100 value_type = CV_NODE_STRING;
1101 else if( memcmp( ptr, "int", 3 ) == 0 )
1102 value_type = CV_NODE_INT;
1103 else if( memcmp( ptr, "seq", 3 ) == 0 )
1104 value_type = CV_NODE_SEQ;
1105 else if( memcmp( ptr, "map", 3 ) == 0 )
1106 value_type = CV_NODE_MAP;
1108 else if( len == 5 && !CV_NODE_IS_USER(value_type) )
1110 if( memcmp( ptr, "float", 5 ) == 0 )
1111 value_type = CV_NODE_REAL;
1113 else if( CV_NODE_IS_USER(value_type) )
1115 node->info = cvFindType( ptr );
1117 node->tag &= ~CV_NODE_USER;
1121 ptr = icvYMLSkipSpaces( fs, endptr, min_indent, INT_MAX );
1125 if( !CV_NODE_IS_USER(value_type) )
1127 if( value_type == CV_NODE_STRING && c != '\'' && c != '\"' )
1129 if( value_type == CV_NODE_INT )
1131 if( value_type == CV_NODE_REAL )
1136 if( cv_isdigit(c) ||
1137 ((c == '-' || c == '+') && (cv_isdigit(d) || d == '.')) ||
1138 (c == '.' && cv_isalnum(d))) // a number
1142 endptr = ptr + (c == '-' || c == '+');
1143 while( cv_isdigit(*endptr) )
1145 if( *endptr == '.' || *endptr == 'e' )
1148 fval = icv_strtod( fs, ptr, &endptr );
1149 /*if( endptr == ptr || cv_isalpha(*endptr) )
1150 icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/
1152 node->tag = CV_NODE_REAL;
1153 node->data.f = fval;
1158 ival = (int)strtol( ptr, &endptr, 0 );
1159 node->tag = CV_NODE_INT;
1160 node->data.i = ival;
1163 if( !endptr || endptr == ptr )
1164 CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
1168 else if( c == '\'' || c == '\"' ) // an explicit string
1170 node->tag = CV_NODE_STRING;
1172 for( len = 0; len < CV_FS_MAX_LEN; )
1175 if( cv_isalnum(c) || (c != '\'' && cv_isprint(c)))
1177 else if( c == '\'' )
1185 CV_PARSE_ERROR( "Invalid character" );
1188 for( len = 0; len < CV_FS_MAX_LEN; )
1191 if( cv_isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c)))
1193 else if( c == '\"' )
1198 else if( c == '\\' )
1203 else if( d == '\"' || d == '\\' || d == '\'' )
1211 else if( d == 'x' || (cv_isdigit(d) && d < '8') )
1213 int val, is_hex = d == 'x';
1216 val = strtol( ptr + is_hex, &endptr, is_hex ? 8 : 16 );
1218 if( endptr == ptr + is_hex )
1222 buf[len++] = (char)val;
1228 CV_PARSE_ERROR( "Invalid character" );
1231 if( len >= CV_FS_MAX_LEN )
1232 CV_PARSE_ERROR( "Too long string literal" );
1234 node->data.str = cvMemStorageAllocString( fs->memstorage, buf, len );
1236 else if( c == '[' || c == '{' ) // collection as a flow
1238 int new_min_indent = min_indent + !is_parent_flow;
1239 int struct_flags = CV_NODE_FLOW + (c == '{' ? CV_NODE_MAP : CV_NODE_SEQ);
1242 icvFSCreateCollection( fs, CV_NODE_TYPE(struct_flags) +
1243 (node->info ? CV_NODE_USER : 0), node );
1245 d = c == '[' ? ']' : '}';
1249 CvFileNode* elem = 0;
1251 ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX );
1252 if( *ptr == '}' || *ptr == ']' )
1255 CV_PARSE_ERROR( "The wrong closing bracket" );
1260 if( node->data.seq->total != 0 )
1263 CV_PARSE_ERROR( "Missing , between the elements" );
1264 ptr = icvYMLSkipSpaces( fs, ptr + 1, new_min_indent, INT_MAX );
1267 if( CV_NODE_IS_MAP(struct_flags) )
1269 ptr = icvYMLParseKey( fs, ptr, node, &elem );
1270 ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX );
1276 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1278 ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, new_min_indent );
1279 if( CV_NODE_IS_MAP(struct_flags) )
1280 elem->tag |= CV_NODE_NAMED;
1281 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1283 node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
1287 int indent, struct_flags, is_simple;
1289 if( is_parent_flow || c != '-' )
1291 // implicit (one-line) string or nested block-style collection
1292 if( !is_parent_flow )
1295 CV_PARSE_ERROR( "Complex keys are not supported" );
1296 if( c == '|' || c == '>' )
1297 CV_PARSE_ERROR( "Multi-line text literals are not supported" );
1304 while( cv_isprint(c) &&
1305 (!is_parent_flow || (c != ',' && c != '}' && c != ']')) &&
1306 (is_parent_flow || c != ':' || value_type == CV_NODE_STRING));
1309 CV_PARSE_ERROR( "Invalid character" );
1311 if( is_parent_flow || c != ':' )
1313 char* str_end = endptr;
1314 node->tag = CV_NODE_STRING;
1315 // strip spaces in the end of string
1317 while( str_end > ptr && c == ' ' );
1319 node->data.str = cvMemStorageAllocString( fs->memstorage, ptr, (int)(str_end - ptr) );
1323 struct_flags = CV_NODE_MAP;
1326 struct_flags = CV_NODE_SEQ;
1328 icvFSCreateCollection( fs, struct_flags +
1329 (node->info ? CV_NODE_USER : 0), node );
1331 indent = (int)(ptr - fs->buffer_start);
1336 CvFileNode* elem = 0;
1338 if( CV_NODE_IS_MAP(struct_flags) )
1340 ptr = icvYMLParseKey( fs, ptr, node, &elem );
1346 CV_PARSE_ERROR( "Block sequence elements must be preceded with \'-\'" );
1348 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1351 ptr = icvYMLSkipSpaces( fs, ptr, indent + 1, INT_MAX );
1352 ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, indent + 1 );
1353 if( CV_NODE_IS_MAP(struct_flags) )
1354 elem->tag |= CV_NODE_NAMED;
1355 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1357 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1358 if( ptr - fs->buffer_start != indent )
1360 if( ptr - fs->buffer_start < indent )
1363 CV_PARSE_ERROR( "Incorrect indentation" );
1365 if( memcmp( ptr, "...", 3 ) == 0 )
1369 node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0;
1377 icvYMLParse( CvFileStorage* fs )
1379 char* ptr = fs->buffer_start;
1384 // 0. skip leading comments and directives and ...
1385 // 1. reach the first item
1388 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1394 if( memcmp( ptr, "%YAML:", 6 ) == 0 &&
1395 memcmp( ptr, "%YAML:1.", 8 ) != 0 )
1396 CV_PARSE_ERROR( "Unsupported YAML version (it must be 1.x)" );
1399 else if( *ptr == '-' )
1401 if( memcmp(ptr, "---", 3) == 0 )
1409 else if( cv_isalnum(*ptr) || *ptr=='_')
1412 CV_PARSE_ERROR( "The YAML streams must start with '---', except the first one" );
1415 else if( fs->dummy_eof )
1418 CV_PARSE_ERROR( "Invalid or unsupported syntax" );
1421 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1422 if( memcmp( ptr, "...", 3 ) != 0 )
1424 // 2. parse the collection
1425 CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
1427 ptr = icvYMLParseValue( fs, ptr, root_node, CV_NODE_NONE, 0 );
1428 if( !CV_NODE_IS_COLLECTION(root_node->tag) )
1429 CV_PARSE_ERROR( "Only collections as YAML streams are supported by this parser" );
1431 // 3. parse until the end of file or next collection
1432 ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX );
1445 /****************************************************************************************\
1447 \****************************************************************************************/
1450 icvYMLWrite( CvFileStorage* fs, const char* key, const char* data )
1457 struct_flags = fs->struct_flags;
1459 if( key && key[0] == '\0' )
1462 if( CV_NODE_IS_COLLECTION(struct_flags) )
1464 if( (CV_NODE_IS_MAP(struct_flags) ^ (key != 0)) )
1465 CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, "
1466 "or add element with key to sequence" );
1471 struct_flags = CV_NODE_EMPTY | (key ? CV_NODE_MAP : CV_NODE_SEQ);
1476 keylen = (int)strlen(key);
1478 CV_Error( CV_StsBadArg, "The key is an empty" );
1480 if( keylen > CV_FS_MAX_LEN )
1481 CV_Error( CV_StsBadArg, "The key is too long" );
1485 datalen = (int)strlen(data);
1487 if( CV_NODE_IS_FLOW(struct_flags) )
1491 if( !CV_NODE_IS_EMPTY(struct_flags) )
1493 new_offset = (int)(ptr - fs->buffer_start) + keylen + datalen;
1494 if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 )
1497 ptr = icvFSFlush(fs);
1504 ptr = icvFSFlush(fs);
1505 if( !CV_NODE_IS_MAP(struct_flags) )
1515 if( !cv_isalpha(key[0]) && key[0] != '_' )
1516 CV_Error( CV_StsBadArg, "Key must start with a letter or _" );
1518 ptr = icvFSResizeWriteBuffer( fs, ptr, keylen );
1520 for( i = 0; i < keylen; i++ )
1525 if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' )
1526 CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" );
1531 if( !CV_NODE_IS_FLOW(struct_flags) && data )
1537 ptr = icvFSResizeWriteBuffer( fs, ptr, datalen );
1538 memcpy( ptr, data, datalen );
1543 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
1548 icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
1549 const char* type_name CV_DEFAULT(0))
1552 char buf[CV_FS_MAX_LEN + 1024];
1553 const char* data = 0;
1555 struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
1556 if( !CV_NODE_IS_COLLECTION(struct_flags))
1557 CV_Error( CV_StsBadArg,
1558 "Some collection type - CV_NODE_SEQ or CV_NODE_MAP, must be specified" );
1560 if( CV_NODE_IS_FLOW(struct_flags) )
1562 char c = CV_NODE_IS_MAP(struct_flags) ? '{' : '[';
1563 struct_flags |= CV_NODE_FLOW;
1566 sprintf( buf, "!!%s %c", type_name, c );
1574 else if( type_name )
1576 sprintf( buf, "!!%s", type_name );
1580 icvYMLWrite( fs, key, data );
1582 parent_flags = fs->struct_flags;
1583 cvSeqPush( fs->write_stack, &parent_flags );
1584 fs->struct_flags = struct_flags;
1586 if( !CV_NODE_IS_FLOW(parent_flags) )
1587 fs->struct_indent += CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1592 icvYMLEndWriteStruct( CvFileStorage* fs )
1594 int parent_flags = 0, struct_flags;
1597 struct_flags = fs->struct_flags;
1598 if( fs->write_stack->total == 0 )
1599 CV_Error( CV_StsError, "EndWriteStruct w/o matching StartWriteStruct" );
1601 cvSeqPop( fs->write_stack, &parent_flags );
1603 if( CV_NODE_IS_FLOW(struct_flags) )
1606 if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) )
1608 *ptr++ = CV_NODE_IS_MAP(struct_flags) ? '}' : ']';
1611 else if( CV_NODE_IS_EMPTY(struct_flags) )
1613 ptr = icvFSFlush(fs);
1614 memcpy( ptr, CV_NODE_IS_MAP(struct_flags) ? "{}" : "[]", 2 );
1615 fs->buffer = ptr + 2;
1618 if( !CV_NODE_IS_FLOW(parent_flags) )
1619 fs->struct_indent -= CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1620 assert( fs->struct_indent >= 0 );
1622 fs->struct_flags = parent_flags;
1627 icvYMLStartNextStream( CvFileStorage* fs )
1631 while( fs->write_stack->total > 0 )
1632 icvYMLEndWriteStruct(fs);
1634 fs->struct_indent = 0;
1636 icvPuts( fs, "...\n" );
1637 icvPuts( fs, "---\n" );
1638 fs->buffer = fs->buffer_start;
1644 icvYMLWriteInt( CvFileStorage* fs, const char* key, int value )
1647 icvYMLWrite( fs, key, icv_itoa( value, buf, 10 ));
1652 icvYMLWriteReal( CvFileStorage* fs, const char* key, double value )
1655 icvYMLWrite( fs, key, icvDoubleToString( buf, value ));
1660 icvYMLWriteString( CvFileStorage* fs, const char* key,
1661 const char* str, int quote CV_DEFAULT(0))
1663 char buf[CV_FS_MAX_LEN*4+16];
1664 char* data = (char*)str;
1668 CV_Error( CV_StsNullPtr, "Null string pointer" );
1670 len = (int)strlen(str);
1671 if( len > CV_FS_MAX_LEN )
1672 CV_Error( CV_StsBadArg, "The written string is too long" );
1674 if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') )
1676 int need_quote = quote || len == 0;
1679 for( i = 0; i < len; i++ )
1683 if( !need_quote && !cv_isalnum(c) && c != '_' && c != ' ' && c != '-' &&
1684 c != '(' && c != ')' && c != '/' && c != '+' && c != ';' )
1687 if( !cv_isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') )
1692 else if( c == '\n' )
1694 else if( c == '\r' )
1696 else if( c == '\t' )
1700 sprintf( data, "x%02x", c );
1707 if( !need_quote && (cv_isdigit(str[0]) ||
1708 str[0] == '+' || str[0] == '-' || str[0] == '.' ))
1714 data = buf + !need_quote;
1717 icvYMLWrite( fs, key, data );
1722 icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
1724 int len; //, indent;
1730 CV_Error( CV_StsNullPtr, "Null comment" );
1732 len = (int)strlen(comment);
1733 eol = strchr(comment, '\n');
1734 multiline = eol != 0;
1737 if( !eol_comment || multiline ||
1738 fs->buffer_end - ptr < len || ptr == fs->buffer_start )
1739 ptr = icvFSFlush( fs );
1749 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
1750 memcpy( ptr, comment, eol - comment + 1 );
1751 fs->buffer = ptr + (eol - comment);
1753 eol = strchr( comment, '\n' );
1757 len = (int)strlen(comment);
1758 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
1759 memcpy( ptr, comment, len );
1760 fs->buffer = ptr + len;
1763 ptr = icvFSFlush( fs );
1768 /****************************************************************************************\
1770 \****************************************************************************************/
1772 #define CV_XML_INSIDE_COMMENT 1
1773 #define CV_XML_INSIDE_TAG 2
1774 #define CV_XML_INSIDE_DIRECTIVE 3
1777 icvXMLSkipSpaces( CvFileStorage* fs, char* ptr, int mode )
1786 if( mode == CV_XML_INSIDE_COMMENT )
1789 while( cv_isprint_or_tab(c) && (c != '-' || ptr[1] != '-' || ptr[2] != '>') );
1793 assert( ptr[1] == '-' && ptr[2] == '>' );
1798 else if( mode == CV_XML_INSIDE_DIRECTIVE )
1800 // !!!NOTE!!! This is not quite correct, but should work in most cases
1808 } while( cv_isprint_or_tab(c) );
1813 while( c == ' ' || c == '\t' );
1815 if( c == '<' && ptr[1] == '!' && ptr[2] == '-' && ptr[3] == '-' )
1818 CV_PARSE_ERROR( "Comments are not allowed here" );
1819 mode = CV_XML_INSIDE_COMMENT;
1822 else if( cv_isprint(c) )
1826 if( !cv_isprint(*ptr) )
1828 int max_size = (int)(fs->buffer_end - fs->buffer_start);
1829 if( *ptr != '\0' && *ptr != '\n' && *ptr != '\r' )
1830 CV_PARSE_ERROR( "Invalid character in the stream" );
1831 ptr = icvGets( fs, fs->buffer_start, max_size );
1834 ptr = fs->buffer_start;
1841 int l = (int)strlen(ptr);
1842 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) )
1843 CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
1853 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
1854 CvAttrList** _list, int* _tag_type );
1857 icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
1858 int value_type CV_DEFAULT(CV_NODE_NONE))
1860 CvFileNode *elem = node;
1861 int have_space = 1, is_simple = 1;
1862 int is_user_type = CV_NODE_IS_USER(value_type);
1863 memset( node, 0, sizeof(*node) );
1865 value_type = CV_NODE_TYPE(value_type);
1872 if( cv_isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') )
1874 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
1881 if( c =='<' || c == '\0' )
1883 CvStringHashNode *key = 0, *key2 = 0;
1884 CvAttrList* list = 0;
1885 CvTypeInfo* info = 0;
1888 const char* type_name = 0;
1889 int elem_type = CV_NODE_NONE;
1891 if( d == '/' || c == '\0' )
1894 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
1896 if( tag_type == CV_XML_DIRECTIVE_TAG )
1897 CV_PARSE_ERROR( "Directive tags are not allowed here" );
1898 if( tag_type == CV_XML_EMPTY_TAG )
1899 CV_PARSE_ERROR( "Empty tags are not supported" );
1901 assert( tag_type == CV_XML_OPENING_TAG );
1903 type_name = list ? cvAttrValue( list, "type_id" ) : 0;
1906 if( strcmp( type_name, "str" ) == 0 )
1907 elem_type = CV_NODE_STRING;
1908 else if( strcmp( type_name, "map" ) == 0 )
1909 elem_type = CV_NODE_MAP;
1910 else if( strcmp( type_name, "seq" ) == 0 )
1911 elem_type = CV_NODE_SEQ;
1914 info = cvFindType( type_name );
1916 elem_type = CV_NODE_USER;
1920 is_noname = key->str.len == 1 && key->str.ptr[0] == '_';
1921 if( !CV_NODE_IS_COLLECTION(node->tag) )
1923 icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node );
1925 else if( is_noname ^ CV_NODE_IS_SEQ(node->tag) )
1926 CV_PARSE_ERROR( is_noname ? "Map element should have a name" :
1927 "Sequence element should not have name (use <_></_>)" );
1930 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1932 elem = cvGetFileNode( fs, node, key, 1 );
1934 ptr = icvXMLParseValue( fs, ptr, elem, elem_type);
1936 elem->tag |= CV_NODE_NAMED;
1937 is_simple &= !CV_NODE_IS_COLLECTION(elem->tag);
1939 ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type );
1940 if( tag_type != CV_XML_CLOSING_TAG || key2 != key )
1941 CV_PARSE_ERROR( "Mismatched closing tag" );
1947 CV_PARSE_ERROR( "There should be space between literals" );
1950 if( node->tag != CV_NODE_NONE )
1952 if( !CV_NODE_IS_COLLECTION(node->tag) )
1953 icvFSCreateCollection( fs, CV_NODE_SEQ, node );
1955 elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 );
1959 if( value_type != CV_NODE_STRING &&
1960 (cv_isdigit(c) || ((c == '-' || c == '+') &&
1961 (cv_isdigit(d) || d == '.')) || (c == '.' && cv_isalnum(d))) ) // a number
1965 endptr = ptr + (c == '-' || c == '+');
1966 while( cv_isdigit(*endptr) )
1968 if( *endptr == '.' || *endptr == 'e' )
1970 fval = icv_strtod( fs, ptr, &endptr );
1971 /*if( endptr == ptr || cv_isalpha(*endptr) )
1972 icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
1973 elem->tag = CV_NODE_REAL;
1974 elem->data.f = fval;
1978 ival = (int)strtol( ptr, &endptr, 0 );
1979 elem->tag = CV_NODE_INT;
1980 elem->data.i = ival;
1984 CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" );
1991 char buf[CV_FS_MAX_LEN+16];
1992 int i = 0, len, is_quoted = 0;
1993 elem->tag = CV_NODE_STRING;
2002 if( !cv_isalnum(c) )
2007 CV_PARSE_ERROR( "Literal \" is not allowed within a string. Use "" );
2011 else if( !cv_isprint(c) || c == '<' || (!is_quoted && cv_isspace(c)))
2014 CV_PARSE_ERROR( "Closing \" is expected" );
2017 else if( c == '\'' || c == '>' )
2019 CV_PARSE_ERROR( "Literal \' or > are not allowed. Use ' or >" );
2032 val = (int)strtol( ptr, &endptr, base );
2033 if( (unsigned)val > (unsigned)255 ||
2034 !endptr || *endptr != ';' )
2035 CV_PARSE_ERROR( "Invalid numeric value in the string" );
2042 while( cv_isalnum(c) );
2044 CV_PARSE_ERROR( "Invalid character in the symbol entity name" );
2045 len = (int)(endptr - ptr);
2046 if( len == 2 && memcmp( ptr, "lt", len ) == 0 )
2048 else if( len == 2 && memcmp( ptr, "gt", len ) == 0 )
2050 else if( len == 3 && memcmp( ptr, "amp", len ) == 0 )
2052 else if( len == 4 && memcmp( ptr, "apos", len ) == 0 )
2054 else if( len == 4 && memcmp( ptr, "quot", len ) == 0 )
2058 memcpy( buf + i, ptr-1, len + 2 );
2066 if( i >= CV_FS_MAX_LEN )
2067 CV_PARSE_ERROR( "Too long string literal" );
2069 elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i );
2072 if( !CV_NODE_IS_COLLECTION(value_type) && value_type != CV_NODE_NONE )
2078 if( (CV_NODE_TYPE(node->tag) == CV_NODE_NONE ||
2079 (CV_NODE_TYPE(node->tag) != value_type &&
2080 !CV_NODE_IS_COLLECTION(node->tag))) &&
2081 CV_NODE_IS_COLLECTION(value_type) )
2083 icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ?
2084 CV_NODE_MAP : CV_NODE_SEQ, node );
2087 if( value_type != CV_NODE_NONE &&
2088 value_type != CV_NODE_TYPE(node->tag) )
2089 CV_PARSE_ERROR( "The actual type is different from the specified type" );
2091 if( CV_NODE_IS_COLLECTION(node->tag) && is_simple )
2092 node->data.seq->flags |= CV_NODE_SEQ_SIMPLE;
2094 node->tag |= is_user_type ? CV_NODE_USER : 0;
2100 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
2101 CvAttrList** _list, int* _tag_type )
2104 CvStringHashNode* tagname = 0;
2105 CvAttrList *first = 0, *last = 0;
2106 int count = 0, max_count = 4;
2107 int attr_buf_size = (max_count*2 + 1)*sizeof(char*) + sizeof(CvAttrList);
2113 CV_PARSE_ERROR( "Preliminary end of the stream" );
2116 CV_PARSE_ERROR( "Tag should start with \'<\'" );
2119 if( cv_isalnum(*ptr) || *ptr == '_' )
2120 tag_type = CV_XML_OPENING_TAG;
2121 else if( *ptr == '/' )
2123 tag_type = CV_XML_CLOSING_TAG;
2126 else if( *ptr == '?' )
2128 tag_type = CV_XML_HEADER_TAG;
2131 else if( *ptr == '!' )
2133 tag_type = CV_XML_DIRECTIVE_TAG;
2134 assert( ptr[1] != '-' || ptr[2] != '-' );
2138 CV_PARSE_ERROR( "Unknown tag type" );
2142 CvStringHashNode* attrname;
2144 if( !cv_isalpha(*ptr) && *ptr != '_' )
2145 CV_PARSE_ERROR( "Name should start with a letter or underscore" );
2149 while( cv_isalnum(c) || c == '_' || c == '-' );
2151 attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 );
2158 if( tag_type == CV_XML_CLOSING_TAG )
2159 CV_PARSE_ERROR( "Closing tag should not contain any attributes" );
2161 if( !last || count >= max_count )
2165 chunk = (CvAttrList*)cvMemStorageAlloc( fs->memstorage, attr_buf_size );
2166 memset( chunk, 0, attr_buf_size );
2167 chunk->attr = (const char**)(chunk + 1);
2170 first = last = chunk;
2172 last = last->next = chunk;
2174 last->attr[count*2] = attrname->str.ptr;
2183 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2185 CV_PARSE_ERROR( "Attribute name should be followed by \'=\'" );
2189 if( c != '\"' && c != '\'' )
2191 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2192 if( *ptr != '\"' && *ptr != '\'' )
2193 CV_PARSE_ERROR( "Attribute value should be put into single or double quotes" );
2196 ptr = icvXMLParseValue( fs, ptr, &stub, CV_NODE_STRING );
2197 assert( stub.tag == CV_NODE_STRING );
2198 last->attr[count*2+1] = stub.data.str.ptr;
2203 have_space = cv_isspace(c) || c == '\0';
2207 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2213 if( tag_type == CV_XML_HEADER_TAG )
2214 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
2218 else if( c == '?' && tag_type == CV_XML_HEADER_TAG )
2221 CV_PARSE_ERROR( "Invalid closing tag for <?xml ..." );
2225 else if( c == '/' && ptr[1] == '>' && tag_type == CV_XML_OPENING_TAG )
2227 tag_type = CV_XML_EMPTY_TAG;
2233 CV_PARSE_ERROR( "There should be space between attributes" );
2237 *_tag_type = tag_type;
2245 icvXMLParse( CvFileStorage* fs )
2247 char* ptr = fs->buffer_start;
2248 CvStringHashNode *key = 0, *key2 = 0;
2249 CvAttrList* list = 0;
2252 // CV_XML_INSIDE_TAG is used to prohibit leading comments
2253 ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG );
2255 if( memcmp( ptr, "<?xml", 5 ) != 0 )
2257 CV_PARSE_ERROR( "Valid XML should start with \'<?xml ...?>\'" );
2259 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
2262 const char* version = cvAttrValue( list, "version" );
2263 if( version && strncmp( version, "1.", 2 ) != 0 )
2264 CV_Error( CV_StsParseError, "Unsupported version of XML" );
2266 // we support any 8-bit encoding, so we do not need to check the actual encoding.
2267 // we do not support utf-16, but in the case of utf-16 we will not get here anyway.
2269 const char* encoding = cvAttrValue( list, "encoding" );
2270 if( encoding && strcmp( encoding, "ASCII" ) != 0 &&
2271 strcmp( encoding, "UTF-8" ) != 0 &&
2272 strcmp( encoding, "utf-8" ) != 0 )
2273 CV_PARSE_ERROR( "Unsupported encoding" );
2276 while( *ptr != '\0' )
2278 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
2282 CvFileNode* root_node;
2283 ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type );
2284 if( tag_type != CV_XML_OPENING_TAG ||
2285 strcmp(key->str.ptr,"opencv_storage") != 0 )
2286 CV_PARSE_ERROR( "<opencv_storage> tag is missing" );
2288 root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
2289 ptr = icvXMLParseValue( fs, ptr, root_node, CV_NODE_NONE );
2290 ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type );
2291 if( tag_type != CV_XML_CLOSING_TAG || key != key2 )
2292 CV_PARSE_ERROR( "</opencv_storage> tag is missing" );
2293 ptr = icvXMLSkipSpaces( fs, ptr, 0 );
2297 assert( fs->dummy_eof != 0 );
2301 /****************************************************************************************\
2303 \****************************************************************************************/
2305 #define icvXMLFlush icvFSFlush
2308 icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList list )
2310 char* ptr = fs->buffer;
2312 int struct_flags = fs->struct_flags;
2314 if( key && key[0] == '\0' )
2317 if( tag_type == CV_XML_OPENING_TAG || tag_type == CV_XML_EMPTY_TAG )
2319 if( CV_NODE_IS_COLLECTION(struct_flags) )
2321 if( CV_NODE_IS_MAP(struct_flags) ^ (key != 0) )
2322 CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, "
2323 "or add element with key to sequence" );
2327 struct_flags = CV_NODE_EMPTY + (key ? CV_NODE_MAP : CV_NODE_SEQ);
2331 if( !CV_NODE_IS_EMPTY(struct_flags) )
2332 ptr = icvXMLFlush(fs);
2337 else if( key[0] == '_' && key[1] == '\0' )
2338 CV_Error( CV_StsBadArg, "A single _ is a reserved tag name" );
2340 len = (int)strlen( key );
2342 if( tag_type == CV_XML_CLOSING_TAG )
2345 CV_Error( CV_StsBadArg, "Closing tag should not include any attributes" );
2349 if( !cv_isalpha(key[0]) && key[0] != '_' )
2350 CV_Error( CV_StsBadArg, "Key should start with a letter or _" );
2352 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2353 for( i = 0; i < len; i++ )
2356 if( !cv_isalnum(c) && c != '_' && c != '-' )
2357 CV_Error( CV_StsBadArg, "Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_'" );
2364 const char** attr = list.attr;
2366 for( ; attr && attr[0] != 0; attr += 2 )
2368 int len0 = (int)strlen(attr[0]);
2369 int len1 = (int)strlen(attr[1]);
2371 ptr = icvFSResizeWriteBuffer( fs, ptr, len0 + len1 + 4 );
2373 memcpy( ptr, attr[0], len0 );
2377 memcpy( ptr, attr[1], len1 );
2386 if( tag_type == CV_XML_EMPTY_TAG )
2390 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
2395 icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2396 const char* type_name CV_DEFAULT(0))
2398 CvXMLStackRecord parent;
2399 const char* attr[10];
2402 struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY;
2403 if( !CV_NODE_IS_COLLECTION(struct_flags))
2404 CV_Error( CV_StsBadArg,
2405 "Some collection type: CV_NODE_SEQ or CV_NODE_MAP must be specified" );
2409 attr[idx++] = "type_id";
2410 attr[idx++] = type_name;
2414 icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(attr,0) );
2416 parent.struct_flags = fs->struct_flags & ~CV_NODE_EMPTY;
2417 parent.struct_indent = fs->struct_indent;
2418 parent.struct_tag = fs->struct_tag;
2419 cvSaveMemStoragePos( fs->strstorage, &parent.pos );
2420 cvSeqPush( fs->write_stack, &parent );
2422 fs->struct_indent += CV_XML_INDENT;
2423 if( !CV_NODE_IS_FLOW(struct_flags) )
2426 fs->struct_flags = struct_flags;
2429 fs->struct_tag = cvMemStorageAllocString( fs->strstorage, (char*)key, -1 );
2433 fs->struct_tag.ptr = 0;
2434 fs->struct_tag.len = 0;
2440 icvXMLEndWriteStruct( CvFileStorage* fs )
2442 CvXMLStackRecord parent;
2444 if( fs->write_stack->total == 0 )
2445 CV_Error( CV_StsError, "An extra closing tag" );
2447 icvXMLWriteTag( fs, fs->struct_tag.ptr, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
2448 cvSeqPop( fs->write_stack, &parent );
2450 fs->struct_indent = parent.struct_indent;
2451 fs->struct_flags = parent.struct_flags;
2452 fs->struct_tag = parent.struct_tag;
2453 cvRestoreMemStoragePos( fs->strstorage, &parent.pos );
2458 icvXMLStartNextStream( CvFileStorage* fs )
2462 while( fs->write_stack->total > 0 )
2463 icvXMLEndWriteStruct(fs);
2465 fs->struct_indent = 0;
2467 /* XML does not allow multiple top-level elements,
2468 so we just put a comment and continue
2469 the current (and the only) "stream" */
2470 icvPuts( fs, "\n<!-- next stream -->\n" );
2471 /*fputs( "</opencv_storage>\n", fs->file );
2472 fputs( "<opencv_storage>\n", fs->file );*/
2473 fs->buffer = fs->buffer_start;
2479 icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len )
2481 if( CV_NODE_IS_MAP(fs->struct_flags) ||
2482 (!CV_NODE_IS_COLLECTION(fs->struct_flags) && key) )
2484 icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(0,0) );
2485 char* ptr = icvFSResizeWriteBuffer( fs, fs->buffer, len );
2486 memcpy( ptr, data, len );
2487 fs->buffer = ptr + len;
2488 icvXMLWriteTag( fs, key, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
2492 char* ptr = fs->buffer;
2493 int new_offset = (int)(ptr - fs->buffer_start) + len;
2496 CV_Error( CV_StsBadArg, "elements with keys can not be written to sequence" );
2498 fs->struct_flags = CV_NODE_SEQ;
2500 if( (new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10) ||
2501 (ptr > fs->buffer_start && ptr[-1] == '>' && !CV_NODE_IS_EMPTY(fs->struct_flags)) )
2503 ptr = icvXMLFlush(fs);
2505 else if( ptr > fs->buffer_start + fs->struct_indent && ptr[-1] != '>' )
2508 memcpy( ptr, data, len );
2509 fs->buffer = ptr + len;
2515 icvXMLWriteInt( CvFileStorage* fs, const char* key, int value )
2517 char buf[128], *ptr = icv_itoa( value, buf, 10 );
2518 int len = (int)strlen(ptr);
2519 icvXMLWriteScalar( fs, key, ptr, len );
2524 icvXMLWriteReal( CvFileStorage* fs, const char* key, double value )
2527 int len = (int)strlen( icvDoubleToString( buf, value ));
2528 icvXMLWriteScalar( fs, key, buf, len );
2533 icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote )
2535 char buf[CV_FS_MAX_LEN*6+16];
2536 char* data = (char*)str;
2540 CV_Error( CV_StsNullPtr, "Null string pointer" );
2542 len = (int)strlen(str);
2543 if( len > CV_FS_MAX_LEN )
2544 CV_Error( CV_StsBadArg, "The written string is too long" );
2546 if( quote || len == 0 || str[0] != '\"' || str[0] != str[len-1] )
2548 int need_quote = quote || len == 0;
2551 for( i = 0; i < len; i++ )
2555 if( (uchar)c >= 128 || c == ' ' )
2560 else if( !cv_isprint(c) || c == '<' || c == '>' || c == '&' || c == '\'' || c == '\"' )
2565 memcpy(data, "lt", 2);
2570 memcpy(data, "gt", 2);
2575 memcpy(data, "amp", 3);
2578 else if( c == '\'' )
2580 memcpy(data, "apos", 4);
2583 else if( c == '\"' )
2585 memcpy( data, "quot", 4);
2590 sprintf( data, "#x%02x", (uchar)c );
2599 if( !need_quote && (cv_isdigit(str[0]) ||
2600 str[0] == '+' || str[0] == '-' || str[0] == '.' ))
2605 len = (int)(data - buf) - !need_quote;
2607 data = buf + !need_quote;
2610 icvXMLWriteScalar( fs, key, data, len );
2615 icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2623 CV_Error( CV_StsNullPtr, "Null comment" );
2625 if( strstr(comment, "--") != 0 )
2626 CV_Error( CV_StsBadArg, "Double hyphen \'--\' is not allowed in the comments" );
2628 len = (int)strlen(comment);
2629 eol = strchr(comment, '\n');
2630 multiline = eol != 0;
2633 if( multiline || !eol_comment || fs->buffer_end - ptr < len + 5 )
2634 ptr = icvXMLFlush( fs );
2635 else if( ptr > fs->buffer_start + fs->struct_indent )
2640 ptr = icvFSResizeWriteBuffer( fs, ptr, len + 9 );
2641 sprintf( ptr, "<!-- %s -->", comment );
2642 len = (int)strlen(ptr);
2646 strcpy( ptr, "<!--" );
2650 fs->buffer = ptr + len;
2651 ptr = icvXMLFlush(fs);
2659 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
2660 memcpy( ptr, comment, eol - comment + 1 );
2661 ptr += eol - comment;
2663 eol = strchr( comment, '\n' );
2667 len = (int)strlen(comment);
2668 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2669 memcpy( ptr, comment, len );
2674 ptr = icvXMLFlush( fs );
2676 sprintf( ptr, "-->" );
2677 fs->buffer = ptr + 3;
2683 /****************************************************************************************\
2684 * Common High-Level Functions *
2685 \****************************************************************************************/
2687 CV_IMPL CvFileStorage*
2688 cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, const char* encoding )
2690 CvFileStorage* fs = 0;
2692 int default_block_size = 1 << 18;
2693 bool append = (flags & 3) == CV_STORAGE_APPEND;
2694 bool mem = (flags & CV_STORAGE_MEMORY) != 0;
2695 bool write_mode = (flags & 3) != 0;
2697 size_t fnamelen = 0;
2699 if( !filename || filename[0] == '\0' )
2702 CV_Error( CV_StsNullPtr, mem ? "NULL or empty filename" : "NULL or empty buffer" );
2706 fnamelen = strlen(filename);
2709 CV_Error( CV_StsBadFlag, "CV_STORAGE_APPEND and CV_STORAGE_MEMORY are not currently compatible" );
2711 fs = (CvFileStorage*)cvAlloc( sizeof(*fs) );
2712 memset( fs, 0, sizeof(*fs));
2714 fs->memstorage = cvCreateMemStorage( default_block_size );
2715 fs->dststorage = dststorage ? dststorage : fs->memstorage;
2717 fs->flags = CV_FILE_STORAGE;
2718 fs->write_mode = write_mode;
2722 fs->filename = (char*)cvMemStorageAlloc( fs->memstorage, fnamelen+1 );
2723 strcpy( fs->filename, filename );
2725 char* dot_pos = strrchr(fs->filename, '.');
2726 char compression = '\0';
2728 if( dot_pos && dot_pos[1] == 'g' && dot_pos[2] == 'z' &&
2729 (dot_pos[3] == '\0' || (cv_isdigit(dot_pos[3]) && dot_pos[4] == '\0')) )
2732 CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" );
2734 compression = dot_pos[3];
2736 dot_pos[3] = '\0', fnamelen--;
2741 fs->file = fopen(fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" );
2748 char mode[] = { fs->write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' };
2749 fs->gzfile = gzopen(fs->filename, mode);
2753 CV_Error(CV_StsNotImplemented, "There is no compressed file storage support in this configuration");
2759 fs->struct_indent = 0;
2760 fs->struct_flags = 0;
2761 fs->wrap_margin = 71;
2763 if( fs->write_mode )
2765 int fmt = flags & CV_STORAGE_FORMAT_MASK;
2768 fs->outbuf = new std::deque<char>;
2770 if( fmt == CV_STORAGE_FORMAT_AUTO && filename )
2772 const char* dot_pos = filename + fnamelen - (isGZ ? 7 : 4);
2773 fs->fmt = (dot_pos >= filename && (memcmp( dot_pos, ".xml", 4) == 0 ||
2774 memcmp(dot_pos, ".XML", 4) == 0 || memcmp(dot_pos, ".Xml", 4) == 0)) ?
2775 CV_STORAGE_FORMAT_XML : CV_STORAGE_FORMAT_YAML;
2778 fs->fmt = fmt != CV_STORAGE_FORMAT_AUTO ? fmt : CV_STORAGE_FORMAT_XML;
2780 // we use factor=6 for XML (the longest characters (' and ") are encoded with 6 bytes (' and ")
2781 // and factor=4 for YAML ( as we use 4 bytes for non ASCII characters (e.g. \xAB))
2782 int buf_size = CV_FS_MAX_LEN*(fs->fmt == CV_STORAGE_FORMAT_XML ? 6 : 4) + 1024;
2785 fseek( fs->file, 0, SEEK_END );
2787 fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->fmt == CV_STORAGE_FORMAT_XML ?
2788 sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage );
2790 fs->struct_indent = 0;
2791 fs->struct_flags = CV_NODE_EMPTY;
2792 fs->buffer_start = fs->buffer = (char*)cvAlloc( buf_size + 1024 );
2793 fs->buffer_end = fs->buffer_start + buf_size;
2794 if( fs->fmt == CV_STORAGE_FORMAT_XML )
2796 size_t file_size = fs->file ? (size_t)ftell( fs->file ) : (size_t)0;
2797 fs->strstorage = cvCreateChildMemStorage( fs->memstorage );
2798 if( !append || file_size == 0 )
2802 if( strcmp( encoding, "UTF-16" ) == 0 ||
2803 strcmp( encoding, "utf-16" ) == 0 ||
2804 strcmp( encoding, "Utf-16" ) == 0 )
2805 CV_Error( CV_StsBadArg, "UTF-16 XML encoding is not supported! Use 8-bit encoding\n");
2807 CV_Assert( strlen(encoding) < 1000 );
2809 sprintf(buf, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", encoding);
2813 icvPuts( fs, "<?xml version=\"1.0\"?>\n" );
2814 icvPuts( fs, "<opencv_storage>\n" );
2818 int xml_buf_size = 1 << 10;
2819 char substr[] = "</opencv_storage>";
2820 int last_occurence = -1;
2821 xml_buf_size = MIN(xml_buf_size, int(file_size));
2822 fseek( fs->file, -xml_buf_size, SEEK_END );
2823 xml_buf = (char*)cvAlloc( xml_buf_size+2 );
2824 // find the last occurence of </opencv_storage>
2827 int line_offset = ftell( fs->file );
2828 char* ptr0 = icvGets( fs, xml_buf, xml_buf_size ), *ptr;
2834 ptr = strstr( ptr, substr );
2837 last_occurence = line_offset + (int)(ptr - ptr0);
2838 ptr += strlen(substr);
2841 if( last_occurence < 0 )
2842 CV_Error( CV_StsError, "Could not find </opencv_storage> in the end of file.\n" );
2844 fs->file = fopen( fs->filename, "r+t" );
2845 fseek( fs->file, last_occurence, SEEK_SET );
2846 // replace the last "</opencv_storage>" with " <!-- resumed -->", which has the same length
2847 icvPuts( fs, " <!-- resumed -->" );
2848 fseek( fs->file, 0, SEEK_END );
2849 icvPuts( fs, "\n" );
2851 fs->start_write_struct = icvXMLStartWriteStruct;
2852 fs->end_write_struct = icvXMLEndWriteStruct;
2853 fs->write_int = icvXMLWriteInt;
2854 fs->write_real = icvXMLWriteReal;
2855 fs->write_string = icvXMLWriteString;
2856 fs->write_comment = icvXMLWriteComment;
2857 fs->start_next_stream = icvXMLStartNextStream;
2862 icvPuts( fs, "%YAML:1.0\n" );
2864 icvPuts( fs, "...\n---\n" );
2865 fs->start_write_struct = icvYMLStartWriteStruct;
2866 fs->end_write_struct = icvYMLEndWriteStruct;
2867 fs->write_int = icvYMLWriteInt;
2868 fs->write_real = icvYMLWriteReal;
2869 fs->write_string = icvYMLWriteString;
2870 fs->write_comment = icvYMLWriteComment;
2871 fs->start_next_stream = icvYMLStartNextStream;
2878 fs->strbuf = filename;
2879 fs->strbufsize = fnamelen;
2882 size_t buf_size = 1 << 20;
2883 const char* yaml_signature = "%YAML:";
2885 icvGets( fs, buf, sizeof(buf)-2 );
2886 fs->fmt = strncmp( buf, yaml_signature, strlen(yaml_signature) ) == 0 ?
2887 CV_STORAGE_FORMAT_YAML : CV_STORAGE_FORMAT_XML;
2893 fseek( fs->file, 0, SEEK_END );
2894 buf_size = ftell( fs->file );
2897 buf_size = fs->strbufsize;
2898 buf_size = MIN( buf_size, (size_t)(1 << 20) );
2899 buf_size = MAX( buf_size, (size_t)(CV_FS_MAX_LEN*2 + 1024) );
2903 fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash),
2904 sizeof(CvStringHashNode), fs->memstorage, 256 );
2906 fs->roots = cvCreateSeq( 0, sizeof(CvSeq),
2907 sizeof(CvFileNode), fs->memstorage );
2909 fs->buffer = fs->buffer_start = (char*)cvAlloc( buf_size + 256 );
2910 fs->buffer_end = fs->buffer_start + buf_size;
2911 fs->buffer[0] = '\n';
2912 fs->buffer[1] = '\0';
2914 //mode = cvGetErrMode();
2915 //cvSetErrMode( CV_ErrModeSilent );
2916 if( fs->fmt == CV_STORAGE_FORMAT_XML )
2920 //cvSetErrMode( mode );
2922 // release resources that we do not need anymore
2923 cvFree( &fs->buffer_start );
2924 fs->buffer = fs->buffer_end = 0;
2926 fs->is_opened = true;
2931 if( cvGetErrStatus() < 0 || (!fs->file && !fs->gzfile && !fs->outbuf && !fs->strbuf) )
2933 cvReleaseFileStorage( &fs );
2935 else if( !fs->write_mode )
2938 // we close the file since it's not needed anymore. But icvCloseFile() resets is_opened,
2939 // which may be misleading. Since we restore the value of is_opened.
2940 fs->is_opened = true;
2950 cvStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2951 const char* type_name, CvAttrList /*attributes*/ )
2953 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2954 fs->start_write_struct( fs, key, struct_flags, type_name );
2959 cvEndWriteStruct( CvFileStorage* fs )
2961 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2962 fs->end_write_struct( fs );
2967 cvWriteInt( CvFileStorage* fs, const char* key, int value )
2969 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2970 fs->write_int( fs, key, value );
2975 cvWriteReal( CvFileStorage* fs, const char* key, double value )
2977 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2978 fs->write_real( fs, key, value );
2983 cvWriteString( CvFileStorage* fs, const char* key, const char* value, int quote )
2985 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2986 fs->write_string( fs, key, value, quote );
2991 cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2993 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2994 fs->write_comment( fs, comment, eol_comment );
2999 cvStartNextStream( CvFileStorage* fs )
3001 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
3002 fs->start_next_stream( fs );
3006 static const char icvTypeSymbol[] = "ucwsifdr";
3007 #define CV_FS_MAX_FMT_PAIRS 128
3010 icvEncodeFormat( int elem_type, char* dt )
3012 sprintf( dt, "%d%c", CV_MAT_CN(elem_type), icvTypeSymbol[CV_MAT_DEPTH(elem_type)] );
3013 return dt + ( dt[2] == '\0' && dt[0] == '1' );
3017 icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len )
3019 int fmt_pair_count = 0;
3020 int i = 0, k = 0, len = dt ? (int)strlen(dt) : 0;
3025 assert( fmt_pairs != 0 && max_len > 0 );
3029 for( ; k < len; k++ )
3035 int count = c - '0';
3036 if( cv_isdigit(dt[k+1]) )
3039 count = (int)strtol( dt+k, &endptr, 10 );
3040 k = (int)(endptr - dt) - 1;
3044 CV_Error( CV_StsBadArg, "Invalid data type specification" );
3046 fmt_pairs[i] = count;
3050 const char* pos = strchr( icvTypeSymbol, c );
3052 CV_Error( CV_StsBadArg, "Invalid data type specification" );
3053 if( fmt_pairs[i] == 0 )
3055 fmt_pairs[i+1] = (int)(pos - icvTypeSymbol);
3056 if( i > 0 && fmt_pairs[i+1] == fmt_pairs[i-1] )
3057 fmt_pairs[i-2] += fmt_pairs[i];
3062 CV_Error( CV_StsBadArg, "Too long data type specification" );
3068 fmt_pair_count = i/2;
3069 return fmt_pair_count;
3074 icvCalcElemSize( const char* dt, int initial_size )
3077 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
3080 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
3081 fmt_pair_count *= 2;
3082 for( i = 0, size = initial_size; i < fmt_pair_count; i += 2 )
3084 comp_size = CV_ELEM_SIZE(fmt_pairs[i+1]);
3085 size = cvAlign( size, comp_size );
3086 size += comp_size * fmt_pairs[i];
3088 if( initial_size == 0 )
3090 comp_size = CV_ELEM_SIZE(fmt_pairs[1]);
3091 size = cvAlign( size, comp_size );
3098 icvDecodeSimpleFormat( const char* dt )
3101 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
3103 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
3104 if( fmt_pair_count != 1 || fmt_pairs[0] > 4 )
3105 CV_Error( CV_StsError, "Too complex format for the matrix" );
3107 elem_type = CV_MAKETYPE( fmt_pairs[1], fmt_pairs[0] );
3114 cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
3116 const char* data0 = (const char*)_data;
3118 int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k, fmt_pair_count;
3121 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
3124 CV_Error( CV_StsOutOfRange, "Negative number of elements" );
3126 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
3132 CV_Error( CV_StsNullPtr, "Null data pointer" );
3134 if( fmt_pair_count == 1 )
3136 fmt_pairs[0] *= len;
3142 for( k = 0; k < fmt_pair_count; k++ )
3144 int i, count = fmt_pairs[k*2];
3145 int elem_type = fmt_pairs[k*2+1];
3146 int elem_size = CV_ELEM_SIZE(elem_type);
3147 const char* data, *ptr;
3149 offset = cvAlign( offset, elem_size );
3150 data = data0 + offset;
3152 for( i = 0; i < count; i++ )
3157 ptr = icv_itoa( *(uchar*)data, buf, 10 );
3161 ptr = icv_itoa( *(char*)data, buf, 10 );
3165 ptr = icv_itoa( *(ushort*)data, buf, 10 );
3166 data += sizeof(ushort);
3169 ptr = icv_itoa( *(short*)data, buf, 10 );
3170 data += sizeof(short);
3173 ptr = icv_itoa( *(int*)data, buf, 10 );
3174 data += sizeof(int);
3177 ptr = icvFloatToString( buf, *(float*)data );
3178 data += sizeof(float);
3181 ptr = icvDoubleToString( buf, *(double*)data );
3182 data += sizeof(double);
3184 case CV_USRTYPE1: /* reference */
3185 ptr = icv_itoa( (int)*(size_t*)data, buf, 10 );
3186 data += sizeof(size_t);
3193 if( fs->fmt == CV_STORAGE_FORMAT_XML )
3195 int buf_len = (int)strlen(ptr);
3196 icvXMLWriteScalar( fs, 0, ptr, buf_len );
3199 icvYMLWrite( fs, 0, ptr );
3202 offset = (int)(data - data0);
3209 cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader )
3212 CV_CHECK_FILE_STORAGE( fs );
3214 if( !src || !reader )
3215 CV_Error( CV_StsNullPtr, "Null pointer to source file node or reader" );
3217 node_type = CV_NODE_TYPE(src->tag);
3218 if( node_type == CV_NODE_INT || node_type == CV_NODE_REAL )
3220 // emulate reading from 1-element sequence
3221 reader->ptr = (schar*)src;
3222 reader->block_max = reader->ptr + sizeof(*src)*2;
3223 reader->block_min = reader->ptr;
3226 else if( node_type == CV_NODE_SEQ )
3228 cvStartReadSeq( src->data.seq, reader, 0 );
3230 else if( node_type == CV_NODE_NONE )
3232 memset( reader, 0, sizeof(*reader) );
3235 CV_Error( CV_StsBadArg, "The file node should be a numerical scalar or a sequence" );
3240 cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,
3241 int len, void* _data, const char* dt )
3243 char* data0 = (char*)_data;
3244 int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k = 0, fmt_pair_count;
3245 int i = 0, offset = 0, count = 0;
3247 CV_CHECK_FILE_STORAGE( fs );
3249 if( !reader || !data0 )
3250 CV_Error( CV_StsNullPtr, "Null pointer to reader or destination array" );
3252 if( !reader->seq && len != 1 )
3253 CV_Error( CV_StsBadSize, "The readed sequence is a scalar, thus len must be 1" );
3255 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
3259 for( k = 0; k < fmt_pair_count; k++ )
3261 int elem_type = fmt_pairs[k*2+1];
3262 int elem_size = CV_ELEM_SIZE(elem_type);
3265 count = fmt_pairs[k*2];
3266 offset = cvAlign( offset, elem_size );
3267 data = data0 + offset;
3269 for( i = 0; i < count; i++ )
3271 CvFileNode* node = (CvFileNode*)reader->ptr;
3272 if( CV_NODE_IS_INT(node->tag) )
3274 int ival = node->data.i;
3279 *(uchar*)data = CV_CAST_8U(ival);
3283 *(char*)data = CV_CAST_8S(ival);
3287 *(ushort*)data = CV_CAST_16U(ival);
3288 data += sizeof(ushort);
3291 *(short*)data = CV_CAST_16S(ival);
3292 data += sizeof(short);
3296 data += sizeof(int);
3299 *(float*)data = (float)ival;
3300 data += sizeof(float);
3303 *(double*)data = (double)ival;
3304 data += sizeof(double);
3306 case CV_USRTYPE1: /* reference */
3307 *(size_t*)data = ival;
3308 data += sizeof(size_t);
3315 else if( CV_NODE_IS_REAL(node->tag) )
3317 double fval = node->data.f;
3323 ival = cvRound(fval);
3324 *(uchar*)data = CV_CAST_8U(ival);
3328 ival = cvRound(fval);
3329 *(char*)data = CV_CAST_8S(ival);
3333 ival = cvRound(fval);
3334 *(ushort*)data = CV_CAST_16U(ival);
3335 data += sizeof(ushort);
3338 ival = cvRound(fval);
3339 *(short*)data = CV_CAST_16S(ival);
3340 data += sizeof(short);
3343 ival = cvRound(fval);
3345 data += sizeof(int);
3348 *(float*)data = (float)fval;
3349 data += sizeof(float);
3352 *(double*)data = fval;
3353 data += sizeof(double);
3355 case CV_USRTYPE1: /* reference */
3356 ival = cvRound(fval);
3357 *(size_t*)data = ival;
3358 data += sizeof(size_t);
3366 CV_Error( CV_StsError,
3367 "The sequence element is not a numerical scalar" );
3369 CV_NEXT_SEQ_ELEM( sizeof(CvFileNode), *reader );
3374 offset = (int)(data - data0);
3379 if( i != count - 1 || k != fmt_pair_count - 1 )
3380 CV_Error( CV_StsBadSize,
3381 "The sequence slice does not fit an integer number of records" );
3384 reader->ptr -= sizeof(CvFileNode);
3389 cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,
3390 void* data, const char* dt )
3395 CV_Error( CV_StsNullPtr, "Null pointers to source file node or destination array" );
3397 cvStartReadRawData( fs, src, &reader );
3398 cvReadRawDataSlice( fs, &reader, CV_NODE_IS_SEQ(src->tag) ?
3399 src->data.seq->total : 1, data, dt );
3404 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node );
3407 icvWriteCollection( CvFileStorage* fs, const CvFileNode* node )
3409 int i, total = node->data.seq->total;
3410 int elem_size = node->data.seq->elem_size;
3411 int is_map = CV_NODE_IS_MAP(node->tag);
3414 cvStartReadSeq( node->data.seq, &reader, 0 );
3416 for( i = 0; i < total; i++ )
3418 CvFileMapNode* elem = (CvFileMapNode*)reader.ptr;
3419 if( !is_map || CV_IS_SET_ELEM(elem) )
3421 const char* name = is_map ? elem->key->str.ptr : 0;
3422 icvWriteFileNode( fs, name, &elem->value );
3424 CV_NEXT_SEQ_ELEM( elem_size, reader );
3429 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node )
3431 switch( CV_NODE_TYPE(node->tag) )
3434 fs->write_int( fs, name, node->data.i );
3437 fs->write_real( fs, name, node->data.f );
3440 fs->write_string( fs, name, node->data.str.ptr, 0 );
3444 fs->start_write_struct( fs, name, CV_NODE_TYPE(node->tag) +
3445 (CV_NODE_SEQ_IS_SIMPLE(node->data.seq) ? CV_NODE_FLOW : 0),
3446 node->info ? node->info->type_name : 0 );
3447 icvWriteCollection( fs, node );
3448 fs->end_write_struct( fs );
3451 fs->start_write_struct( fs, name, CV_NODE_SEQ, 0 );
3452 fs->end_write_struct( fs );
3455 CV_Error( CV_StsBadFlag, "Unknown type of file node" );
3461 cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,
3462 const CvFileNode* node, int embed )
3464 CvFileStorage* dst = 0;
3465 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
3470 if( CV_NODE_IS_COLLECTION(node->tag) && embed )
3472 icvWriteCollection( fs, node );
3476 icvWriteFileNode( fs, new_node_name, node );
3479 int i, stream_count;
3480 stream_count = fs->roots->total;
3481 for( i = 0; i < stream_count; i++ )
3483 CvFileNode* node = (CvFileNode*)cvGetSeqElem( fs->roots, i, 0 );
3484 icvDumpCollection( dst, node );
3485 if( i < stream_count - 1 )
3486 dst->start_next_stream( dst );
3488 cvReleaseFileStorage( &dst );
3493 cvGetFileNodeName( const CvFileNode* file_node )
3495 return file_node && CV_NODE_HAS_NAME(file_node->tag) ?
3496 ((CvFileMapNode*)file_node)->key->str.ptr : 0;
3499 /****************************************************************************************\
3500 * Reading/Writing etc. for standard types *
3501 \****************************************************************************************/
3503 /*#define CV_TYPE_NAME_MAT "opencv-matrix"
3504 #define CV_TYPE_NAME_MATND "opencv-nd-matrix"
3505 #define CV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix"
3506 #define CV_TYPE_NAME_IMAGE "opencv-image"
3507 #define CV_TYPE_NAME_SEQ "opencv-sequence"
3508 #define CV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree"
3509 #define CV_TYPE_NAME_GRAPH "opencv-graph"*/
3511 /******************************* CvMat ******************************/
3514 icvIsMat( const void* ptr )
3516 return CV_IS_MAT_HDR_Z(ptr);
3520 icvWriteMat( CvFileStorage* fs, const char* name,
3521 const void* struct_ptr, CvAttrList /*attr*/ )
3523 const CvMat* mat = (const CvMat*)struct_ptr;
3528 assert( CV_IS_MAT_HDR_Z(mat) );
3530 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT );
3531 cvWriteInt( fs, "rows", mat->rows );
3532 cvWriteInt( fs, "cols", mat->cols );
3533 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3534 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3536 size = cvGetSize(mat);
3537 if( size.height > 0 && size.width > 0 && mat->data.ptr )
3539 if( CV_IS_MAT_CONT(mat->type) )
3541 size.width *= size.height;
3545 for( y = 0; y < size.height; y++ )
3546 cvWriteRawData( fs, mat->data.ptr + (size_t)y*mat->step, size.width, dt );
3548 cvEndWriteStruct( fs );
3549 cvEndWriteStruct( fs );
3554 icvFileNodeSeqLen( CvFileNode* node )
3556 return CV_NODE_IS_COLLECTION(node->tag) ? node->data.seq->total :
3557 CV_NODE_TYPE(node->tag) != CV_NODE_NONE;
3562 icvReadMat( CvFileStorage* fs, CvFileNode* node )
3568 int rows, cols, elem_type;
3570 rows = cvReadIntByName( fs, node, "rows", -1 );
3571 cols = cvReadIntByName( fs, node, "cols", -1 );
3572 dt = cvReadStringByName( fs, node, "dt", 0 );
3574 if( rows < 0 || cols < 0 || !dt )
3575 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3577 elem_type = icvDecodeSimpleFormat( dt );
3579 data = cvGetFileNodeByName( fs, node, "data" );
3581 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3583 int nelems = icvFileNodeSeqLen( data );
3584 if( nelems > 0 && nelems != rows*cols*CV_MAT_CN(elem_type) )
3585 CV_Error( CV_StsUnmatchedSizes,
3586 "The matrix size does not match to the number of stored elements" );
3590 mat = cvCreateMat( rows, cols, elem_type );
3591 cvReadRawData( fs, data, mat->data.ptr, dt );
3593 else if( rows == 0 && cols == 0 )
3594 mat = cvCreateMatHeader( 0, 1, elem_type );
3596 mat = cvCreateMatHeader( rows, cols, elem_type );
3603 /******************************* CvMatND ******************************/
3606 icvIsMatND( const void* ptr )
3608 return CV_IS_MATND_HDR(ptr);
3613 icvWriteMatND( CvFileStorage* fs, const char* name,
3614 const void* struct_ptr, CvAttrList /*attr*/ )
3616 CvMatND* mat = (CvMatND*)struct_ptr;
3618 CvNArrayIterator iterator;
3619 int dims, sizes[CV_MAX_DIM];
3622 assert( CV_IS_MATND_HDR(mat) );
3624 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MATND );
3625 dims = cvGetDims( mat, sizes );
3626 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3627 cvWriteRawData( fs, sizes, dims, "i" );
3628 cvEndWriteStruct( fs );
3629 cvWriteString( fs, "dt", icvEncodeFormat( cvGetElemType(mat), dt ), 0 );
3630 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3632 if( mat->dim[0].size > 0 && mat->data.ptr )
3634 cvInitNArrayIterator( 1, (CvArr**)&mat, 0, &stub, &iterator );
3637 cvWriteRawData( fs, iterator.ptr[0], iterator.size.width, dt );
3638 while( cvNextNArraySlice( &iterator ));
3640 cvEndWriteStruct( fs );
3641 cvEndWriteStruct( fs );
3646 icvReadMatND( CvFileStorage* fs, CvFileNode* node )
3652 CvFileNode* sizes_node;
3653 int sizes[CV_MAX_DIM], dims, elem_type;
3656 sizes_node = cvGetFileNodeByName( fs, node, "sizes" );
3657 dt = cvReadStringByName( fs, node, "dt", 0 );
3659 if( !sizes_node || !dt )
3660 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3662 dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
3663 CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
3665 if( dims <= 0 || dims > CV_MAX_DIM )
3666 CV_Error( CV_StsParseError, "Could not determine the matrix dimensionality" );
3668 cvReadRawData( fs, sizes_node, sizes, "i" );
3669 elem_type = icvDecodeSimpleFormat( dt );
3671 data = cvGetFileNodeByName( fs, node, "data" );
3673 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3677 for( total_size = CV_MAT_CN(elem_type), i = 0; i < dims; i++ )
3678 total_size *= sizes[i];
3680 int nelems = icvFileNodeSeqLen( data );
3682 if( nelems > 0 && nelems != total_size )
3683 CV_Error( CV_StsUnmatchedSizes,
3684 "The matrix size does not match to the number of stored elements" );
3688 mat = cvCreateMatND( dims, sizes, elem_type );
3689 cvReadRawData( fs, data, mat->data.ptr, dt );
3692 mat = cvCreateMatNDHeader( dims, sizes, elem_type );
3699 /******************************* CvSparseMat ******************************/
3702 icvIsSparseMat( const void* ptr )
3704 return CV_IS_SPARSE_MAT(ptr);
3709 icvSortIdxCmpFunc( const void* _a, const void* _b, void* userdata )
3711 int i, dims = *(int*)userdata;
3712 const int* a = *(const int**)_a;
3713 const int* b = *(const int**)_b;
3715 for( i = 0; i < dims; i++ )
3717 int delta = a[i] - b[i];
3727 icvWriteSparseMat( CvFileStorage* fs, const char* name,
3728 const void* struct_ptr, CvAttrList /*attr*/ )
3730 CvMemStorage* memstorage = 0;
3731 const CvSparseMat* mat = (const CvSparseMat*)struct_ptr;
3732 CvSparseMatIterator iterator;
3740 assert( CV_IS_SPARSE_MAT(mat) );
3742 memstorage = cvCreateMemStorage();
3744 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SPARSE_MAT );
3745 dims = cvGetDims( mat, 0 );
3747 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3748 cvWriteRawData( fs, mat->size, dims, "i" );
3749 cvEndWriteStruct( fs );
3750 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3751 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3753 elements = cvCreateSeq( CV_SEQ_ELTYPE_PTR, sizeof(CvSeq), sizeof(int*), memstorage );
3755 node = cvInitSparseMatIterator( mat, &iterator );
3758 int* idx = CV_NODE_IDX( mat, node );
3759 cvSeqPush( elements, &idx );
3760 node = cvGetNextSparseNode( &iterator );
3763 cvSeqSort( elements, icvSortIdxCmpFunc, &dims );
3764 cvStartReadSeq( elements, &reader, 0 );
3766 for( i = 0; i < elements->total; i++ )
3772 CV_READ_SEQ_ELEM( idx, reader );
3775 for( ; idx[k] == prev_idx[k]; k++ )
3778 fs->write_int( fs, 0, k - dims + 1 );
3780 for( ; k < dims; k++ )
3781 fs->write_int( fs, 0, idx[k] );
3784 node = (CvSparseNode*)((uchar*)idx - mat->idxoffset );
3785 val = CV_NODE_VAL( mat, node );
3787 cvWriteRawData( fs, val, 1, dt );
3790 cvEndWriteStruct( fs );
3791 cvEndWriteStruct( fs );
3792 cvReleaseMemStorage( &memstorage );
3797 icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
3803 CvFileNode* sizes_node;
3806 int sizes[CV_MAX_DIM_HEAP], dims, elem_type, cn;
3809 sizes_node = cvGetFileNodeByName( fs, node, "sizes" );
3810 dt = cvReadStringByName( fs, node, "dt", 0 );
3812 if( !sizes_node || !dt )
3813 CV_Error( CV_StsError, "Some of essential matrix attributes are absent" );
3815 dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total :
3816 CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1;
3818 if( dims <= 0 || dims > CV_MAX_DIM_HEAP )
3819 CV_Error( CV_StsParseError, "Could not determine sparse matrix dimensionality" );
3821 cvReadRawData( fs, sizes_node, sizes, "i" );
3822 elem_type = icvDecodeSimpleFormat( dt );
3824 data = cvGetFileNodeByName( fs, node, "data" );
3825 if( !data || !CV_NODE_IS_SEQ(data->tag) )
3826 CV_Error( CV_StsError, "The matrix data is not found in file storage" );
3828 mat = cvCreateSparseMat( dims, sizes, elem_type );
3830 cn = CV_MAT_CN(elem_type);
3831 int idx[CV_MAX_DIM_HEAP];
3832 elements = data->data.seq;
3833 cvStartReadRawData( fs, data, &reader );
3835 for( i = 0; i < elements->total; )
3837 CvFileNode* elem = (CvFileNode*)reader.ptr;
3840 if( !CV_NODE_IS_INT(elem->tag ))
3841 CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" );
3843 if( i > 0 && k >= 0 )
3851 for( ; k < dims; k++ )
3853 CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
3855 elem = (CvFileNode*)reader.ptr;
3856 if( !CV_NODE_IS_INT(elem->tag ) || elem->data.i < 0 )
3857 CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" );
3858 idx[k] = elem->data.i;
3861 CV_NEXT_SEQ_ELEM( elements->elem_size, reader );
3863 val = cvPtrND( mat, idx, 0, 1, 0 );
3864 cvReadRawDataSlice( fs, &reader, cn, val, dt );
3873 /******************************* IplImage ******************************/
3876 icvIsImage( const void* ptr )
3878 return CV_IS_IMAGE_HDR(ptr);
3882 icvWriteImage( CvFileStorage* fs, const char* name,
3883 const void* struct_ptr, CvAttrList /*attr*/ )
3885 const IplImage* image = (const IplImage*)struct_ptr;
3886 char dt_buf[16], *dt;
3890 assert( CV_IS_IMAGE(image) );
3892 if( image->dataOrder == IPL_DATA_ORDER_PLANE )
3893 CV_Error( CV_StsUnsupportedFormat,
3894 "Images with planar data layout are not supported" );
3896 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_IMAGE );
3897 cvWriteInt( fs, "width", image->width );
3898 cvWriteInt( fs, "height", image->height );
3899 cvWriteString( fs, "origin", image->origin == IPL_ORIGIN_TL
3900 ? "top-left" : "bottom-left", 0 );
3901 cvWriteString( fs, "layout", image->dataOrder == IPL_DATA_ORDER_PLANE
3902 ? "planar" : "interleaved", 0 );
3905 cvStartWriteStruct( fs, "roi", CV_NODE_MAP + CV_NODE_FLOW );
3906 cvWriteInt( fs, "x", image->roi->xOffset );
3907 cvWriteInt( fs, "y", image->roi->yOffset );
3908 cvWriteInt( fs, "width", image->roi->width );
3909 cvWriteInt( fs, "height", image->roi->height );
3910 cvWriteInt( fs, "coi", image->roi->coi );
3911 cvEndWriteStruct( fs );
3914 depth = IPL2CV_DEPTH(image->depth);
3915 sprintf( dt_buf, "%d%c", image->nChannels, icvTypeSymbol[depth] );
3916 dt = dt_buf + (dt_buf[2] == '\0' && dt_buf[0] == '1');
3917 cvWriteString( fs, "dt", dt, 0 );
3919 size = cvSize(image->width, image->height);
3920 if( size.width*image->nChannels*CV_ELEM_SIZE(depth) == image->widthStep )
3922 size.width *= size.height;
3926 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3927 for( y = 0; y < size.height; y++ )
3928 cvWriteRawData( fs, image->imageData + y*image->widthStep, size.width, dt );
3929 cvEndWriteStruct( fs );
3930 cvEndWriteStruct( fs );
3935 icvReadImage( CvFileStorage* fs, CvFileNode* node )
3941 CvFileNode* roi_node;
3944 int y, width, height, elem_type, coi, depth;
3945 const char* origin, *data_order;
3947 width = cvReadIntByName( fs, node, "width", 0 );
3948 height = cvReadIntByName( fs, node, "height", 0 );
3949 dt = cvReadStringByName( fs, node, "dt", 0 );
3950 origin = cvReadStringByName( fs, node, "origin", 0 );
3952 if( width == 0 || height == 0 || dt == 0 || origin == 0 )
3953 CV_Error( CV_StsError, "Some of essential image attributes are absent" );
3955 elem_type = icvDecodeSimpleFormat( dt );
3956 data_order = cvReadStringByName( fs, node, "layout", "interleaved" );
3957 if( strcmp( data_order, "interleaved" ) != 0 )
3958 CV_Error( CV_StsError, "Only interleaved images can be read" );
3960 data = cvGetFileNodeByName( fs, node, "data" );
3962 CV_Error( CV_StsError, "The image data is not found in file storage" );
3964 if( icvFileNodeSeqLen( data ) != width*height*CV_MAT_CN(elem_type) )
3965 CV_Error( CV_StsUnmatchedSizes,
3966 "The matrix size does not match to the number of stored elements" );
3968 depth = cvIplDepth(elem_type);
3969 image = cvCreateImage( cvSize(width,height), depth, CV_MAT_CN(elem_type) );
3971 roi_node = cvGetFileNodeByName( fs, node, "roi" );
3974 roi.x = cvReadIntByName( fs, roi_node, "x", 0 );
3975 roi.y = cvReadIntByName( fs, roi_node, "y", 0 );
3976 roi.width = cvReadIntByName( fs, roi_node, "width", 0 );
3977 roi.height = cvReadIntByName( fs, roi_node, "height", 0 );
3978 coi = cvReadIntByName( fs, roi_node, "coi", 0 );
3980 cvSetImageROI( image, roi );
3981 cvSetImageCOI( image, coi );
3984 if( width*CV_ELEM_SIZE(elem_type) == image->widthStep )
3990 width *= CV_MAT_CN(elem_type);
3991 cvStartReadRawData( fs, data, &reader );
3992 for( y = 0; y < height; y++ )
3994 cvReadRawDataSlice( fs, &reader, width,
3995 image->imageData + y*image->widthStep, dt );
4003 /******************************* CvSeq ******************************/
4006 icvIsSeq( const void* ptr )
4008 return CV_IS_SEQ(ptr);
4013 icvReleaseSeq( void** ptr )
4016 CV_Error( CV_StsNullPtr, "NULL double pointer" );
4017 *ptr = 0; // it's impossible now to release seq, so just clear the pointer
4022 icvCloneSeq( const void* ptr )
4024 return cvSeqSlice( (CvSeq*)ptr, CV_WHOLE_SEQ,
4025 0 /* use the same storage as for the original sequence */, 1 );
4030 icvWriteHeaderData( CvFileStorage* fs, const CvSeq* seq,
4031 CvAttrList* attr, int initial_header_size )
4033 char header_dt_buf[128];
4034 const char* header_dt = cvAttrValue( attr, "header_dt" );
4039 dt_header_size = icvCalcElemSize( header_dt, initial_header_size );
4040 if( dt_header_size > seq->header_size )
4041 CV_Error( CV_StsUnmatchedSizes,
4042 "The size of header calculated from \"header_dt\" is greater than header_size" );
4044 else if( seq->header_size > initial_header_size )
4046 if( CV_IS_SEQ(seq) && CV_IS_SEQ_POINT_SET(seq) &&
4047 seq->header_size == sizeof(CvPoint2DSeq) &&
4048 seq->elem_size == sizeof(int)*2 )
4050 CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
4052 cvStartWriteStruct( fs, "rect", CV_NODE_MAP + CV_NODE_FLOW );
4053 cvWriteInt( fs, "x", point_seq->rect.x );
4054 cvWriteInt( fs, "y", point_seq->rect.y );
4055 cvWriteInt( fs, "width", point_seq->rect.width );
4056 cvWriteInt( fs, "height", point_seq->rect.height );
4057 cvEndWriteStruct( fs );
4058 cvWriteInt( fs, "color", point_seq->color );
4060 else if( CV_IS_SEQ(seq) && CV_IS_SEQ_CHAIN(seq) &&
4061 CV_MAT_TYPE(seq->flags) == CV_8UC1 )
4063 CvChain* chain = (CvChain*)seq;
4065 cvStartWriteStruct( fs, "origin", CV_NODE_MAP + CV_NODE_FLOW );
4066 cvWriteInt( fs, "x", chain->origin.x );
4067 cvWriteInt( fs, "y", chain->origin.y );
4068 cvEndWriteStruct( fs );
4072 unsigned extra_size = seq->header_size - initial_header_size;
4073 // a heuristic to provide nice defaults for sequences of int's & float's
4074 if( extra_size % sizeof(int) == 0 )
4075 sprintf( header_dt_buf, "%ui", (unsigned)(extra_size/sizeof(int)) );
4077 sprintf( header_dt_buf, "%uu", extra_size );
4078 header_dt = header_dt_buf;
4084 cvWriteString( fs, "header_dt", header_dt, 0 );
4085 cvStartWriteStruct( fs, "header_user_data", CV_NODE_SEQ + CV_NODE_FLOW );
4086 cvWriteRawData( fs, (uchar*)seq + sizeof(CvSeq), 1, header_dt );
4087 cvEndWriteStruct( fs );
4093 icvGetFormat( const CvSeq* seq, const char* dt_key, CvAttrList* attr,
4094 int initial_elem_size, char* dt_buf )
4097 dt = (char*)cvAttrValue( attr, dt_key );
4102 dt_elem_size = icvCalcElemSize( dt, initial_elem_size );
4103 if( dt_elem_size != seq->elem_size )
4104 CV_Error( CV_StsUnmatchedSizes,
4105 "The size of element calculated from \"dt\" and "
4106 "the elem_size do not match" );
4108 else if( CV_MAT_TYPE(seq->flags) != 0 || seq->elem_size == 1 )
4110 if( CV_ELEM_SIZE(seq->flags) != seq->elem_size )
4111 CV_Error( CV_StsUnmatchedSizes,
4112 "Size of sequence element (elem_size) is inconsistent with seq->flags" );
4113 dt = icvEncodeFormat( CV_MAT_TYPE(seq->flags), dt_buf );
4115 else if( seq->elem_size > initial_elem_size )
4117 unsigned extra_elem_size = seq->elem_size - initial_elem_size;
4118 // a heuristic to provide nice defaults for sequences of int's & float's
4119 if( extra_elem_size % sizeof(int) == 0 )
4120 sprintf( dt_buf, "%ui", (unsigned)(extra_elem_size/sizeof(int)) );
4122 sprintf( dt_buf, "%uu", extra_elem_size );
4131 icvWriteSeq( CvFileStorage* fs, const char* name,
4132 const void* struct_ptr,
4133 CvAttrList attr, int level )
4135 const CvSeq* seq = (CvSeq*)struct_ptr;
4138 char dt_buf[128], *dt;
4140 assert( CV_IS_SEQ( seq ));
4141 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ );
4144 cvWriteInt( fs, "level", level );
4146 dt = icvGetFormat( seq, "dt", &attr, 0, dt_buf );
4149 if( CV_IS_SEQ_CLOSED(seq) )
4150 strcat(buf, " closed");
4151 if( CV_IS_SEQ_HOLE(seq) )
4152 strcat(buf, " hole");
4153 if( CV_IS_SEQ_CURVE(seq) )
4154 strcat(buf, " curve");
4155 if( CV_SEQ_ELTYPE(seq) == 0 && seq->elem_size != 1 )
4156 strcat(buf, " untyped");
4158 cvWriteString( fs, "flags", buf + (buf[0] ? 1 : 0), 1 );
4160 cvWriteInt( fs, "count", seq->total );
4162 cvWriteString( fs, "dt", dt, 0 );
4164 icvWriteHeaderData( fs, seq, &attr, sizeof(CvSeq) );
4165 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
4167 for( block = seq->first; block; block = block->next )
4169 cvWriteRawData( fs, block->data, block->count, dt );
4170 if( block == seq->first->prev )
4173 cvEndWriteStruct( fs );
4174 cvEndWriteStruct( fs );
4179 icvWriteSeqTree( CvFileStorage* fs, const char* name,
4180 const void* struct_ptr, CvAttrList attr )
4182 const CvSeq* seq = (CvSeq*)struct_ptr;
4183 const char* recursive_value = cvAttrValue( &attr, "recursive" );
4184 int is_recursive = recursive_value &&
4185 strcmp(recursive_value,"0") != 0 &&
4186 strcmp(recursive_value,"false") != 0 &&
4187 strcmp(recursive_value,"False") != 0 &&
4188 strcmp(recursive_value,"FALSE") != 0;
4190 assert( CV_IS_SEQ( seq ));
4194 icvWriteSeq( fs, name, seq, attr, -1 );
4198 CvTreeNodeIterator tree_iterator;
4200 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ_TREE );
4201 cvStartWriteStruct( fs, "sequences", CV_NODE_SEQ );
4202 cvInitTreeNodeIterator( &tree_iterator, seq, INT_MAX );
4206 if( !tree_iterator.node )
4208 icvWriteSeq( fs, 0, tree_iterator.node, attr, tree_iterator.level );
4209 cvNextTreeNode( &tree_iterator );
4212 cvEndWriteStruct( fs );
4213 cvEndWriteStruct( fs );
4219 icvReadSeq( CvFileStorage* fs, CvFileNode* node )
4224 CvFileNode *data, *header_node, *rect_node, *origin_node;
4227 int elem_size, header_size = sizeof(CvSeq);
4228 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count;
4229 int items_per_elem = 0;
4230 const char* flags_str;
4231 const char* header_dt;
4235 flags_str = cvReadStringByName( fs, node, "flags", 0 );
4236 total = cvReadIntByName( fs, node, "count", -1 );
4237 dt = cvReadStringByName( fs, node, "dt", 0 );
4239 if( !flags_str || total == -1 || !dt )
4240 CV_Error( CV_StsError, "Some of essential sequence attributes are absent" );
4242 flags = CV_SEQ_MAGIC_VAL;
4244 if( cv_isdigit(flags_str[0]) )
4246 const int OLD_SEQ_ELTYPE_BITS = 9;
4247 const int OLD_SEQ_ELTYPE_MASK = (1 << OLD_SEQ_ELTYPE_BITS) - 1;
4248 const int OLD_SEQ_KIND_BITS = 3;
4249 const int OLD_SEQ_KIND_MASK = ((1 << OLD_SEQ_KIND_BITS) - 1) << OLD_SEQ_ELTYPE_BITS;
4250 const int OLD_SEQ_KIND_CURVE = 1 << OLD_SEQ_ELTYPE_BITS;
4251 const int OLD_SEQ_FLAG_SHIFT = OLD_SEQ_KIND_BITS + OLD_SEQ_ELTYPE_BITS;
4252 const int OLD_SEQ_FLAG_CLOSED = 1 << OLD_SEQ_FLAG_SHIFT;
4253 const int OLD_SEQ_FLAG_HOLE = 8 << OLD_SEQ_FLAG_SHIFT;
4255 int flags0 = (int)strtol( flags_str, &endptr, 16 );
4256 if( endptr == flags_str || (flags0 & CV_MAGIC_MASK) != CV_SEQ_MAGIC_VAL )
4257 CV_Error( CV_StsError, "The sequence flags are invalid" );
4258 if( (flags0 & OLD_SEQ_KIND_MASK) == OLD_SEQ_KIND_CURVE )
4259 flags |= CV_SEQ_KIND_CURVE;
4260 if( flags0 & OLD_SEQ_FLAG_CLOSED )
4261 flags |= CV_SEQ_FLAG_CLOSED;
4262 if( flags0 & OLD_SEQ_FLAG_HOLE )
4263 flags |= CV_SEQ_FLAG_HOLE;
4264 flags |= flags0 & OLD_SEQ_ELTYPE_MASK;
4268 if( strstr(flags_str, "curve") )
4269 flags |= CV_SEQ_KIND_CURVE;
4270 if( strstr(flags_str, "closed") )
4271 flags |= CV_SEQ_FLAG_CLOSED;
4272 if( strstr(flags_str, "hole") )
4273 flags |= CV_SEQ_FLAG_HOLE;
4274 if( !strstr(flags_str, "untyped") )
4278 flags |= icvDecodeSimpleFormat(dt);
4286 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4287 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4289 if( (header_dt != 0) ^ (header_node != 0) )
4290 CV_Error( CV_StsError,
4291 "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
4293 rect_node = cvGetFileNodeByName( fs, node, "rect" );
4294 origin_node = cvGetFileNodeByName( fs, node, "origin" );
4296 if( (header_node != 0) + (rect_node != 0) + (origin_node != 0) > 1 )
4297 CV_Error( CV_StsError, "Only one of \"header_user_data\", \"rect\" and \"origin\" tags may occur" );
4301 header_size = icvCalcElemSize( header_dt, header_size );
4303 else if( rect_node )
4304 header_size = sizeof(CvPoint2DSeq);
4305 else if( origin_node )
4306 header_size = sizeof(CvChain);
4308 elem_size = icvCalcElemSize( dt, 0 );
4309 seq = cvCreateSeq( flags, header_size, elem_size, fs->dststorage );
4313 cvReadRawData( fs, header_node, (char*)seq + sizeof(CvSeq), header_dt );
4315 else if( rect_node )
4317 CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq;
4318 point_seq->rect.x = cvReadIntByName( fs, rect_node, "x", 0 );
4319 point_seq->rect.y = cvReadIntByName( fs, rect_node, "y", 0 );
4320 point_seq->rect.width = cvReadIntByName( fs, rect_node, "width", 0 );
4321 point_seq->rect.height = cvReadIntByName( fs, rect_node, "height", 0 );
4322 point_seq->color = cvReadIntByName( fs, node, "color", 0 );
4324 else if( origin_node )
4326 CvChain* chain = (CvChain*)seq;
4327 chain->origin.x = cvReadIntByName( fs, origin_node, "x", 0 );
4328 chain->origin.y = cvReadIntByName( fs, origin_node, "y", 0 );
4331 cvSeqPushMulti( seq, 0, total, 0 );
4332 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4333 fmt_pair_count *= 2;
4334 for( i = 0; i < fmt_pair_count; i += 2 )
4335 items_per_elem += fmt_pairs[i];
4337 data = cvGetFileNodeByName( fs, node, "data" );
4339 CV_Error( CV_StsError, "The image data is not found in file storage" );
4341 if( icvFileNodeSeqLen( data ) != total*items_per_elem )
4342 CV_Error( CV_StsError, "The number of stored elements does not match to \"count\"" );
4344 cvStartReadRawData( fs, data, &reader );
4345 for( block = seq->first; block; block = block->next )
4347 int delta = block->count*items_per_elem;
4348 cvReadRawDataSlice( fs, &reader, delta, block->data, dt );
4349 if( block == seq->first->prev )
4359 icvReadSeqTree( CvFileStorage* fs, CvFileNode* node )
4362 CvFileNode *sequences_node = cvGetFileNodeByName( fs, node, "sequences" );
4366 CvSeq* prev_seq = 0;
4371 if( !sequences_node || !CV_NODE_IS_SEQ(sequences_node->tag) )
4372 CV_Error( CV_StsParseError,
4373 "opencv-sequence-tree instance should contain a field \"sequences\" that should be a sequence" );
4375 sequences = sequences_node->data.seq;
4376 total = sequences->total;
4378 cvStartReadSeq( sequences, &reader, 0 );
4379 for( i = 0; i < total; i++ )
4381 CvFileNode* elem = (CvFileNode*)reader.ptr;
4384 seq = (CvSeq*)cvRead( fs, elem );
4385 level = cvReadIntByName( fs, elem, "level", -1 );
4387 CV_Error( CV_StsParseError, "All the sequence tree nodes should contain \"level\" field" );
4390 if( level > prev_level )
4392 assert( level == prev_level + 1 );
4396 parent->v_next = seq;
4398 else if( level < prev_level )
4400 for( ; prev_level > level; prev_level-- )
4401 prev_seq = prev_seq->v_prev;
4402 parent = prev_seq->v_prev;
4404 seq->h_prev = prev_seq;
4406 prev_seq->h_next = seq;
4407 seq->v_prev = parent;
4410 CV_NEXT_SEQ_ELEM( sequences->elem_size, reader );
4417 /******************************* CvGraph ******************************/
4420 icvIsGraph( const void* ptr )
4422 return CV_IS_GRAPH(ptr);
4427 icvReleaseGraph( void** ptr )
4430 CV_Error( CV_StsNullPtr, "NULL double pointer" );
4432 *ptr = 0; // it's impossible now to release graph, so just clear the pointer
4437 icvCloneGraph( const void* ptr )
4439 return cvCloneGraph( (const CvGraph*)ptr, 0 );
4444 icvWriteGraph( CvFileStorage* fs, const char* name,
4445 const void* struct_ptr, CvAttrList attr )
4448 char* write_buf = 0;
4449 const CvGraph* graph = (const CvGraph*)struct_ptr;
4452 int i, k, vtx_count, edge_count;
4453 char vtx_dt_buf[128], *vtx_dt;
4454 char edge_dt_buf[128], *edge_dt;
4457 assert( CV_IS_GRAPH(graph) );
4458 vtx_count = cvGraphGetVtxCount( graph );
4459 edge_count = cvGraphGetEdgeCount( graph );
4460 flag_buf = (int*)cvAlloc( vtx_count*sizeof(flag_buf[0]));
4463 cvStartReadSeq( (CvSeq*)graph, &reader );
4464 for( i = 0, k = 0; i < graph->total; i++ )
4466 if( CV_IS_SET_ELEM( reader.ptr ))
4468 CvGraphVtx* vtx = (CvGraphVtx*)reader.ptr;
4469 flag_buf[k] = vtx->flags;
4472 CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
4476 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH );
4478 cvWriteString(fs, "flags", CV_IS_GRAPH_ORIENTED(graph) ? "oriented" : "", 1);
4480 cvWriteInt( fs, "vertex_count", vtx_count );
4481 vtx_dt = icvGetFormat( (CvSeq*)graph, "vertex_dt",
4482 &attr, sizeof(CvGraphVtx), vtx_dt_buf );
4484 cvWriteString( fs, "vertex_dt", vtx_dt, 0 );
4486 cvWriteInt( fs, "edge_count", edge_count );
4487 edge_dt = icvGetFormat( (CvSeq*)graph->edges, "edge_dt",
4488 &attr, sizeof(CvGraphEdge), buf );
4489 sprintf( edge_dt_buf, "2if%s", edge_dt ? edge_dt : "" );
4490 edge_dt = edge_dt_buf;
4491 cvWriteString( fs, "edge_dt", edge_dt, 0 );
4493 icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) );
4495 write_buf_size = MAX( 3*graph->elem_size, 1 << 16 );
4496 write_buf_size = MAX( 3*graph->edges->elem_size, write_buf_size );
4497 write_buf = (char*)cvAlloc( write_buf_size );
4499 // as vertices and edges are written in similar way,
4500 // do it as a parametrized 2-iteration loop
4501 for( k = 0; k < 2; k++ )
4503 const char* dt = k == 0 ? vtx_dt : edge_dt;
4506 CvSet* data = k == 0 ? (CvSet*)graph : graph->edges;
4507 int elem_size = data->elem_size;
4508 int write_elem_size = icvCalcElemSize( dt, 0 );
4509 char* src_ptr = write_buf;
4510 int write_max = write_buf_size / write_elem_size, write_count = 0;
4512 // alignment of user part of the edge data following 2if
4513 int edge_user_align = sizeof(float);
4517 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
4518 fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4519 if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[2*2+1]) >= (int)sizeof(double))
4520 edge_user_align = sizeof(double);
4523 cvStartWriteStruct( fs, k == 0 ? "vertices" : "edges",
4524 CV_NODE_SEQ + CV_NODE_FLOW );
4525 cvStartReadSeq( (CvSeq*)data, &reader );
4526 for( i = 0; i < data->total; i++ )
4528 if( CV_IS_SET_ELEM( reader.ptr ))
4530 if( k == 0 ) // vertices
4531 memcpy( src_ptr, reader.ptr + sizeof(CvGraphVtx), write_elem_size );
4534 CvGraphEdge* edge = (CvGraphEdge*)reader.ptr;
4535 src_ptr = (char*)cvAlignPtr( src_ptr, sizeof(int) );
4536 ((int*)src_ptr)[0] = edge->vtx[0]->flags;
4537 ((int*)src_ptr)[1] = edge->vtx[1]->flags;
4538 *(float*)(src_ptr + sizeof(int)*2) = edge->weight;
4539 if( elem_size > (int)sizeof(CvGraphEdge) )
4541 char* src_ptr2 = (char*)cvAlignPtr( src_ptr + 2*sizeof(int)
4542 + sizeof(float), edge_user_align );
4543 memcpy( src_ptr2, edge + 1, elem_size - sizeof(CvGraphEdge) );
4546 src_ptr += write_elem_size;
4547 if( ++write_count >= write_max )
4549 cvWriteRawData( fs, write_buf, write_count, dt );
4551 src_ptr = write_buf;
4554 CV_NEXT_SEQ_ELEM( data->elem_size, reader );
4557 if( write_count > 0 )
4558 cvWriteRawData( fs, write_buf, write_count, dt );
4559 cvEndWriteStruct( fs );
4563 cvEndWriteStruct( fs );
4565 // final stage. restore the graph flags
4566 cvStartReadSeq( (CvSeq*)graph, &reader );
4568 for( i = 0; i < graph->total; i++ )
4570 if( CV_IS_SET_ELEM( reader.ptr ))
4571 ((CvGraphVtx*)reader.ptr)->flags = flag_buf[vtx_count++];
4572 CV_NEXT_SEQ_ELEM( graph->elem_size, reader );
4575 cvFree( &write_buf );
4576 cvFree( &flag_buf );
4581 icvReadGraph( CvFileStorage* fs, CvFileNode* node )
4585 CvGraphVtx** vtx_buf = 0;
4587 CvFileNode *header_node, *vtx_node, *edge_node;
4588 int flags, vtx_count, edge_count;
4589 int vtx_size = sizeof(CvGraphVtx), edge_size, header_size = sizeof(CvGraph);
4590 int src_vtx_size = 0, src_edge_size;
4591 int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count;
4592 int vtx_items_per_elem = 0, edge_items_per_elem = 0;
4593 int edge_user_align = sizeof(float);
4596 const char* flags_str;
4597 const char* header_dt;
4599 const char* edge_dt;
4602 flags_str = cvReadStringByName( fs, node, "flags", 0 );
4603 vtx_dt = cvReadStringByName( fs, node, "vertex_dt", 0 );
4604 edge_dt = cvReadStringByName( fs, node, "edge_dt", 0 );
4605 vtx_count = cvReadIntByName( fs, node, "vertex_count", -1 );
4606 edge_count = cvReadIntByName( fs, node, "edge_count", -1 );
4608 if( !flags_str || vtx_count == -1 || edge_count == -1 || !edge_dt )
4609 CV_Error( CV_StsError, "Some of essential graph attributes are absent" );
4611 flags = CV_SET_MAGIC_VAL + CV_GRAPH;
4613 if( isxdigit(flags_str[0]) )
4615 const int OLD_SEQ_ELTYPE_BITS = 9;
4616 const int OLD_SEQ_KIND_BITS = 3;
4617 const int OLD_SEQ_FLAG_SHIFT = OLD_SEQ_KIND_BITS + OLD_SEQ_ELTYPE_BITS;
4618 const int OLD_GRAPH_FLAG_ORIENTED = 1 << OLD_SEQ_FLAG_SHIFT;
4620 int flags0 = (int)strtol( flags_str, &endptr, 16 );
4621 if( endptr == flags_str || (flags0 & CV_MAGIC_MASK) != CV_SET_MAGIC_VAL )
4622 CV_Error( CV_StsError, "The sequence flags are invalid" );
4623 if( flags0 & OLD_GRAPH_FLAG_ORIENTED )
4624 flags |= CV_GRAPH_FLAG_ORIENTED;
4628 if( strstr(flags_str, "oriented") )
4629 flags |= CV_GRAPH_FLAG_ORIENTED;
4632 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4633 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4635 if( (header_dt != 0) ^ (header_node != 0) )
4636 CV_Error( CV_StsError,
4637 "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" );
4640 header_size = icvCalcElemSize( header_dt, header_size );
4644 src_vtx_size = icvCalcElemSize( vtx_dt, 0 );
4645 vtx_size = icvCalcElemSize( vtx_dt, vtx_size );
4646 fmt_pair_count = icvDecodeFormat( edge_dt,
4647 fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4648 fmt_pair_count *= 2;
4649 for( i = 0; i < fmt_pair_count; i += 2 )
4650 vtx_items_per_elem += fmt_pairs[i];
4654 char dst_edge_dt_buf[128];
4655 const char* dst_edge_dt = 0;
4657 fmt_pair_count = icvDecodeFormat( edge_dt,
4658 fmt_pairs, CV_FS_MAX_FMT_PAIRS );
4659 if( fmt_pair_count < 2 ||
4660 fmt_pairs[0] != 2 || fmt_pairs[1] != CV_32S ||
4661 fmt_pairs[2] < 1 || fmt_pairs[3] != CV_32F )
4662 CV_Error( CV_StsBadArg,
4663 "Graph edges should start with 2 integers and a float" );
4665 // alignment of user part of the edge data following 2if
4666 if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[5]) >= (int)sizeof(double))
4667 edge_user_align = sizeof(double);
4669 fmt_pair_count *= 2;
4670 for( i = 0; i < fmt_pair_count; i += 2 )
4671 edge_items_per_elem += fmt_pairs[i];
4673 if( edge_dt[2] == 'f' || (edge_dt[2] == '1' && edge_dt[3] == 'f') )
4674 dst_edge_dt = edge_dt + 3 + cv_isdigit(edge_dt[2]);
4677 int val = (int)strtol( edge_dt + 2, &endptr, 10 );
4678 sprintf( dst_edge_dt_buf, "%df%s", val-1, endptr );
4679 dst_edge_dt = dst_edge_dt_buf;
4682 edge_size = icvCalcElemSize( dst_edge_dt, sizeof(CvGraphEdge) );
4683 src_edge_size = icvCalcElemSize( edge_dt, 0 );
4686 graph = cvCreateGraph( flags, header_size, vtx_size, edge_size, fs->dststorage );
4689 cvReadRawData( fs, header_node, (char*)graph + sizeof(CvGraph), header_dt );
4691 read_buf_size = MAX( src_vtx_size*3, 1 << 16 );
4692 read_buf_size = MAX( src_edge_size*3, read_buf_size );
4693 read_buf = (char*)cvAlloc( read_buf_size );
4694 vtx_buf = (CvGraphVtx**)cvAlloc( vtx_count * sizeof(vtx_buf[0]) );
4696 vtx_node = cvGetFileNodeByName( fs, node, "vertices" );
4697 edge_node = cvGetFileNodeByName( fs, node, "edges" );
4699 CV_Error( CV_StsBadArg, "No edges data" );
4700 if( vtx_dt && !vtx_node )
4701 CV_Error( CV_StsBadArg, "No vertices data" );
4703 // as vertices and edges are read in similar way,
4704 // do it as a parametrized 2-iteration loop
4705 for( k = 0; k < 2; k++ )
4707 const char* dt = k == 0 ? vtx_dt : edge_dt;
4708 int elem_size = k == 0 ? vtx_size : edge_size;
4709 int src_elem_size = k == 0 ? src_vtx_size : src_edge_size;
4710 int items_per_elem = k == 0 ? vtx_items_per_elem : edge_items_per_elem;
4711 int elem_count = k == 0 ? vtx_count : edge_count;
4712 char* dst_ptr = read_buf;
4713 int read_max = read_buf_size /MAX(src_elem_size, 1), read_count = 0;
4716 cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
4718 for( i = 0; i < elem_count; i++ )
4720 if( read_count == 0 && dt )
4722 int count = MIN( elem_count - i, read_max )*items_per_elem;
4723 cvReadRawDataSlice( fs, &reader, count, read_buf, dt );
4731 cvGraphAddVtx( graph, 0, &vtx );
4734 memcpy( vtx + 1, dst_ptr, src_elem_size );
4738 CvGraphEdge* edge = 0;
4739 int vtx1 = ((int*)dst_ptr)[0];
4740 int vtx2 = ((int*)dst_ptr)[1];
4743 if( (unsigned)vtx1 >= (unsigned)vtx_count ||
4744 (unsigned)vtx2 >= (unsigned)vtx_count )
4745 CV_Error( CV_StsOutOfRange,
4746 "Some of stored vertex indices are out of range" );
4748 result = cvGraphAddEdgeByPtr( graph,
4749 vtx_buf[vtx1], vtx_buf[vtx2], 0, &edge );
4752 CV_Error( CV_StsBadArg, "Duplicated edge has occured" );
4754 edge->weight = *(float*)(dst_ptr + sizeof(int)*2);
4755 if( elem_size > (int)sizeof(CvGraphEdge) )
4757 char* dst_ptr2 = (char*)cvAlignPtr( dst_ptr + sizeof(int)*2 +
4758 sizeof(float), edge_user_align );
4759 memcpy( edge + 1, dst_ptr2, elem_size - sizeof(CvGraphEdge) );
4763 dst_ptr += src_elem_size;
4769 cvFree( &read_buf );
4775 /****************************************************************************************\
4777 \****************************************************************************************/
4779 CvTypeInfo *CvType::first = 0, *CvType::last = 0;
4781 CvType::CvType( const char* type_name,
4782 CvIsInstanceFunc is_instance, CvReleaseFunc release,
4783 CvReadFunc read, CvWriteFunc write, CvCloneFunc clone )
4787 _info.header_size = sizeof(_info);
4788 _info.type_name = type_name;
4789 _info.prev = _info.next = 0;
4790 _info.is_instance = is_instance;
4791 _info.release = release;
4792 _info.clone = clone;
4794 _info.write = write;
4796 cvRegisterType( &_info );
4803 cvUnregisterType( info->type_name );
4807 CvType seq_type( CV_TYPE_NAME_SEQ, icvIsSeq, icvReleaseSeq, icvReadSeq,
4808 icvWriteSeqTree /* this is the entry point for
4809 writing a single sequence too */, icvCloneSeq );
4811 CvType seq_tree_type( CV_TYPE_NAME_SEQ_TREE, icvIsSeq, icvReleaseSeq,
4812 icvReadSeqTree, icvWriteSeqTree, icvCloneSeq );
4814 CvType seq_graph_type( CV_TYPE_NAME_GRAPH, icvIsGraph, icvReleaseGraph,
4815 icvReadGraph, icvWriteGraph, icvCloneGraph );
4817 CvType sparse_mat_type( CV_TYPE_NAME_SPARSE_MAT, icvIsSparseMat,
4818 (CvReleaseFunc)cvReleaseSparseMat, icvReadSparseMat,
4819 icvWriteSparseMat, (CvCloneFunc)cvCloneSparseMat );
4821 CvType image_type( CV_TYPE_NAME_IMAGE, icvIsImage, (CvReleaseFunc)cvReleaseImage,
4822 icvReadImage, icvWriteImage, (CvCloneFunc)cvCloneImage );
4824 CvType mat_type( CV_TYPE_NAME_MAT, icvIsMat, (CvReleaseFunc)cvReleaseMat,
4825 icvReadMat, icvWriteMat, (CvCloneFunc)cvCloneMat );
4827 CvType matnd_type( CV_TYPE_NAME_MATND, icvIsMatND, (CvReleaseFunc)cvReleaseMatND,
4828 icvReadMatND, icvWriteMatND, (CvCloneFunc)cvCloneMatND );
4831 cvRegisterType( const CvTypeInfo* _info )
4833 CvTypeInfo* info = 0;
4837 //if( !CvType::first )
4838 // icvCreateStandardTypes();
4840 if( !_info || _info->header_size != sizeof(CvTypeInfo) )
4841 CV_Error( CV_StsBadSize, "Invalid type info" );
4843 if( !_info->is_instance || !_info->release ||
4844 !_info->read || !_info->write )
4845 CV_Error( CV_StsNullPtr,
4846 "Some of required function pointers "
4847 "(is_instance, release, read or write) are NULL");
4849 c = _info->type_name[0];
4850 if( !cv_isalpha(c) && c != '_' )
4851 CV_Error( CV_StsBadArg, "Type name should start with a letter or _" );
4853 len = (int)strlen(_info->type_name);
4855 for( i = 0; i < len; i++ )
4857 c = _info->type_name[i];
4858 if( !cv_isalnum(c) && c != '-' && c != '_' )
4859 CV_Error( CV_StsBadArg,
4860 "Type name should contain only letters, digits, - and _" );
4863 info = (CvTypeInfo*)malloc( sizeof(*info) + len + 1 );
4866 info->type_name = (char*)(info + 1);
4867 memcpy( (char*)info->type_name, _info->type_name, len + 1 );
4870 info->next = CvType::first;
4873 CvType::first->prev = info;
4875 CvType::last = info;
4876 CvType::first = info;
4881 cvUnregisterType( const char* type_name )
4885 info = cvFindType( type_name );
4889 info->prev->next = info->next;
4891 CvType::first = info->next;
4894 info->next->prev = info->prev;
4896 CvType::last = info->prev;
4898 if( !CvType::first || !CvType::last )
4899 CvType::first = CvType::last = 0;
4909 return CvType::first;
4914 cvFindType( const char* type_name )
4916 CvTypeInfo* info = 0;
4919 for( info = CvType::first; info != 0; info = info->next )
4920 if( strcmp( info->type_name, type_name ) == 0 )
4928 cvTypeOf( const void* struct_ptr )
4930 CvTypeInfo* info = 0;
4934 for( info = CvType::first; info != 0; info = info->next )
4935 if( info->is_instance( struct_ptr ))
4943 /* universal functions */
4945 cvRelease( void** struct_ptr )
4950 CV_Error( CV_StsNullPtr, "NULL double pointer" );
4954 info = cvTypeOf( *struct_ptr );
4956 CV_Error( CV_StsError, "Unknown object type" );
4957 if( !info->release )
4958 CV_Error( CV_StsError, "release function pointer is NULL" );
4960 info->release( struct_ptr );
4966 void* cvClone( const void* struct_ptr )
4968 void* struct_copy = 0;
4972 CV_Error( CV_StsNullPtr, "NULL structure pointer" );
4974 info = cvTypeOf( struct_ptr );
4976 CV_Error( CV_StsError, "Unknown object type" );
4978 CV_Error( CV_StsError, "clone function pointer is NULL" );
4980 struct_copy = info->clone( struct_ptr );
4985 /* reads matrix, image, sequence, graph etc. */
4987 cvRead( CvFileStorage* fs, CvFileNode* node, CvAttrList* list )
4990 CV_CHECK_FILE_STORAGE( fs );
4995 if( !CV_NODE_IS_USER(node->tag) || !node->info )
4996 CV_Error( CV_StsError, "The node does not represent a user object (unknown type?)" );
4998 obj = node->info->read( fs, node );
5000 *list = cvAttrList(0,0);
5006 /* writes matrix, image, sequence, graph etc. */
5008 cvWrite( CvFileStorage* fs, const char* name,
5009 const void* ptr, CvAttrList attributes )
5013 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
5016 CV_Error( CV_StsNullPtr, "Null pointer to the written object" );
5018 info = cvTypeOf( ptr );
5020 CV_Error( CV_StsBadArg, "Unknown object" );
5023 CV_Error( CV_StsBadArg, "The object does not have write function" );
5025 info->write( fs, name, ptr, attributes );
5029 /* simple API for reading/writing data */
5031 cvSave( const char* filename, const void* struct_ptr,
5032 const char* _name, const char* comment, CvAttrList attributes )
5034 CvFileStorage* fs = 0;
5037 CV_Error( CV_StsNullPtr, "NULL object pointer" );
5039 fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE );
5041 CV_Error( CV_StsError, "Could not open the file storage. Check the path and permissions" );
5043 cv::string name = _name ? cv::string(_name) : cv::FileStorage::getDefaultObjectName(filename);
5046 cvWriteComment( fs, comment, 0 );
5047 cvWrite( fs, name.c_str(), struct_ptr, attributes );
5048 cvReleaseFileStorage( &fs );
5052 cvLoad( const char* filename, CvMemStorage* memstorage,
5053 const char* name, const char** _real_name )
5056 const char* real_name = 0;
5057 cv::FileStorage fs(cvOpenFileStorage(filename, memstorage, CV_STORAGE_READ));
5059 CvFileNode* node = 0;
5061 if( !fs.isOpened() )
5066 node = cvGetFileNodeByName( *fs, 0, name );
5071 for( k = 0; k < (*fs)->roots->total; k++ )
5076 node = (CvFileNode*)cvGetSeqElem( (*fs)->roots, k );
5077 if( !CV_NODE_IS_MAP( node->tag ))
5079 seq = node->data.seq;
5082 cvStartReadSeq( seq, &reader, 0 );
5084 // find the first element in the map
5085 for( i = 0; i < seq->total; i++ )
5087 if( CV_IS_SET_ELEM( reader.ptr ))
5089 node = (CvFileNode*)reader.ptr;
5092 CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
5101 CV_Error( CV_StsObjectNotFound, "Could not find the/an object in file storage" );
5103 real_name = cvGetFileNodeName( node );
5104 ptr = cvRead( *fs, node, 0 );
5107 if( !memstorage && (CV_IS_SEQ( ptr ) || CV_IS_SET( ptr )) )
5108 CV_Error( CV_StsNullPtr,
5109 "NULL memory storage is passed - the loaded dynamic structure can not be stored" );
5111 if( cvGetErrStatus() < 0 )
5113 cvRelease( (void**)&ptr );
5121 *_real_name = (const char*)cvAlloc(strlen(real_name));
5122 memcpy((void*)*_real_name, real_name, strlen(real_name));
5132 ///////////////////////// new C++ interface for CvFileStorage ///////////////////////////
5137 static void getElemSize( const string& fmt, size_t& elemSize, size_t& cn )
5139 const char* dt = fmt.c_str();
5141 if( cv_isdigit(dt[0]) )
5147 elemSize = cn*(c == 'u' || c == 'c' ? sizeof(uchar) : c == 'w' || c == 's' ? sizeof(ushort) :
5148 c == 'i' ? sizeof(int) : c == 'f' ? sizeof(float) : c == 'd' ? sizeof(double) :
5149 c == 'r' ? sizeof(void*) : (size_t)0);
5152 FileStorage::FileStorage()
5157 FileStorage::FileStorage(const string& filename, int flags, const string& encoding)
5160 open( filename, flags, encoding );
5163 FileStorage::FileStorage(CvFileStorage* _fs)
5165 fs = Ptr<CvFileStorage>(_fs);
5166 state = _fs ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED;
5169 FileStorage::~FileStorage()
5171 while( structs.size() > 0 )
5173 cvEndWriteStruct(fs);
5178 bool FileStorage::open(const string& filename, int flags, const string& encoding)
5181 fs = Ptr<CvFileStorage>(cvOpenFileStorage( filename.c_str(), 0, flags,
5182 !encoding.empty() ? encoding.c_str() : 0));
5183 bool ok = isOpened();
5184 state = ok ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED;
5188 bool FileStorage::isOpened() const
5190 return !fs.empty() && fs.obj->is_opened;
5193 void FileStorage::release()
5200 string FileStorage::releaseAndGetString()
5203 buf.reserve(16); // HACK: Work around for compiler bug
5204 if( fs.obj && fs.obj->outbuf )
5205 icvClose(fs.obj, &buf);
5211 FileNode FileStorage::root(int streamidx) const
5213 return isOpened() ? FileNode(fs, cvGetRootFileNode(fs, streamidx)) : FileNode();
5216 FileStorage& operator << (FileStorage& fs, const string& str)
5218 enum { NAME_EXPECTED = FileStorage::NAME_EXPECTED,
5219 VALUE_EXPECTED = FileStorage::VALUE_EXPECTED,
5220 INSIDE_MAP = FileStorage::INSIDE_MAP };
5221 const char* _str = str.c_str();
5222 if( !fs.isOpened() || !_str )
5224 if( *_str == '}' || *_str == ']' )
5226 if( fs.structs.empty() )
5227 CV_Error_( CV_StsError, ("Extra closing '%c'", *_str) );
5228 if( (*_str == ']' ? '[' : '{') != fs.structs.back() )
5229 CV_Error_( CV_StsError,
5230 ("The closing '%c' does not match the opening '%c'", *_str, fs.structs.back()));
5231 fs.structs.pop_back();
5232 fs.state = fs.structs.empty() || fs.structs.back() == '{' ?
5233 INSIDE_MAP + NAME_EXPECTED : VALUE_EXPECTED;
5234 cvEndWriteStruct( *fs );
5235 fs.elname = string();
5237 else if( fs.state == NAME_EXPECTED + INSIDE_MAP )
5239 if( !cv_isalpha(*_str) )
5240 CV_Error_( CV_StsError, ("Incorrect element name %s", _str) );
5242 fs.state = VALUE_EXPECTED + INSIDE_MAP;
5244 else if( (fs.state & 3) == VALUE_EXPECTED )
5246 if( *_str == '{' || *_str == '[' )
5248 fs.structs.push_back(*_str);
5249 int flags = *_str++ == '{' ? CV_NODE_MAP : CV_NODE_SEQ;
5250 fs.state = flags == CV_NODE_MAP ? INSIDE_MAP +
5251 NAME_EXPECTED : VALUE_EXPECTED;
5254 flags |= CV_NODE_FLOW;
5257 cvStartWriteStruct( *fs, fs.elname.size() > 0 ? fs.elname.c_str() : 0,
5258 flags, *_str ? _str : 0 );
5259 fs.elname = string();
5263 write( fs, fs.elname, (_str[0] == '\\' && (_str[1] == '{' || _str[1] == '}' ||
5264 _str[1] == '[' || _str[1] == ']')) ? string(_str+1) : str );
5265 if( fs.state == INSIDE_MAP + VALUE_EXPECTED )
5266 fs.state = INSIDE_MAP + NAME_EXPECTED;
5270 CV_Error( CV_StsError, "Invalid fs.state" );
5275 void FileStorage::writeRaw( const string& fmt, const uchar* vec, size_t len )
5279 size_t elemSize, cn;
5280 getElemSize( fmt, elemSize, cn );
5281 CV_Assert( len % elemSize == 0 );
5282 cvWriteRawData( fs, vec, (int)(len/elemSize), fmt.c_str());
5286 void FileStorage::writeObj( const string& name, const void* obj )
5290 cvWrite( fs, name.size() > 0 ? name.c_str() : 0, obj );
5294 FileNode FileStorage::operator[](const string& nodename) const
5296 return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename.c_str()));
5299 FileNode FileStorage::operator[](const char* nodename) const
5301 return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename));
5304 FileNode FileNode::operator[](const string& nodename) const
5306 return FileNode(fs, cvGetFileNodeByName(fs, node, nodename.c_str()));
5309 FileNode FileNode::operator[](const char* nodename) const
5311 return FileNode(fs, cvGetFileNodeByName(fs, node, nodename));
5314 FileNode FileNode::operator[](int i) const
5316 return isSeq() ? FileNode(fs, (CvFileNode*)cvGetSeqElem(node->data.seq, i)) :
5317 i == 0 ? *this : FileNode();
5320 string FileNode::name() const
5323 return !node || (str = cvGetFileNodeName(node)) == 0 ? string() : string(str);
5326 void* FileNode::readObj() const
5330 return cvRead( (CvFileStorage*)fs, (CvFileNode*)node );
5333 FileNodeIterator::FileNodeIterator()
5341 FileNodeIterator::FileNodeIterator(const CvFileStorage* _fs,
5342 const CvFileNode* _node, size_t _ofs)
5344 if( _fs && _node && CV_NODE_TYPE(_node->tag) != CV_NODE_NONE )
5346 int node_type = _node->tag & FileNode::TYPE_MASK;
5349 if( !(_node->tag & FileNode::USER) && (node_type == FileNode::SEQ || node_type == FileNode::MAP) )
5351 cvStartReadSeq( _node->data.seq, &reader );
5352 remaining = FileNode(_fs, _node).size();
5356 reader.ptr = (schar*)_node;
5360 (*this) += (int)_ofs;
5371 FileNodeIterator::FileNodeIterator(const FileNodeIterator& it)
5374 container = it.container;
5376 remaining = it.remaining;
5379 FileNodeIterator& FileNodeIterator::operator ++()
5384 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
5390 FileNodeIterator FileNodeIterator::operator ++(int)
5392 FileNodeIterator it = *this;
5397 FileNodeIterator& FileNodeIterator::operator --()
5399 if( remaining < FileNode(fs, container).size() )
5402 CV_PREV_SEQ_ELEM( reader.seq->elem_size, reader );
5408 FileNodeIterator FileNodeIterator::operator --(int)
5410 FileNodeIterator it = *this;
5415 FileNodeIterator& FileNodeIterator::operator += (int ofs)
5420 ofs = std::min(ofs, (int)remaining);
5423 size_t count = FileNode(fs, container).size();
5424 ofs = (int)(remaining - std::min(remaining - ofs, count));
5428 cvSetSeqReaderPos( &reader, ofs, 1 );
5432 FileNodeIterator& FileNodeIterator::operator -= (int ofs)
5434 return operator += (-ofs);
5438 FileNodeIterator& FileNodeIterator::readRaw( const string& fmt, uchar* vec, size_t maxCount )
5440 if( fs && container && remaining > 0 )
5442 size_t elem_size, cn;
5443 getElemSize( fmt, elem_size, cn );
5444 CV_Assert( elem_size > 0 );
5445 size_t count = std::min(remaining, maxCount);
5449 cvReadRawDataSlice( fs, &reader, (int)count, vec, fmt.c_str() );
5450 remaining -= count*cn;
5454 cvReadRawData( fs, container, vec, fmt.c_str() );
5462 void write( FileStorage& fs, const string& name, int value )
5463 { cvWriteInt( *fs, name.size() ? name.c_str() : 0, value ); }
5465 void write( FileStorage& fs, const string& name, float value )
5466 { cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); }
5468 void write( FileStorage& fs, const string& name, double value )
5469 { cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); }
5471 void write( FileStorage& fs, const string& name, const string& value )
5472 { cvWriteString( *fs, name.size() ? name.c_str() : 0, value.c_str() ); }
5474 void writeScalar(FileStorage& fs, int value )
5475 { cvWriteInt( *fs, 0, value ); }
5477 void writeScalar(FileStorage& fs, float value )
5478 { cvWriteReal( *fs, 0, value ); }
5480 void writeScalar(FileStorage& fs, double value )
5481 { cvWriteReal( *fs, 0, value ); }
5483 void writeScalar(FileStorage& fs, const string& value )
5484 { cvWriteString( *fs, 0, value.c_str() ); }
5487 void write( FileStorage& fs, const string& name, const Mat& value )
5489 if( value.dims <= 2 )
5492 cvWrite( *fs, name.size() ? name.c_str() : 0, &mat );
5496 CvMatND mat = value;
5497 cvWrite( *fs, name.size() ? name.c_str() : 0, &mat );
5501 // TODO: the 4 functions below need to be implemented more efficiently
5502 void write( FileStorage& fs, const string& name, const SparseMat& value )
5504 Ptr<CvSparseMat> mat = (CvSparseMat*)value;
5505 cvWrite( *fs, name.size() ? name.c_str() : 0, mat );
5509 WriteStructContext::WriteStructContext(FileStorage& _fs, const string& name,
5510 int flags, const string& typeName) : fs(&_fs)
5512 cvStartWriteStruct(**fs, !name.empty() ? name.c_str() : 0, flags,
5513 !typeName.empty() ? typeName.c_str() : 0);
5516 WriteStructContext::~WriteStructContext() { cvEndWriteStruct(**fs); }
5519 void read( const FileNode& node, Mat& mat, const Mat& default_mat )
5523 default_mat.copyTo(mat);
5526 void* obj = cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node);
5527 if(CV_IS_MAT_HDR_Z(obj))
5529 Mat((const CvMat*)obj).copyTo(mat);
5530 cvReleaseMat((CvMat**)&obj);
5532 else if(CV_IS_MATND_HDR(obj))
5534 Mat((const CvMatND*)obj).copyTo(mat);
5535 cvReleaseMatND((CvMatND**)&obj);
5540 CV_Error(CV_StsBadArg, "Unknown array type");
5544 void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat )
5548 default_mat.copyTo(mat);
5551 Ptr<CvSparseMat> m = (CvSparseMat*)cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node);
5552 CV_Assert(CV_IS_SPARSE_MAT(m));
5553 SparseMat(m).copyTo(mat);