XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or reading data to/from a file. More...
#include <persistence.hpp>
Public Types | |
enum | Mode { READ = 0 , WRITE = 1 , APPEND = 2 , MEMORY = 4 , FORMAT_MASK = (7<<3) , FORMAT_AUTO = 0 , FORMAT_XML = (1<<3) , FORMAT_YAML = (2<<3) , FORMAT_JSON = (3<<3) , BASE64 = 64 , WRITE_BASE64 = BASE64 | WRITE } |
file storage mode More... | |
enum | State { UNDEFINED = 0 , VALUE_EXPECTED = 1 , NAME_EXPECTED = 2 , INSIDE_MAP = 4 } |
Public Member Functions | |
CV_WRAP | FileStorage () |
The constructors. More... | |
CV_WRAP | FileStorage (const String &filename, int flags, const String &encoding=String()) |
Opens a file. More... | |
virtual | ~FileStorage () |
the destructor. calls release() More... | |
virtual CV_WRAP bool | open (const String &filename, int flags, const String &encoding=String()) |
Opens a file. More... | |
virtual CV_WRAP bool | isOpened () const |
Checks whether the file is opened. More... | |
virtual CV_WRAP void | release () |
Closes the file and releases all the memory buffers. More... | |
virtual CV_WRAP String | releaseAndGetString () |
Closes the file and releases all the memory buffers. 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. More... | |
FileNode | operator[] (const String &nodename) const |
Returns the specified element of the top-level mapping. More... | |
CV_WRAP_AS (getNode) FileNode operator[](const char *nodename) const | |
CV_WRAP void | write (const String &name, int val) |
Simplified writing API to use with bindings. More... | |
CV_WRAP void | write (const String &name, double val) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
CV_WRAP void | write (const String &name, const String &val) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
CV_WRAP void | write (const String &name, const Mat &val) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
CV_WRAP void | write (const String &name, const std::vector< String > &val) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
void | writeRaw (const String &fmt, const void *vec, size_t len) |
Writes multiple numbers. More... | |
CV_WRAP void | writeComment (const String &comment, bool append=false) |
Writes a comment. More... | |
CV_WRAP void | startWriteStruct (const String &name, int flags, const String &typeName=String()) |
Starts to write a nested structure (sequence or a mapping). More... | |
CV_WRAP void | endWriteStruct () |
Finishes writing nested structure (should pair startWriteStruct()) More... | |
CV_WRAP int | getFormat () const |
Returns the current format. More... | |
Static Public Member Functions | |
static String | getDefaultObjectName (const String &filename) |
Returns the normalized object name for the specified name of a file. More... | |
Public Attributes | |
int | state |
std::string | elname |
Ptr< Impl > | p |
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or reading data to/from a file.
file storage mode
Enumerator | |
---|---|
READ | value, open the file for reading |
WRITE | value, open the file for writing |
APPEND | value, open the file for appending |
MEMORY | flag, read data from source or write data to the internal buffer (which is returned by FileStorage::release) |
FORMAT_MASK | mask for format flags |
FORMAT_AUTO | flag, auto format |
FORMAT_XML | flag, XML format |
FORMAT_YAML | flag, YAML format |
FORMAT_JSON | flag, JSON format |
BASE64 | flag, write rawdata in Base64 by default. (consider using WRITE_BASE64) |
WRITE_BASE64 | flag, enable both WRITE and BASE64 |
CV_WRAP cv::FileStorage::FileStorage | ( | ) |
The constructors.
The full constructor opens the file. Alternatively you can use the default constructor and then call FileStorage::open.
CV_WRAP cv::FileStorage::FileStorage | ( | const String & | filename, |
int | flags, | ||
const String & | encoding = String() |
||
) |
Opens a file.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release before opening the file.
filename | Name of the file to open or the text string to read the data from. Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters. You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to FileStorage::BASE64 flag. |
flags | Mode of operation. One of FileStorage::Mode |
encoding | Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it. |
|
virtual |
the destructor. calls release()
cv::FileStorage::CV_WRAP_AS | ( | getNode | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
CV_WRAP void cv::FileStorage::endWriteStruct | ( | ) |
Finishes writing nested structure (should pair startWriteStruct())
Returns the normalized object name for the specified name of a file.
filename | Name of a file |
Returns the first element of the top-level mapping.
CV_WRAP int cv::FileStorage::getFormat | ( | ) | const |
Returns the current format.
|
virtual |
Checks whether the file is opened.
|
virtual |
Opens a file.
See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release before opening the file.
filename | Name of the file to open or the text string to read the data from. Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters. You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to FileStorage::BASE64 flag. |
flags | Mode of operation. One of FileStorage::Mode |
encoding | Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it. |
Returns the specified element of the top-level mapping.
nodename | Name of the file node. |
|
virtual |
Closes the file and releases all the memory buffers.
Call this method after all I/O operations with the storage are finished.
Closes the file and releases all the memory buffers.
Call this method after all I/O operations with the storage are finished. If the storage was opened for writing data and FileStorage::WRITE was specified
Returns the top-level mapping.
streamidx | Zero-based index of the stream. In most cases there is only one stream in the file. However, YAML supports multiple streams and so there can be several. |
CV_WRAP void cv::FileStorage::startWriteStruct | ( | const String & | name, |
int | flags, | ||
const String & | typeName = String() |
||
) |
Starts to write a nested structure (sequence or a mapping).
name | name of the structure. When writing to sequences (a.k.a. "arrays"), pass an empty string. |
flags | type of the structure (FileNode::MAP or FileNode::SEQ (both with optional FileNode::FLOW)). |
typeName | optional name of the type you store. The effect of setting this depends on the storage format. I.e. if the format has a specification for storing type information, this parameter is used. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
CV_WRAP void cv::FileStorage::write | ( | const String & | name, |
const std::vector< String > & | val | ||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Simplified writing API to use with bindings.
name | Name of the written object. When writing to sequences (a.k.a. "arrays"), pass an empty string. |
val | Value of the written object. |
Writes a comment.
The function writes a comment into file storage. The comments are skipped when the storage is read.
comment | The written comment, single-line or multi-line |
append | If true, the function tries to put the comment at the end of current line. Else if the comment is multi-line, or if it does not fit at the end of the current line, the comment starts a new line. |
void cv::FileStorage::writeRaw | ( | const String & | fmt, |
const void * | vec, | ||
size_t | len | ||
) |
Writes multiple numbers.
Writes one or more numbers of the specified format to the currently written structure. Usually it is more convenient to use operator <<
instead of this method.
fmt | Specification of each array element, see format specification |
vec | Pointer to the written array. |
len | Number of the uchar elements to write. |
std::string cv::FileStorage::elname |
Ptr<Impl> cv::FileStorage::p |
int cv::FileStorage::state |