src/files update + write_binary_json compression argument
This commit is contained in:
+20
-23
@@ -6,6 +6,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "../coders/json.h"
|
#include "../coders/json.h"
|
||||||
|
#include "../coders/gzip.h"
|
||||||
#include "../util/stringutil.h"
|
#include "../util/stringutil.h"
|
||||||
#include "../data/dynamic.h"
|
#include "../data/dynamic.h"
|
||||||
|
|
||||||
@@ -32,21 +33,21 @@ void files::rafile::read(char* buffer, std::streamsize size) {
|
|||||||
file.read(buffer, size);
|
file.read(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool files::write_bytes(fs::path filename, const char* data, size_t size) {
|
bool files::write_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||||
std::ofstream output(filename, std::ios::binary);
|
std::ofstream output(filename, std::ios::binary);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return false;
|
return false;
|
||||||
output.write(data, size);
|
output.write((const char*)data, size);
|
||||||
output.close();
|
output.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint files::append_bytes(fs::path filename, const char* data, size_t size) {
|
uint files::append_bytes(fs::path filename, const ubyte* data, size_t size) {
|
||||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return 0;
|
return 0;
|
||||||
uint position = output.tellp();
|
uint position = output.tellp();
|
||||||
output.write(data, size);
|
output.write((const char*)data, size);
|
||||||
output.close();
|
output.close();
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
@@ -60,7 +61,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* files::read_bytes(fs::path filename, size_t& length) {
|
ubyte* files::read_bytes(fs::path filename, size_t& length) {
|
||||||
std::ifstream input(filename, std::ios::binary);
|
std::ifstream input(filename, std::ios::binary);
|
||||||
if (!input.is_open())
|
if (!input.is_open())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -71,17 +72,17 @@ char* files::read_bytes(fs::path filename, size_t& length) {
|
|||||||
std::unique_ptr<char> data(new char[length]);
|
std::unique_ptr<char> data(new char[length]);
|
||||||
input.read(data.get(), length);
|
input.read(data.get(), length);
|
||||||
input.close();
|
input.close();
|
||||||
return data.release();
|
return (ubyte*)data.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string files::read_string(fs::path filename) {
|
std::string files::read_string(fs::path filename) {
|
||||||
size_t size;
|
size_t size;
|
||||||
std::unique_ptr<char> chars (read_bytes(filename, size));
|
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||||
if (chars == nullptr) {
|
if (bytes == nullptr) {
|
||||||
throw std::runtime_error("could not to load file '"+
|
throw std::runtime_error("could not to load file '"+
|
||||||
filename.string()+"'");
|
filename.string()+"'");
|
||||||
}
|
}
|
||||||
return std::string(chars.get(), size);
|
return std::string((const char*)bytes.get(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool files::write_string(fs::path filename, const std::string content) {
|
bool files::write_string(fs::path filename, const std::string content) {
|
||||||
@@ -94,27 +95,21 @@ bool files::write_string(fs::path filename, const std::string content) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||||
// -- binary json tests
|
|
||||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
|
||||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) {
|
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||||
std::vector<ubyte> bytes = json::to_binary(obj);
|
auto bytes = json::to_binary(obj);
|
||||||
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
if (compression) {
|
||||||
|
bytes = gzip::compress(bytes.data(), bytes.size());
|
||||||
|
}
|
||||||
|
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
||||||
// binary json tests
|
|
||||||
// fs::path binfile = fs::path(filename.u8string()+".bin");
|
|
||||||
// if (fs::is_regular_file(binfile)){
|
|
||||||
// return read_binary_json(binfile);
|
|
||||||
// }
|
|
||||||
|
|
||||||
std::string text = files::read_string(filename);
|
std::string text = files::read_string(filename);
|
||||||
try {
|
try {
|
||||||
auto obj = json::parse(filename.string(), text);
|
auto obj = json::parse(filename.string(), text);
|
||||||
//write_binary_json(binfile, obj);
|
|
||||||
return obj;
|
return obj;
|
||||||
} catch (const parsing_error& error) {
|
} catch (const parsing_error& error) {
|
||||||
std::cerr << error.errorLog() << std::endl;
|
std::cerr << error.errorLog() << std::endl;
|
||||||
@@ -124,8 +119,10 @@ std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
|
|||||||
|
|
||||||
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
|
||||||
size_t size;
|
size_t size;
|
||||||
std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
|
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||||
return std::unique_ptr<dynamic::Map>(json::from_binary((const ubyte*)bytes.get(), size));
|
return std::unique_ptr<dynamic::Map>(
|
||||||
|
json::from_binary(bytes.get(), size)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> files::read_list(fs::path filename) {
|
std::vector<std::string> files::read_list(fs::path filename) {
|
||||||
|
|||||||
+23
-5
@@ -27,15 +27,33 @@ namespace files {
|
|||||||
size_t length() const;
|
size_t length() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Write bytes array to the file without any extra data */
|
||||||
|
extern bool write_bytes(fs::path, const ubyte* data, size_t size);
|
||||||
|
|
||||||
extern bool write_bytes(fs::path, const char* data, size_t size);
|
/* Append bytes array to the file without any extra data */
|
||||||
extern uint append_bytes(fs::path, const char* data, size_t size);
|
extern uint append_bytes(fs::path, const ubyte* data, size_t size);
|
||||||
|
|
||||||
|
/* Write string to the file */
|
||||||
extern bool write_string(fs::path filename, const std::string content);
|
extern bool write_string(fs::path filename, const std::string content);
|
||||||
extern bool write_json(fs::path filename, const dynamic::Map* obj, bool nice=true);
|
|
||||||
extern bool write_binary_json(fs::path filename, const dynamic::Map* obj);
|
/* Write dynamic data to the JSON file
|
||||||
|
@param nice if true,
|
||||||
|
human readable format will be used, otherwise minimal */
|
||||||
|
extern bool write_json(
|
||||||
|
fs::path filename,
|
||||||
|
const dynamic::Map* obj,
|
||||||
|
bool nice=true);
|
||||||
|
|
||||||
|
/* Write dynamic data to the binary JSON file
|
||||||
|
(see src/coders/binary_json_spec.md)
|
||||||
|
@param compressed use gzip compression */
|
||||||
|
extern bool write_binary_json(
|
||||||
|
fs::path filename,
|
||||||
|
const dynamic::Map* obj,
|
||||||
|
bool compressed=false);
|
||||||
|
|
||||||
extern bool read(fs::path, char* data, size_t size);
|
extern bool read(fs::path, char* data, size_t size);
|
||||||
extern char* read_bytes(fs::path, size_t& length);
|
extern ubyte* read_bytes(fs::path, size_t& length);
|
||||||
extern std::string read_string(fs::path filename);
|
extern std::string read_string(fs::path filename);
|
||||||
extern std::unique_ptr<dynamic::Map> read_json(fs::path file);
|
extern std::unique_ptr<dynamic::Map> read_json(fs::path file);
|
||||||
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
|
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
|
||||||
|
|||||||
Reference in New Issue
Block a user