Rework file error messages handling

pull/3028/head
Andrei Kortunov 4 years ago
parent 96e22bd44e
commit 4acd910b37

@ -4,6 +4,11 @@
#include <sstream> #include <sstream>
#include <cassert> #include <cassert>
#if FILE_API == FILE_API_STDIO
#include <errno.h>
#include <string.h>
#endif
#if FILE_API == FILE_API_POSIX #if FILE_API == FILE_API_POSIX
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -39,7 +44,7 @@ void LowLevelFile::open (char const * filename)
if (mHandle == nullptr) if (mHandle == nullptr)
{ {
std::ostringstream os; std::ostringstream os;
os << "Failed to open '" << filename << "' for reading."; os << "Failed to open '" << filename << "' for reading: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
} }
@ -58,42 +63,63 @@ size_t LowLevelFile::size ()
assert (mHandle != nullptr); assert (mHandle != nullptr);
long oldPosition = ftell (mHandle); long oldPosition = ftell (mHandle);
if (oldPosition == -1) if (oldPosition == -1)
throw std::runtime_error ("A query operation on a file failed."); {
std::ostringstream os;
os << "An ftell() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
if (fseek (mHandle, 0, SEEK_END) != 0) if (fseek (mHandle, 0, SEEK_END) != 0)
throw std::runtime_error ("A query operation on a file failed."); {
std::ostringstream os;
long Size = ftell (mHandle); os << "An fseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
if (Size == -1) long size = ftell (mHandle);
throw std::runtime_error ("A query operation on a file failed."); if (size == -1)
{
std::ostringstream os;
os << "An ftell() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
if (fseek (mHandle, oldPosition, SEEK_SET) != 0) if (fseek (mHandle, oldPosition, SEEK_SET) != 0)
throw std::runtime_error ("A query operation on a file failed."); {
std::ostringstream os;
os << "An fseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
return size_t (Size); return size_t (size);
} }
void LowLevelFile::seek (size_t Position) void LowLevelFile::seek (size_t position)
{ {
assert (mHandle != nullptr); assert (mHandle != nullptr);
if (fseek (mHandle, Position, SEEK_SET) != 0) if (fseek (mHandle, position, SEEK_SET) != 0)
throw std::runtime_error ("A seek operation on a file failed."); {
std::ostringstream os;
os << "An fseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
} }
size_t LowLevelFile::tell () size_t LowLevelFile::tell ()
{ {
assert (mHandle != nullptr); assert (mHandle != nullptr);
long Position = ftell (mHandle); long position = ftell (mHandle);
if (position == -1)
if (Position == -1) {
throw std::runtime_error ("A query operation on a file failed."); std::ostringstream os;
os << "An ftell() call failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
return size_t (Position); return size_t (position);
} }
size_t LowLevelFile::read (void * data, size_t size) size_t LowLevelFile::read (void * data, size_t size)
@ -103,7 +129,11 @@ size_t LowLevelFile::read (void * data, size_t size)
int amount = fread (data, 1, size, mHandle); int amount = fread (data, 1, size, mHandle);
if (amount == 0 && ferror (mHandle)) if (amount == 0 && ferror (mHandle))
throw std::runtime_error ("A read operation on a file failed."); {
std::ostringstream os;
os << "An attempt to read " << size << " bytes failed: " << strerror(errno);
throw std::runtime_error (os.str ());
}
return amount; return amount;
} }
@ -164,37 +194,37 @@ size_t LowLevelFile::size ()
if (oldPosition == size_t (-1)) if (oldPosition == size_t (-1))
{ {
std::ostringstream os; std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno); os << "An lseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
size_t Size = ::lseek (mHandle, 0, SEEK_END); size_t size = ::lseek (mHandle, 0, SEEK_END);
if (Size == size_t (-1)) if (size == size_t (-1))
{ {
std::ostringstream os; std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno); os << "An lseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
if (lseek (mHandle, oldPosition, SEEK_SET) == -1) if (lseek (mHandle, oldPosition, SEEK_SET) == -1)
{ {
std::ostringstream os; std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno); os << "An lseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
return Size; return size;
} }
void LowLevelFile::seek (size_t Position) void LowLevelFile::seek (size_t position)
{ {
assert (mHandle != -1); assert (mHandle != -1);
if (::lseek (mHandle, Position, SEEK_SET) == -1) if (::lseek (mHandle, position, SEEK_SET) == -1)
{ {
std::ostringstream os; std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno); os << "An lseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
} }
@ -203,16 +233,16 @@ size_t LowLevelFile::tell ()
{ {
assert (mHandle != -1); assert (mHandle != -1);
size_t Position = ::lseek (mHandle, 0, SEEK_CUR); size_t position = ::lseek (mHandle, 0, SEEK_CUR);
if (Position == size_t (-1)) if (position == size_t (-1))
{ {
std::ostringstream os; std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno); os << "An lseek() call failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
return Position; return position;
} }
size_t LowLevelFile::read (void * data, size_t size) size_t LowLevelFile::read (void * data, size_t size)
@ -224,7 +254,7 @@ size_t LowLevelFile::read (void * data, size_t size)
if (amount == -1) if (amount == -1)
{ {
std::ostringstream os; std::ostringstream os;
os << "An attempt to read " << size << "bytes failed:" << strerror(errno); os << "An attempt to read " << size << " bytes failed: " << strerror(errno);
throw std::runtime_error (os.str ()); throw std::runtime_error (os.str ());
} }
@ -292,11 +322,11 @@ size_t LowLevelFile::size ()
return info.nFileSizeLow; return info.nFileSizeLow;
} }
void LowLevelFile::seek (size_t Position) void LowLevelFile::seek (size_t position)
{ {
assert (mHandle != INVALID_HANDLE_VALUE); assert (mHandle != INVALID_HANDLE_VALUE);
if (SetFilePointer (mHandle, Position, nullptr, SEEK_SET) == INVALID_SET_FILE_POINTER) if (SetFilePointer (mHandle, position, nullptr, SEEK_SET) == INVALID_SET_FILE_POINTER)
if (GetLastError () != NO_ERROR) if (GetLastError () != NO_ERROR)
throw std::runtime_error ("A seek operation on a file failed."); throw std::runtime_error ("A seek operation on a file failed.");
} }

Loading…
Cancel
Save