|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html |
| 4 | + |
| 5 | + |
| 6 | +#ifndef SRC_PERSISTENCE_HPP |
| 7 | +#define SRC_PERSISTENCE_HPP |
| 8 | + |
| 9 | +#include "opencv2/core/types_c.h" |
| 10 | +#include <deque> |
| 11 | +#include <deque> |
| 12 | +#include <sstream> |
| 13 | +#include <string> |
| 14 | +#include <iterator> |
| 15 | + |
| 16 | +#define USE_ZLIB 1 |
| 17 | +#if USE_ZLIB |
| 18 | +# ifndef _LFS64_LARGEFILE |
| 19 | +# define _LFS64_LARGEFILE 0 |
| 20 | +# endif |
| 21 | +# ifndef _FILE_OFFSET_BITS |
| 22 | +# define _FILE_OFFSET_BITS 0 |
| 23 | +# endif |
| 24 | +# include <zlib.h> |
| 25 | +#else |
| 26 | +typedef void* gzFile; |
| 27 | +#endif |
| 28 | + |
| 29 | +//===================================================================================== |
| 30 | + |
| 31 | +static const size_t PARSER_BASE64_BUFFER_SIZE = 1024U * 1024U / 8U; |
| 32 | + |
| 33 | +namespace base64 { |
| 34 | + |
| 35 | +namespace fs { |
| 36 | +enum State |
| 37 | +{ |
| 38 | + Uncertain, |
| 39 | + NotUse, |
| 40 | + InUse, |
| 41 | +}; |
| 42 | +} // fs:: |
| 43 | + |
| 44 | +static const size_t HEADER_SIZE = 24U; |
| 45 | +static const size_t ENCODED_HEADER_SIZE = 32U; |
| 46 | + |
| 47 | +size_t base64_encode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt); |
| 48 | +size_t base64_encode( char const * src, char * dst, size_t off = 0U, size_t cnt = 0U); |
| 49 | +size_t base64_decode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt); |
| 50 | +size_t base64_decode( char const * src, char * dst, size_t off = 0U, size_t cnt = 0U); |
| 51 | +bool base64_valid (uint8_t const * src, size_t off, size_t cnt); |
| 52 | +bool base64_valid ( char const * src, size_t off = 0U, size_t cnt = 0U); |
| 53 | +size_t base64_encode_buffer_size(size_t cnt, bool is_end_with_zero = true); |
| 54 | +size_t base64_decode_buffer_size(size_t cnt, bool is_end_with_zero = true); |
| 55 | +size_t base64_decode_buffer_size(size_t cnt, char const * src, bool is_end_with_zero = true); |
| 56 | +size_t base64_decode_buffer_size(size_t cnt, uchar const * src, bool is_end_with_zero = true); |
| 57 | +std::string make_base64_header(const char * dt); |
| 58 | +bool read_base64_header(std::vector<char> const & header, std::string & dt); |
| 59 | +void make_seq(void * binary_data, int elem_cnt, const char * dt, CvSeq & seq); |
| 60 | +void cvWriteRawDataBase64(::CvFileStorage* fs, const void* _data, int len, const char* dt); |
| 61 | + |
| 62 | +class Base64ContextEmitter; |
| 63 | + |
| 64 | +class Base64Writer |
| 65 | +{ |
| 66 | +public: |
| 67 | + Base64Writer(::CvFileStorage * fs); |
| 68 | + ~Base64Writer(); |
| 69 | + void write(const void* _data, size_t len, const char* dt); |
| 70 | + template<typename _to_binary_convertor_t> void write(_to_binary_convertor_t & convertor, const char* dt); |
| 71 | + |
| 72 | +private: |
| 73 | + void check_dt(const char* dt); |
| 74 | + |
| 75 | +private: |
| 76 | + // disable copy and assignment |
| 77 | + Base64Writer(const Base64Writer &); |
| 78 | + Base64Writer & operator=(const Base64Writer &); |
| 79 | + |
| 80 | +private: |
| 81 | + |
| 82 | + Base64ContextEmitter * emitter; |
| 83 | + std::string data_type_string; |
| 84 | +}; |
| 85 | + |
| 86 | +class Base64ContextParser |
| 87 | +{ |
| 88 | +public: |
| 89 | + explicit Base64ContextParser(uchar * buffer, size_t size); |
| 90 | + ~Base64ContextParser(); |
| 91 | + Base64ContextParser & read(const uchar * beg, const uchar * end); |
| 92 | + bool flush(); |
| 93 | +private: |
| 94 | + static const size_t BUFFER_LEN = 120U; |
| 95 | + uchar * dst_cur; |
| 96 | + uchar * dst_end; |
| 97 | + std::vector<uchar> base64_buffer; |
| 98 | + uchar * src_beg; |
| 99 | + uchar * src_cur; |
| 100 | + uchar * src_end; |
| 101 | + std::vector<uchar> binary_buffer; |
| 102 | +}; |
| 103 | + |
| 104 | +} // base64:: |
| 105 | + |
| 106 | +//===================================================================================== |
| 107 | + |
| 108 | +#define CV_FS_MAX_LEN 4096 |
| 109 | +#define CV_FS_MAX_FMT_PAIRS 128 |
| 110 | + |
| 111 | +#define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24)) |
| 112 | + |
| 113 | +#define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE) |
| 114 | + |
| 115 | +#define CV_CHECK_FILE_STORAGE(fs) \ |
| 116 | +{ \ |
| 117 | + if( !CV_IS_FILE_STORAGE(fs) ) \ |
| 118 | + CV_Error( (fs) ? CV_StsBadArg : CV_StsNullPtr, \ |
| 119 | + "Invalid pointer to file storage" ); \ |
| 120 | +} |
| 121 | + |
| 122 | +#define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \ |
| 123 | +{ \ |
| 124 | + CV_CHECK_FILE_STORAGE(fs); \ |
| 125 | + if( !fs->write_mode ) \ |
| 126 | + CV_Error( CV_StsError, "The file storage is opened for reading" ); \ |
| 127 | +} |
| 128 | + |
| 129 | +#define CV_PARSE_ERROR( errmsg ) \ |
| 130 | + icvParseError( fs, CV_Func, (errmsg), __FILE__, __LINE__ ) |
| 131 | + |
| 132 | +typedef struct CvGenericHash |
| 133 | +{ |
| 134 | + CV_SET_FIELDS() |
| 135 | + int tab_size; |
| 136 | + void** table; |
| 137 | +} |
| 138 | +CvGenericHash; |
| 139 | +typedef CvGenericHash CvStringHash; |
| 140 | + |
| 141 | +//typedef void (*CvParse)( struct CvFileStorage* fs ); |
| 142 | +typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key, |
| 143 | + int struct_flags, const char* type_name ); |
| 144 | +typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs ); |
| 145 | +typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value ); |
| 146 | +typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value ); |
| 147 | +typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key, |
| 148 | + const char* value, int quote ); |
| 149 | +typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment ); |
| 150 | +typedef void (*CvStartNextStream)( struct CvFileStorage* fs ); |
| 151 | + |
| 152 | +typedef struct CvFileStorage |
| 153 | +{ |
| 154 | + int flags; |
| 155 | + int fmt; |
| 156 | + int write_mode; |
| 157 | + int is_first; |
| 158 | + CvMemStorage* memstorage; |
| 159 | + CvMemStorage* dststorage; |
| 160 | + CvMemStorage* strstorage; |
| 161 | + CvStringHash* str_hash; |
| 162 | + CvSeq* roots; |
| 163 | + CvSeq* write_stack; |
| 164 | + int struct_indent; |
| 165 | + int struct_flags; |
| 166 | + CvString struct_tag; |
| 167 | + int space; |
| 168 | + char* filename; |
| 169 | + FILE* file; |
| 170 | + gzFile gzfile; |
| 171 | + char* buffer; |
| 172 | + char* buffer_start; |
| 173 | + char* buffer_end; |
| 174 | + int wrap_margin; |
| 175 | + int lineno; |
| 176 | + int dummy_eof; |
| 177 | + const char* errmsg; |
| 178 | + char errmsgbuf[128]; |
| 179 | + |
| 180 | + CvStartWriteStruct start_write_struct; |
| 181 | + CvEndWriteStruct end_write_struct; |
| 182 | + CvWriteInt write_int; |
| 183 | + CvWriteReal write_real; |
| 184 | + CvWriteString write_string; |
| 185 | + CvWriteComment write_comment; |
| 186 | + CvStartNextStream start_next_stream; |
| 187 | + |
| 188 | + const char* strbuf; |
| 189 | + size_t strbufsize, strbufpos; |
| 190 | + std::deque<char>* outbuf; |
| 191 | + |
| 192 | + base64::Base64Writer * base64_writer; |
| 193 | + bool is_default_using_base64; |
| 194 | + base64::fs::State state_of_writing_base64; /**< used in WriteRawData only */ |
| 195 | + |
| 196 | + bool is_write_struct_delayed; |
| 197 | + char* delayed_struct_key; |
| 198 | + int delayed_struct_flags; |
| 199 | + char* delayed_type_name; |
| 200 | + |
| 201 | + bool is_opened; |
| 202 | +} |
| 203 | +CvFileStorage; |
| 204 | + |
| 205 | +typedef struct CvFileMapNode |
| 206 | +{ |
| 207 | + CvFileNode value; |
| 208 | + const CvStringHashNode* key; |
| 209 | + struct CvFileMapNode* next; |
| 210 | +} |
| 211 | +CvFileMapNode; |
| 212 | + |
| 213 | +/****************************************************************************************\ |
| 214 | +* Common macros and type definitions * |
| 215 | +\****************************************************************************************/ |
| 216 | + |
| 217 | +#define cv_isprint(c) ((uchar)(c) >= (uchar)' ') |
| 218 | +#define cv_isprint_or_tab(c) ((uchar)(c) >= (uchar)' ' || (c) == '\t') |
| 219 | + |
| 220 | +inline bool cv_isalnum(char c) |
| 221 | +{ |
| 222 | + return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 223 | +} |
| 224 | + |
| 225 | +inline bool cv_isalpha(char c) |
| 226 | +{ |
| 227 | + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 228 | +} |
| 229 | + |
| 230 | +inline bool cv_isdigit(char c) |
| 231 | +{ |
| 232 | + return '0' <= c && c <= '9'; |
| 233 | +} |
| 234 | + |
| 235 | +inline bool cv_isspace(char c) |
| 236 | +{ |
| 237 | + return (9 <= c && c <= 13) || c == ' '; |
| 238 | +} |
| 239 | + |
| 240 | +inline char* cv_skip_BOM(char* ptr) |
| 241 | +{ |
| 242 | + if((uchar)ptr[0] == 0xef && (uchar)ptr[1] == 0xbb && (uchar)ptr[2] == 0xbf) //UTF-8 BOM |
| 243 | + { |
| 244 | + return ptr + 3; |
| 245 | + } |
| 246 | + return ptr; |
| 247 | +} |
| 248 | + |
| 249 | +char* icv_itoa( int _val, char* buffer, int /*radix*/ ); |
| 250 | +double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr ); |
| 251 | +char* icvFloatToString( char* buf, float value ); |
| 252 | +char* icvDoubleToString( char* buf, double value ); |
| 253 | + |
| 254 | +char icvTypeSymbol(int depth); |
| 255 | +void icvClose( CvFileStorage* fs, cv::String* out ); |
| 256 | +void icvCloseFile( CvFileStorage* fs ); |
| 257 | +void icvPuts( CvFileStorage* fs, const char* str ); |
| 258 | +char* icvGets( CvFileStorage* fs, char* str, int maxCount ); |
| 259 | +int icvEof( CvFileStorage* fs ); |
| 260 | +void icvRewind( CvFileStorage* fs ); |
| 261 | +char* icvFSFlush( CvFileStorage* fs ); |
| 262 | +void icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection ); |
| 263 | +char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len ); |
| 264 | +int icvCalcStructSize( const char* dt, int initial_size ); |
| 265 | +int icvCalcElemSize( const char* dt, int initial_size ); |
| 266 | +void icvParseError( CvFileStorage* fs, const char* func_name, const char* err_msg, const char* source_file, int source_line ); |
| 267 | +char* icvEncodeFormat( int elem_type, char* dt ); |
| 268 | +int icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len ); |
| 269 | +int icvDecodeSimpleFormat( const char* dt ); |
| 270 | +void icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node ); |
| 271 | +void icvWriteCollection( CvFileStorage* fs, const CvFileNode* node ); |
| 272 | +void switch_to_Base64_state( CvFileStorage* fs, base64::fs::State state ); |
| 273 | +void make_write_struct_delayed( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name ); |
| 274 | +void check_if_write_struct_is_delayed( CvFileStorage* fs, bool change_type_to_base64 = false ); |
| 275 | +CvGenericHash* cvCreateMap( int flags, int header_size, int elem_size, CvMemStorage* storage, int start_tab_size ); |
| 276 | + |
| 277 | +// |
| 278 | +// XML |
| 279 | +// |
| 280 | +void icvXMLParse( CvFileStorage* fs ); |
| 281 | +void icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); |
| 282 | +void icvXMLEndWriteStruct( CvFileStorage* fs ); |
| 283 | +void icvXMLStartNextStream( CvFileStorage* fs ); |
| 284 | +void icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len ); |
| 285 | +void icvXMLWriteInt( CvFileStorage* fs, const char* key, int value ); |
| 286 | +void icvXMLWriteReal( CvFileStorage* fs, const char* key, double value ); |
| 287 | +void icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote ); |
| 288 | +void icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); |
| 289 | + |
| 290 | +typedef struct CvXMLStackRecord |
| 291 | +{ |
| 292 | + CvMemStoragePos pos; |
| 293 | + CvString struct_tag; |
| 294 | + int struct_indent; |
| 295 | + int struct_flags; |
| 296 | +} |
| 297 | +CvXMLStackRecord; |
| 298 | + |
| 299 | +// |
| 300 | +// YML |
| 301 | +// |
| 302 | +void icvYMLParse( CvFileStorage* fs ); |
| 303 | +void icvYMLWrite( CvFileStorage* fs, const char* key, const char* data ); |
| 304 | +void icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); |
| 305 | +void icvYMLEndWriteStruct( CvFileStorage* fs ); |
| 306 | +void icvYMLStartNextStream( CvFileStorage* fs ); |
| 307 | +void icvYMLWriteInt( CvFileStorage* fs, const char* key, int value ); |
| 308 | +void icvYMLWriteReal( CvFileStorage* fs, const char* key, double value ); |
| 309 | +void icvYMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote CV_DEFAULT(0)); |
| 310 | +void icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); |
| 311 | + |
| 312 | +// |
| 313 | +// JSON |
| 314 | +// |
| 315 | +void icvJSONParse( CvFileStorage* fs ); |
| 316 | +void icvJSONWrite( CvFileStorage* fs, const char* key, const char* data ); |
| 317 | +void icvJSONStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); |
| 318 | +void icvJSONEndWriteStruct( CvFileStorage* fs ); |
| 319 | +void icvJSONStartNextStream( CvFileStorage* fs ); |
| 320 | +void icvJSONWriteInt( CvFileStorage* fs, const char* key, int value ); |
| 321 | +void icvJSONWriteReal( CvFileStorage* fs, const char* key, double value ); |
| 322 | +void icvJSONWriteString( CvFileStorage* fs, const char* key, const char* str, int quote CV_DEFAULT(0)); |
| 323 | +void icvJSONWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); |
| 324 | + |
| 325 | +#endif // SRC_PERSISTENCE_HPP |
0 commit comments