-
-
Notifications
You must be signed in to change notification settings - Fork 55.6k
/
persistence.cpp
5698 lines (4817 loc) · 165 KB
/
persistence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include <ctype.h>
#include <deque>
#include <iterator>
#define USE_ZLIB 1
#ifdef __APPLE__
# include "TargetConditionals.h"
# if (defined TARGET_OS_IPHONE && TARGET_OS_IPHONE) || (defined TARGET_IPHONE_SIMULATOR && TARGET_IPHONE_SIMULATOR)
# undef USE_ZLIB
# define USE_ZLIB 0
typedef void* gzFile;
# endif
#endif
#if USE_ZLIB
# ifndef _LFS64_LARGEFILE
# define _LFS64_LARGEFILE 0
# endif
# ifndef _FILE_OFFSET_BITS
# define _FILE_OFFSET_BITS 0
# endif
# include <zlib.h>
#endif
/****************************************************************************************\
* Common macros and type definitions *
\****************************************************************************************/
#define cv_isprint(c) ((uchar)(c) >= (uchar)' ')
#define cv_isprint_or_tab(c) ((uchar)(c) >= (uchar)' ' || (c) == '\t')
static inline bool cv_isalnum(char c)
{
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
static inline bool cv_isalpha(char c)
{
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
static inline bool cv_isdigit(char c)
{
return '0' <= c && c <= '9';
}
static inline bool cv_isspace(char c)
{
return (9 <= c && c <= 13) || c == ' ';
}
static char* icv_itoa( int _val, char* buffer, int /*radix*/ )
{
const int radix = 10;
char* ptr=buffer + 23 /* enough even for 64-bit integers */;
unsigned val = abs(_val);
*ptr = '\0';
do
{
unsigned r = val / radix;
*--ptr = (char)(val - (r*radix) + '0');
val = r;
}
while( val != 0 );
if( _val < 0 )
*--ptr = '-';
return ptr;
}
cv::String cv::FileStorage::getDefaultObjectName(const cv::String& _filename)
{
static const char* stubname = "unnamed";
const char* filename = _filename.c_str();
const char* ptr2 = filename + _filename.size();
const char* ptr = ptr2 - 1;
cv::AutoBuffer<char> name_buf(_filename.size()+1);
while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' )
{
if( *ptr == '.' && (!*ptr2 || strncmp(ptr2, ".gz", 3) == 0) )
ptr2 = ptr;
ptr--;
}
ptr++;
if( ptr == ptr2 )
CV_Error( CV_StsBadArg, "Invalid filename" );
char* name = name_buf;
// name must start with letter or '_'
if( !cv_isalpha(*ptr) && *ptr!= '_' ){
*name++ = '_';
}
while( ptr < ptr2 )
{
char c = *ptr++;
if( !cv_isalnum(c) && c != '-' && c != '_' )
c = '_';
*name++ = c;
}
*name = '\0';
name = name_buf;
if( strcmp( name, "_" ) == 0 )
strcpy( name, stubname );
return String(name);
}
typedef struct CvGenericHash
{
CV_SET_FIELDS()
int tab_size;
void** table;
}
CvGenericHash;
typedef CvGenericHash CvStringHash;
typedef struct CvFileMapNode
{
CvFileNode value;
const CvStringHashNode* key;
struct CvFileMapNode* next;
}
CvFileMapNode;
typedef struct CvXMLStackRecord
{
CvMemStoragePos pos;
CvString struct_tag;
int struct_indent;
int struct_flags;
}
CvXMLStackRecord;
#define CV_XML_OPENING_TAG 1
#define CV_XML_CLOSING_TAG 2
#define CV_XML_EMPTY_TAG 3
#define CV_XML_HEADER_TAG 4
#define CV_XML_DIRECTIVE_TAG 5
//typedef void (*CvParse)( struct CvFileStorage* fs );
typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key,
int struct_flags, const char* type_name );
typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs );
typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value );
typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value );
typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key,
const char* value, int quote );
typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment );
typedef void (*CvStartNextStream)( struct CvFileStorage* fs );
typedef struct CvFileStorage
{
int flags;
int fmt;
int write_mode;
int is_first;
CvMemStorage* memstorage;
CvMemStorage* dststorage;
CvMemStorage* strstorage;
CvStringHash* str_hash;
CvSeq* roots;
CvSeq* write_stack;
int struct_indent;
int struct_flags;
CvString struct_tag;
int space;
char* filename;
FILE* file;
gzFile gzfile;
char* buffer;
char* buffer_start;
char* buffer_end;
int wrap_margin;
int lineno;
int dummy_eof;
const char* errmsg;
char errmsgbuf[128];
CvStartWriteStruct start_write_struct;
CvEndWriteStruct end_write_struct;
CvWriteInt write_int;
CvWriteReal write_real;
CvWriteString write_string;
CvWriteComment write_comment;
CvStartNextStream start_next_stream;
const char* strbuf;
size_t strbufsize, strbufpos;
std::deque<char>* outbuf;
bool is_opened;
}
CvFileStorage;
static void icvPuts( CvFileStorage* fs, const char* str )
{
if( fs->outbuf )
std::copy(str, str + strlen(str), std::back_inserter(*fs->outbuf));
else if( fs->file )
fputs( str, fs->file );
#if USE_ZLIB
else if( fs->gzfile )
gzputs( fs->gzfile, str );
#endif
else
CV_Error( CV_StsError, "The storage is not opened" );
}
static char* icvGets( CvFileStorage* fs, char* str, int maxCount )
{
if( fs->strbuf )
{
size_t i = fs->strbufpos, len = fs->strbufsize;
int j = 0;
const char* instr = fs->strbuf;
while( i < len && j < maxCount-1 )
{
char c = instr[i++];
if( c == '\0' )
break;
str[j++] = c;
if( c == '\n' )
break;
}
str[j++] = '\0';
fs->strbufpos = i;
return j > 1 ? str : 0;
}
if( fs->file )
return fgets( str, maxCount, fs->file );
#if USE_ZLIB
if( fs->gzfile )
return gzgets( fs->gzfile, str, maxCount );
#endif
CV_Error( CV_StsError, "The storage is not opened" );
return 0;
}
static int icvEof( CvFileStorage* fs )
{
if( fs->strbuf )
return fs->strbufpos >= fs->strbufsize;
if( fs->file )
return feof(fs->file);
#if USE_ZLIB
if( fs->gzfile )
return gzeof(fs->gzfile);
#endif
return false;
}
static void icvCloseFile( CvFileStorage* fs )
{
if( fs->file )
fclose( fs->file );
#if USE_ZLIB
else if( fs->gzfile )
gzclose( fs->gzfile );
#endif
fs->file = 0;
fs->gzfile = 0;
fs->strbuf = 0;
fs->strbufpos = 0;
fs->is_opened = false;
}
static void icvRewind( CvFileStorage* fs )
{
if( fs->file )
rewind(fs->file);
#if USE_ZLIB
else if( fs->gzfile )
gzrewind(fs->gzfile);
#endif
fs->strbufpos = 0;
}
#define CV_YML_INDENT 3
#define CV_XML_INDENT 2
#define CV_YML_INDENT_FLOW 1
#define CV_FS_MAX_LEN 4096
#define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24))
#define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE)
#define CV_CHECK_FILE_STORAGE(fs) \
{ \
if( !CV_IS_FILE_STORAGE(fs) ) \
CV_Error( (fs) ? CV_StsBadArg : CV_StsNullPtr, \
"Invalid pointer to file storage" ); \
}
#define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \
{ \
CV_CHECK_FILE_STORAGE(fs); \
if( !fs->write_mode ) \
CV_Error( CV_StsError, "The file storage is opened for reading" ); \
}
CV_IMPL const char*
cvAttrValue( const CvAttrList* attr, const char* attr_name )
{
while( attr && attr->attr )
{
int i;
for( i = 0; attr->attr[i*2] != 0; i++ )
{
if( strcmp( attr_name, attr->attr[i*2] ) == 0 )
return attr->attr[i*2+1];
}
attr = attr->next;
}
return 0;
}
static CvGenericHash*
cvCreateMap( int flags, int header_size, int elem_size,
CvMemStorage* storage, int start_tab_size )
{
if( header_size < (int)sizeof(CvGenericHash) )
CV_Error( CV_StsBadSize, "Too small map header_size" );
if( start_tab_size <= 0 )
start_tab_size = 16;
CvGenericHash* map = (CvGenericHash*)cvCreateSet( flags, header_size, elem_size, storage );
map->tab_size = start_tab_size;
start_tab_size *= sizeof(map->table[0]);
map->table = (void**)cvMemStorageAlloc( storage, start_tab_size );
memset( map->table, 0, start_tab_size );
return map;
}
#define CV_PARSE_ERROR( errmsg ) \
icvParseError( fs, CV_Func, (errmsg), __FILE__, __LINE__ )
static void
icvParseError( CvFileStorage* fs, const char* func_name,
const char* err_msg, const char* source_file, int source_line )
{
char buf[1<<10];
sprintf( buf, "%s(%d): %s", fs->filename, fs->lineno, err_msg );
cvError( CV_StsParseError, func_name, buf, source_file, source_line );
}
static void
icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection )
{
if( CV_NODE_IS_MAP(tag) )
{
if( collection->tag != CV_NODE_NONE )
{
assert( fs->fmt == CV_STORAGE_FORMAT_XML );
CV_PARSE_ERROR( "Sequence element should not have name (use <_></_>)" );
}
collection->data.map = cvCreateMap( 0, sizeof(CvFileNodeHash),
sizeof(CvFileMapNode), fs->memstorage, 16 );
}
else
{
CvSeq* seq;
seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage );
// if <collection> contains some scalar element, add it to the newly created collection
if( CV_NODE_TYPE(collection->tag) != CV_NODE_NONE )
cvSeqPush( seq, collection );
collection->data.seq = seq;
}
collection->tag = tag;
cvSetSeqBlockSize( collection->data.seq, 8 );
}
/*static void
icvFSReleaseCollection( CvSeq* seq )
{
if( seq )
{
int is_map = CV_IS_SET(seq);
CvSeqReader reader;
int i, total = seq->total;
cvStartReadSeq( seq, &reader, 0 );
for( i = 0; i < total; i++ )
{
CvFileNode* node = (CvFileNode*)reader.ptr;
if( (!is_map || CV_IS_SET_ELEM( node )) && CV_NODE_IS_COLLECTION(node->tag) )
{
if( CV_NODE_IS_USER(node->tag) && node->info && node->data.obj.decoded )
cvRelease( (void**)&node->data.obj.decoded );
if( !CV_NODE_SEQ_IS_SIMPLE( node->data.seq ))
icvFSReleaseCollection( node->data.seq );
}
CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
}
}
}*/
static char*
icvFSDoResize( CvFileStorage* fs, char* ptr, int len )
{
char* new_ptr = 0;
int written_len = (int)(ptr - fs->buffer_start);
int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2);
new_size = MAX( written_len + len, new_size );
new_ptr = (char*)cvAlloc( new_size + 256 );
fs->buffer = new_ptr + (fs->buffer - fs->buffer_start);
if( written_len > 0 )
memcpy( new_ptr, fs->buffer_start, written_len );
fs->buffer_start = new_ptr;
fs->buffer_end = fs->buffer_start + new_size;
new_ptr += written_len;
return new_ptr;
}
inline char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len )
{
return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len );
}
static char*
icvFSFlush( CvFileStorage* fs )
{
char* ptr = fs->buffer;
int indent;
if( ptr > fs->buffer_start + fs->space )
{
ptr[0] = '\n';
ptr[1] = '\0';
icvPuts( fs, fs->buffer_start );
fs->buffer = fs->buffer_start;
}
indent = fs->struct_indent;
if( fs->space != indent )
{
if( fs->space < indent )
memset( fs->buffer_start + fs->space, ' ', indent - fs->space );
fs->space = indent;
}
ptr = fs->buffer = fs->buffer_start + fs->space;
return ptr;
}
static void
icvClose( CvFileStorage* fs, cv::String* out )
{
if( out )
out->clear();
if( !fs )
CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" );
if( fs->is_opened )
{
if( fs->write_mode && (fs->file || fs->gzfile || fs->outbuf) )
{
if( fs->write_stack )
{
while( fs->write_stack->total > 0 )
cvEndWriteStruct(fs);
}
icvFSFlush(fs);
if( fs->fmt == CV_STORAGE_FORMAT_XML )
icvPuts( fs, "</opencv_storage>\n" );
}
icvCloseFile(fs);
}
if( fs->outbuf && out )
{
*out = cv::String(fs->outbuf->begin(), fs->outbuf->end());
}
}
/* closes file storage and deallocates buffers */
CV_IMPL void
cvReleaseFileStorage( CvFileStorage** p_fs )
{
if( !p_fs )
CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" );
if( *p_fs )
{
CvFileStorage* fs = *p_fs;
*p_fs = 0;
icvClose(fs, 0);
cvReleaseMemStorage( &fs->strstorage );
cvFree( &fs->buffer_start );
cvReleaseMemStorage( &fs->memstorage );
if( fs->outbuf )
delete fs->outbuf;
memset( fs, 0, sizeof(*fs) );
cvFree( &fs );
}
}
#define CV_HASHVAL_SCALE 33
CV_IMPL CvStringHashNode*
cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing )
{
CvStringHashNode* node = 0;
unsigned hashval = 0;
int i, tab_size;
if( !fs )
return 0;
CvStringHash* map = fs->str_hash;
if( len < 0 )
{
for( i = 0; str[i] != '\0'; i++ )
hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
len = i;
}
else for( i = 0; i < len; i++ )
hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
hashval &= INT_MAX;
tab_size = map->tab_size;
if( (tab_size & (tab_size - 1)) == 0 )
i = (int)(hashval & (tab_size - 1));
else
i = (int)(hashval % tab_size);
for( node = (CvStringHashNode*)(map->table[i]); node != 0; node = node->next )
{
if( node->hashval == hashval &&
node->str.len == len &&
memcmp( node->str.ptr, str, len ) == 0 )
break;
}
if( !node && create_missing )
{
node = (CvStringHashNode*)cvSetNew( (CvSet*)map );
node->hashval = hashval;
node->str = cvMemStorageAllocString( map->storage, str, len );
node->next = (CvStringHashNode*)(map->table[i]);
map->table[i] = node;
}
return node;
}
CV_IMPL CvFileNode*
cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node,
const CvStringHashNode* key,
int create_missing )
{
CvFileNode* value = 0;
int k = 0, attempts = 1;
if( !fs )
return 0;
CV_CHECK_FILE_STORAGE(fs);
if( !key )
CV_Error( CV_StsNullPtr, "Null key element" );
if( _map_node )
{
if( !fs->roots )
return 0;
attempts = fs->roots->total;
}
for( k = 0; k < attempts; k++ )
{
int i, tab_size;
CvFileNode* map_node = _map_node;
CvFileMapNode* another;
CvFileNodeHash* map;
if( !map_node )
map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
if( !CV_NODE_IS_MAP(map_node->tag) )
{
if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
return 0;
}
map = map_node->data.map;
tab_size = map->tab_size;
if( (tab_size & (tab_size - 1)) == 0 )
i = (int)(key->hashval & (tab_size - 1));
else
i = (int)(key->hashval % tab_size);
for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
if( another->key == key )
{
if( !create_missing )
{
value = &another->value;
return value;
}
CV_PARSE_ERROR( "Duplicated key" );
}
if( k == attempts - 1 && create_missing )
{
CvFileMapNode* node = (CvFileMapNode*)cvSetNew( (CvSet*)map );
node->key = key;
node->next = (CvFileMapNode*)(map->table[i]);
map->table[i] = node;
value = (CvFileNode*)node;
}
}
return value;
}
CV_IMPL CvFileNode*
cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str )
{
CvFileNode* value = 0;
int i, len, tab_size;
unsigned hashval = 0;
int k = 0, attempts = 1;
if( !fs )
return 0;
CV_CHECK_FILE_STORAGE(fs);
if( !str )
CV_Error( CV_StsNullPtr, "Null element name" );
for( i = 0; str[i] != '\0'; i++ )
hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i];
hashval &= INT_MAX;
len = i;
if( !_map_node )
{
if( !fs->roots )
return 0;
attempts = fs->roots->total;
}
for( k = 0; k < attempts; k++ )
{
CvFileNodeHash* map;
const CvFileNode* map_node = _map_node;
CvFileMapNode* another;
if( !map_node )
map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k );
if( !CV_NODE_IS_MAP(map_node->tag) )
{
if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) &&
CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE )
CV_Error( CV_StsError, "The node is neither a map nor an empty collection" );
return 0;
}
map = map_node->data.map;
tab_size = map->tab_size;
if( (tab_size & (tab_size - 1)) == 0 )
i = (int)(hashval & (tab_size - 1));
else
i = (int)(hashval % tab_size);
for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next )
{
const CvStringHashNode* key = another->key;
if( key->hashval == hashval &&
key->str.len == len &&
memcmp( key->str.ptr, str, len ) == 0 )
{
value = &another->value;
return value;
}
}
}
return value;
}
CV_IMPL CvFileNode*
cvGetRootFileNode( const CvFileStorage* fs, int stream_index )
{
CV_CHECK_FILE_STORAGE(fs);
if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total )
return 0;
return (CvFileNode*)cvGetSeqElem( fs->roots, stream_index );
}
/* returns the sequence element by its index */
/*CV_IMPL CvFileNode*
cvGetFileNodeFromSeq( CvFileStorage* fs,
CvFileNode* seq_node, int index )
{
CvFileNode* value = 0;
CvSeq* seq;
if( !seq_node )
seq = fs->roots;
else if( !CV_NODE_IS_SEQ(seq_node->tag) )
{
if( CV_NODE_IS_MAP(seq_node->tag) )
CV_Error( CV_StsError, "The node is map. Use cvGetFileNodeFromMap()." );
if( CV_NODE_TYPE(seq_node->tag) == CV_NODE_NONE )
CV_Error( CV_StsError, "The node is an empty object (None)." );
if( index != 0 && index != -1 )
CV_Error( CV_StsOutOfRange, "" );
value = seq_node;
EXIT;
}
else
seq = seq_node->data.seq;
if( !seq )
CV_Error( CV_StsNullPtr, "The file storage is empty" );
value = (CvFileNode*)cvGetSeqElem( seq, index, 0 );
return value;
}*/
static char*
icvDoubleToString( char* buf, double value )
{
Cv64suf val;
unsigned ieee754_hi;
val.f = value;
ieee754_hi = (unsigned)(val.u >> 32);
if( (ieee754_hi & 0x7ff00000) != 0x7ff00000 )
{
int ivalue = cvRound(value);
if( ivalue == value )
sprintf( buf, "%d.", ivalue );
else
{
static const char* fmt = "%.16e";
char* ptr = buf;
sprintf( buf, fmt, value );
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )
;
if( *ptr == ',' )
*ptr = '.';
}
}
else
{
unsigned ieee754_lo = (unsigned)val.u;
if( (ieee754_hi & 0x7fffffff) + (ieee754_lo != 0) > 0x7ff00000 )
strcpy( buf, ".Nan" );
else
strcpy( buf, (int)ieee754_hi < 0 ? "-.Inf" : ".Inf" );
}
return buf;
}
static char*
icvFloatToString( char* buf, float value )
{
Cv32suf val;
unsigned ieee754;
val.f = value;
ieee754 = val.u;
if( (ieee754 & 0x7f800000) != 0x7f800000 )
{
int ivalue = cvRound(value);
if( ivalue == value )
sprintf( buf, "%d.", ivalue );
else
{
static const char* fmt = "%.8e";
char* ptr = buf;
sprintf( buf, fmt, value );
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )
;
if( *ptr == ',' )
*ptr = '.';
}
}
else
{
if( (ieee754 & 0x7fffffff) != 0x7f800000 )
strcpy( buf, ".Nan" );
else
strcpy( buf, (int)ieee754 < 0 ? "-.Inf" : ".Inf" );
}
return buf;
}
static void
icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr )
{
char c = buf[0];
int inf_hi = 0x7ff00000;
if( c == '-' || c == '+' )
{
inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000;
c = *++buf;
}
if( c != '.' )
CV_PARSE_ERROR( "Bad format of floating-point constant" );
union{double d; uint64 i;} v;
v.d = 0.;
if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' )
v.i = (uint64)inf_hi << 32;
else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' )
v.i = (uint64)-1;
else
CV_PARSE_ERROR( "Bad format of floating-point constant" );
*value = v.d;
*endptr = buf + 4;
}
static double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr )
{
double fval = strtod( ptr, endptr );
if( **endptr == '.' )
{
char* dot_pos = *endptr;
*dot_pos = ',';
double fval2 = strtod( ptr, endptr );
*dot_pos = '.';
if( *endptr > dot_pos )
fval = fval2;
else
*endptr = dot_pos;
}
if( *endptr == ptr || cv_isalpha(**endptr) )
icvProcessSpecialDouble( fs, ptr, &fval, endptr );
return fval;
}
/****************************************************************************************\
* YAML Parser *
\****************************************************************************************/
static char*
icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent )
{
for(;;)
{
while( *ptr == ' ' )
ptr++;
if( *ptr == '#' )
{
if( ptr - fs->buffer_start > max_comment_indent )
return ptr;
*ptr = '\0';
}
else if( cv_isprint(*ptr) )
{
if( ptr - fs->buffer_start < min_indent )
CV_PARSE_ERROR( "Incorrect indentation" );
break;
}
else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' )
{
int max_size = (int)(fs->buffer_end - fs->buffer_start);
ptr = icvGets( fs, fs->buffer_start, max_size );
if( !ptr )
{
// emulate end of stream
ptr = fs->buffer_start;
ptr[0] = ptr[1] = ptr[2] = '.';
ptr[3] = '\0';
fs->dummy_eof = 1;
break;
}
else
{
int l = (int)strlen(ptr);
if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) )
CV_PARSE_ERROR( "Too long string or a last string w/o newline" );
}
fs->lineno++;
}
else
CV_PARSE_ERROR( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" );
}
return ptr;
}
static char*
icvYMLParseKey( CvFileStorage* fs, char* ptr,
CvFileNode* map_node, CvFileNode** value_placeholder )