Merge pull request #2476 from minkless:3.0-bugfix-core-filestorage

This commit is contained in:
Roman Donchenko 2014-03-28 12:17:16 +04:00 committed by OpenCV Buildbot
commit d6b54ff9d1
2 changed files with 59 additions and 0 deletions

View File

@ -5486,11 +5486,27 @@ internal::WriteStructContext::WriteStructContext(FileStorage& _fs,
{
cvStartWriteStruct(**fs, !name.empty() ? name.c_str() : 0, flags,
!typeName.empty() ? typeName.c_str() : 0);
fs->elname = String();
if ((flags & FileNode::TYPE_MASK) == FileNode::SEQ)
{
fs->state = FileStorage::VALUE_EXPECTED;
fs->structs.push_back('[');
}
else
{
fs->state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
fs->structs.push_back('{');
}
}
internal::WriteStructContext::~WriteStructContext()
{
cvEndWriteStruct(**fs);
fs->structs.pop_back();
fs->state = fs->structs.empty() || fs->structs.back() == '{' ?
FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP :
FileStorage::VALUE_EXPECTED;
fs->elname = String();
}

View File

@ -380,6 +380,40 @@ TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run
extern void testFormatter();
struct UserDefinedType
{
int a;
float b;
};
static inline bool operator==(const UserDefinedType &x,
const UserDefinedType &y) {
return (x.a == y.a) && (x.b == y.b);
}
static inline void write(FileStorage &fs,
const String&,
const UserDefinedType &value)
{
fs << "{:" << "a" << value.a << "b" << value.b << "}";
}
static inline void read(const FileNode& node,
UserDefinedType& value,
const UserDefinedType& default_value
= UserDefinedType()) {
if(node.empty())
{
value = default_value;
}
else
{
node["a"] >> value.a;
node["b"] >> value.b;
}
}
class CV_MiscIOTest : public cvtest::BaseTest
{
public:
@ -393,11 +427,14 @@ protected:
string fname = cv::tempfile(".xml");
vector<int> mi, mi2, mi3, mi4;
vector<Mat> mv, mv2, mv3, mv4;
vector<UserDefinedType> vudt, vudt2, vudt3, vudt4;
Mat m(10, 9, CV_32F);
Mat empty;
UserDefinedType udt = { 8, 3.3f };
randu(m, 0, 1);
mi3.push_back(5);
mv3.push_back(m);
vudt3.push_back(udt);
Point_<float> p1(1.1f, 2.2f), op1;
Point3i p2(3, 4, 5), op2;
Size s1(6, 7), os1;
@ -412,6 +449,8 @@ protected:
fs << "mv" << mv;
fs << "mi3" << mi3;
fs << "mv3" << mv3;
fs << "vudt" << vudt;
fs << "vudt3" << vudt3;
fs << "empty" << empty;
fs << "p1" << p1;
fs << "p2" << p2;
@ -428,6 +467,8 @@ protected:
fs["mv"] >> mv2;
fs["mi3"] >> mi4;
fs["mv3"] >> mv4;
fs["vudt"] >> vudt2;
fs["vudt3"] >> vudt4;
fs["empty"] >> empty;
fs["p1"] >> op1;
fs["p2"] >> op2;
@ -442,6 +483,8 @@ protected:
CV_Assert( norm(mi3, mi4, CV_C) == 0 );
CV_Assert( mv4.size() == 1 );
double n = norm(mv3[0], mv4[0], CV_C);
CV_Assert( vudt2.empty() );
CV_Assert( vudt3 == vudt4 );
CV_Assert( n == 0 );
CV_Assert( op1 == p1 );
CV_Assert( op2 == p2 );