OpenCV  3.2.0-dev
Open Source Computer Vision
cv::FileStorage Class Reference

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"

Collaboration diagram for cv::FileStorage:

Public Types

enum  {
  UNDEFINED = 0,
  VALUE_EXPECTED = 1,
  NAME_EXPECTED = 2,
  INSIDE_MAP = 4
}
 
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...
 

Public Member Functions

 FileStorage ()
 The constructors. More...
 
 FileStorage (const String &source, int flags, const String &encoding=String())
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
 FileStorage (CvFileStorage *fs, bool owning=true)
 
virtual ~FileStorage ()
 the destructor. calls release() More...
 
FileNode getFirstTopLevelNode () const
 Returns the first element of the top-level mapping. More...
 
virtual bool isOpened () const
 Checks whether the file is opened. More...
 
virtual bool open (const String &filename, int flags, const String &encoding=String())
 Opens a file. More...
 
CvFileStorageoperator* ()
 Returns the obsolete C FileStorage structure. More...
 
const CvFileStorageoperator* () const
 
FileNode operator[] (const String &nodename) const
 Returns the specified element of the top-level mapping. More...
 
FileNode operator[] (const char *nodename) const
 
virtual void release ()
 Closes the file and releases all the memory buffers. More...
 
virtual String releaseAndGetString ()
 Closes the file and releases all the memory buffers. More...
 
FileNode root (int streamidx=0) const
 Returns the top-level mapping. More...
 
void write (const String &name, double val)
 Simplified writing API to use with bindings. More...
 
void write (const String &name, const String &val)
 
void write (const String &name, InputArray val)
 
void writeComment (const String &comment, bool append=false)
 Writes a comment. More...
 
void writeObj (const String &name, const void *obj)
 Writes the registered C structure (CvMat, CvMatND, CvSeq). More...
 
void writeRaw (const String &fmt, const uchar *vec, size_t len)
 Writes multiple numbers. 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

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

Related Functions

(Note that these are not member functions.)

FileStorageoperator<< (FileStorage &fs, const String &str)
 Writes string to a file storage. More...
 
void write (FileStorage &fs, const String &name, int value)
 
void write (FileStorage &fs, const String &name, float value)
 
void write (FileStorage &fs, const String &name, double value)
 
void write (FileStorage &fs, const String &name, const String &value)
 
void write (FileStorage &fs, const String &name, const Mat &value)
 
void write (FileStorage &fs, const String &name, const SparseMat &value)
 
void write (FileStorage &fs, const String &name, const std::vector< KeyPoint > &value)
 
void write (FileStorage &fs, const String &name, const std::vector< DMatch > &value)
 
void writeScalar (FileStorage &fs, int value)
 
void writeScalar (FileStorage &fs, float value)
 
void writeScalar (FileStorage &fs, double value)
 
void writeScalar (FileStorage &fs, const String &value)
 
template<typename _Tp >
static void write (FileStorage &fs, const _Tp &value)
 
template<>
void write (FileStorage &fs, const int &value)
 
template<>
void write (FileStorage &fs, const float &value)
 
template<>
void write (FileStorage &fs, const double &value)
 
template<>
void write (FileStorage &fs, const String &value)
 
template<typename _Tp >
static void write (FileStorage &fs, const Point_< _Tp > &pt)
 
template<typename _Tp >
static void write (FileStorage &fs, const Point3_< _Tp > &pt)
 
template<typename _Tp >
static void write (FileStorage &fs, const Size_< _Tp > &sz)
 
template<typename _Tp >
static void write (FileStorage &fs, const Complex< _Tp > &c)
 
template<typename _Tp >
static void write (FileStorage &fs, const Rect_< _Tp > &r)
 
template<typename _Tp , int cn>
static void write (FileStorage &fs, const Vec< _Tp, cn > &v)
 
template<typename _Tp >
static void write (FileStorage &fs, const Scalar_< _Tp > &s)
 
