Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileStream.cpp « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 11fd58e2661f4b919cbf213968bf98313e215bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "FileStream.h"

#include <stdexcept>
#include "gzfilebuf.h"

using namespace std;

namespace {
bool IsGzipFile(const std::string &filename) {
  return filename.size() > 3 &&
      filename.substr(filename.size() - 3, 3) == ".gz";
}
} // namespace

inputfilestream::inputfilestream(const std::string &filePath)
    : std::istream(0), m_streambuf(0), is_good(false)
{
  // check if file is readable
  std::filebuf* fb = new std::filebuf();
  is_good = (fb->open(filePath.c_str(), std::ios::in) != NULL);

  if (IsGzipFile(filePath)) {
    fb->close();
    delete fb;
    m_streambuf = new gzfilebuf(filePath.c_str());
  } else {
    m_streambuf = fb;
  }
  this->init(m_streambuf);
}

inputfilestream::~inputfilestream()
{
  delete m_streambuf;
  m_streambuf = 0;
}

void inputfilestream::close()
{
}

outputfilestream::outputfilestream(const std::string &filePath)
    : std::ostream(0), m_streambuf(0), is_good(false)
{
  // check if file is readable
  std::filebuf* fb = new std::filebuf();
  is_good = (fb->open(filePath.c_str(), std::ios::out) != NULL);

  if (IsGzipFile(filePath)) {
    throw runtime_error("Output to a zipped file not supported!");
  } else {
    m_streambuf = fb;
  }
  this->init(m_streambuf);
}

outputfilestream::~outputfilestream()
{
  delete m_streambuf;
  m_streambuf = 0;
}

void outputfilestream::close()
{
}