2016-03-04 15:00:12 -08:00
|
|
|
#include "athena/FileWriter.hpp"
|
2014-12-28 20:46:43 -08:00
|
|
|
|
2016-10-27 16:44:50 -07:00
|
|
|
#if __APPLE__ || __FreeBSD__
|
2015-04-04 17:35:32 -07:00
|
|
|
#include "osx_largefilewrapper.h"
|
2018-09-20 10:47:48 -07:00
|
|
|
#elif GEKKO || __SWITCH__
|
2015-10-16 01:07:54 -07:00
|
|
|
#include "gekko_support.h"
|
|
|
|
#include "osx_largefilewrapper.h"
|
2014-12-28 20:46:43 -08:00
|
|
|
#endif
|
|
|
|
|
2018-09-23 19:12:29 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-12-28 23:55:42 -08:00
|
|
|
namespace athena::io
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
2017-11-12 22:12:37 -08:00
|
|
|
FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr)
|
2015-11-13 21:53:13 -08:00
|
|
|
: m_fileHandle(NULL),
|
2015-11-22 19:08:48 -08:00
|
|
|
m_globalErr(globalErr)
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
2017-01-26 18:21:43 -08:00
|
|
|
#if _WIN32
|
2017-01-26 18:27:41 -08:00
|
|
|
m_filename = utility::utf8ToWide(filename);
|
2017-01-26 18:21:43 -08:00
|
|
|
#else
|
2015-11-13 21:59:17 -08:00
|
|
|
m_filename = filename;
|
2017-01-26 18:21:43 -08:00
|
|
|
#endif
|
2015-05-18 14:03:13 -07:00
|
|
|
open(overwrite);
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
|
2017-11-12 22:12:37 -08:00
|
|
|
FileWriter::FileWriter(std::wstring_view filename, bool overwrite, bool globalErr)
|
2015-11-13 21:53:13 -08:00
|
|
|
: m_fileHandle(NULL),
|
2015-11-22 19:08:48 -08:00
|
|
|
m_globalErr(globalErr)
|
2015-07-22 11:59:40 -07:00
|
|
|
{
|
2017-01-26 18:21:43 -08:00
|
|
|
#if _WIN32
|
|
|
|
m_filename = filename;
|
|
|
|
#else
|
2015-11-13 21:53:13 -08:00
|
|
|
m_filename = utility::wideToUtf8(filename);
|
2017-01-26 18:21:43 -08:00
|
|
|
#endif
|
2015-07-22 11:59:40 -07:00
|
|
|
open(overwrite);
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:14:15 -07:00
|
|
|
FileWriter::~FileWriter()
|
|
|
|
{
|
|
|
|
if (isOpen())
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:03:13 -07:00
|
|
|
void FileWriter::open(bool overwrite)
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
2015-05-18 14:03:13 -07:00
|
|
|
if (overwrite)
|
2017-11-25 19:02:50 -08:00
|
|
|
{
|
|
|
|
std::string tmpFilename = m_filename + '~';
|
|
|
|
m_fileHandle = fopen(tmpFilename.c_str(), "w+b");
|
|
|
|
}
|
2015-05-18 14:03:13 -07:00
|
|
|
else
|
2016-09-17 14:32:09 -07:00
|
|
|
{
|
|
|
|
m_fileHandle = fopen(m_filename.c_str(), "a+b");
|
|
|
|
if (m_fileHandle)
|
|
|
|
{
|
|
|
|
fclose(m_fileHandle);
|
|
|
|
m_fileHandle = fopen(m_filename.c_str(), "r+b");
|
|
|
|
}
|
|
|
|
}
|
2015-07-22 11:59:40 -07:00
|
|
|
|
2014-04-20 02:14:15 -07:00
|
|
|
if (!m_fileHandle)
|
2015-07-21 22:37:22 -07:00
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("Unable to open file '%s'", filename().c_str());
|
2015-07-22 13:40:22 -07:00
|
|
|
setError();
|
2015-07-21 22:37:22 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-04-20 02:14:15 -07:00
|
|
|
|
2015-11-12 18:10:50 -08:00
|
|
|
// reset error
|
|
|
|
m_hasError = false;
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileWriter::close()
|
|
|
|
{
|
|
|
|
if (!m_fileHandle)
|
2015-07-21 22:37:22 -07:00
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("Cannot close an unopened stream");
|
2015-07-22 13:40:22 -07:00
|
|
|
setError();
|
2015-07-21 22:37:22 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-04-20 02:14:15 -07:00
|
|
|
|
|
|
|
fclose(m_fileHandle);
|
|
|
|
m_fileHandle = NULL;
|
2017-11-25 19:02:50 -08:00
|
|
|
|
|
|
|
std::string tmpFilename = m_filename + '~';
|
2018-09-23 19:12:29 -07:00
|
|
|
#ifdef __SWITCH__
|
|
|
|
/* Due to Horizon not being a fully POSIX compatible OS, we need to make sure the file *does not* exist before attempting to rename */
|
|
|
|
unlink(m_filename.c_str());
|
|
|
|
#endif
|
2017-11-25 19:02:50 -08:00
|
|
|
rename(tmpFilename.c_str(), m_filename.c_str());
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
|
2014-06-17 21:51:18 -07:00
|
|
|
void FileWriter::seek(atInt64 pos, SeekOrigin origin)
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
2015-11-14 00:47:05 -08:00
|
|
|
if (!isOpen())
|
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("Unable to seek in file, not open");
|
2015-11-14 00:47:05 -08:00
|
|
|
setError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:14:15 -07:00
|
|
|
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
|
2015-07-22 13:40:22 -07:00
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("Unable to seek in file");
|
2015-07-22 13:40:22 -07:00
|
|
|
setError();
|
|
|
|
}
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
|
2014-06-17 21:51:18 -07:00
|
|
|
atUint64 FileWriter::position() const
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
|
|
|
return ftello64(m_fileHandle);
|
|
|
|
}
|
|
|
|
|
2014-06-17 21:51:18 -07:00
|
|
|
atUint64 FileWriter::length() const
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
2014-12-28 14:38:55 -08:00
|
|
|
return utility::fileSize(m_filename);
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
|
2015-06-30 20:01:04 -07:00
|
|
|
void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
|
2014-04-20 02:14:15 -07:00
|
|
|
{
|
|
|
|
if (!isOpen())
|
2015-07-21 22:37:22 -07:00
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("File not open for writing");
|
2015-07-22 13:40:22 -07:00
|
|
|
setError();
|
2015-07-21 22:37:22 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-04-20 02:14:15 -07:00
|
|
|
|
|
|
|
if (fwrite(data, 1, len, m_fileHandle) != len)
|
2015-07-22 13:40:22 -07:00
|
|
|
{
|
2015-11-22 19:08:48 -08:00
|
|
|
if (m_globalErr)
|
|
|
|
atError("Unable to write to stream");
|
2015-07-22 13:40:22 -07:00
|
|
|
setError();
|
|
|
|
}
|
2014-04-20 02:14:15 -07:00
|
|
|
}
|
|
|
|
} // Athena
|