Not sure how this slipped through the cracks, but I'm guessing the build broke when we upgraded the cpp compilers (to clang 17 and gcc 13). * Include <cstdint> in a few places * Suppress -Wstringop-overflow in sparkd-gcc-release build * Change const char * param to explicit std::string for proper type deduction in call to 'std::make_tuple'. For some reason, the gcc build was failing with an array-bounds werror (changing the arg fixed it).
218 lines
6.0 KiB
C++
218 lines
6.0 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <stdio.h>
|
|
#include <string>
|
|
#include <sys/stat.h>
|
|
#include <vector>
|
|
|
|
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 <class T>
|
|
static void write_to_bin(std::ostream &out, T value);
|
|
|
|
static std::string read_file(const std::string &path);
|
|
|
|
template <class T>
|
|
static void read_from_bin(std::istream &in, T &value);
|
|
|
|
template <class T>
|
|
static T read_from_bin(std::istream &in);
|
|
|
|
template <class T>
|
|
static void read_from_file(std::vector<T> &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 <class T, class TOper>
|
|
static bool read_from_file(
|
|
std::vector<T> &values, const std::string &path, TOper op);
|
|
|
|
template <class T, class TOper>
|
|
static bool read_from_file(
|
|
std::vector<T> &values,
|
|
const std::string &path,
|
|
TOper op,
|
|
std::uint16_t version);
|
|
|
|
template <class TOper>
|
|
static bool read_from_file(
|
|
const std::string &path, TOper op, std::uint16_t version);
|
|
|
|
static std::string to_string(std::istream &istr);
|
|
|
|
template <class Iterator, class TOper>
|
|
static void write_to_file(
|
|
Iterator first, Iterator last, const std::string &path, TOper op);
|
|
|
|
template <class Iterator, class TOper>
|
|
static void write_to_file(
|
|
Iterator first,
|
|
Iterator last,
|
|
const std::string &path,
|
|
TOper op,
|
|
std::uint16_t version);
|
|
|
|
template <class Iter>
|
|
static void write_to_file(Iter first, Iter last, const std::string &path);
|
|
};
|
|
|
|
//////////////////////////////////////////////////
|
|
// Definition
|
|
//////////////////////////////////////////////////
|
|
|
|
template <class T>
|
|
inline void FileUtils::write_to_bin(std::ostream &out, T value) {
|
|
out.write((char *)&value, sizeof(value));
|
|
}
|
|
|
|
template <class T>
|
|
inline void FileUtils::read_from_bin(std::istream &in, T &value) {
|
|
in.read((char *)&value, sizeof(value));
|
|
}
|
|
|
|
template <class T>
|
|
inline T FileUtils::read_from_bin(std::istream &in) {
|
|
T value;
|
|
in.read((char *)&value, sizeof(value));
|
|
return value;
|
|
}
|
|
|
|
template <class T>
|
|
inline void FileUtils::read_from_file(
|
|
std::vector<T> &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 <class T, class TOper>
|
|
inline bool FileUtils::read_from_file(
|
|
std::vector<T> &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 <class T, class TOper>
|
|
inline bool FileUtils::read_from_file(
|
|
std::vector<T> &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<std::uint16_t>(istr))
|
|
return false;
|
|
T item;
|
|
while (op(istr, item))
|
|
values.push_back(item);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template <class TOper>
|
|
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<std::uint16_t>(istr))
|
|
return false;
|
|
while (op(istr)) {
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template <class Iterator, class TOper>
|
|
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 <class Iterator, class TOper>
|
|
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 <class Iter>
|
|
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
|