Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <fcntl.h></fcntl.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys stat.h=""></sys>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <fstream></fstream>
Toshihiro Shimizu 890ddd
#include <iostream></iostream>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <io.h></io.h>
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!Return a pointer to a \b FILE, if exist. The file is opened using _wfopen_s,
Shinya Kitaoka 120a6e
   documentation \b
Shinya Kitaoka 120a6e
   http://msdn2.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx.
Shinya Kitaoka 120a6e
         \b fp is file path, \b mode is the way open, read ("r"), write ("w"),
Shinya Kitaoka 120a6e
   ... , to
Shinya Kitaoka 120a6e
         know mode view _wfopen_s documentation.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
FILE *fopen(const TFilePath &fp, string mode) {
Shinya Kitaoka 120a6e
  FILE *pFile;
Shinya Kitaoka 120a6e
  errno_t err =
Shinya Kitaoka 120a6e
      _wfopen_s(&pFile, fp.getWideString().c_str(), ::to_wstring(mode).c_str());
Shinya Kitaoka 120a6e
  if (err == -1) return NULL;
Shinya Kitaoka 120a6e
  return pFile;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 5b724a
Tifstream::Tifstream(const TFilePath &fp) : ifstream(m_file = fopen(fp, "rb")) {
shun-iwasawa 5b724a
  // manually set the fail bit here, since constructor ifstream::ifstream(FILE*)
shun-iwasawa 5b724a
  // does not so even if the argument is null pointer.
luz paz 6454c4
  // the state will be referred in "TIStream::operator bool()" ( in tstream.cpp )
shun-iwasawa 5b724a
  if (!m_file) setstate(failbit);
shun-iwasawa 5b724a
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tifstream::~Tifstream() {
Shinya Kitaoka 120a6e
  if (m_file) {
Shinya Kitaoka 120a6e
    int ret = fclose(m_file);
Shinya Kitaoka 120a6e
    assert(ret == 0);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Tifstream::close() {
Shinya Kitaoka 120a6e
  m_file = 0;
Shinya Kitaoka 120a6e
  std::ifstream::close();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
shun-iwasawa 5b724a
    : ofstream(m_file = fopen(fp, append_existing ? "ab" : "wb")) {
shun-iwasawa 5b724a
  // manually set the fail bit here, since constructor ofstream::ofstream(FILE*)
shun-iwasawa 5b724a
  // does not so even if the argument is null pointer.
luz paz 6454c4
  // the state will be referred in "TOStream::operator bool()" ( in tstream.cpp )
shun-iwasawa 5b724a
  if (!m_file) setstate(failbit);
shun-iwasawa 5b724a
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tofstream::~Tofstream() {
Shinya Kitaoka 120a6e
  if (m_file) {
Shinya Kitaoka 120a6e
    flush();
Shinya Kitaoka 120a6e
    int ret = fclose(m_file);
Shinya Kitaoka 120a6e
    assert(ret == 0);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Tofstream::close() {
Shinya Kitaoka 120a6e
  m_file = 0;
Shinya Kitaoka 120a6e
  std::ofstream::close();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Tifstream::isOpen() const { return m_file != 0; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Tofstream::isOpen() const { return m_file != 0; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//======================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Versione non windows
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//======================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FILE *fopen(const TFilePath &fp, string mode) {
Shinya Kitaoka 120a6e
  return fopen(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
Shinya Kitaoka 120a6e
               mode.c_str());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tifstream::Tifstream(const TFilePath &fp)
Shinya Kitaoka 120a6e
    : ifstream(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
Shinya Kitaoka 120a6e
               ios::binary)
Shinya Kitaoka 120a6e
/*: ifstream(openFileForReading(fp), ios::binary)
Shinya Kitaoka 120a6e
NO! Questo costruttore non e' standard, anche se e' presente
Toshihiro Shimizu 890ddd
in molte versioni. Nel MAC non c'e e fa un cast a char*
Toshihiro Shimizu 890ddd
sperando che sia il nome del file => compila ma non funziona
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
{}
Toshihiro Shimizu 890ddd
e280ae
Tifstream::~Tifstream() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
Shinya Kitaoka 120a6e
    : ofstream(
Shinya Kitaoka 120a6e
          QString::fromStdWString(fp.getWideString()).toUtf8().data(),
Shinya Kitaoka 120a6e
          ios::binary | (append_existing ? ios_base::app : ios_base::trunc)) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Tofstream::~Tofstream() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Tofstream::close() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Tifstream::isOpen() const {
Shinya Kitaoka 120a6e
  // TODO
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Tofstream::isOpen() const {
Shinya Kitaoka 120a6e
  // TODO
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif