format: reformat project
Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
+39
-32
@@ -1,24 +1,25 @@
|
||||
#include "files.hpp"
|
||||
|
||||
#include "../coders/commons.hpp"
|
||||
#include "../coders/json.hpp"
|
||||
#include "../coders/toml.hpp"
|
||||
#include "../coders/gzip.hpp"
|
||||
#include "../util/stringutil.hpp"
|
||||
#include "../data/dynamic.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdint.h>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../coders/commons.hpp"
|
||||
#include "../coders/gzip.hpp"
|
||||
#include "../coders/json.hpp"
|
||||
#include "../coders/toml.hpp"
|
||||
#include "../data/dynamic.hpp"
|
||||
#include "../util/stringutil.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
files::rafile::rafile(const fs::path& filename)
|
||||
: file(filename, std::ios::binary | std::ios::ate) {
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.string());
|
||||
throw std::runtime_error("could not to open file " + filename.string());
|
||||
}
|
||||
filelength = file.tellg();
|
||||
file.seekg(0);
|
||||
@@ -36,19 +37,21 @@ void files::rafile::read(char* buffer, std::streamsize size) {
|
||||
file.read(buffer, size);
|
||||
}
|
||||
|
||||
bool files::write_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
bool files::write_bytes(
|
||||
const fs::path& filename, const ubyte* data, size_t size
|
||||
) {
|
||||
std::ofstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
if (!output.is_open()) return false;
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t size) {
|
||||
uint files::append_bytes(
|
||||
const 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;
|
||||
if (!output.is_open()) return 0;
|
||||
uint position = output.tellp();
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
@@ -57,17 +60,17 @@ uint files::append_bytes(const fs::path& filename, const ubyte* data, size_t siz
|
||||
|
||||
bool files::read(const fs::path& filename, char* data, size_t size) {
|
||||
std::ifstream output(filename, std::ios::binary);
|
||||
if (!output.is_open())
|
||||
return false;
|
||||
if (!output.is_open()) return false;
|
||||
output.read(data, size);
|
||||
output.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& length) {
|
||||
std::unique_ptr<ubyte[]> files::read_bytes(
|
||||
const fs::path& filename, size_t& length
|
||||
) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open())
|
||||
return nullptr;
|
||||
if (!input.is_open()) return nullptr;
|
||||
input.seekg(0, std::ios_base::end);
|
||||
length = input.tellg();
|
||||
input.seekg(0, std::ios_base::beg);
|
||||
@@ -80,8 +83,7 @@ std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& len
|
||||
|
||||
std::vector<ubyte> files::read_bytes(const fs::path& filename) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open())
|
||||
return {};
|
||||
if (!input.is_open()) return {};
|
||||
input.seekg(0, std::ios_base::end);
|
||||
size_t length = input.tellg();
|
||||
input.seekg(0, std::ios_base::beg);
|
||||
@@ -95,10 +97,11 @@ std::vector<ubyte> files::read_bytes(const fs::path& filename) {
|
||||
|
||||
std::string files::read_string(const fs::path& filename) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));
|
||||
std::unique_ptr<ubyte[]> bytes(read_bytes(filename, size));
|
||||
if (bytes == nullptr) {
|
||||
throw std::runtime_error("could not to load file '"+
|
||||
filename.string()+"'");
|
||||
throw std::runtime_error(
|
||||
"could not to load file '" + filename.string() + "'"
|
||||
);
|
||||
}
|
||||
return std::string((const char*)bytes.get(), size);
|
||||
}
|
||||
@@ -112,11 +115,15 @@ bool files::write_string(const fs::path& filename, const std::string content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool files::write_json(const fs::path& filename, const dynamic::Map* obj, bool nice) {
|
||||
bool files::write_json(
|
||||
const fs::path& filename, const dynamic::Map* obj, bool nice
|
||||
) {
|
||||
return files::write_string(filename, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj, bool compression) {
|
||||
bool files::write_binary_json(
|
||||
const fs::path& filename, const dynamic::Map* obj, bool compression
|
||||
) {
|
||||
auto bytes = json::to_binary(obj, compression);
|
||||
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||
}
|
||||
@@ -128,7 +135,7 @@ std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) {
|
||||
|
||||
std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) {
|
||||
size_t size;
|
||||
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
|
||||
std::unique_ptr<ubyte[]> bytes(files::read_bytes(file, size));
|
||||
return json::from_binary(bytes.get(), size);
|
||||
}
|
||||
|
||||
@@ -139,16 +146,16 @@ std::shared_ptr<dynamic::Map> files::read_toml(const fs::path& file) {
|
||||
std::vector<std::string> files::read_list(const fs::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file "+filename.u8string());
|
||||
throw std::runtime_error(
|
||||
"could not to open file " + filename.u8string()
|
||||
);
|
||||
}
|
||||
std::vector<std::string> lines;
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
util::trim(line);
|
||||
if (line.length() == 0)
|
||||
continue;
|
||||
if (line[0] == '#')
|
||||
continue;
|
||||
if (line.length() == 0) continue;
|
||||
if (line[0] == '#') continue;
|
||||
lines.push_back(line);
|
||||
}
|
||||
return lines;
|
||||
|
||||
Reference in New Issue
Block a user