10 #ifndef META_FILESYSTEM_H_
11 #define META_FILESYSTEM_H_
31 inline void delete_file(
const std::string&
filename)
33 remove(filename.c_str());
41 inline void rename_file(
const std::string& old_name,
42 const std::string& new_name)
44 rename(old_name.c_str(), new_name.c_str());
52 inline bool make_directory(
const std::string& dir_name)
54 return mkdir(dir_name.c_str(), 0755) == -1;
61 inline bool file_exists(
const std::string& filename)
63 FILE* f = fopen(filename.c_str(),
"r");
77 inline uint64_t file_size(
const std::string& filename)
79 if (!file_exists(filename))
85 stat(filename.c_str(), &st);
88 stat64(filename.c_str(), &st);
99 inline bool copy_file(
const std::string& source,
const std::string& dest)
101 if (!file_exists(source))
105 auto size = file_size(source);
106 uint64_t max_size = 1024UL * 1024UL * 1024UL * 128UL;
109 printing::progress prog{
"Copying file ", size};
110 std::ifstream source_file{source};
111 std::ofstream dest_file{dest};
112 uint64_t buf_size = 1024UL * 1024UL * 32UL;
113 uint64_t total_processed = 0;
114 std::vector<char> buffer(buf_size);
117 source_file.read(buffer.data(), buf_size);
118 auto processed = source_file.gcount();
119 total_processed += processed;
120 dest_file.write(buffer.data(), total_processed);
128 std::ifstream source_file{source, std::ios::binary};
129 std::ofstream dest_file{dest, std::ios::binary};
130 dest_file << source_file.rdbuf();
140 inline std::string file_text(
const std::string& in_name)
142 std::ifstream infile{in_name};
143 std::ostringstream buf;
144 buf << infile.rdbuf();
154 inline uint64_t num_lines(
const std::string& filename,
char delimiter =
'\n')
156 io::mmap_file file{filename};
159 printing::progress progress{
" > Counting lines in file: ", file.size(), 500,
161 for (uint64_t idx = 0; idx < file.size(); ++idx)
164 if (file[idx] == delimiter)