static void write (FileStorage &fs, const Range &r)
 
template<typename _Tp >
static void write (FileStorage &fs, const std::vector< _Tp > &vec)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Point_< _Tp > &pt)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Point3_< _Tp > &pt)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Size_< _Tp > &sz)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Complex< _Tp > &c)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Rect_< _Tp > &r)
 
template<typename _Tp , int cn>
static void write (FileStorage &fs, const String &name, const Vec< _Tp, cn > &v)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const Scalar_< _Tp > &s)
 
static void write (FileStorage &fs, const String &name, const Range &r)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const std::vector< _Tp > &vec)
 
template<typename _Tp >
static void write (FileStorage &fs, const String &name, const std::vector< std::vector< _Tp > > &vec)
 
template<typename _Tp >
static FileStorageoperator<< (FileStorage &fs, const _Tp &value)
 Writes data to a file storage. More...
 
static FileStorageoperator<< (FileStorage &fs, const char *str)
 Writes data to a file storage. More...
 
static FileStorageoperator<< (FileStorage &fs, char *value)
 Writes data to a file storage. More...
 

Detailed Description

XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or reading data to/from a file.

Examples:
filestorage.cpp.

Member Enumeration Documentation

anonymous enum
Enumerator
UNDEFINED 
VALUE_EXPECTED 
NAME_EXPECTED 
INSIDE_MAP 

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

Constructor & Destructor Documentation

cv::FileStorage::FileStorage ( )

The constructors.

