You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3mp/components/files/lowlevelfile.cpp

329 lines
6.8 KiB
C++

#include "lowlevelfile.hpp"
#include <stdexcept>
#include <sstream>
#include <cassert>
#if FILE_API == FILE_API_POSIX
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#endif
#if FILE_API == FILE_API_STDIO
/*
*
10 years ago
* Implementation of LowLevelFile methods using c stdio
*
*/
LowLevelFile::LowLevelFile ()
{
10 years ago
mHandle = NULL;
}
LowLevelFile::~LowLevelFile ()
{
10 years ago
if (mHandle != NULL)
fclose (mHandle);
}
void LowLevelFile::open (char const * filename)
{
10 years ago
assert (mHandle == NULL);
10 years ago
mHandle = fopen (filename, "rb");
10 years ago
if (mHandle == NULL)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading.";
throw std::runtime_error (os.str ());
}
}
void LowLevelFile::close ()
{
10 years ago
assert (mHandle != NULL);
10 years ago
fclose (mHandle);
10 years ago
mHandle = NULL;
}
size_t LowLevelFile::size ()
{
10 years ago
assert (mHandle != NULL);
10 years ago
long oldPosition = ftell (mHandle);
10 years ago
if (oldPosition == -1)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
if (fseek (mHandle, 0, SEEK_END) != 0)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
long Size = ftell (mHandle);
10 years ago
if (Size == -1)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
if (fseek (mHandle, oldPosition, SEEK_SET) != 0)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
return size_t (Size);
}
void LowLevelFile::seek (size_t Position)
{
10 years ago
assert (mHandle != NULL);
10 years ago
if (fseek (mHandle, Position, SEEK_SET) != 0)
throw std::runtime_error ("A seek operation on a file failed.");
}
size_t LowLevelFile::tell ()
{
10 years ago
assert (mHandle != NULL);
10 years ago
long Position = ftell (mHandle);
10 years ago
if (Position == -1)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
return size_t (Position);
}
size_t LowLevelFile::read (void * data, size_t size)
{
10 years ago
assert (mHandle != NULL);
10 years ago
int amount = fread (data, 1, size, mHandle);
10 years ago
if (amount == 0 && ferror (mHandle))
throw std::runtime_error ("A read operation on a file failed.");
10 years ago
return amount;
}
#elif FILE_API == FILE_API_POSIX
/*
*
10 years ago
* Implementation of LowLevelFile methods using posix IO calls
*
*/
LowLevelFile::LowLevelFile ()
{
10 years ago
mHandle = -1;
}
LowLevelFile::~LowLevelFile ()
{
10 years ago
if (mHandle != -1)
::close (mHandle);
}
void LowLevelFile::open (char const * filename)
{
10 years ago
assert (mHandle == -1);
#ifdef O_BINARY
10 years ago
static const int openFlags = O_RDONLY | O_BINARY;
#else
10 years ago
static const int openFlags = O_RDONLY;
#endif
10 years ago
mHandle = ::open (filename, openFlags, 0);
10 years ago
if (mHandle == -1)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading: " << strerror(errno);
10 years ago
throw std::runtime_error (os.str ());
}
}
void LowLevelFile::close ()
{
10 years ago
assert (mHandle != -1);
10 years ago
::close (mHandle);
10 years ago
mHandle = -1;
}
size_t LowLevelFile::size ()
{
10 years ago
assert (mHandle != -1);
10 years ago
size_t oldPosition = ::lseek (mHandle, 0, SEEK_CUR);
10 years ago
if (oldPosition == size_t (-1))
{
std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
10 years ago
size_t Size = ::lseek (mHandle, 0, SEEK_END);
10 years ago
if (Size == size_t (-1))
{
std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
10 years ago
if (lseek (mHandle, oldPosition, SEEK_SET) == -1)
{
std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
10 years ago
return Size;
}
void LowLevelFile::seek (size_t Position)
{
10 years ago
assert (mHandle != -1);
10 years ago
if (::lseek (mHandle, Position, SEEK_SET) == -1)
{
std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
}
size_t LowLevelFile::tell ()
{
10 years ago
assert (mHandle != -1);
10 years ago
size_t Position = ::lseek (mHandle, 0, SEEK_CUR);
10 years ago
if (Position == size_t (-1))
{
std::ostringstream os;
os << "An lseek() call failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
10 years ago
return Position;
}
size_t LowLevelFile::read (void * data, size_t size)
{
10 years ago
assert (mHandle != -1);
10 years ago
int amount = ::read (mHandle, data, size);
10 years ago
if (amount == -1)
{
std::ostringstream os;
os << "An attempt to read " << size << "bytes failed:" << strerror(errno);
throw std::runtime_error (os.str ());
}
10 years ago
return amount;
}
#elif FILE_API == FILE_API_WIN32
#include <boost/locale.hpp>
/*
*
10 years ago
* Implementation of LowLevelFile methods using Win32 API calls
*
*/
LowLevelFile::LowLevelFile ()
{
10 years ago
mHandle = INVALID_HANDLE_VALUE;
}
LowLevelFile::~LowLevelFile ()
{
10 years ago
if (mHandle != INVALID_HANDLE_VALUE)
CloseHandle (mHandle);
}
void LowLevelFile::open (char const * filename)
{
10 years ago
assert (mHandle == INVALID_HANDLE_VALUE);
10 years ago
std::wstring wname = boost::locale::conv::utf_to_utf<wchar_t>(filename);
HANDLE handle = CreateFileW (wname.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
10 years ago
if (handle == INVALID_HANDLE_VALUE)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading.";
throw std::runtime_error (os.str ());
}
10 years ago
mHandle = handle;
}
void LowLevelFile::close ()
{
10 years ago
assert (mHandle != INVALID_HANDLE_VALUE);
10 years ago
CloseHandle (mHandle);
10 years ago
mHandle = INVALID_HANDLE_VALUE;
}
size_t LowLevelFile::size ()
{
10 years ago
assert (mHandle != INVALID_HANDLE_VALUE);
10 years ago
BY_HANDLE_FILE_INFORMATION info;
10 years ago
if (!GetFileInformationByHandle (mHandle, &info))
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
if (info.nFileSizeHigh != 0)
throw std::runtime_error ("Files greater that 4GB are not supported.");
10 years ago
return info.nFileSizeLow;
}
void LowLevelFile::seek (size_t Position)
{
10 years ago
assert (mHandle != INVALID_HANDLE_VALUE);
10 years ago
if (SetFilePointer (mHandle, Position, NULL, SEEK_SET) == INVALID_SET_FILE_POINTER)
if (GetLastError () != NO_ERROR)
throw std::runtime_error ("A seek operation on a file failed.");
}
size_t LowLevelFile::tell ()
{
10 years ago
assert (mHandle != INVALID_HANDLE_VALUE);
10 years ago
DWORD value = SetFilePointer (mHandle, 0, NULL, SEEK_CUR);
10 years ago
if (value == INVALID_SET_FILE_POINTER && GetLastError () != NO_ERROR)
throw std::runtime_error ("A query operation on a file failed.");
10 years ago
return value;
}
size_t LowLevelFile::read (void * data, size_t size)
{
10 years ago
assert (mHandle != INVALID_HANDLE_VALUE);
10 years ago
DWORD read;
10 years ago
if (!ReadFile (mHandle, data, size, &read, NULL))
throw std::runtime_error ("A read operation on a file failed.");
10 years ago
return read;
}
#endif