src/files update + write_binary_json compression argument
This commit is contained in:
parent
d2fe51e1ff
commit
6efbaac18a
@ -6,6 +6,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdexcept>
|
||||
#include "../coders/json.h"
|
||||
#include "../coders/gzip.h"
|
||||
#include "../util/stringutil.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
@ -32,21 +33,21 @@ void files::rafile::read(char* buffer, std::streamsize 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);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
output.write(data, size);
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
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);
|
||||
if (!output.is_open())
|
||||
return 0;
|
||||
uint position = output.tellp();
|
||||
output.write(data, size);
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
return position;
|
||||
}
|
||||
@ -60,7 +61,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
|
||||
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);
|
||||
if (!input.is_open())
|
||||
return nullptr;
|
||||
@ -71,17 +72,17 @@ char* files::read_bytes(fs::path filename, size_t& length) {
|
||||
std::unique_ptr<char> data(new char[length]);
|
||||
input.read(data.get(), length);
|
||||
input.close();
|
||||
return data.release();
|
||||
return (ubyte*)data.release();
|
||||
}
|
||||
|
||||
std::string files::read_string(fs::path filename) {
|
||||
size_t size;
|
||||
std::unique_ptr<char> chars (read_bytes(filename, size));
|
||||
if (chars == nullptr) {
|
||||
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||
if (bytes == nullptr) {
|
||||
throw std::runtime_error("could not to load file '"+
|
||||
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) {
|
||||
@ -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) {
|
||||
// -- binary json tests
|
||||
//return write_binary_json(fs::path(filename.u8string()+".bin"), obj);
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj) {
|
||||
std::vector<ubyte> bytes = json::to_binary(obj);
|
||||
return files::write_bytes(filename, (const char*)bytes.data(), bytes.size());
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||
auto bytes = json::to_binary(obj);
|
||||
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) {
|
||||
// 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);
|
||||
try {
|
||||
auto obj = json::parse(filename.string(), text);
|
||||
//write_binary_json(binfile, obj);
|
||||
return obj;
|
||||
} catch (const parsing_error& error) {
|
||||
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) {
|
||||
size_t size;
|
||||
std::unique_ptr<char[]> bytes (files::read_bytes(file, size));
|
||||
return std::unique_ptr<dynamic::Map>(json::from_binary((const ubyte*)bytes.get(), size));
|
||||
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||
return std::unique_ptr<dynamic::Map>(
|
||||
json::from_binary(bytes.get(), size)
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::string> files::read_list(fs::path filename) {
|
||||
|
||||
@ -27,15 +27,33 @@ namespace files {
|
||||
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);
|
||||
extern uint append_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 ubyte* data, size_t size);
|
||||
|
||||
/* Write string to the file */
|
||||
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 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::unique_ptr<dynamic::Map> read_json(fs::path file);
|
||||
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user