rag
graphic 2d engine
File.h
1 #ifndef _RAG_FILE_SYSTEM_H_
2 #define _RAG_FILE_SYSTEM_H_
3 
4 #include <string>
5 #include <map>
6 
7 namespace rag
8 {
9 
11  class File
12  {
13 
14  public:
15 
17 
22  File(const std::string& path, bool bundle = true, bool logEnabled = true);
23  virtual ~File();
24 
26  static std::string load(std::string filename, bool bundle = true, std::string mode = "rb", bool showErrors = true);
27 
29  bool open(std::string mode = "rb", bool showErrors = true);
30 
32  void close();
33 
35  size_t read(void* buffer, size_t count);
36 
38  long getSize();
39 
41  size_t write(const void * ptr, size_t size, size_t count);
42 
44  bool exists();
45 
47  const std::string& getFullPath();
48 
50  static bool existsPath(const std::string& path);
51 
53  static bool makePath(const std::string& path);
54 
56  static void clearPatchFiles();
57 
59  static void setPatchFile(const std::string& filename, const std::string& filepath);
60 
62  static const std::map<std::string, std::string>& getPatchFiles();
63 
64  protected:
65 
66  FILE* pFile;
67  long size;
68  std::string path;
69  std::string osPath;
70  bool bundle;
71 
72  static bool sPatchFilesLoaded;
73  static std::map<std::string, std::string> patchFiles;
74  };
75 
76 
78  namespace fs
79  {
81  class path
82  {
83 
84  public:
85 
86  path(const std::string& name = ""):
87  name(name)
88  {
89 
90  }
91 
92  path parent_path()
93  {
94  size_t found = name.find_last_of('/');
95  if (found != std::string::npos)
96  return name.substr(0, found);
97 
98  return path("");
99  }
100 
101  path extension() const
102  {
103  if (!has_extension())
104  return path("");
105 
106  size_t dot = name.find_last_of('.');
107  return name.substr(dot);
108  }
109 
110  bool has_parent_path()
111  {
112  return name.find('/') != std::string::npos;
113  }
114 
115  bool has_extension() const
116  {
117  size_t dot = name.find_last_of('.');
118 
119  // No dot, no extension
120  if (dot == std::string::npos)
121  return false;
122 
123  // Dot but not slash, there's extension
124  size_t slash = name.find_last_of('/');
125  if (slash == std::string::npos)
126  return true;
127 
128  // Otherwise extension exists only if dot is after slash.
129  return dot > slash;
130  }
131 
132  path& replace_extension(const std::string extension)
133  {
134  size_t dot = name.find_last_of('.');
135  if (dot != std::string::npos)
136  {
137  // Replace previous extension
138  name = name.substr(0, dot) + extension;
139  }
140  else
141  {
142  // Add a extension
143  name = name + extension;
144  }
145  return *this;
146  }
147 
148  path filename() const
149  {
150  size_t slash = name.find_last_of("/\\");
151  if (slash == std::string::npos)
152  return *this;
153  else
154  {
155  return name.substr(slash+1);
156  }
157  }
158 
159  std::string string() const
160  {
161  return name;
162  }
163 
164  const path operator/(const path &rhs) const
165  {
166  return name + "/" + rhs.string();
167  }
168 
169  private:
170 
171  std::string name;
172 
173  };
174  }
175 
176 } // rag
177 
178 
179 
180 #endif
size_t read(void *buffer, size_t count)
Read into buffer the number of 'count' bytes.
static void setPatchFile(const std::string &filename, const std::string &filepath)
Override files in bundle.
static const std::map< std::string, std::string > & getPatchFiles()
Returns overrided files in bundle.
size_t write(const void *ptr, size_t size, size_t count)
Writes into the file.
static void clearPatchFiles()
Clean overrided files in bundle.
static std::string load(std::string filename, bool bundle=true, std::string mode="rb", bool showErrors=true)
Convenient function to load files without deal with low level api.
Definition: File.cpp:16
File multiplatform abstraction to read contents of a file.
Definition: File.h:11
Definition: Bitmap.h:8
static bool makePath(const std::string &path)
Creates a folder.
Definition: File.cpp:226
void close()
Close the file.
const std::string & getFullPath()
Returns the full path of the file, may contain bundle folder.
bool exists()
Returns true if the file exists.
bool open(std::string mode="rb", bool showErrors=true)
Open the file.
long getSize()
Returns the size of the file.
File(const std::string &path, bool bundle=true, bool logEnabled=true)
Creates a File object.
Definition: File.cpp:32
Mimics boost fs::path class with some limited functionality.
Definition: File.h:81
static bool existsPath(const std::string &path)
Returns true if the path exists.
Definition: File.cpp:210