28 #ifndef _FILE_IO_UTILS_H_ 29 #define _FILE_IO_UTILS_H_ 32 #include <boost/filesystem/path.hpp> 33 #include <boost/filesystem/operations.hpp> 63 namespace file_io_utils
68 boost::filesystem::path p(path);
69 return boost::filesystem::exists(p);
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)
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)
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);
109 boost::system::error_code ec;
110 ft = boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ec);
120 boost::system::error_code ec;
121 boost::filesystem::last_write_time(boost::filesystem::path(path_to_file), ft, ec);
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)
139 if ((file_size == INVALID_FILE_SIZE) || (
uint64_t)file_size > (
uint64_t)max_size) {
140 CloseHandle(file_handle);
143 target_str.resize(file_size);
145 BOOL result = ReadFile(file_handle, &target_str[0], file_size, &bytes_read, NULL);
146 CloseHandle(file_handle);
147 if (bytes_read != file_size)
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);
157 std::ifstream::pos_type file_size = fstream.tellg();
161 size_t file_size_t =
static_cast<size_t>(file_size);
163 target_str.resize(file_size_t);
165 fstream.seekg (0, std::ios::beg);
166 fstream.read((
char*)target_str.data(), target_str.size());
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);
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)
207 LARGE_INTEGER file_size;
208 BOOL result = GetFileSizeEx(file_handle, &file_size);
209 CloseHandle(file_handle);
211 size = file_size.QuadPart;
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();
235 #endif //_FILE_IO_UTILS_H_
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
bool is_file_exist(const std::string &path)
GTEST_API_ size_t GetFileSize(FILE *file)
bool append_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)
unsigned __int64 uint64_t
bool save_string_to_file(const std::string &path_to_file, const std::string &str)
bool set_file_time(const std::string &path_to_file, const time_t &ft)
bool get_file_size(const std::string &path_to_file, uint64_t &size)