#pragma once #include #include #include #include #include #include #include #include namespace bts::spartan { // TODO: // * consolidate 'file_utils.h/cpp' into this class; class FileUtils { public: static std::string file_name_from_path(const std::string &path); static std::string dir_from_path(const std::string &path); static std::string current_working_dir(); static bool file_exists(const std::string &name); static std::string change_file_ext( const std::string &path, const char *new_ext, std::uint16_t dot_count = 1); // If 'path' contains 'expected_ext' (which may contain multiple // dots, like 'pcap.xz), the entire extension will be replaced with // 'new_ext'. If require_expected_ext is false, the single dot extension will // be replaced with 'new_ext' in the event that 'path' does not contain // 'exected_ext'. static std::string change_file_ext( const std::string &path, const char *new_ext, const char *expected_ext, bool require_expected_ext = false); template static void write_to_bin(std::ostream &out, T value); static std::string read_file(const std::string &path); template static void read_from_bin(std::istream &in, T &value); template static T read_from_bin(std::istream &in); template static void read_from_file(std::vector &values, const std::string &path); // Opens the file in binary format, // then uses the operator to read consecutive values // from the file stream, and add them to the values vector. // returns a boolean indicating whether or not the file was successfully // opened. template static bool read_from_file( std::vector &values, const std::string &path, TOper op); template static bool read_from_file( std::vector &values, const std::string &path, TOper op, std::uint16_t version); template static bool read_from_file( const std::string &path, TOper op, std::uint16_t version); static std::string to_string(std::istream &istr); template static void write_to_file( Iterator first, Iterator last, const std::string &path, TOper op); template static void write_to_file( Iterator first, Iterator last, const std::string &path, TOper op, std::uint16_t version); template static void write_to_file(Iter first, Iter last, const std::string &path); }; ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// template inline void FileUtils::write_to_bin(std::ostream &out, T value) { out.write((char *)&value, sizeof(value)); } template inline void FileUtils::read_from_bin(std::istream &in, T &value) { in.read((char *)&value, sizeof(value)); } template inline T FileUtils::read_from_bin(std::istream &in) { T value; in.read((char *)&value, sizeof(value)); return value; } template inline void FileUtils::read_from_file( std::vector &values, const std::string &path) { std::ifstream istr(path, std::ios::binary); if (istr) { T item; while (istr >> item) values.push_back(item); } else { std::cout << "error opening file: " << path << std::endl; } } // Opens the file in binary format, // then uses the operator to read consecutive values // from the file stream, and add them to the values vector. // returns a boolean indicating whether or not the file was successfully opened. template inline bool FileUtils::read_from_file( std::vector &values, const std::string &path, TOper op) { std::ifstream istr(path, std::ios::binary); if (istr) { T item; while (op(istr, item)) values.push_back(item); return true; } return false; } template inline bool FileUtils::read_from_file( std::vector &values, const std::string &path, TOper op, std::uint16_t version) { std::ifstream istr(path, std::ios::binary); if (istr) { if (version != read_from_bin(istr)) return false; T item; while (op(istr, item)) values.push_back(item); return true; } return false; } template inline bool FileUtils::read_from_file( const std::string &path, TOper op, std::uint16_t version) { std::ifstream istr(path, std::ios::binary); if (istr) { if (version != read_from_bin(istr)) return false; while (op(istr)) { } return true; } return false; } template inline void FileUtils::write_to_file( Iterator first, Iterator last, const std::string &path, TOper op) { std::cout << "opening file '" << path << "'" << std::endl; std::ofstream o(path, std::ios::binary); if (!o) { std::cout << "unable to open file '" << path << "'" << std::endl; return; } while (first != last) { op(o, *first); first++; } } template inline void FileUtils::write_to_file( Iterator first, Iterator last, const std::string &path, TOper op, std::uint16_t version) { std::cout << "opening file '" << path << "'" << std::endl; std::ofstream o(path, std::ios::binary); if (!o) { std::cout << "unable to open file '" << path << "'" << std::endl; return; } write_to_bin(o, version); while (first != last) { op(o, *first); first++; } } template inline void FileUtils::write_to_file( Iter first, Iter last, const std::string &path) { std::ofstream o(path, std::ios::binary); if (!o) { std::cout << "unable to open file '" << path << "'" << std::endl; return; } while (first != last) { o << *first; first++; } } } // namespace bts::spartan