mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-04-30 10:20:03 +02:00

- Consistent use of ssize_t to avoid issues on 32 bit platforms and also mac with (long long int as ssize_t)
45 lines
1.0 KiB
C++
45 lines
1.0 KiB
C++
|
|
#include "aare/FilePtr.hpp"
|
|
#include <fmt/format.h>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
namespace aare {
|
|
|
|
FilePtr::FilePtr(const std::filesystem::path& fname, const std::string& mode = "rb") {
|
|
fp_ = fopen(fname.c_str(), mode.c_str());
|
|
if (!fp_)
|
|
throw std::runtime_error(fmt::format("Could not open: {}", fname.c_str()));
|
|
}
|
|
|
|
FilePtr::FilePtr(FilePtr &&other) { std::swap(fp_, other.fp_); }
|
|
|
|
FilePtr &FilePtr::operator=(FilePtr &&other) {
|
|
std::swap(fp_, other.fp_);
|
|
return *this;
|
|
}
|
|
|
|
FILE *FilePtr::get() { return fp_; }
|
|
|
|
ssize_t FilePtr::tell() {
|
|
auto pos = ftell(fp_);
|
|
if (pos == -1)
|
|
throw std::runtime_error(fmt::format("Error getting file position: {}", error_msg()));
|
|
return pos;
|
|
}
|
|
FilePtr::~FilePtr() {
|
|
if (fp_)
|
|
fclose(fp_); // check?
|
|
}
|
|
|
|
std::string FilePtr::error_msg(){
|
|
if (feof(fp_)) {
|
|
return "End of file reached";
|
|
}
|
|
if (ferror(fp_)) {
|
|
return fmt::format("Error reading file: {}", std::strerror(errno));
|
|
}
|
|
return "";
|
|
}
|
|
} // namespace aare
|