lots of stuff

This commit is contained in:
Mars 2024-05-31 22:59:00 -04:00
parent e8fb8ec19f
commit 791e237470
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
224 changed files with 19811 additions and 129 deletions

View file

@ -0,0 +1,29 @@
#ifndef RFL_IO_LOAD_BYTES_HPP_
#define RFL_IO_LOAD_BYTES_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "../Result.hpp"
namespace rfl {
namespace io {
inline Result<std::vector<char>> load_bytes(const std::string& _fname) {
std::ifstream input(_fname, std::ios::binary);
if (input.is_open()) {
std::istreambuf_iterator<char> begin(input), end;
const auto bytes = std::vector<char>(begin, end);
input.close();
return bytes;
} else {
return rfl::Error("File '" + _fname + "' not found!");
}
}
} // namespace io
} // namespace rfl
#endif

View file

@ -0,0 +1,29 @@
#ifndef RFL_IO_LOAD_STRING_HPP_
#define RFL_IO_LOAD_STRING_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
namespace rfl {
namespace io {
inline Result<std::string> load_string(const std::string& _fname) {
std::ifstream infile(_fname);
if (infile.is_open()) {
auto r = std::string(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>());
infile.close();
return r;
} else {
return Error("Unable to open file '" + _fname +
"' or file could not be found.");
}
}
} // namespace io
} // namespace rfl
#endif

View file

@ -0,0 +1,30 @@
#ifndef RFL_IO_SAVE_BYTES_HPP_
#define RFL_IO_SAVE_BYTES_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "../Result.hpp"
namespace rfl {
namespace io {
template <class T, class WriteFunction>
Result<Nothing> save_bytes(const std::string& _fname, const T& _obj,
const WriteFunction& _write) {
try {
std::ofstream output(_fname, std::ios::out | std::ios::binary);
_write(_obj, output);
output.close();
} catch (std::exception& e) {
return Error(e.what());
}
return Nothing{};
}
} // namespace io
} // namespace rfl
#endif

View file

@ -0,0 +1,30 @@
#ifndef RFL_IO_SAVE_STRING_HPP_
#define RFL_IO_SAVE_STRING_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
namespace rfl {
namespace io {
template <class T, class WriteFunction>
Result<Nothing> save_string(const std::string& _fname, const T& _obj,
const WriteFunction& _write) {
try {
std::ofstream outfile;
outfile.open(_fname);
_write(_obj, outfile);
outfile.close();
} catch (std::exception& e) {
return Error(e.what());
}
return Nothing{};
}
} // namespace io
} // namespace rfl
#endif