synfig-core  1.0.3
filesystem.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_FILESYSTEM_H
26 #define __SYNFIG_FILESYSTEM_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include <cstdio>
31 #include <string>
32 #include <streambuf>
33 #include <istream>
34 #include <ostream>
35 #include <ETL/handle>
36 
37 /* === M A C R O S ========================================================= */
38 
39 /* === T Y P E D E F S ===================================================== */
40 
41 /* === C L A S S E S & S T R U C T S ======================================= */
42 
43 namespace synfig
44 {
45 
46  class FileSystem : public etl::rshared_object
47  {
48  public:
49  typedef etl::handle< FileSystem > Handle;
50 
51  class Stream : public etl::rshared_object
52  {
53  protected:
56  public:
57  virtual ~Stream();
58  Handle file_system() const { return file_system_; }
59  };
60 
61  class ReadStream :
62  public Stream,
63  private std::streambuf,
64  public std::istream
65  {
66  protected:
67  char buffer_;
68 
70  virtual int underflow();
71  virtual size_t internal_read(void *buffer, size_t size) = 0;
72 
73  public:
74  size_t read_block(void *buffer, size_t size)
75  { return read((char*)buffer, size).gcount(); }
76  bool read_whole_block(void *buffer, size_t size)
77  { return size == read_block(buffer, size); }
78  template<typename T> bool read_variable(T &v)
79  { return read_whole_block(&v, sizeof(T)); }
80  };
81 
82  typedef etl::handle< ReadStream > ReadStreamHandle;
83 
84  class WriteStream :
85  public Stream,
86  private std::streambuf,
87  public std::ostream
88  {
89  protected:
91  virtual int overflow(int ch);
92  virtual size_t internal_write(const void *buffer, size_t size) = 0;
93 
94  public:
95  bool write_block(const void *buffer, size_t size)
96  {
97  for(size_t i = 0; i < size; i++)
98  if (!put(((const char*)buffer)[i]).good())
99  return i;
100  return size;
101  }
102  bool write_whole_block(const void *buffer, size_t size)
103  { return size == write_block(buffer, size); }
104  bool write_whole_stream(std::streambuf &streambuf)
105  { return (*this << &streambuf).good(); }
106  bool write_whole_stream(std::istream &stream)
107  { return write_whole_stream(*stream.rdbuf()); }
109  { return !stream || write_whole_stream(*(std::istream*)&(*stream)); }
110  template<typename T> bool write_variable(const T &v)
111  { return write_whole_block(&v, sizeof(T)); }
112  };
113 
114  typedef etl::handle< WriteStream > WriteStreamHandle;
115 
116  class Identifier {
117  public:
122  file_system(file_system), filename(filename) { }
123 
124  bool empty() const { return file_system; }
125  operator bool () const { return !empty(); }
126 
127  bool operator < (const Identifier &other) const
128  {
129  if (file_system.get() < other.file_system.get()) return true;
130  if (other.file_system.get() < file_system.get()) return false;
131  if (filename < other.filename) return true;
132  if (other.filename < filename) return false;
133  return false;
134  }
135  bool operator > (const Identifier &other) const
136  { return other < *this; }
137  bool operator != (const Identifier &other) const
138  { return *this > other || other < *this; }
139  bool operator == (const Identifier &other) const
140  { return !(*this != other); }
141 
144  };
145 
146  FileSystem();
147  virtual ~FileSystem();
148 
149  virtual bool is_file(const std::string &filename) = 0;
150  virtual bool is_directory(const std::string &filename) = 0;
151 
152  virtual bool directory_create(const std::string &dirname) = 0;
153 
154  virtual bool file_remove(const std::string &filename) = 0;
155  virtual bool file_rename(const std::string &from_filename, const std::string &to_filename);
156  virtual ReadStreamHandle get_read_stream(const std::string &filename) = 0;
157  virtual WriteStreamHandle get_write_stream(const std::string &filename) = 0;
158 
159  inline bool is_exists(const std::string filename) { return is_file(filename) || is_directory(filename); }
160 
161  Identifier get_identifier(const std::string &filename) { return Identifier(this, filename); }
162  static bool copy(Handle from_file_system, const std::string &from_filename, Handle to_file_system, const std::string &to_filename);
163 
164  static std::string fix_slashes(const std::string &filename);
165 
167  static std::istream& safeGetline(std::istream& is, std::string& t);
168  };
169 
170 }
171 
172 /* === E N D =============================================================== */
173 
174 #endif