Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
cv::FileStorage Class Reference

XML/YAML File Storage Class. More...

#include <core.hpp>

Public Types

enum  {
  READ =0, WRITE =1, APPEND =2, MEMORY =4,
  FORMAT_MASK =(7<<3), FORMAT_AUTO =0, FORMAT_XML =(1<<3), FORMAT_YAML =(2<<3)
}
 file storage mode More...
 
enum  { UNDEFINED =0, VALUE_EXPECTED =1, NAME_EXPECTED =2, INSIDE_MAP =4 }
 

Public Member Functions

CV_WRAP FileStorage ()
 the default constructor More...
 
CV_WRAP FileStorage (const string &source, int flags, const string &encoding=string())
 the full constructor that opens file storage for reading or writing More...
 
 FileStorage (CvFileStorage *fs)
 the constructor that takes pointer to the C FileStorage structure More...
 
virtual ~FileStorage ()
 the destructor. calls release() More...
 
virtual CV_WRAP bool open (const string &filename, int flags, const string &encoding=string())
 opens file storage for reading or writing. The previous storage is closed with release() More...
 
virtual CV_WRAP bool isOpened () const
 returns true if the object is associated with currently opened file. More...
 
virtual CV_WRAP void release ()
 closes the file and releases all the memory buffers More...
 
CV_WRAP string releaseAndGetString ()
 closes the file, releases all the memory buffers and returns the text string More...
 
CV_WRAP FileNode getFirstTopLevelNode () const
 returns the first element of the top-level mapping More...
 
CV_WRAP FileNode root (int streamidx=0) const
 returns the top-level mapping. YAML supports multiple streams More...
 
FileNode operator[] (const string &nodename) const
 returns the specified element of the top-level mapping More...
 
CV_WRAP FileNode operator[] (const char *nodename) const
 returns the specified element of the top-level mapping More...
 
CvFileStorageoperator* ()
 returns pointer to the underlying C FileStorage structure More...
 
const CvFileStorageoperator* () const
 returns pointer to the underlying C FileStorage structure More...
 
void writeRaw (const string &fmt, const uchar *vec, size_t len)
 writes one or more numbers of the specified format to the currently written structure More...
 
void writeObj (const string &name, const void *obj)
 writes the registered C structure (CvMat, CvMatND, CvSeq). See cvWrite() More...
 

Static Public Member Functions

static string getDefaultObjectName (const string &filename)
 returns the normalized object name for the specified file name More...
 

Public Attributes

Ptr< CvFileStoragefs
 the underlying C FileStorage structure More...
 
string elname
 the currently written element More...
 
vector< char > structs
 the stack of written structures More...
 
int state
 the writer state More...
 

Detailed Description

XML/YAML File Storage Class.

The class describes an object associated with XML or YAML file. It can be used to store data to such a file or read and decode the data.

The storage is organized as a tree of nested sequences (or lists) and mappings. Sequence is a heterogenious array, which elements are accessed by indices or sequentially using an iterator. Mapping is analogue of std::map or C structure, which elements are accessed by names. The most top level structure is a mapping. Leaves of the file storage tree are integers, floating-point numbers and text strings.

For example, the following code:

// open file storage for writing. Type of the file is determined from the extension
fs << "test_int" << 5 << "test_real" << 3.1 << "test_string" << "ABCDEFGH";
fs << "test_mat" << Mat::eye(3,3,CV_32F);
fs << "test_list" << "[" << 0.0000000000001 << 2 << CV_PI << -3435345 << "2-502 2-029 3egegeg" <<
"{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
fs << "test_map" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";
const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));
fs << "]" << "}";

will produce the following file:

%YAML:1.0
test_int: 5
test_real: 3.1000000000000001e+00
test_string: ABCDEFGH
test_mat: !!opencv-matrix
    rows: 3
    cols: 3
    dt: f
    data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1. ]
test_list:
    - 1.0000000000000000e-13
    - 2
    - 3.1415926535897931e+00
    - -3435345
    - "2-502 2-029 3egegeg"
    - { month:12, day:31, year:1969 }
