2010-04-08 18:52:35 +04:00
|
|
|
#ifndef moses_DynSAInclude_file_h
|
|
|
|
#define moses_DynSAInclude_file_h
|
2010-02-12 14:05:43 +03:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string>
|
2011-11-18 16:07:41 +04:00
|
|
|
#include "util/check.hh"
|
2010-02-12 14:05:43 +03:00
|
|
|
#include "fdstream.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
typedef std::string FileExtension;
|
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
//! @todo ask abby2
|
2011-02-24 16:14:42 +03:00
|
|
|
class FileHandler: public std::fstream
|
|
|
|
{
|
2010-02-12 14:05:43 +03:00
|
|
|
public:
|
2011-02-24 16:14:42 +03:00
|
|
|
// 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();
|
2010-02-12 14:05:43 +03:00
|
|
|
protected:
|
2011-02-24 16:14:42 +03:00
|
|
|
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
|
2010-02-12 14:05:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
2010-02-24 14:15:44 +03:00
|
|
|
|
|
|
|
#endif
|