The full constructor opens the file. Alternatively you can use the default constructor and then call FileStorage::open.

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
sourceName 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.).
flagsMode of operation. See FileStorage::Mode
encodingEncoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.
cv::FileStorage::FileStorage ( CvFileStorage fs,
bool  owning = true 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

the destructor. calls release()

Member Function Documentation

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

Returns the normalized object name for the specified name of a file.

Parameters
filenameName of a file
Returns
The normalized object name.
FileNode cv::FileStorage::getFirstTopLevelNode ( ) const

Returns the first element of the top-level mapping.

Returns
The first element of the top-level mapping.

Referenced by cv::Algorithm::load(), cv::Algorithm::loadFromString(), and cv::FileNodeIterator::operator<().

Here is the caller graph for this function:

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

Checks whether the file is opened.

Returns
true if the object is associated with the current file and false otherwise. It is a good practice to call this method after you tried to open a file.
Examples:
filestorage.cpp.

Referenced by operator<<().

Here is the caller graph for this function:

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

Opens a file.

See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release before opening the file.

Parameters
filenameName 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.
flagsMode of operation. One of FileStorage::Mode
encodingEncoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.
CvFileStorage* cv::FileStorage::operator* ( )
inline

Returns the obsolete C FileStorage structure.

Returns
Pointer to the underlying C FileStorage structure
const CvFileStorage* cv::FileStorage::operator* ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References CV_WRAP.

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

Returns the specified element of the top-level mapping.

Parameters
nodenameName of the file node.
Returns
Node with the given name.
FileNode cv::FileStorage::operator[] ( const char *  nodename) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

Closes the file and releases all the memory buffers.

Call this method after all I/O operations with the storage are finished.

virtual String cv::FileStorage::releaseAndGetString ( )
virtual

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

Examples:
filestorage.cpp.
FileNode cv::FileStorage::root ( int  streamidx = 0) const

Returns the top-level mapping.

Parameters
streamidxZero-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.
Returns
The top-level mapping.

Referenced by cv::DescriptorMatcher::read().

Here is the caller graph for this function:

void cv::FileStorage::write ( const String name,
double  val 
)

Simplified writing API to use with bindings.

Parameters
nameName of the written object
valValue of the written object
void cv::FileStorage::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.

void cv::FileStorage::write ( const String name,
InputArray  val 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void cv::FileStorage::writeComment ( const String comment,
bool  append = false 
)

Writes a comment.

The function writes a comment into file storage. The comments are skipped when the storage is read.

Parameters
commentThe written comment, single-line or multi-line
appendIf 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::writeObj ( const String name,
const void *  obj 
)

Writes the registered C structure (CvMat, CvMatND, CvSeq).

Parameters
nameName of the written object.
objPointer to the object.
See also
ocvWrite for details.
void cv::FileStorage::writeRaw ( const String fmt,
const uchar 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.

Parameters
fmtSpecification of each array element, see format specification
vecPointer to the written array.
lenNumber of the uchar elements to write.

Referenced by cv::FileNode::read().

Here is the caller graph for this function:

Friends And Related Function Documentation

FileStorage & operator<< ( FileStorage fs,
const String str 
)
related

Writes string to a file storage.

template<typename _Tp >
static FileStorage & operator<< ( FileStorage fs,
const _Tp &  value 
)
related

Writes data to a file storage.

References CV_Error, elname, INSIDE_MAP, isOpened(), NAME_EXPECTED, state, and cv::Error::StsError.

Here is the call graph for this function:

static FileStorage & operator<< ( FileStorage fs,
const char *  str 
)
related

Writes data to a file storage.

static FileStorage & operator<< ( FileStorage fs,
char *  value 
)
related

Writes data to a file storage.

void write ( FileStorage fs,
const String name,
int  value 
)
related
void write ( FileStorage fs,
const String name,
float  value 
)
related
void write ( FileStorage fs,
const String name,
double  value 
)
related
void write ( FileStorage fs,
const String name,
const String value 
)
related
void write ( FileStorage fs,
const String name,
const Mat value 
)
related
void write ( FileStorage fs,
const String name,
const SparseMat value 
)
related
void write ( FileStorage fs,
const String name,
const std::vector< KeyPoint > &  value 
)
related
void write ( FileStorage fs,
const String name,
const std::vector< DMatch > &  value 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const _Tp &  value 
)
related
void write ( FileStorage fs,
const int &  value 
)
related
void write ( FileStorage fs,
const float &  value 
)
related
void write ( FileStorage fs,
const double &  value 
)
related
void write ( FileStorage fs,
const String value 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Point_< _Tp > &  pt 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Point3_< _Tp > &  pt 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Size_< _Tp > &  sz 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Complex< _Tp > &  c 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Rect_< _Tp > &  r 
)
related
template<typename _Tp , int cn>
static void write ( FileStorage fs,
const Vec< _Tp, cn > &  v 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const Scalar_< _Tp > &  s 
)
related
static void write ( FileStorage fs,
const Range r 
)
related

References cv::Range::end, and cv::Range::start.

template<typename _Tp >
static void write ( FileStorage fs,
const std::vector< _Tp > &  vec 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Point_< _Tp > &  pt 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Point3_< _Tp > &  pt 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Size_< _Tp > &  sz 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Complex< _Tp > &  c 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Rect_< _Tp > &  r 
)
related
template<typename _Tp , int cn>
static void write ( FileStorage fs,
const String name,
const Vec< _Tp, cn > &  v 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const Scalar_< _Tp > &  s 
)
related
static void write ( FileStorage fs,
const String name,
const Range r 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const std::vector< _Tp > &  vec 
)
related
template<typename _Tp >
static void write ( FileStorage fs,
const String name,
const std::vector< std::vector< _Tp > > &  vec 
)
related
void writeScalar ( FileStorage fs,
int  value 
)
related
void writeScalar ( FileStorage fs,
float  value 
)
related
void writeScalar ( FileStorage fs,
double  value 
)
related
void writeScalar ( FileStorage fs,
const String value 
)
related

Member Data Documentation

String cv::FileStorage::elname

the currently written element

Referenced by operator<<().

Ptr<CvFileStorage> cv::FileStorage::fs

the underlying C FileStorage structure

int cv::FileStorage::state

the writer state

Referenced by operator<<().

std::vector<char> cv::FileStorage::structs

the stack of written structures


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