CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPathname.h
1 //
2 // CUPathname.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for an abstract pathname. This pathname
6 // provides a cross-platform way to create directories and access files.
7 // It is used by all of the other I/O classes.
8 //
9 // Because pathnames are intended to be on the stack, we do not provide any
10 // shared pointer support in this class.
11 //
12 // This module is heavily inspired by the Java File class. Much of the API
13 // is taken from that class.
14 //
15 // CUGL zlib License:
16 // This software is provided 'as-is', without any express or implied
17 // warranty. In no event will the authors be held liable for any damages
18 // arising from the use of this software.
19 //
20 // Permission is granted to anyone to use this software for any purpose,
21 // including commercial applications, and to alter it and redistribute it
22 // freely, subject to the following restrictions:
23 //
24 // 1. The origin of this software must not be misrepresented; you must not
25 // claim that you wrote the original software. If you use this software
26 // in a product, an acknowledgment in the product documentation would be
27 // appreciated but is not required.
28 //
29 // 2. Altered source versions must be plainly marked as such, and must not
30 // be misrepresented as being the original software.
31 //
32 // 3. This notice may not be removed or altered from any source distribution.
33 //
34 // Author: Walker White
35 // Version: 11/22/16
36 //
37 #ifndef __CU_PATHNAME_H__
38 #define __CU_PATHNAME_H__
39 #include <string>
40 #include <vector>
41 #include <functional>
42 
43 namespace cugl {
44 
45 #pragma mark -
46 #pragma mark Pathname
47 
85 class Pathname {
86 private:
88  std::string _shortname;
90  std::string _pathname;
92  std::string _fullpath;
93 
94 #pragma mark Normalization
95 
107  static std::string normalize(const std::string& path);
108 
126  static std::string canonicalize(const std::string& path);
127 
137  static bool isAbsolute(const std::string& path);
138 
139 
140 #pragma mark -
141 #pragma mark Constructors
142 
148  Pathname() {}
149 
150 public:
159  Pathname(const Pathname& copy);
160 
169  Pathname(Pathname&& copy) noexcept;
170 
182  Pathname(const std::string& path);
183 
195  Pathname(const char* path) : Pathname(std::string(path)) {}
196 
213  Pathname(const std::string& parent, const std::string& child);
214 
231  Pathname(const char* parent, const std::string& child) : Pathname(std::string(parent),child) {}
232 
249  Pathname(const std::string& parent, const char* child) : Pathname(parent,std::string(child)) {}
250 
267  Pathname(const char* parent, const char* child) : Pathname(std::string(parent),std::string(child)) {}
268 
279  Pathname(const Pathname& parent, const std::string& child);
280 
291  Pathname(const Pathname& parent, const char* child) : Pathname(parent,std::string(child)) {}
292 
297 
298 
299 #pragma mark -
300 #pragma mark Path Types
301 
309  bool exists() const;
310 
320  bool isDirectory() const;
321 
331  bool isFile() const;
332 
342  bool isHidden() const { return exists() && _shortname[0] == '.'; }
343 
344 
345 #pragma mark -
346 #pragma mark Path Names
347 
352  bool isAbsolute() const { return _pathname == _fullpath; }
353 
361  std::string getName() const { return _shortname; }
362 
373  std::string getPathname() const { return _pathname; }
374 
385  std::string getParentName() const;
386 
397  Pathname getParentPath() const;
398 
407  std::string getAbsoluteName() const { return _fullpath; }
408 
417  Pathname getAbsolutePath() const;
418 
427  std::string getSuffix() const;
428 
429 
430 #pragma mark -
431 #pragma mark Path Hierarchy
432 
439  std::string getVolume() const;
440 
449  static std::string getSeparator();
450 
461  std::vector<std::string> getComponents() const;
462 
471  std::vector<std::string> list() const;
472 
484  std::vector<std::string> list(const std::function<bool(const std::string& file)>& filter) const;
485 
494  std::vector<Pathname> listPaths() const;
495 
504  std::vector<Pathname> listPaths(const std::function<bool(const Pathname& path)>& filter) const;
505 
506 
507 #pragma mark -
508 #pragma mark Path Creation
509 
517  bool createFile();
518 
527  bool deleteFile();
528 
537  bool createDirectory();
538 
547  bool deleteDirectory();
548 
559  bool createPath();
560 
574  bool renameTo(const std::string& path) {
575  return renameTo(Pathname(path));
576  }
577 
591  bool renameTo(const char* path) {
592  return renameTo(Pathname(path));
593  }
594 
608  bool renameTo(const Pathname& dest);
609 
610 
611 #pragma mark -
612 #pragma mark Path Access
613 
622  bool canRead();
623 
636  bool canSearch();
637 
647  bool canWrite();
648 
659  bool setReadable(bool readable);
660 
672  bool setReadable(bool readable, bool ownerOnly);
673 
682  bool setReadOnly();
683 
697  bool setSearchable(bool searchable);
698 
713  bool setSearchable(bool searchable, bool ownerOnly);
714 
725  bool setWritable(bool writable);
726 
738  bool setWritable(bool writable, bool ownerOnly);
739 
740 
741 
742 #pragma mark -
743 #pragma mark Path Size
744 
751  size_t length() const;
752 
761  long lastModified() const;
762 
772  size_t getFreeSpace() const;
773 
783  size_t getAvailableSpace() const;
784 
792  size_t getTotalSpace() const;
793 
794 
795 #pragma mark -
796 #pragma mark Path Operators
797 
806  bool operator<(const Pathname& other) const {
807  return getAbsoluteName() < other.getAbsoluteName();
808  }
809 
819  bool operator<=(const Pathname& other) const {
820  return getAbsoluteName() <= other.getAbsoluteName();
821  }
822 
832  bool operator>(const Pathname& other) const {
833  return getAbsoluteName() > other.getAbsoluteName();
834  }
835 
845  bool operator>=(const Pathname& other) const {
846  return getAbsoluteName() >= other.getAbsoluteName();
847  }
848 
858  bool operator==(const Pathname& other) const {
859  return getAbsoluteName() == other.getAbsoluteName();
860  }
861 
871  bool operator!=(const Pathname& other) const{
872  return getAbsoluteName() != other.getAbsoluteName();
873  }
874 
875 };
876 
877 }
878 #endif /* __CU_PATHNAME_H__ */
std::string getPathname() const
Definition: CUPathname.h:373
std::vector< std::string > list() const
bool createDirectory()
size_t getTotalSpace() const
~Pathname()
Definition: CUPathname.h:296
Pathname(const Pathname &parent, const char *child)
Definition: CUPathname.h:291
bool operator<=(const Pathname &other) const
Definition: CUPathname.h:819
bool renameTo(const char *path)
Definition: CUPathname.h:591
std::string getParentName() const
bool setSearchable(bool searchable)
bool isFile() const
size_t getFreeSpace() const
bool renameTo(const std::string &path)
Definition: CUPathname.h:574
bool exists() const
Pathname(const char *parent, const char *child)
Definition: CUPathname.h:267
bool operator>(const Pathname &other) const
Definition: CUPathname.h:832
bool operator>=(const Pathname &other) const
Definition: CUPathname.h:845
Pathname getParentPath() const
bool setReadOnly()
bool setReadable(bool readable)
Pathname getAbsolutePath() const
bool isHidden() const
Definition: CUPathname.h:342
size_t getAvailableSpace() const
std::string getAbsoluteName() const
Definition: CUPathname.h:407
bool deleteDirectory()
bool operator<(const Pathname &other) const
Definition: CUPathname.h:806
Pathname(const std::string &parent, const char *child)
Definition: CUPathname.h:249
std::string getSuffix() const
Pathname(const char *parent, const std::string &child)
Definition: CUPathname.h:231
bool isAbsolute() const
Definition: CUPathname.h:352
size_t length() const
std::vector< std::string > getComponents() const
static std::string getSeparator()
std::string getVolume() const
Pathname(const char *path)
Definition: CUPathname.h:195
long lastModified() const
Definition: CUAnimationNode.h:52
bool operator==(const Pathname &other) const
Definition: CUPathname.h:858
Definition: CUPathname.h:85
bool operator!=(const Pathname &other) const
Definition: CUPathname.h:871
bool isDirectory() const
std::string getName() const
Definition: CUPathname.h:361
bool setWritable(bool writable)
std::vector< Pathname > listPaths() const