fixed a few bugs in XML/YAML input/output.

This commit is contained in:
Vadim Pisarevsky 2012-04-03 12:23:46 +00:00
parent 85364ac9b1
commit 5e15557155
3 changed files with 59 additions and 42 deletions

View File

@ -2834,29 +2834,26 @@ public:
{
int _fmt = DataType<_Tp>::fmt;
char fmt[] = { (char)((_fmt>>8)+'1'), (char)_fmt, '\0' };
fs->writeRaw( string(fmt), (uchar*)&vec[0], vec.size()*sizeof(_Tp) );
fs->writeRaw( string(fmt), !vec.empty() ? (uchar*)&vec[0] : 0, vec.size()*sizeof(_Tp) );
}
FileStorage* fs;
};
template<typename _Tp> static inline void write( FileStorage& fs, const vector<_Tp>& vec )
{
VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs);
w(vec);
}
template<typename _Tp> static inline FileStorage&
operator << ( FileStorage& fs, const vector<_Tp>& vec )
template<typename _Tp> static inline void write( FileStorage& fs, const string& name,
const vector<_Tp>& vec )
{
VecWriterProxy<_Tp, DataType<_Tp>::generic_type == 0> w(&fs);
w(vec);
return fs;
WriteStructContext ws(fs, name, CV_NODE_SEQ+(DataType<_Tp>::fmt != 0 ? CV_NODE_FLOW : 0));
write(fs, vec);
}
CV_EXPORTS_W void write( FileStorage& fs, const string& name, const Mat& value );
CV_EXPORTS void write( FileStorage& fs, const string& name, const SparseMat& value );
CV_EXPORTS void write( FileStorage& fs, const string& name, const vector<Mat>& value );
template<typename _Tp> static inline FileStorage& operator << (FileStorage& fs, const _Tp& value)
{
@ -2894,7 +2891,7 @@ inline size_t FileNode::size() const
{
int t = type();
return t == MAP ? ((CvSet*)node->data.map)->active_count :
t == SEQ ? node->data.seq->total : node != 0;
t == SEQ ? node->data.seq->total : (size_t)!isNone();
}
inline CvFileNode* FileNode::operator *() { return (CvFileNode*)node; }
@ -2958,7 +2955,6 @@ static inline void read(const FileNode& node, string& value, const string& defau
CV_EXPORTS_W void read(const FileNode& node, Mat& mat, const Mat& default_mat=Mat() );
CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat=SparseMat() );
CV_EXPORTS void read(const FileNode& node, vector<Mat>& mat, const vector<Mat>& default_mat_vector=vector<Mat>() );
inline FileNode::operator int() const
{
@ -3029,9 +3025,10 @@ read( FileNodeIterator& it, vector<_Tp>& vec, size_t maxCount=(size_t)INT_MAX )
}
template<typename _Tp> static inline void
read( FileNode& node, vector<_Tp>& vec, const vector<_Tp>& default_value=vector<_Tp>() )
read( const FileNode& node, vector<_Tp>& vec, const vector<_Tp>& default_value=vector<_Tp>() )
{
read( node.begin(), vec );
FileNodeIterator it = node.begin();
read( it, vec );
}
inline FileNodeIterator FileNode::begin() const

View File

@ -2987,9 +2987,6 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
CV_CHECK_OUTPUT_FILE_STORAGE( fs );
if( !data0 )
CV_Error( CV_StsNullPtr, "Null data pointer" );
if( len < 0 )
CV_Error( CV_StsOutOfRange, "Negative number of elements" );
@ -2998,6 +2995,9 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
if( !len )
return;
if( !data0 )
CV_Error( CV_StsNullPtr, "Null data pointer" );
if( fmt_pair_count == 1 )
{
fmt_pairs[0] *= len;
@ -5195,7 +5195,7 @@ FileNodeIterator::FileNodeIterator()
FileNodeIterator::FileNodeIterator(const CvFileStorage* _fs,
const CvFileNode* _node, size_t _ofs)
{
if( _fs && _node )
if( _fs && _node && CV_NODE_TYPE(_node->tag) != CV_NODE_NONE )
{
int node_type = _node->tag & FileNode::TYPE_MASK;
fs = _fs;
@ -5359,12 +5359,6 @@ void write( FileStorage& fs, const string& name, const SparseMat& value )
cvWrite( *fs, name.size() ? name.c_str() : 0, mat );
}
void write( FileStorage& fs, const string& name, const vector<Mat>& value )
{
WriteStructContext ws(fs, name, CV_NODE_SEQ);
for( size_t i = 0; i < value.size(); i++ )
write(fs, string(), value[i]);
}
WriteStructContext::WriteStructContext(FileStorage& _fs, const string& name,
int flags, const string& typeName) : fs(&_fs)
@ -5413,24 +5407,6 @@ void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat )
SparseMat(m).copyTo(mat);
}
void read( const FileNode& node, vector<Mat>& mat_vector, const vector<Mat>& default_mat_vector )
{
if( node.empty() )
{
mat_vector = default_mat_vector;
return;
}
FileNodeIterator it = node.begin(), it_end = node.end();
mat_vector.clear();
for( ; it != it_end; ++it )
{
Mat m;
*it >> m;
mat_vector.push_back(m);
}
}
}
/* End of file. */

View File

@ -377,6 +377,50 @@ protected:
TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); }
class CV_MiscIOTest : public cvtest::BaseTest
{
public:
CV_MiscIOTest() {}
~CV_MiscIOTest() {}
protected:
void run(int)
{
//try
{
FileStorage fs("test.xml", FileStorage::WRITE);
vector<int> mi, mi2, mi3, mi4;
vector<Mat> mv, mv2, mv3, mv4;
Mat m(10, 9, CV_32F);
randu(m, 0, 1);
mi3.push_back(5);
mv3.push_back(m);
fs << "mi" << mi;
fs << "mv" << mv;
fs << "mi3" << mi3;
fs << "mv3" << mv3;
fs.release();
fs.open("test.xml", FileStorage::READ);
fs["mi"] >> mi2;
fs["mv"] >> mv2;
fs["mi3"] >> mi4;
fs["mv3"] >> mv4;
CV_Assert( mi2.empty() );
CV_Assert( mv2.empty() );
CV_Assert( norm(mi3, mi4, CV_C) == 0 );
CV_Assert( mv4.size() == 1 );
double n = norm(mv3[0], mv4[0], CV_C);
CV_Assert( n == 0 );
}
/*catch(...)
{
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
}*/
}
};
TEST(Core_InputOutput, misc) { CV_MiscIOTest test; test.safe_run(); }
/*class CV_BigMatrixIOTest : public cvtest::BaseTest
{
public: