refactor: Modernize AutoFile

* Add m_ prefix to the std::FILE member variable
* Add std namespace where possible, to avoid confusion with member
  functions of the same name.
* Add AutoFile::feof() member function, to be used in place of
  std::feof(AutoFile::Get())
* Simplify fclose() in terms of release()
* Fix typo in the error message in the ignore member function.
This commit is contained in:
MarcoFalke 2023-07-12 09:40:24 +02:00
parent fa8d227d58
commit fa7724bc9d
No known key found for this signature in database

View File

@ -480,73 +480,74 @@ public:
class AutoFile
{
protected:
FILE* file;
std::FILE* m_file;
public:
explicit AutoFile(FILE* filenew) : file{filenew} {}
explicit AutoFile(std::FILE* file) : m_file{file} {}
~AutoFile()
{
fclose();
}
~AutoFile() { fclose(); }
// Disallow copies
AutoFile(const AutoFile&) = delete;
AutoFile& operator=(const AutoFile&) = delete;
bool feof() const { return std::feof(m_file); }
int fclose()
{
int retval{0};
if (file) {
retval = ::fclose(file);
file = nullptr;
}
return retval;
if (auto rel{release()}) return std::fclose(rel);
return 0;
}
/** Get wrapped FILE* with transfer of ownership.
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
* of this function to clean up the returned FILE*.
*/
FILE* release() { FILE* ret = file; file = nullptr; return ret; }
std::FILE* release()
{
std::FILE* ret{m_file};
m_file = nullptr;
return ret;
}
/** Get wrapped FILE* without transfer of ownership.
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
* AutoFile outlives use of the passed pointer.
*/
FILE* Get() const { return file; }
std::FILE* Get() const { return m_file; }
/** Return true if the wrapped FILE* is nullptr, false otherwise.
*/
bool IsNull() const { return (file == nullptr); }
bool IsNull() const { return m_file == nullptr; }
//
// Stream subset
//
void read(Span<std::byte> dst)
{
if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
if (std::fread(dst.data(), 1, dst.size(), m_file) != dst.size()) {
throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
}
}
void ignore(size_t nSize)
{
if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
unsigned char data[4096];
while (nSize > 0) {
size_t nNow = std::min<size_t>(nSize, sizeof(data));
if (fread(data, 1, nNow, file) != nNow)
throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
if (std::fread(data, 1, nNow, m_file) != nNow) {
throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");
}
nSize -= nNow;
}
}
void write(Span<const std::byte> src)
{
if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure("AutoFile::write: write failed");
}
}