Compare commits
4 Commits
973b94a348
...
99d3ea7b83
| Author | SHA1 | Date | |
|---|---|---|---|
| 99d3ea7b83 | |||
| 5500cd6984 | |||
| b5c13725de | |||
| 8ef02f9470 |
@@ -25,6 +25,19 @@ enum ValueType {
|
||||
/// On-disk iKv format versions supported for explicit writes.
|
||||
enum class Version : unsigned { v1 = 1, v2 = 2 };
|
||||
|
||||
/// Serialized storage kind of an iKv document.
|
||||
enum class StorageKind : unsigned {
|
||||
unknown = 0,
|
||||
text,
|
||||
binary
|
||||
};
|
||||
|
||||
/// File format information reported by the library.
|
||||
struct FileFormat {
|
||||
StorageKind storage = StorageKind::unknown;
|
||||
Version version = Version::v2;
|
||||
};
|
||||
|
||||
/// Error raised for invalid types, unsupported operations, parse failures,
|
||||
/// and I/O failures.
|
||||
class Error : public std::runtime_error {
|
||||
@@ -116,6 +129,8 @@ public:
|
||||
double asDouble() const;
|
||||
bool asBool() const;
|
||||
|
||||
ikv_node_t* get_node() const { return node_; }
|
||||
|
||||
/// Writes text or binary output, using iKv2 unless a version is specified.
|
||||
void write(const std::string& path, Version version = Version::v2) const;
|
||||
void writeBinary(const std::string& path, Version version = Version::v2) const;
|
||||
@@ -143,4 +158,8 @@ private:
|
||||
bool read_only_ = false;
|
||||
};
|
||||
|
||||
/// Inspects an iKv file and reports whether it is text or binary plus its version.
|
||||
/// Throws Error when the file cannot be identified.
|
||||
FileFormat inspectFile(const std::string& path);
|
||||
|
||||
} // namespace ikv
|
||||
|
||||
@@ -39,6 +39,10 @@ ValueType publicType(ikv_type_t type) noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
Version publicVersion(ikv_version_t version) {
|
||||
return version == IKV_VERSION_1 ? Version::v1 : Version::v2;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Value::Deleter::operator()(ikv_node_t* node) const noexcept { ikv_free(node); }
|
||||
@@ -251,4 +255,18 @@ void Value::refresh(const std::string& path) {
|
||||
if (!ikv_refresh_from_path(node_, path.c_str())) throw Error("failed to refresh iKv root from: " + path);
|
||||
}
|
||||
|
||||
FileFormat inspectFile(const std::string& path) {
|
||||
if (ikv_version_t version = ikv_detect_file_version(path.c_str(), true);
|
||||
version != IKV_VERSION_UNKNOWN) {
|
||||
return {StorageKind::binary, publicVersion(version)};
|
||||
}
|
||||
|
||||
if (ikv_version_t version = ikv_detect_file_version(path.c_str(), false);
|
||||
version != IKV_VERSION_UNKNOWN) {
|
||||
return {StorageKind::text, publicVersion(version)};
|
||||
}
|
||||
|
||||
throw Error("failed to identify iKv file format: " + path);
|
||||
}
|
||||
|
||||
} // namespace ikv
|
||||
|
||||
Reference in New Issue
Block a user