Electroneum
epee::file_io_utils Namespace Reference

Functions

bool is_file_exist (const std::string &path)
 
bool save_string_to_file (const std::string &path_to_file, const std::string &str)
 
bool get_file_time (const std::string &path_to_file, time_t &ft)
 
bool set_file_time (const std::string &path_to_file, const time_t &ft)
 
bool load_file_to_string (const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
 
bool append_string_to_file (const std::string &path_to_file, const std::string &str)
 
bool get_file_size (const std::string &path_to_file, uint64_t &size)
 

Function Documentation

◆ append_string_to_file()

bool epee::file_io_utils::append_string_to_file ( const std::string &  path_to_file,
const std::string &  str 
)
inline

Definition at line 179 of file file_io_utils.h.

180  {
181  // No special Windows implementation because so far not used in Electroneum code
182  try
183  {
184  std::ofstream fstream;
185  fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
186  fstream.open(path_to_file.c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::app);
187  fstream << str;
188  fstream.close();
189  return true;
190  }
191 
192  catch(...)
193  {
194  return false;
195  }
196  }

◆ get_file_size()

bool epee::file_io_utils::get_file_size ( const std::string &  path_to_file,
uint64_t size 
)
inline

Definition at line 199 of file file_io_utils.h.

200  {
201 #ifdef WIN32
202  std::wstring wide_path;
203  try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
204  HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
205  if (file_handle == INVALID_HANDLE_VALUE)
206  return false;
207  LARGE_INTEGER file_size;
208  BOOL result = GetFileSizeEx(file_handle, &file_size);
209  CloseHandle(file_handle);
210  if (result) {
211  size = file_size.QuadPart;
212  }
213  return size;
214 #else
215  try
216  {
217  std::ifstream fstream;
218  fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
219  fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
220  size = fstream.tellg();
221  fstream.close();
222  return true;
223  }
224 
225  catch(...)
226  {
227  return false;
228  }
229 #endif
230  }
::std::wstring wstring
Definition: gtest-port.h:1103

◆ get_file_time()

bool epee::file_io_utils::get_file_time ( const std::string &  path_to_file,
time_t &  ft 
)
inline

Definition at line 107 of file file_io_utils.h.

108  {
109  boost::system::error_code ec;
110  ft = boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ec);
111  if(!ec)
112  return true;
113  else
114  return false;
115  }

◆ is_file_exist()

bool epee::file_io_utils::is_file_exist ( const std::string &  path)
inline

Definition at line 66 of file file_io_utils.h.

67  {
68  boost::filesystem::path p(path);
69  return boost::filesystem::exists(p);
70  }
Here is the caller graph for this function:

◆ load_file_to_string()

bool epee::file_io_utils::load_file_to_string ( const std::string &  path_to_file,
std::string &  target_str,
size_t  max_size = 1000000000 
)
inline

Definition at line 130 of file file_io_utils.h.

131  {
132 #ifdef WIN32
133  std::wstring wide_path;
134  try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
135  HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
136  if (file_handle == INVALID_HANDLE_VALUE)
137  return false;
138  DWORD file_size = GetFileSize(file_handle, NULL);
139  if ((file_size == INVALID_FILE_SIZE) || (uint64_t)file_size > (uint64_t)max_size) {
140  CloseHandle(file_handle);
141  return false;
142  }
143  target_str.resize(file_size);
144  DWORD bytes_read;
145  BOOL result = ReadFile(file_handle, &target_str[0], file_size, &bytes_read, NULL);
146  CloseHandle(file_handle);
147  if (bytes_read != file_size)
148  result = FALSE;
149  return result;
150 #else
151  try
152  {
153  std::ifstream fstream;
154  fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
155  fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
156 
157  std::ifstream::pos_type file_size = fstream.tellg();
158 
159  if((uint64_t)file_size > (uint64_t)max_size) // ensure a large domain for comparison, and negative -> too large
160  return false;//don't go crazy
161  size_t file_size_t = static_cast<size_t>(file_size);
162 
163  target_str.resize(file_size_t);
164 
165  fstream.seekg (0, std::ios::beg);
166  fstream.read((char*)target_str.data(), target_str.size());
167  fstream.close();
168  return true;
169  }
170 
171  catch(...)
172  {
173  return false;
174  }
175 #endif
176  }
GTEST_API_ size_t GetFileSize(FILE *file)
::std::wstring wstring
Definition: gtest-port.h:1103
unsigned __int64 uint64_t
Definition: stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_string_to_file()

bool epee::file_io_utils::save_string_to_file ( const std::string &  path_to_file,
const std::string &  str 
)
inline

Definition at line 73 of file file_io_utils.h.

74  {
75 #ifdef WIN32
76  std::wstring wide_path;
77  try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
78  HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
79  if (file_handle == INVALID_HANDLE_VALUE)
80  return false;
81  DWORD bytes_written;
82  DWORD bytes_to_write = (DWORD)str.size();
83  BOOL result = WriteFile(file_handle, str.data(), bytes_to_write, &bytes_written, NULL);
84  CloseHandle(file_handle);
85  if (bytes_written != bytes_to_write)
86  result = FALSE;
87  return result;
88 #else
89  try
90  {
91  std::ofstream fstream;
92  fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
93  fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
94  fstream << str;
95  fstream.close();
96  return true;
97  }
98 
99  catch(...)
100  {
101  return false;
102  }
103 #endif
104  }
::std::wstring wstring
Definition: gtest-port.h:1103
Here is the caller graph for this function:

◆ set_file_time()

bool epee::file_io_utils::set_file_time ( const std::string &  path_to_file,
const time_t &  ft 
)
inline

Definition at line 118 of file file_io_utils.h.

119  {
120  boost::system::error_code ec;
121  boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ft, ec);
122  if(!ec)
123  return true;
124  else
125  return false;
126  }