| |
| |
| #include "tfilepath_io.h" |
| #include "tconvert.h" |
| |
| #include <fcntl.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| |
| #include <fstream> |
| #include <iostream> |
| |
| using namespace std; |
| #ifdef _WIN32 |
| |
| #include <io.h> |
| #include <windows.h> |
| |
| |
| |
| |
| |
| |
| |
| |
| FILE *fopen(const TFilePath &fp, string mode) { |
| FILE *pFile; |
| errno_t err = |
| _wfopen_s(&pFile, fp.getWideString().c_str(), ::to_wstring(mode).c_str()); |
| if (err == -1) return NULL; |
| return pFile; |
| } |
| |
| Tifstream::Tifstream(const TFilePath &fp) |
| : ifstream(m_file = fopen(fp, "rb")) {} |
| |
| Tifstream::~Tifstream() { |
| if (m_file) { |
| int ret = fclose(m_file); |
| assert(ret == 0); |
| } |
| } |
| |
| void Tifstream::close() { |
| m_file = 0; |
| std::ifstream::close(); |
| } |
| |
| Tofstream::Tofstream(const TFilePath &fp, bool append_existing) |
| : ofstream(m_file = fopen(fp, append_existing ? "ab" : "wb")) {} |
| |
| Tofstream::~Tofstream() { |
| if (m_file) { |
| flush(); |
| int ret = fclose(m_file); |
| assert(ret == 0); |
| } |
| } |
| |
| void Tofstream::close() { |
| m_file = 0; |
| std::ofstream::close(); |
| } |
| |
| bool Tifstream::isOpen() const { return m_file != 0; } |
| |
| bool Tofstream::isOpen() const { return m_file != 0; } |
| |
| #else |
| |
| |
| |
| |
| |
| |
| |
| FILE *fopen(const TFilePath &fp, string mode) { |
| return fopen(QString::fromStdWString(fp.getWideString()).toUtf8().data(), |
| mode.c_str()); |
| } |
| |
| Tifstream::Tifstream(const TFilePath &fp) |
| : ifstream(QString::fromStdWString(fp.getWideString()).toUtf8().data(), |
| ios::binary) |
| |
| |
| |
| |
| |
| {} |
| |
| Tifstream::~Tifstream() { |
| #ifdef _WIN32 |
| fclose(m_file); |
| #endif |
| } |
| |
| Tofstream::Tofstream(const TFilePath &fp, bool append_existing) |
| : ofstream( |
| QString::fromStdWString(fp.getWideString()).toUtf8().data(), |
| ios::binary | (append_existing ? ios_base::app : ios_base::trunc)) {} |
| |
| Tofstream::~Tofstream() {} |
| |
| void Tofstream::close() {} |
| |
| bool Tifstream::isOpen() const { |
| |
| return true; |
| } |
| |
| bool Tofstream::isOpen() const { |
| |
| return true; |
| } |
| |
| #endif |
| |