diff options
author | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-07-19 21:33:17 +0000 |
---|---|---|
committer | graehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-07-19 21:33:17 +0000 |
commit | a429a0f0f510751b842b7161f5e1c8feab05fc1b (patch) | |
tree | 0f1667737581b3b6dc5b9cff8d904a596e9d2335 /decoder/filelib.h | |
parent | 648c350aa8b90bd60ca1448bd3bb702004b9ad26 (diff) |
shared_ptr for ReadFile and doc_scorer; init ds to GetOne() in oracle
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@322 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/filelib.h')
-rw-r--r-- | decoder/filelib.h | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/decoder/filelib.h b/decoder/filelib.h index 03c22b0d..1630481d 100644 --- a/decoder/filelib.h +++ b/decoder/filelib.h @@ -5,6 +5,7 @@ #include <string> #include <iostream> #include <cstdlib> +#include <boost/shared_ptr.hpp> #include "gzstream.h" bool FileExists(const std::string& file_name); @@ -13,35 +14,57 @@ bool DirectoryExists(const std::string& dir_name); // reads from standard in if filename is - // uncompresses if file ends with .gz // otherwise, reads from a normal file +struct file_null_deleter { + void operator()(void*) const {} +}; + class ReadFile { public: - ReadFile(const std::string& filename) : - no_delete_on_exit_(filename == "-"), - in_(no_delete_on_exit_ ? static_cast<std::istream*>(&std::cin) : - (EndsWith(filename, ".gz") ? - static_cast<std::istream*>(new igzstream(filename.c_str())) : - static_cast<std::istream*>(new std::ifstream(filename.c_str())))) { - if (!no_delete_on_exit_ && !FileExists(filename)) { - std::cerr << "File does not exist: " << filename << std::endl; - abort(); - } - if (!*in_) { - std::cerr << "Failed to open " << filename << std::endl; - abort(); + typedef boost::shared_ptr<std::istream> PS; + ReadFile() { } + std::string filename_; + void Init(const std::string& filename) { + bool stdin=(filename == "-"); + if (stdin) { + in_=PS(&std::cin,file_null_deleter()); + } else { + if (!FileExists(filename)) { + std::cerr << "File does not exist: " << filename << std::endl; + abort(); + } + filename_=filename; + in_.reset(EndsWith(filename, ".gz") ? + static_cast<std::istream*>(new igzstream(filename.c_str())) : + static_cast<std::istream*>(new std::ifstream(filename.c_str()))); + if (!*in_) { + std::cerr << "Failed to open " << filename << std::endl; + abort(); + } } } + void Reset() { + in_.reset(); + } + bool is_null() const { return !in_; } + operator bool() const { + return in_; + } + + explicit ReadFile(const std::string& filename) { + Init(filename); + } ~ReadFile() { - if (!no_delete_on_exit_) delete in_; } - inline std::istream* stream() { return in_; } - + std::istream* stream() { return in_.get(); } + std::istream* operator->() { return in_.get(); } // compat with old ReadFile * -> new Readfile. remove? + std::istream &get() const { return *in_; } + private: static bool EndsWith(const std::string& f, const std::string& suf) { return (f.size() > suf.size()) && (f.rfind(suf) == f.size() - suf.size()); } - const bool no_delete_on_exit_; - std::istream* const in_; + PS in_; }; class WriteFile { @@ -58,7 +81,8 @@ class WriteFile { } inline std::ostream* stream() { return out_; } - + std::ostream &get() const { return *out_; } + private: static bool EndsWith(const std::string& f, const std::string& suf) { return (f.size() > suf.size()) && (f.rfind(suf) == f.size() - suf.size()); |