lots of stuff
This commit is contained in:
parent
e8fb8ec19f
commit
791e237470
224 changed files with 19811 additions and 129 deletions
29
include/rfl/io/load_bytes.hpp
Normal file
29
include/rfl/io/load_bytes.hpp
Normal 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
|
29
include/rfl/io/load_string.hpp
Normal file
29
include/rfl/io/load_string.hpp
Normal 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
|
30
include/rfl/io/save_bytes.hpp
Normal file
30
include/rfl/io/save_bytes.hpp
Normal 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
|
30
include/rfl/io/save_string.hpp
Normal file
30
include/rfl/io/save_string.hpp
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue