51 lines
1012 B
C++
51 lines
1012 B
C++
|
|
#include "Core/FileWriter.h"
|
||
|
|
#include <cstring>
|
||
|
|
|
||
|
|
namespace XCEngine {
|
||
|
|
namespace Core {
|
||
|
|
|
||
|
|
FileWriter::FileWriter() = default;
|
||
|
|
|
||
|
|
FileWriter::FileWriter(const char* filePath, bool append) {
|
||
|
|
Open(filePath, append);
|
||
|
|
}
|
||
|
|
|
||
|
|
FileWriter::~FileWriter() {
|
||
|
|
Close();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FileWriter::Open(const char* filePath, bool append) {
|
||
|
|
Close();
|
||
|
|
const char* mode = append ? "ab" : "wb";
|
||
|
|
m_file = std::fopen(filePath, mode);
|
||
|
|
return m_file != nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
void FileWriter::Close() {
|
||
|
|
if (m_file) {
|
||
|
|
std::fclose(m_file);
|
||
|
|
m_file = nullptr;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FileWriter::Write(const char* data, size_t length) {
|
||
|
|
if (!m_file || !data || length == 0) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return std::fwrite(data, 1, length, m_file) == length;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FileWriter::Write(const Containers::String& str) {
|
||
|
|
return Write(str.CStr(), str.Length());
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FileWriter::Flush() {
|
||
|
|
if (!m_file) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return std::fflush(m_file) == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace Core
|
||
|
|
} // namespace XCEngine
|