tudocomp
– The TU Dortmund Compression Framework
IOUtil.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fstream>
4 #include <iostream>
5 #include <string>
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 
11 namespace tdc {
12 namespace io {
13 
15 
16 // TODO: Error handling
17 
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");
20 }
21 
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");
24 }
25 
26 template<class T>
27 T read_bytes(std::istream& inp, size_t bytes = sizeof(T)) {
28  char c;
29  T ret = 0;
30  for(size_t i = 0; i < bytes; i++) {
31  if (!inp.get(c)) {
32  break;
33  }
34  ret <<= 8;
35  ret |= T((unsigned char)c);
36  }
37  return ret;
38 }
39 
40 template<class T>
41 void read_bytes_to_vec(std::istream& inp, T& vec, size_t bytes) {
42  char c;
43  for(size_t i = 0; i < bytes; i++) {
44  if (!inp.get(c)) {
45  break;
46  }
47  vec.push_back((unsigned char)c);
48  }
49 }
50 
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);
53  if (bool(in)) {
54  in.seekg(offset, std::ios::beg);
55  return in;
56  }
57  throw tdc_input_file_not_found_error(filename);
58 }
59 
60 template<class T>
61 T read_file_to_stl_byte_container(const std::string& filename,
62  size_t offset = 0,
63  size_t len = -1) {
64  auto in = create_tdc_ifstream(filename, offset);
65  T contents;
66 
67  // if needed, determine length from offset to end of file
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();
73  len = end - start;
74  }
75 
76  // use length to allocate a single buffer for the file
77  contents.reserve(len);
78  contents.resize(len);
79 
80  // set position back to the start position at offset
81  in.seekg(offset, std::ios::beg);
82 
83  // read file into contents without reallocating
84  in.read((char*)&contents[0], contents.size());
85  in.close();
86 
87  return(contents);
88 }
89 
90 template<class T, class S>
91 T read_stream_to_stl_byte_container(S& stream) {
92  T vector;
93  char c;
94  while (stream.get(c)) {
95  vector.push_back(typename T::value_type(c));
96  }
97  return(vector);
98 }
99 
100 inline size_t read_file_size(const std::string& file) {
101  // TODO: Maybe replace by calling stat
102  auto in = create_tdc_ifstream(file);
103  in.seekg(0, std::ios::end);
104 
105  return in.tellg();
106 }
107 
109 
110 inline bool file_exists(const std::string& filename) {
111  auto path = filename.c_str();
112  struct stat path_stat;
113 
114  bool does_exist = (stat(path, &path_stat) == 0);
115 
116  // Check if it exists, and if its also a regular file
117  return does_exist && S_ISREG(path_stat.st_mode);
118 }
119 
120 }
121 }
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
len_compact_t len
Definition: LZSSFactors.hpp:38
bool file_exists(const std::string &filename)
Definition: IOUtil.hpp:110