Gorgon Game Engine
Filesystem.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <string>
6 #include <stdexcept>
7 #include <vector>
8 
9 namespace Gorgon {
10 
18  namespace Filesystem {
19 
22  class PathNotFoundError : public std::runtime_error {
23  public:
26  PathNotFoundError() : std::runtime_error("File not found") { }
27 
30  PathNotFoundError(const std::string &what) : std::runtime_error(what) { }
31  };
32 
36  class EntryPoint {
37  public:
38 
41  EntryPoint() { }
42 
44  std::string Path;
45 
47  bool Readable = false;
48 
53  bool Writable = false;
54 
56  bool Removable = false;
57 
59  std::string Name;
60  };
61 
62 
67  void Initialize();
68 
69 
77  bool CreateDirectory(const std::string &path);
78 
83  bool IsDirectory(const std::string &path);
84 
89  bool IsFile(const std::string &path);
90 
95  bool IsExists(const std::string &path);
96 
103  bool IsWritable(const std::string &path);
104 
109  bool IsHidden(const std::string &path);
110 
122  std::string Canonical(const std::string &path);
123 
132  std::string Relative(std::string path, std::string base=".");
133 
140  bool Delete(const std::string &path);
141 
146  bool ChangeDirectory(const std::string &path);
147 
151  std::string CurrentDirectory();
152 
154  inline std::string Join(std::string path1, const std::string &path2) {
155  if(path1.empty() || path1.back()!='/') {
156  path1.push_back('/');
157  }
158  path1+=path2;
159 
160  return path1;
161  }
162 
167  inline std::string GetDirectory(std::string filepath) {
168  auto pos=filepath.find_last_of('/');
169 
170  if(pos!=filepath.npos) {
171  filepath=filepath.substr(0,pos);
172  }
173  else {
174  filepath=CurrentDirectory();
175  }
176 
177  return filepath;
178  }
179 
183  inline std::string GetFilename(std::string path) {
184  auto pos=path.find_last_of('/');
185 
186  if(pos!=path.npos) {
187  path=path.substr(pos+1);
188  }
189 
190  return path;
191  }
192 
194  std::string GetExtension(std::string path);
195 
197  inline std::string GetBasename(std::string path) {
198  path = GetFilename(path);
199 
200  auto pos = path.find_last_of('.');
201 
202  if(pos == path.npos) return path;
203 
204  path = path.substr(0, pos);
205 
206  return path;
207  }
208 
214  bool Copy(const std::string &source, const std::string &target);
215 
222  template<template<class> class C_>
223  bool Copy(const C_<std::string> &source, const std::string &target) {
224  for(const auto &s : source) {
225  if(!Copy(s, target+"/"+GetFilename(s))) return false;
226  }
227 
228  return true;
229  }
230 
237  template<template<class, class> class C_, class A_>
238  bool Copy(const C_<std::string, A_> &source, const std::string &target) {
239  for(const auto &s : source) {
240  if(!Copy(s, target+"/"+GetFilename(s))) return false;
241  }
242 
243  return true;
244  }
245 
253  template<class I_>
254  bool Copy(const I_ &begin, const I_ &end, const std::string &target) {
255  for(I_ it=begin;it!=end;++it) {
256  if(!Copy(*it, target+"/"+GetFilename(*it))) return false;
257  }
258 
259  return true;
260  }
261 
269  template<template<class> class C_>
270  bool Copy(const std::string &sourcedir, const C_<std::string> &source, const std::string &target) {
271  for(const auto &s : source) {
272  if(!Copy(sourcedir+"/"+s, target+"/"+s)) return false;
273  }
274 
275  return true;
276  }
277 
286  template<class I_>
287  bool Copy(const std::string &sourcedir, const I_ &begin, const I_ &end, const std::string &target) {
288  for(I_ it=begin;it!=end;++it) {
289  if(!Copy(sourcedir+"/"+*it, target+"/"+*it)) return false;
290  }
291 
292  return true;
293  }
294 
302  bool Move(const std::string &source, const std::string &target);
303 
307  unsigned long long Size(const std::string &filename);
308 
311  bool Save(const std::string &filename, const std::string &data, bool append=false);
312 
320  std::string Load(const std::string &filename);
321 
324  std::string StartupDirectory();
325 
327  std::string ExePath();
328 
331  std::string ExeDirectory();
332 
337  std::vector<EntryPoint> EntryPoints();
338 
366  std::string LocateResource(const std::string &path, const std::string &directory="", bool localonly=true);
367 
368  }
369 
370 }
Gorgon::Filesystem::EntryPoint::Path
std::string Path
The path of the entry point.
Definition: Filesystem.h:44
Gorgon::Filesystem::GetFilename
std::string GetFilename(std::string path)
Returns the filename portion of a file path.
Definition: Filesystem.h:183
Gorgon::Filesystem::Move
bool Move(const std::string &source, const std::string &target)
Moves a given file or directory.
Definition: Linux.cpp:216
Gorgon::Filesystem::EntryPoint::Writable
bool Writable
Whether the entry point is writable.
Definition: Filesystem.h:53
Gorgon::Filesystem::ChangeDirectory
bool ChangeDirectory(const std::string &path)
Changes current working directory.
Definition: Linux.cpp:145
Gorgon::begin
std::vector< T_ >::const_iterator begin(enum_type_id< T_ >)
Definition: Enum.h:283
Gorgon::Filesystem::PathNotFoundError
This object is thrown from functions that return information rather than status.
Definition: Filesystem.h:22
Gorgon::Filesystem::Relative
std::string Relative(std::string path, std::string base)
Determine shortest relative path from the given path.
Definition: Filesystem.cpp:67
Gorgon::Filesystem::GetDirectory
std::string GetDirectory(std::string filepath)
Returns the directory portion of a file path.
Definition: Filesystem.h:167
Gorgon::Filesystem::IsDirectory
bool IsDirectory(const std::string &path)
Checks whether the given path is a directory.
Definition: Linux.cpp:32
Gorgon::Filesystem::Join
std::string Join(std::string path1, const std::string &path2)
Joins two given paths or a path and filename.
Definition: Filesystem.h:154
Gorgon::Filesystem::IsHidden
bool IsHidden(const std::string &path)
Checks whether the given path is hidden.
Definition: Linux.cpp:73
Gorgon::Filesystem::Copy
bool Copy(const std::string &source, const std::string &target)
Copies a file or directory from the given source to destination.
Definition: Linux.cpp:180
Gorgon::Filesystem::ExeDirectory
std::string ExeDirectory()
Returns the directory where the program resides.
Definition: Linux.cpp:220
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Filesystem::Load
std::string Load(const std::string &filename)
Loads the given file and returns it in a string form.
Definition: Filesystem.cpp:45
Gorgon::Filesystem::StartupDirectory
std::string StartupDirectory()
Returns the directory where the program is started from.
Definition: Filesystem.cpp:18
Gorgon::Filesystem::IsExists
bool IsExists(const std::string &path)
Checks whether the given path exists.
Definition: Linux.cpp:60
Gorgon::Filesystem::GetExtension
std::string GetExtension(std::string path)
Returns the extension of the given path, also converts the extension to lower case.
Definition: Filesystem.cpp:140
Gorgon::Filesystem::LocateResource
std::string LocateResource(const std::string &path, const std::string &directory="", bool localonly=true)
Locates the given file or directory.
Gorgon::Filesystem::Save
bool Save(const std::string &filename, const std::string &data, bool append)
Saves a given data into the filename.
Definition: Filesystem.cpp:31
Gorgon::Filesystem::PathNotFoundError::PathNotFoundError
PathNotFoundError(const std::string &what)
Constructor that sets error text.
Definition: Filesystem.h:30
Gorgon::Filesystem::Size
unsigned long long Size(const std::string &filename)
Returns the size of the given file.
Definition: Filesystem.cpp:22
Gorgon::Filesystem::EntryPoint::Readable
bool Readable
Whether the entry point is readable. Currently all entry points are readable.
Definition: Filesystem.h:47
Gorgon::Filesystem::EntryPoint::Name
std::string Name
Name or label of the entry point.
Definition: Filesystem.h:59
Gorgon::Filesystem::Delete
bool Delete(const std::string &path)
Deletes the given file or directory.
Definition: Linux.cpp:106
Gorgon::Filesystem::ExePath
std::string ExePath()
Returns the the full path of the application.
Definition: Linux.cpp:226
Gorgon::Filesystem::EntryPoint::Removable
bool Removable
Whether the device is removable.
Definition: Filesystem.h:56
Gorgon::Filesystem::IsFile
bool IsFile(const std::string &path)
Checks whether the given path is a file.
Definition: Linux.cpp:46
Gorgon::Filesystem::IsWritable
bool IsWritable(const std::string &path)
Checks whether the given path is writable.
Definition: Linux.cpp:64
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Filesystem::Initialize
void Initialize()
Initializes the filesystem module.
Definition: Filesystem.cpp:14
Gorgon::Filesystem::GetBasename
std::string GetBasename(std::string path)
Returns the filename from the given path, without extension.
Definition: Filesystem.h:197
Gorgon::Filesystem::CurrentDirectory
std::string CurrentDirectory()
Returns the current working directory.
Definition: Linux.cpp:149
Gorgon::Filesystem::EntryPoints
std::vector< EntryPoint > EntryPoints()
This function returns all entry points in the current system.
Definition: Linux.cpp:232
Gorgon::Filesystem::Canonical
std::string Canonical(const std::string &path)
Canonicalizes a given relative path.
Definition: Linux.cpp:94
Gorgon::Filesystem::EntryPoint
This class represents filesystem entry points (roots, drives).
Definition: Filesystem.h:36
Gorgon::Filesystem::PathNotFoundError::PathNotFoundError
PathNotFoundError()
Default constructor.
Definition: Filesystem.h:26
Gorgon::Filesystem::CreateDirectory
bool CreateDirectory(const std::string &path)
Creates a new directory.
Definition: Linux.cpp:19
Gorgon::Filesystem::EntryPoint::EntryPoint
EntryPoint()
Default constructor.
Definition: Filesystem.h:41