From b6cf365f217bc7b528243071af733d4a251ff77c Mon Sep 17 00:00:00 2001 From: graehl Date: Tue, 20 Jul 2010 00:11:39 +0000 Subject: shared_ptr WriteFile and shared impl between read/write git-svn-id: https://ws10smt.googlecode.com/svn/trunk@330 ec762483-ff6d-05da-a07a-a48fb63a330f --- decoder/filelib.h | 103 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/decoder/filelib.h b/decoder/filelib.h index 1630481d..85e1c29a 100644 --- a/decoder/filelib.h +++ b/decoder/filelib.h @@ -18,77 +18,80 @@ struct file_null_deleter { void operator()(void*) const {} }; -class ReadFile { +template +struct BaseFile { + typedef Stream S; + typedef boost::shared_ptr PS; + void Reset() { + ps_.reset(); + } + bool is_null() const { return !ps_; } + operator bool() const { + return ps_; + } + S* stream() { return ps_.get(); } + S* operator->() { return ps_.get(); } // compat with old ReadFile * -> new Readfile. remove? + S &get() const { return *ps_; } + bool is_std() { + return filename_=="-"; + } + std::string filename_; +protected: + PS ps_; + static bool EndsWith(const std::string& f, const std::string& suf) { + return (f.size() > suf.size()) && (f.rfind(suf) == f.size() - suf.size()); + } +}; + +class ReadFile : public BaseFile { public: - typedef boost::shared_ptr PS; ReadFile() { } - std::string filename_; + explicit ReadFile(const std::string& filename) { + Init(filename); + } void Init(const std::string& filename) { - bool stdin=(filename == "-"); - if (stdin) { - in_=PS(&std::cin,file_null_deleter()); + filename_=filename; + if (is_std()) { + ps_=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") ? + ps_.reset(EndsWith(filename, ".gz") ? static_cast(new igzstream(filename.c_str())) : static_cast(new std::ifstream(filename.c_str()))); - if (!*in_) { + if (!*ps_) { 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() { - } - - 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()); - } - PS in_; }; -class WriteFile { +class WriteFile : public BaseFile { public: - WriteFile(const std::string& filename) : - no_delete_on_exit_(filename == "-"), - out_(no_delete_on_exit_ ? static_cast(&std::cout) : - (EndsWith(filename, ".gz") ? - static_cast(new ogzstream(filename.c_str())) : - static_cast(new std::ofstream(filename.c_str())))) {} - ~WriteFile() { - (*out_) << std::flush; - if (!no_delete_on_exit_) delete out_; + WriteFile() {} + explicit WriteFile(std::string const& filename) { Init(filename); } + void Init(const std::string& filename) { + filename_=filename; + if (is_std()) { + ps_=PS(&std::cout,file_null_deleter()); + } else { + ps_.reset(EndsWith(filename, ".gz") ? + static_cast(new ogzstream(filename.c_str())) : + static_cast(new std::ofstream(filename.c_str()))); + if (!*ps_) { + std::cerr << "Failed to open " << filename << std::endl; + abort(); + } + } } - - 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()); + ~WriteFile() { + if (ps_) + get() << std::flush; } - const bool no_delete_on_exit_; - std::ostream* const out_; }; #endif -- cgit v1.2.3