summaryrefslogtreecommitdiff
path: root/decoder/filelib.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-20 00:11:39 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-20 00:11:39 +0000
commitb6cf365f217bc7b528243071af733d4a251ff77c (patch)
tree815e0561eeafc1eea044f75e0de30e7e75933a77 /decoder/filelib.h
parent190ad8ae1e131ac0e29ff975b0d6502f3cc57af6 (diff)
shared_ptr WriteFile and shared impl between read/write
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@330 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/filelib.h')
-rw-r--r--decoder/filelib.h103
1 files 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 <class Stream>
+struct BaseFile {
+ typedef Stream S;
+ typedef boost::shared_ptr<Stream> 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<std::istream> {
public:
- typedef boost::shared_ptr<std::istream> 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<std::istream*>(new igzstream(filename.c_str())) :
static_cast<std::istream*>(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<std::ostream> {
public:
- WriteFile(const std::string& filename) :
- no_delete_on_exit_(filename == "-"),
- out_(no_delete_on_exit_ ? static_cast<std::ostream*>(&std::cout) :
- (EndsWith(filename, ".gz") ?
- static_cast<std::ostream*>(new ogzstream(filename.c_str())) :
- static_cast<std::ostream*>(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<std::ostream*>(new ogzstream(filename.c_str())) :
+ static_cast<std::ostream*>(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