summaryrefslogtreecommitdiff
path: root/decoder/filelib.h
blob: 2d347c003ecd2836d4b66bda9feee3559227eb7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _FILELIB_H_
#define _FILELIB_H_

#include <cassert>
#include <string>
#include <iostream>
#include <cstdlib>
#include <boost/shared_ptr.hpp>
#include "gzstream.h"

bool FileExists(const std::string& file_name);
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 {}
};

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:
  ReadFile() {  }
  explicit ReadFile(const std::string& filename) {
    Init(filename);
  }
  void Init(const std::string& filename) {
    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();
      }
      char const* file=filename_.c_str(); // just in case the gzstream keeps using the filename for longer than the constructor, e.g. inflateReset2.  warning in valgrind that I'm hoping will disappear - it makes no sense.
      ps_=PS(EndsWith(filename, ".gz") ?
                static_cast<std::istream*>(new igzstream(file)) :
             static_cast<std::istream*>(new std::ifstream(file)));
      if (!*ps_) {
        std::cerr << "Failed to open " << filename << std::endl;
        abort();
      }
    }
  }

};

class WriteFile : public BaseFile<std::ostream> {
 public:
  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 {
      char const* file=filename_.c_str(); // just in case the gzstream keeps using the filename for longer than the constructor, e.g. inflateReset2.  warning in valgrind that I'm hoping will disappear - it makes no sense.
      ps_=PS(EndsWith(filename, ".gz") ?
                static_cast<std::ostream*>(new ogzstream(file)) :
                static_cast<std::ostream*>(new std::ofstream(file)));
      if (!*ps_) {
        std::cerr << "Failed to open " << filename << std::endl;
        abort();
      }
    }
  }
  ~WriteFile() {
    if (ps_)
      get() << std::flush;
  }
};

#endif