summaryrefslogtreecommitdiff
path: root/decoder
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
commitbcad98e114d468edf16369b3a30d98556a3d0e61 (patch)
tree17145c1910a6c884badb0e6303d6bc68f8337229 /decoder
parentdc51c15064e012a83b87716aa9524557fc488afc (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')
-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