Lines Matching refs:fs
107 //typedef void (*CvParse)( struct CvFileStorage* fs );
108 typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key,
110 typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs );
111 typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value );
112 typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value );
113 typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key,
115 typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment );
116 typedef void (*CvStartNextStream)( struct CvFileStorage* fs );
163 #define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE)
165 #define CV_CHECK_FILE_STORAGE(fs) \
167 if( !CV_IS_FILE_STORAGE(fs) ) \
168 CV_ERROR( (fs) ? CV_StsBadArg : CV_StsNullPtr, \
172 #define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \
174 CV_CHECK_FILE_STORAGE(fs); \
175 if( !fs->write_mode ) \
231 icvParseError( fs, cvFuncName, (errmsg), __FILE__, __LINE__ ); \
237 icvParseError( CvFileStorage* fs, const char* func_name,
241 sprintf( buf, "%s(%d): %s", fs->filename, fs->lineno, err_msg );
247 icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection )
257 assert( fs->is_xml != 0 );
262 sizeof(CvFileMapNode), fs->memstorage, 16 ));
267 CV_CALL( seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage ));
311 icvFSDoResize( CvFileStorage* fs, char* ptr, int len )
318 int written_len = (int)(ptr - fs->buffer_start);
319 int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2);
322 fs->buffer = new_ptr + (fs->buffer - fs->buffer_start);
324 memcpy( new_ptr, fs->buffer_start, written_len );
325 fs->buffer_start = new_ptr;
326 fs->buffer_end = fs->buffer_start + new_size;
335 inline char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len )
337 return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len );
342 icvFSFlush( CvFileStorage* fs )
344 char* ptr = fs->buffer;
347 if( ptr > fs->buffer_start + fs->space )
351 fputs( fs->buffer_start, fs->file );
352 fs->buffer = fs->buffer_start;
355 indent = fs->struct_indent;
357 if( fs->space != indent )
359 if( fs->space < indent )
360 memset( fs->buffer_start + fs->space, ' ', indent - fs->space );
361 fs->space = indent;
364 ptr = fs->buffer = fs->buffer_start + fs->space;
383 CvFileStorage* fs = *p_fs;
386 if( fs->write_mode && fs->file )
388 if( fs->write_stack )
390 while( fs->write_stack->total > 0 )
391 cvEndWriteStruct(fs);
393 icvFSFlush(fs);
394 if( fs->is_xml )
395 fputs("</opencv_storage>\n", fs->file );
398 //icvFSReleaseCollection( fs->roots ); // delete all the user types recursively
400 if( fs->file )
402 fclose( fs->file );
403 fs->file = 0;
406 cvReleaseMemStorage( &fs->strstorage );
408 cvFree( &fs->buffer_start );
409 cvReleaseMemStorage( &fs->memstorage );
411 memset( fs, 0, sizeof(*fs) );
412 cvFree( &fs );
422 cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing )
431 CvStringHash* map = fs->str_hash;
433 if( !fs )
476 cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node,
488 if( !fs )
491 CV_CHECK_FILE_STORAGE(fs);
498 if( !fs->roots )
500 attempts = fs->roots->total;
511 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
558 cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str )
569 if( !fs )
572 CV_CHECK_FILE_STORAGE(fs);
584 if( !fs->roots )
586 attempts = fs->roots->total;
596 map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
635 cvGetRootFileNode( const CvFileStorage* fs, int stream_index )
642 CV_CHECK_FILE_STORAGE(fs);
644 if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total )
647 value = (CvFileNode*)cvGetSeqElem( fs->roots, stream_index );
657 cvGetFileNodeFromSeq( CvFileStorage* fs,
669 seq = fs->roots;
776 icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr )
807 static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
823 icvProcessSpecialDouble( fs, ptr, &fval, endptr );
834 icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent )
846 if( ptr - fs->buffer_start > max_comment_indent )
852 if( ptr - fs->buffer_start < min_indent )
858 int max_size = (int)(fs->buffer_end - fs->buffer_start);
859 ptr = fgets( fs->buffer_start, max_size, fs->file );
863 ptr = fs->buffer_start;
866 fs->dummy_eof = 1;
872 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !feof(fs->file) )
876 fs->lineno++;
889 icvYMLParseKey( CvFileStorage* fs, char* ptr,
917 CV_CALL( str_hash_node = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ));
918 CV_CALL( *value_placeholder = cvGetFileNode( fs, map_node, str_hash_node, 1 ));
928 icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
985 CV_CALL( ptr = icvYMLSkipSpaces( fs, endptr, min_indent, INT_MAX ));
1012 fval = icv_strtod( fs, ptr, &endptr );
1014 CV_CALL( icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/
1098 CV_CALL( node->data.str = cvMemStorageAllocString( fs->memstorage, buf, len ));
1106 CV_CALL( icvFSCreateCollection( fs, CV_NODE_TYPE(struct_flags) +
1115 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ));
1128 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr + 1, new_min_indent, INT_MAX ));
1133 CV_CALL( ptr = icvYMLParseKey( fs, ptr, node, &elem ));
1134 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ));
1142 CV_CALL( ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, new_min_indent ));
1183 CV_CALL( node->data.str = cvMemStorageAllocString( fs->memstorage, ptr, (int)(str_end - ptr) ));
1192 CV_CALL( icvFSCreateCollection( fs, struct_flags +
1195 indent = (int)(ptr - fs->buffer_start);
1204 CV_CALL( ptr = icvYMLParseKey( fs, ptr, node, &elem ));
1215 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, indent + 1, INT_MAX ));
1216 CV_CALL( ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, indent + 1 ));
1221 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
1222 if( ptr - fs->buffer_start != indent )
1224 if( ptr - fs->buffer_start < indent )
1243 icvYMLParse( CvFileStorage* fs )
1249 char* ptr = fs->buffer_start;
1258 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
1289 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
1293 CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
1295 CV_CALL( ptr = icvYMLParseValue( fs, ptr, root_node, CV_NODE_NONE, 0 ));
1300 CV_CALL( ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ));
1305 if( fs->dummy_eof )
1320 icvYMLWrite( CvFileStorage* fs, const char* key, const char* data, const char* cvFuncName )
1331 struct_flags = fs->struct_flags;
1344 fs->is_first = 0;
1364 ptr = fs->buffer;
1367 new_offset = (int)(ptr - fs->buffer_start) + keylen + datalen;
1368 if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 )
1370 fs->buffer = ptr;
1371 ptr = icvFSFlush(fs);
1378 ptr = icvFSFlush(fs);
1392 ptr = icvFSResizeWriteBuffer( fs, ptr, keylen );
1411 ptr = icvFSResizeWriteBuffer( fs, ptr, datalen );
1416 fs->buffer = ptr;
1417 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
1424 icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
1460 CV_CALL( icvYMLWrite( fs, key, data, cvFuncName ));
1462 parent_flags = fs->struct_flags;
1463 cvSeqPush( fs->write_stack, &parent_flags );
1464 fs->struct_flags = struct_flags;
1467 fs->struct_indent += CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1474 icvYMLEndWriteStruct( CvFileStorage* fs )
1483 struct_flags = fs->struct_flags;
1484 if( fs->write_stack->total == 0 )
1487 cvSeqPop( fs->write_stack, &parent_flags );
1491 ptr = fs->buffer;
1492 if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) )
1495 fs->buffer = ptr;
1499 ptr = icvFSFlush(fs);
1501 fs->buffer = ptr + 2;
1505 fs->struct_indent -= CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags);
1506 assert( fs->struct_indent >= 0 );
1508 fs->struct_flags = parent_flags;
1515 icvYMLStartNextStream( CvFileStorage* fs )
1521 if( !fs->is_first )
1523 while( fs->write_stack->total > 0 )
1524 icvYMLEndWriteStruct(fs);
1526 fs->struct_indent = 0;
1527 icvFSFlush(fs);
1528 fputs( "...\n", fs->file );
1529 fputs( "---\n", fs->file );
1530 fs->buffer = fs->buffer_start;
1538 icvYMLWriteInt( CvFileStorage* fs, const char* key, int value )
1545 CV_CALL( icvYMLWrite( fs, key, icv_itoa( value, buf, 10 ), cvFuncName ));
1552 icvYMLWriteReal( CvFileStorage* fs, const char* key, double value )
1559 CV_CALL( icvYMLWrite( fs, key, icvDoubleToString( buf, value ), cvFuncName ));
1566 icvYMLWriteString( CvFileStorage* fs, const char* key,
1627 CV_CALL( icvYMLWrite( fs, key, data, cvFuncName ));
1634 icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
1651 ptr = fs->buffer;
1654 fs->buffer_end - ptr < len || ptr == fs->buffer_start )
1655 ptr = icvFSFlush( fs );
1665 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
1667 fs->buffer = ptr + (eol - comment);
1674 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
1676 fs->buffer = ptr + len;
1679 ptr = icvFSFlush( fs );
1695 icvXMLSkipSpaces( CvFileStorage* fs, char* ptr, int mode )
1750 int max_size = (int)(fs->buffer_end - fs->buffer_start);
1753 ptr = fgets( fs->buffer_start, max_size, fs->file );
1756 ptr = fs->buffer_start;
1758 fs->dummy_eof = 1;
1764 if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !feof(fs->file) )
1767 fs->lineno++;
1778 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
1782 icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node,
1803 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
1823 CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
1852 CV_CALL( icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node ));
1861 CV_CALL( elem = cvGetFileNode( fs, node, key, 1 ));
1863 CV_CALL( ptr = icvXMLParseValue( fs, ptr, elem, elem_type));
1868 CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type ));
1882 CV_CALL( icvFSCreateCollection( fs, CV_NODE_SEQ, node ));
1899 fval = icv_strtod( fs, ptr, &endptr );
1901 CV_CALL( icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/
1993 CV_CALL( elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i ));
2007 CV_CALL( icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ?
2027 icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag,
2080 CV_CALL( attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ));
2094 CV_CALL( chunk = (CvAttrList*)cvMemStorageAlloc( fs->memstorage, attr_buf_size ));
2112 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
2120 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
2125 ptr = icvXMLParseValue( fs, ptr, &stub, CV_NODE_STRING );
2136 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
2176 icvXMLParse( CvFileStorage* fs )
2182 char* ptr = fs->buffer_start;
2188 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ));
2193 CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
2208 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
2213 CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ));
2218 root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 );
2219 CV_CALL( ptr = icvXMLParseValue( fs, ptr, root_node, CV_NODE_NONE ));
2220 CV_CALL( ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type ));
2223 CV_CALL( ptr = icvXMLSkipSpaces( fs, ptr, 0 ));
2227 assert( fs->dummy_eof != 0 );
2240 icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList list )
2246 char* ptr = fs->buffer;
2248 int struct_flags = fs->struct_flags;
2264 fs->is_first = 0;
2268 ptr = icvXMLFlush(fs);
2288 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2307 ptr = icvFSResizeWriteBuffer( fs, ptr, len0 + len1 + 4 );
2325 fs->buffer = ptr;
2326 fs->struct_flags = struct_flags & ~CV_NODE_EMPTY;
2333 icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2356 CV_CALL( icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(attr,0) ));
2358 parent.struct_flags = fs->struct_flags & ~CV_NODE_EMPTY;
2359 parent.struct_indent = fs->struct_indent;
2360 parent.struct_tag = fs->struct_tag;
2361 cvSaveMemStoragePos( fs->strstorage, &parent.pos );
2362 cvSeqPush( fs->write_stack, &parent );
2364 fs->struct_indent += CV_XML_INDENT;
2366 icvXMLFlush( fs );
2368 fs->struct_flags = struct_flags;
2371 CV_CALL( fs->struct_tag = cvMemStorageAllocString( fs->strstorage, (char*)key, -1 ));
2375 fs->struct_tag.ptr = 0;
2376 fs->struct_tag.len = 0;
2384 icvXMLEndWriteStruct( CvFileStorage* fs )
2392 if( fs->write_stack->total == 0 )
2395 CV_CALL( icvXMLWriteTag( fs, fs->struct_tag.ptr, CV_XML_CLOSING_TAG, cvAttrList(0,0) ));
2396 cvSeqPop( fs->write_stack, &parent );
2398 fs->struct_indent = parent.struct_indent;
2399 fs->struct_flags = parent.struct_flags;
2400 fs->struct_tag = parent.struct_tag;
2401 cvRestoreMemStoragePos( fs->strstorage, &parent.pos );
2408 icvXMLStartNextStream( CvFileStorage* fs )
2414 if( !fs->is_first )
2416 while( fs->write_stack->total > 0 )
2417 icvXMLEndWriteStruct(fs);
2419 fs->struct_indent = 0;
2420 icvXMLFlush(fs);
2424 fputs( "\n<!-- next stream -->\n", fs->file );
2425 /*fputs( "</opencv_storage>\n", fs->file );
2426 fputs( "<opencv_storage>\n", fs->file );*/
2427 fs->buffer = fs->buffer_start;
2435 icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len )
2441 if( CV_NODE_IS_MAP(fs->struct_flags) ||
2442 (!CV_NODE_IS_COLLECTION(fs->struct_flags) && key) )
2444 icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(0,0) );
2445 char* ptr = icvFSResizeWriteBuffer( fs, fs->buffer, len );
2447 fs->buffer = ptr + len;
2448 icvXMLWriteTag( fs, key, CV_XML_CLOSING_TAG, cvAttrList(0,0) );
2452 char* ptr = fs->buffer;
2453 int new_offset = (int)(ptr - fs->buffer_start) + len;
2458 fs->struct_flags = CV_NODE_SEQ;
2460 if( (new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10) ||
2461 (ptr > fs->buffer_start && ptr[-1] == '>' && !CV_NODE_IS_EMPTY(fs->struct_flags)) )
2463 ptr = icvXMLFlush(fs);
2465 else if( ptr > fs->buffer_start + fs->struct_indent && ptr[-1] != '>' )
2469 fs->buffer = ptr + len;
2477 icvXMLWriteInt( CvFileStorage* fs, const char* key, int value )
2485 icvXMLWriteScalar( fs, key, ptr, len );
2492 icvXMLWriteReal( CvFileStorage* fs, const char* key, double value )
2500 icvXMLWriteScalar( fs, key, buf, len );
2507 icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote )
2587 icvXMLWriteScalar( fs, key, data, len );
2594 icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2614 ptr = fs->buffer;
2616 if( multiline || !eol_comment || fs->buffer_end - ptr < len + 5 )
2617 ptr = icvXMLFlush( fs );
2618 else if( ptr > fs->buffer_start + fs->struct_indent )
2623 ptr = icvFSResizeWriteBuffer( fs, ptr, len + 9 );
2633 fs->buffer = ptr + len;
2634 ptr = icvXMLFlush(fs);
2642 ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 );
2651 ptr = icvFSResizeWriteBuffer( fs, ptr, len );
2656 fs->buffer = ptr;
2657 ptr = icvXMLFlush( fs );
2660 fs->buffer = ptr + 3;
2661 icvXMLFlush( fs );
2675 CvFileStorage* fs = 0;
2688 CV_CALL( fs = (CvFileStorage*)cvAlloc( sizeof(*fs) ));
2689 memset( fs, 0, sizeof(*fs));
2691 CV_CALL( fs->memstorage = cvCreateMemStorage( default_block_size ));
2692 fs->dststorage = dststorage ? dststorage : fs->memstorage;
2694 CV_CALL( fs->filename = (char*)cvMemStorageAlloc( fs->memstorage, strlen(filename)+1 ));
2695 strcpy( fs->filename, filename );
2697 fs->flags = CV_FILE_STORAGE;
2698 fs->write_mode = (flags & 3) != 0;
2699 fs->file = fopen( fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" );
2700 if( !fs->file )
2703 fs->roots = 0;
2704 fs->struct_indent = 0;
2705 fs->struct_flags = 0;
2706 fs->wrap_margin = 71;
2708 if( fs->write_mode )
2712 int buf_size = CV_FS_MAX_LEN*(fs->is_xml ? 6 : 4) + 1024;
2714 char* dot_pos = strrchr( fs->filename, '.' );
2715 fs->is_xml = dot_pos && (strcmp( dot_pos, ".xml" ) == 0 ||
2719 fseek( fs->file, 0, SEEK_END );
2721 fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->is_xml ?
2722 sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage );
2723 fs->is_first = 1;
2724 fs->struct_indent = 0;
2725 fs->struct_flags = CV_NODE_EMPTY;
2726 CV_CALL( fs->buffer_start = fs->buffer = (char*)cvAlloc( buf_size + 1024 ));
2727 fs->buffer_end = fs->buffer_start + buf_size;
2728 if( fs->is_xml )
2730 int file_size = (int)ftell( fs->file );
2731 CV_CALL( fs->strstorage = cvCreateChildMemStorage( fs->memstorage ));
2734 fputs( "<?xml version=\"1.0\"?>\n", fs->file );
2735 fputs( "<opencv_storage>\n", fs->file );
2743 fseek( fs->file, -xml_buf_size, SEEK_END );
2748 int line_offset = ftell( fs->file );
2749 char* ptr0 = fgets( xml_buf, xml_buf_size, fs->file ), *ptr;
2764 fclose( fs->file );
2765 fs->file = fopen( fs->filename, "r+t" );
2766 fseek( fs->file, last_occurence, SEEK_SET );
2768 fputs( " <!-- resumed -->", fs->file );
2769 fseek( fs->file, 0, SEEK_END );
2770 fputs( "\n", fs->file );
2772 fs->start_write_struct = icvXMLStartWriteStruct;
2773 fs->end_write_struct = icvXMLEndWriteStruct;
2774 fs->write_int = icvXMLWriteInt;
2775 fs->write_real = icvXMLWriteReal;
2776 fs->write_string = icvXMLWriteString;
2777 fs->write_comment = icvXMLWriteComment;
2778 fs->start_next_stream = icvXMLStartNextStream;
2783 fputs( "%YAML:1.0\n", fs->file );
2785 fputs( "...\n---\n", fs->file );
2786 fs->start_write_struct = icvYMLStartWriteStruct;
2787 fs->end_write_struct = icvYMLEndWriteStruct;
2788 fs->write_int = icvYMLWriteInt;
2789 fs->write_real = icvYMLWriteReal;
2790 fs->write_string = icvYMLWriteString;
2791 fs->write_comment = icvYMLWriteComment;
2792 fs->start_next_stream = icvYMLStartNextStream;
2800 fgets( buf, sizeof(buf)-2, fs->file );
2801 fs->is_xml = strncmp( buf, yaml_signature, strlen(yaml_signature) ) != 0;
2803 fseek( fs->file, 0, SEEK_END );
2804 buf_size = ftell( fs->file );
2805 fseek( fs->file, 0, SEEK_SET );
2810 CV_CALL( fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash),
2811 sizeof(CvStringHashNode), fs->memstorage, 256 ));
2813 CV_CALL( fs->roots = cvCreateSeq( 0, sizeof(CvSeq),
2814 sizeof(CvFileNode), fs->memstorage ));
2816 CV_CALL( fs->buffer = fs->buffer_start = (char*)cvAlloc( buf_size + 256 ));
2817 fs->buffer_end = fs->buffer_start + buf_size;
2818 fs->buffer[0] = '\n';
2819 fs->buffer[1] = '\0';
2823 if( fs->is_xml )
2824 icvXMLParse( fs );
2826 icvYMLParse( fs );
2830 cvFree( &fs->buffer_start );
2831 fs->buffer = fs->buffer_end = 0;
2836 if( fs )
2838 if( cvGetErrStatus() < 0 || !fs->file )
2840 cvReleaseFileStorage( &fs );
2842 else if( !fs->write_mode )
2844 fclose( fs->file );
2845 fs->file = 0;
2851 fs;
2856 cvStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags,
2863 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2864 CV_CALL( fs->start_write_struct( fs, key, struct_flags, type_name ));
2871 cvEndWriteStruct( CvFileStorage* fs )
2877 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2878 CV_CALL( fs->end_write_struct( fs ));
2885 cvWriteInt( CvFileStorage* fs, const char* key, int value )
2891 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2892 CV_CALL( fs->write_int( fs, key, value ));
2899 cvWriteReal( CvFileStorage* fs, const char* key, double value )
2905 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2906 CV_CALL( fs->write_real( fs, key, value ));
2913 cvWriteString( CvFileStorage* fs, const char* key, const char* value, int quote )
2919 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2920 CV_CALL( fs->write_string( fs, key, value, quote ));
2927 cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment )
2933 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2934 CV_CALL( fs->write_comment( fs, comment, eol_comment ));
2941 cvStartNextStream( CvFileStorage* fs )
2947 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
2948 CV_CALL( fs->start_next_stream( fs ));
3083 cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
3095 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
3167 if( fs->is_xml )
3170 CV_CALL( icvXMLWriteScalar( fs, 0, ptr, buf_len ));
3173 CV_CALL( icvYMLWrite( fs, 0, ptr, cvFuncName ));
3185 cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader )
3192 CV_CHECK_FILE_STORAGE( fs );
3222 cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,
3233 CV_CHECK_FILE_STORAGE( fs );
3375 cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,
3387 CV_CALL( cvStartReadRawData( fs, src, &reader ));
3388 cvReadRawDataSlice( fs, &reader, CV_NODE_IS_SEQ(src->tag) ?
3396 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node );
3399 icvWriteCollection( CvFileStorage* fs, const CvFileNode* node )
3414 icvWriteFileNode( fs, name, &elem->value );
3421 icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node )
3430 fs->write_int( fs, name, node->data.i );
3433 fs->write_real( fs, name, node->data.f );
3436 fs->write_string( fs, name, node->data.str.ptr, 0 );
3440 fs->start_write_struct( fs, name, CV_NODE_TYPE(node->tag) +
3443 icvWriteCollection( fs, node );
3444 fs->end_write_struct( fs );
3447 fs->start_write_struct( fs, name, CV_NODE_SEQ, 0 );
3448 fs->end_write_struct( fs );
3459 cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,
3468 CV_CHECK_OUTPUT_FILE_STORAGE(fs);
3475 CV_CALL( icvWriteCollection( fs, node ));
3479 CV_CALL( icvWriteFileNode( fs, new_node_name, node ));
3483 stream_count = fs->roots->total;
3486 CvFileNode* node = (CvFileNode*)cvGetSeqElem( fs->roots, i, 0 );
3526 icvWriteMat( CvFileStorage* fs, const char* name,
3540 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT ));
3541 cvWriteInt( fs, "rows", mat->rows );
3542 cvWriteInt( fs, "cols", mat->cols );
3543 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3544 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3554 cvWriteRawData( fs, mat->data.ptr + y*mat->step, size.width, dt );
3555 cvEndWriteStruct( fs );
3556 cvEndWriteStruct( fs );
3571 icvReadMat( CvFileStorage* fs, CvFileNode* node )
3583 CV_CALL( rows = cvReadIntByName( fs, node, "rows", 0 ));
3584 cols = cvReadIntByName( fs, node, "cols", 0 );
3585 dt = cvReadStringByName( fs, node, "dt", 0 );
3592 data = cvGetFileNodeByName( fs, node, "data" );
3601 CV_CALL( cvReadRawData( fs, data, mat->data.ptr, dt ));
3621 icvWriteMatND( CvFileStorage* fs, const char* name,
3636 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MATND ));
3638 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3639 cvWriteRawData( fs, sizes, dims, "i" );
3640 cvEndWriteStruct( fs );
3641 cvWriteString( fs, "dt", icvEncodeFormat( cvGetElemType(mat), dt ), 0 );
3642 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3647 cvWriteRawData( fs, iterator.ptr[0], iterator.size.width, dt );
3649 cvEndWriteStruct( fs );
3650 cvEndWriteStruct( fs );
3657 icvReadMatND( CvFileStorage* fs, CvFileNode* node )
3671 CV_CALL( sizes_node = cvGetFileNodeByName( fs, node, "sizes" ));
3672 dt = cvReadStringByName( fs, node, "dt", 0 );
3683 CV_CALL( cvReadRawData( fs, sizes_node, sizes, "i" ));
3686 data = cvGetFileNodeByName( fs, node, "data" );
3698 CV_CALL( cvReadRawData( fs, data, mat->data.ptr, dt ));
3736 icvWriteSparseMat( CvFileStorage* fs, const char* name,
3758 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SPARSE_MAT ));
3761 cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW );
3762 cvWriteRawData( fs, mat->size, dims, "i" );
3763 cvEndWriteStruct( fs );
3764 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 );
3765 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3792 fs->write_int( fs, 0, k - dims + 1 );
3795 fs->write_int( fs, 0, idx[k] );
3801 cvWriteRawData( fs, val, 1, dt );
3804 cvEndWriteStruct( fs );
3805 cvEndWriteStruct( fs );
3814 icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
3831 CV_CALL( sizes_node = cvGetFileNodeByName( fs, node, "sizes" ));
3832 dt = cvReadStringByName( fs, node, "dt", 0 );
3844 CV_CALL( cvReadRawData( fs, sizes_node, sizes, "i" ));
3847 data = cvGetFileNodeByName( fs, node, "data" );
3856 cvStartReadRawData( fs, data, &reader );
3887 CV_CALL( cvReadRawDataSlice( fs, &reader, cn, val, dt ));
3908 icvWriteImage( CvFileStorage* fs, const char* name,
3926 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_IMAGE ));
3927 cvWriteInt( fs, "width", image->width );
3928 cvWriteInt( fs, "height", image->height );
3929 cvWriteString( fs, "origin", image->origin == IPL_ORIGIN_TL
3931 cvWriteString( fs, "layout", image->dataOrder == IPL_DATA_ORDER_PLANE
3935 cvStartWriteStruct( fs, "roi", CV_NODE_MAP + CV_NODE_FLOW );
3936 cvWriteInt( fs, "x", image->roi->xOffset );
3937 cvWriteInt( fs, "y", image->roi->yOffset );
3938 cvWriteInt( fs, "width", image->roi->width );
3939 cvWriteInt( fs, "height", image->roi->height );
3940 cvWriteInt( fs, "coi", image->roi->coi );
3941 cvEndWriteStruct( fs );
3947 cvWriteString( fs, "dt", dt, 0 );
3956 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
3958 cvWriteRawData( fs, image->imageData + y*image->widthStep, size.width, dt );
3959 cvEndWriteStruct( fs );
3960 cvEndWriteStruct( fs );
3967 icvReadImage( CvFileStorage* fs, CvFileNode* node )
3983 CV_CALL( width = cvReadIntByName( fs, node, "width", 0 ));
3984 height = cvReadIntByName( fs, node, "height", 0 );
3985 dt = cvReadStringByName( fs, node, "dt", 0 );
3986 origin = cvReadStringByName( fs, node, "origin", 0 );
3992 data_order = cvReadStringByName( fs, node, "layout", "interleaved" );
3996 data = cvGetFileNodeByName( fs, node, "data" );
4007 roi_node = cvGetFileNodeByName( fs, node, "roi" );
4010 roi.x = cvReadIntByName( fs, roi_node, "x", 0 );
4011 roi.y = cvReadIntByName( fs, roi_node, "y", 0 );
4012 roi.width = cvReadIntByName( fs, roi_node, "width", 0 );
4013 roi.height = cvReadIntByName( fs, roi_node, "height", 0 );
4014 coi = cvReadIntByName( fs, roi_node, "coi", 0 );
4027 cvStartReadRawData( fs, data, &reader );
4030 CV_CALL( cvReadRawDataSlice( fs, &reader, width,
4076 icvWriteHeaderData( CvFileStorage* fs, const CvSeq* seq,
4102 cvStartWriteStruct( fs, "rect", CV_NODE_MAP + CV_NODE_FLOW );
4103 cvWriteInt( fs, "x", point_seq->rect.x );
4104 cvWriteInt( fs, "y", point_seq->rect.y );
4105 cvWriteInt( fs, "width", point_seq->rect.width );
4106 cvWriteInt( fs, "height", point_seq->rect.height );
4107 cvEndWriteStruct( fs );
4108 cvWriteInt( fs, "color", point_seq->color );
4115 cvStartWriteStruct( fs, "origin", CV_NODE_MAP + CV_NODE_FLOW );
4116 cvWriteInt( fs, "x", chain->origin.x );
4117 cvWriteInt( fs, "y", chain->origin.y );
4118 cvEndWriteStruct( fs );
4134 cvWriteString( fs, "header_dt", header_dt, 0 );
4135 cvStartWriteStruct( fs, "header_user_data", CV_NODE_SEQ + CV_NODE_FLOW );
4136 cvWriteRawData( fs, (uchar*)seq + sizeof(CvSeq), 1, header_dt );
4137 cvEndWriteStruct( fs );
4192 icvWriteSeq( CvFileStorage* fs, const char* name,
4206 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ ));
4209 cvWriteInt( fs, "level", level );
4212 cvWriteString( fs, "flags", buf, 1 );
4213 cvWriteInt( fs, "count", seq->total );
4215 cvWriteString( fs, "dt", dt, 0 );
4217 CV_CALL( icvWriteHeaderData( fs, seq, &attr, sizeof(CvSeq) ));
4218 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW );
4222 cvWriteRawData( fs, block->data, block->count, dt );
4226 cvEndWriteStruct( fs );
4227 cvEndWriteStruct( fs );
4234 icvWriteSeqTree( CvFileStorage* fs, const char* name,
4253 CV_CALL( icvWriteSeq( fs, name, seq, attr, -1 ));
4259 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ_TREE ));
4260 CV_CALL( cvStartWriteStruct( fs, "sequences", CV_NODE_SEQ ));
4267 CV_CALL( icvWriteSeq( fs, 0, tree_iterator.node, attr, tree_iterator.level ));
4271 cvEndWriteStruct( fs );
4272 cvEndWriteStruct( fs );
4280 icvReadSeq( CvFileStorage* fs, CvFileNode* node )
4300 CV_CALL( flags_str = cvReadStringByName( fs, node, "flags", 0 ));
4301 total = cvReadIntByName( fs, node, "count", -1 );
4302 dt = cvReadStringByName( fs, node, "dt", 0 );
4311 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4312 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4318 rect_node = cvGetFileNodeByName( fs, node, "rect" );
4319 origin_node = cvGetFileNodeByName( fs, node, "origin" );
4334 CV_CALL( seq = cvCreateSeq( flags, header_size, elem_size, fs->dststorage ));
4338 CV_CALL( cvReadRawData( fs, header_node, (char*)seq + sizeof(CvSeq), header_dt ));
4343 point_seq->rect.x = cvReadIntByName( fs, rect_node, "x", 0 );
4344 point_seq->rect.y = cvReadIntByName( fs, rect_node, "y", 0 );
4345 point_seq->rect.width = cvReadIntByName( fs, rect_node, "width", 0 );
4346 point_seq->rect.height = cvReadIntByName( fs, rect_node, "height", 0 );
4347 point_seq->color = cvReadIntByName( fs, node, "color", 0 );
4352 chain->origin.x = cvReadIntByName( fs, origin_node, "x", 0 );
4353 chain->origin.y = cvReadIntByName( fs, origin_node, "y", 0 );
4362 data = cvGetFileNodeByName( fs, node, "data" );
4369 cvStartReadRawData( fs, data, &reader );
4373 cvReadRawDataSlice( fs, &reader, delta, block->data, dt );
4387 icvReadSeqTree( CvFileStorage* fs, CvFileNode* node )
4394 CvFileNode *sequences_node = cvGetFileNodeByName( fs, node, "sequences" );
4416 CV_CALL( seq = (CvSeq*)cvRead( fs, elem ));
4417 CV_CALL( level = cvReadIntByName( fs, elem, "level", -1 ));
4485 icvWriteGraph( CvFileStorage* fs, const char* name,
4521 CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH ));
4524 cvWriteString( fs, "flags", buf, 1 );
4526 cvWriteInt( fs, "vertex_count", vtx_count );
4530 cvWriteString( fs, "vertex_dt", vtx_dt, 0 );
4532 cvWriteInt( fs, "edge_count", edge_count );
4537 cvWriteString( fs, "edge_dt", edge_dt, 0 );
4539 CV_CALL( icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) ));
4569 cvStartWriteStruct( fs, k == 0 ? "vertices" : "edges",
4595 cvWriteRawData( fs, write_buf, write_count, dt );
4604 cvWriteRawData( fs, write_buf, write_count, dt );
4605 cvEndWriteStruct( fs );
4609 cvEndWriteStruct( fs );
4629 icvReadGraph( CvFileStorage* fs, CvFileNode* node )
4654 CV_CALL( flags_str = cvReadStringByName( fs, node, "flags", 0 ));
4655 vtx_dt = cvReadStringByName( fs, node, "vertex_dt", 0 );
4656 edge_dt = cvReadStringByName( fs, node, "edge_dt", 0 );
4657 vtx_count = cvReadIntByName( fs, node, "vertex_count", -1 );
4658 edge_count = cvReadIntByName( fs, node, "edge_count", -1 );
4668 header_dt = cvReadStringByName( fs, node, "header_dt", 0 );
4669 header_node = cvGetFileNodeByName( fs, node, "header_user_data" );
4722 CV_CALL( graph = cvCreateGraph( flags, header_size, vtx_size, edge_size, fs->dststorage ));
4725 CV_CALL( cvReadRawData( fs, header_node, (char*)graph + sizeof(CvGraph), header_dt ));
4732 vtx_node = cvGetFileNodeByName( fs, node, "vertices" );
4733 edge_node = cvGetFileNodeByName( fs, node, "edges" );
4751 cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
4758 cvReadRawDataSlice( fs, &reader, count, read_buf, dt );
5047 cvRead( CvFileStorage* fs, CvFileNode* node, CvAttrList* list )
5055 CV_CHECK_FILE_STORAGE( fs );
5063 CV_CALL( obj = node->info->read( fs, node ));
5076 cvWrite( CvFileStorage* fs, const char* name,
5085 CV_CHECK_OUTPUT_FILE_STORAGE( fs );
5097 CV_CALL( info->write( fs, name, ptr, attributes ));
5108 CvFileStorage* fs = 0;
5120 CV_CALL( fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE ));
5121 if( !fs )
5161 CV_CALL( cvWriteComment( fs, comment, 0 ));
5162 CV_CALL( cvWrite( fs, name, struct_ptr, attributes ));
5166 cvReleaseFileStorage( &fs );
5176 CvFileStorage* fs = 0;
5183 CV_CALL( fs = cvOpenFileStorage( filename, memstorage, CV_STORAGE_READ ));
5185 if( !fs )
5190 CV_CALL( node = cvGetFileNodeByName( fs, 0, name ));
5195 for( k = 0; k < fs->roots->total; k++ )
5200 node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
5228 CV_CALL( ptr = cvRead( fs, node, 0 ));
5237 cvReleaseFileStorage( &fs );