Skip to content

Commit

Permalink
FileStorage: add simplified API for bindings
Browse files Browse the repository at this point in the history
at least it is possible to read/ write calibration files. Updates #4282.

Also add CPP method for writing comments.
  • Loading branch information
paroj committed Apr 27, 2016
1 parent 6e5e5d8 commit a64d8be
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
34 changes: 33 additions & 1 deletion modules/core/include/opencv2/core/persistence.hpp
Expand Up @@ -398,7 +398,7 @@ class CV_EXPORTS_W FileStorage
FileNode operator[](const String& nodename) const;

/** @overload */
CV_WRAP FileNode operator[](const char* nodename) const;
CV_WRAP_AS(getFileNode) FileNode operator[](const char* nodename) const;

/** @brief Returns the obsolete C FileStorage structure.
@returns Pointer to the underlying C FileStorage structure
Expand All @@ -425,6 +425,38 @@ class CV_EXPORTS_W FileStorage
*/
void writeObj( const String& name, const void* obj );

/**
* @brief Simplified writing API to use with bindings.
* @param name Name of the written object
* @param val Value of the written object
*/
CV_WRAP void write(const String& name, double val);
/// @overload
CV_WRAP void write(const String& name, const String& val);
/// @overload
CV_WRAP void write(const String& name, InputArray val);

/**
* @brief Simplified reading API to use with bindings.
* @param name Name of the object
* @param val Value of the object
*/
CV_WRAP_AS(readReal) void read(const String& name, CV_OUT double& val);
/// @overload
CV_WRAP_AS(readString) void read(const String& name, CV_OUT String& val);
/// @overload
CV_WRAP_AS(readMat) void read(const String& name, CV_OUT Mat& val);

/** @brief Writes a comment.
The function writes a comment into file storage. The comments are skipped when the storage is read.
@param comment The written comment, single-line or multi-line
@param 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.
*/
CV_WRAP void writeComment(const String& comment, bool append = false);

/** @brief Returns the normalized object name for the specified name of a file.
@param filename Name of a file
@returns The normalized object name.
Expand Down
34 changes: 34 additions & 0 deletions modules/core/src/persistence.cpp
Expand Up @@ -5271,6 +5271,40 @@ void FileStorage::writeObj( const String& name, const void* obj )
cvWrite( fs, name.size() > 0 ? name.c_str() : 0, obj );
}

void FileStorage::write( const String& name, double val )
{
*this << name << val;
}

void FileStorage::write( const String& name, const String& val )
{
*this << name << val;
}

void FileStorage::write( const String& name, InputArray val )
{
*this << name << val.getMat();
}

void FileStorage::read( const String& name, double& val )
{
(*this)[name] >> val;
}

void FileStorage::read( const String& name, String& val )
{
(*this)[name] >> val;
}

void FileStorage::read( const String& name, Mat& val )
{
(*this)[name] >> val;
}

void FileStorage::writeComment( const String& comment, bool append )
{
cvWriteComment(fs, comment.c_str(), append ? 1 : 0);
}

FileNode FileStorage::operator[](const String& nodename) const
{
Expand Down

0 comments on commit a64d8be

Please sign in to comment.