18 inline std::runtime_error tdc_input_file_not_found_error(
const std::string& path) {
19 return std::runtime_error(std::string(
"input file ") + path +
" does not exist");
22 inline std::runtime_error tdc_output_file_not_found_error(
const std::string& path) {
23 return std::runtime_error(std::string(
"output file ") + path +
" can not be created/accessed");
27 T read_bytes(std::istream& inp,
size_t bytes =
sizeof(T)) {
30 for(
size_t i = 0; i < bytes; i++) {
35 ret |= T((
unsigned char)c);
41 void read_bytes_to_vec(std::istream& inp, T& vec,
size_t bytes) {
43 for(
size_t i = 0; i < bytes; i++) {
47 vec.push_back((
unsigned char)c);
51 inline std::ifstream create_tdc_ifstream(
const std::string& filename,
size_t offset = 0) {
52 std::ifstream in(filename, std::ios::in | std::ios::binary);
54 in.seekg(offset, std::ios::beg);
57 throw tdc_input_file_not_found_error(filename);
61 T read_file_to_stl_byte_container(
const std::string& filename,
64 auto in = create_tdc_ifstream(filename, offset);
68 if (
len ==
size_t(-1)) {
69 in.seekg(offset, std::ios::beg);
70 auto start = in.tellg();
71 in.seekg(0, std::ios::end);
72 auto end = in.tellg();
77 contents.reserve(
len);
81 in.seekg(offset, std::ios::beg);
84 in.read((
char*)&contents[0], contents.size());
90 template<
class T,
class S>
91 T read_stream_to_stl_byte_container(S& stream) {
94 while (stream.get(c)) {
95 vector.push_back(
typename T::value_type(c));
100 inline size_t read_file_size(
const std::string& file) {
102 auto in = create_tdc_ifstream(file);
103 in.seekg(0, std::ios::end);
111 auto path = filename.c_str();
112 struct stat path_stat;
114 bool does_exist = (stat(path, &path_stat) == 0);
117 return does_exist && S_ISREG(path_stat.st_mode);
Contains the text compression and encoding framework.
bool file_exists(const std::string &filename)