test_map:
    x: 1
    y: 2
    width: 100
    height: 200
    lbp: [ 0, 1, 1, 0, 1, 1, 0, 1 ]

and to read the file above, the following code can be used:

// open file storage for reading.
// Type of the file is determined from the content, not the extension
int test_int = (int)fs["test_int"];
double test_real = (double)fs["test_real"];
string test_string = (string)fs["test_string"];
Mat M;
fs["test_mat"] >> M;
FileNode tl = fs["test_list"];
CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 6);
double tl0 = (double)tl[0];
int tl1 = (int)tl[1];
double tl2 = (double)tl[2];
int tl3 = (int)tl[3];
string tl4 = (string)tl[4];
CV_Assert(tl[5].type() == FileNode::MAP && tl[5].size() == 3);
int month = (int)tl[5]["month"];
int day = (int)tl[5]["day"];
int year = (int)tl[5]["year"];
FileNode tm = fs["test_map"];
int x = (int)tm["x"];
int y = (int)tm["y"];
int width = (int)tm["width"];
int height = (int)tm["height"];
int lbp_val = 0;
FileNodeIterator it = tm["lbp"].begin();
for(int k = 0; k < 8; k++, ++it)
lbp_val |= ((int)*it) << k;

Member Enumeration Documentation

anonymous enum

file storage mode

Enumerator
READ 
WRITE 

read mode

APPEND 

write mode

MEMORY 

append mode

FORMAT_MASK 
FORMAT_AUTO 
FORMAT_XML 
FORMAT_YAML 
anonymous enum
Enumerator
UNDEFINED 
VALUE_EXPECTED 
NAME_EXPECTED 
INSIDE_MAP 

Constructor & Destructor Documentation

CV_WRAP cv::FileStorage::FileStorage ( )

the default constructor

CV_WRAP cv::FileStorage::FileStorage ( const string source,
int  flags,
const string encoding = string() 
)

the full constructor that opens file storage for reading or writing

cv::FileStorage::FileStorage ( CvFileStorage fs)

the constructor that takes pointer to the C FileStorage structure

virtual cv::FileStorage::~FileStorage ( )
virtual

the destructor. calls release()

Member Function Documentation

virtual CV_WRAP bool cv::FileStorage::open ( const string filename,
int  flags,
const string encoding = string() 
)
virtual

opens file storage for reading or writing. The previous storage is closed with release()

virtual CV_WRAP bool cv::FileStorage::isOpened ( ) const
virtual

returns true if the object is associated with currently opened file.

virtual CV_WRAP void cv::FileStorage::release ( )
virtual

closes the file and releases all the memory buffers

CV_WRAP string cv::FileStorage::releaseAndGetString ( )

closes the file, releases all the memory buffers and returns the text string

FileNode cv::FileStorage::getFirstTopLevelNode ( ) const

returns the first element of the top-level mapping

CV_WRAP FileNode cv::FileStorage::root ( int  streamidx = 0) const

returns the top-level mapping. YAML supports multiple streams

FileNode cv::FileStorage::operator[] ( const string nodename) const

returns the specified element of the top-level mapping

CV_WRAP FileNode cv::FileStorage::operator[] ( const char *  nodename) const

returns the specified element of the top-level mapping

CvFileStorage* cv::FileStorage::operator* ( )

returns pointer to the underlying C FileStorage structure

const CvFileStorage* cv::FileStorage::operator* ( ) const

returns pointer to the underlying C FileStorage structure

void cv::FileStorage::writeRaw ( const string fmt,
const uchar vec,
size_t  len 
)

writes one or more numbers of the specified format to the currently written structure

void cv::FileStorage::writeObj ( const string name,
const void obj 
)

writes the registered C structure (CvMat, CvMatND, CvSeq). See cvWrite()

static string cv::FileStorage::getDefaultObjectName ( const string filename)
static

returns the normalized object name for the specified file name

Member Data Documentation

Ptr<CvFileStorage> cv::FileStorage::fs

the underlying C FileStorage structure

string cv::FileStorage::elname

the currently written element

vector<char> cv::FileStorage::structs

the stack of written structures

int cv::FileStorage::state

the writer state


The documentation for this class was generated from the following files: