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

file.h « DynSAInclude « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3157f918b211ddb60f9f1f6d1a869e9f4568c170 (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
66
67
68
69
70
71
72
#ifndef moses_DynSAInclude_file_h
#define moses_DynSAInclude_file_h

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
#include <string>
#include "util/check.hh"
#include "fdstream.h"
#include "utils.h"

namespace Moses
{
typedef std::string FileExtension;


class FileHandler: public std::fstream
{
public:
  // descriptors for stdin and stdout
  static const std::string kStdInDescriptor;	// file name for std::cin
  static const std::string kStdOutDescriptor;	// file name for std::cout
  // compression commands
  static const std::string kCatCommand;	// i.e. no compression
  static const std::string kGzipCommand;	// gzip -f
  static const std::string kGunzipCommand;	// gunzip -f
  static const std::string kBzip2Command;	// bzip2 -f
  static const std::string kBunzip2Command;	// bunzip2 -f

  // open file or wrap stdin or stdout
  FileHandler(const std::string & path,
              std::ios_base::openmode flags = std::ios::in,
              bool checkExists = true);
  ~FileHandler();
  // file utilities
  static bool getCompressionCmds(const std::string & filepath,
                                 std::string & compressionCmd,
                                 std::string & decompressionCmd,
                                 std::string & compressionSuffix);

  // data accessors
  std::string getPath() {
    return path_;
  }
  std::ios_base::openmode getFlags() {
    return flags_;
  }
  bool isStdIn() {
    return path_ == FileHandler::kStdInDescriptor;
  }
  bool isStdOut() {
    return path_ == FileHandler::kStdOutDescriptor;
  }
  bool reset();
protected:
  static const FileExtension kGzipped;
  static const FileExtension kBzipped2;
  bool fileExists();
  bool setStreamBuffer(bool checkExists);
  bool isCompressedFile(std::string & cmd);
  fdstreambuf* openCompressedFile(const char* cmd);
  std::string path_; // file path
  std::ios_base::openmode flags_;	// open flags
  std::streambuf* buffer_;	// buffer to either gzipped or standard data
  std::FILE* fp_;	//file pointer to handle pipe data
};

} // end namespace

#endif