Skip to content

Commit bc3c7e8

Browse files
committed
Merge pull request opencv#9209 from alalek:fix_persistence_format
2 parents 32683ee + ec7ce81 commit bc3c7e8

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

modules/core/include/opencv2/core/persistence.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ class CV_EXPORTS_W FileStorage
462462
*/
463463
static String getDefaultObjectName(const String& filename);
464464

465+
/** @brief Returns the current format.
466+
* @returns The current format, see FileStorage::Mode
467+
*/
468+
CV_WRAP int getFormat() const;
469+
465470
Ptr<CvFileStorage> fs; //!< the underlying C FileStorage structure
466471
String elname; //!< the currently written element
467472
std::vector<char> structs; //!< the stack of written structures

modules/core/src/persistence.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,11 +4270,25 @@ cvOpenFileStorage( const char* query, CvMemStorage* dststorage, int flags, const
42704270

42714271
if( fmt == CV_STORAGE_FORMAT_AUTO && filename )
42724272
{
4273-
const char* dot_pos = strrchr( filename, '.' );
4273+
const char* dot_pos = NULL;
4274+
const char* dot_pos2 = NULL;
4275+
// like strrchr() implementation, but save two last positions simultaneously
4276+
for (const char* pos = filename; pos[0] != 0; pos++)
4277+
{
4278+
if (pos[0] == '.')
4279+
{
4280+
dot_pos2 = dot_pos;
4281+
dot_pos = pos;
4282+
}
4283+
}
4284+
if (cv_strcasecmp(dot_pos, ".gz") && dot_pos2 != NULL)
4285+
{
4286+
dot_pos = dot_pos2;
4287+
}
42744288
fs->fmt
4275-
= cv_strcasecmp( dot_pos, ".xml" )
4289+
= (cv_strcasecmp(dot_pos, ".xml") || cv_strcasecmp(dot_pos, ".xml.gz"))
42764290
? CV_STORAGE_FORMAT_XML
4277-
: cv_strcasecmp( dot_pos, ".json" )
4291+
: (cv_strcasecmp(dot_pos, ".json") || cv_strcasecmp(dot_pos, ".json.gz"))
42784292
? CV_STORAGE_FORMAT_JSON
42794293
: CV_STORAGE_FORMAT_YAML
42804294
;
@@ -6920,6 +6934,12 @@ FileNode FileStorage::root(int streamidx) const
69206934
return isOpened() ? FileNode(fs, cvGetRootFileNode(fs, streamidx)) : FileNode();
69216935
}
69226936

6937+
int FileStorage::getFormat() const
6938+
{
6939+
CV_Assert(!fs.empty());
6940+
return fs->fmt & FORMAT_MASK;
6941+
}
6942+
69236943
FileStorage& operator << (FileStorage& fs, const String& str)
69246944
{
69256945
CV_TRACE_REGION_VERBOSE();

modules/core/test/test_io.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,3 +1236,59 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
12361236
}
12371237
}
12381238
}
1239+
1240+
TEST(Core_InputOutput, FileStorage_format_xml)
1241+
{
1242+
FileStorage fs;
1243+
fs.open("opencv_storage.xml", FileStorage::WRITE | FileStorage::MEMORY);
1244+
EXPECT_EQ(FileStorage::FORMAT_XML, fs.getFormat());
1245+
}
1246+
1247+
TEST(Core_InputOutput, FileStorage_format_xml_gz)
1248+
{
1249+
FileStorage fs;
1250+
fs.open("opencv_storage.xml.gz", FileStorage::WRITE | FileStorage::MEMORY);
1251+
EXPECT_EQ(FileStorage::FORMAT_XML, fs.getFormat());
1252+
}
1253+
1254+
TEST(Core_InputOutput, FileStorage_format_json)
1255+
{
1256+
FileStorage fs;
1257+
fs.open("opencv_storage.json", FileStorage::WRITE | FileStorage::MEMORY);
1258+
EXPECT_EQ(FileStorage::FORMAT_JSON, fs.getFormat());
1259+
}
1260+
1261+
TEST(Core_InputOutput, FileStorage_format_json_gz)
1262+
{
1263+
FileStorage fs;
1264+
fs.open("opencv_storage.json.gz", FileStorage::WRITE | FileStorage::MEMORY);
1265+
EXPECT_EQ(FileStorage::FORMAT_JSON, fs.getFormat());
1266+
}
1267+
1268+
TEST(Core_InputOutput, FileStorage_format_yaml)
1269+
{
1270+
FileStorage fs;
1271+
fs.open("opencv_storage.yaml", FileStorage::WRITE | FileStorage::MEMORY);
1272+
EXPECT_EQ(FileStorage::FORMAT_YAML, fs.getFormat());
1273+
}
1274+
1275+
TEST(Core_InputOutput, FileStorage_format_yaml_gz)
1276+
{
1277+
FileStorage fs;
1278+
fs.open("opencv_storage.yaml.gz", FileStorage::WRITE | FileStorage::MEMORY);
1279+
EXPECT_EQ(FileStorage::FORMAT_YAML, fs.getFormat());
1280+
}
1281+
1282+
TEST(Core_InputOutput, FileStorage_format_yml)
1283+
{
1284+
FileStorage fs;
1285+
fs.open("opencv_storage.yml", FileStorage::WRITE | FileStorage::MEMORY);
1286+
EXPECT_EQ(FileStorage::FORMAT_YAML, fs.getFormat());
1287+
}
1288+
1289+
TEST(Core_InputOutput, FileStorage_format_yml_gz)
1290+
{
1291+
FileStorage fs;
1292+
fs.open("opencv_storage.yml.gz", FileStorage::WRITE | FileStorage::MEMORY);
1293+
EXPECT_EQ(FileStorage::FORMAT_YAML, fs.getFormat());
1294+
}

0 commit comments

Comments
 (0)