tahoma2d/toonz/sources/common/tsystem/tfilepath_io.cpp

118 lines
2.5 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#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;
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#include <io.h>
#include <windows.h>
/*!Return a pointer to a \b FILE, if exist. The file is opened using _wfopen_s,
2016-06-15 18:43:10 +12:00
documentation \b
http://msdn2.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx.
\b fp is file path, \b mode is the way open, read ("r"), write ("w"),
... , to
know mode view _wfopen_s documentation.
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
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;
2016-03-19 06:57:51 +13:00
}
Tifstream::Tifstream(const TFilePath &fp)
2016-06-15 18:43:10 +12:00
: ifstream(m_file = fopen(fp, "rb")) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
Tifstream::~Tifstream() {
if (m_file) {
int ret = fclose(m_file);
assert(ret == 0);
}
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void Tifstream::close() {
m_file = 0;
std::ifstream::close();
2016-03-19 06:57:51 +13:00
}
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
2016-06-15 18:43:10 +12:00
: ofstream(m_file = fopen(fp, append_existing ? "ab" : "wb")) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
Tofstream::~Tofstream() {
if (m_file) {
flush();
int ret = fclose(m_file);
assert(ret == 0);
}
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void Tofstream::close() {
m_file = 0;
std::ofstream::close();
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
bool Tifstream::isOpen() const { return m_file != 0; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool Tofstream::isOpen() const { return m_file != 0; }
2016-03-19 06:57:51 +13:00
#else
//======================
//
// Versione non windows
//
//======================
2016-06-15 18:43:10 +12:00
FILE *fopen(const TFilePath &fp, string mode) {
return fopen(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
mode.c_str());
2016-03-19 06:57:51 +13:00
}
Tifstream::Tifstream(const TFilePath &fp)
2016-06-15 18:43:10 +12:00
: ifstream(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
ios::binary)
/*: ifstream(openFileForReading(fp), ios::binary)
NO! Questo costruttore non e' standard, anche se e' presente
2016-03-19 06:57:51 +13:00
in molte versioni. Nel MAC non c'e e fa un cast a char*
sperando che sia il nome del file => compila ma non funziona
*/
2016-06-15 18:43:10 +12:00
{}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
Tifstream::~Tifstream() {
#ifdef _WIN32
2016-06-17 22:51:40 +12:00
fclose(m_file);
2016-03-19 06:57:51 +13:00
#endif
}
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
2016-06-15 18:43:10 +12:00
: ofstream(
QString::fromStdWString(fp.getWideString()).toUtf8().data(),
ios::binary | (append_existing ? ios_base::app : ios_base::trunc)) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
Tofstream::~Tofstream() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void Tofstream::close() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool Tifstream::isOpen() const {
// TODO
return true;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
bool Tofstream::isOpen() const {
// TODO
return true;
2016-03-19 06:57:51 +13:00
}
#endif