Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vfsnode_h 00009 #define vfsnode_h 00010 00013 #include "vstring.h" 00014 00077 class VFSNodeInfo { 00078 public: 00079 00080 VFSNodeInfo(); 00081 ~VFSNodeInfo() {} 00082 00083 Vs64 mCreationDate; // a VInstant offset value 00084 Vs64 mModificationDate; // a VInstant offset value 00085 VFSize mFileSize; 00086 bool mIsFile; 00087 bool mIsDirectory; 00088 int mErrNo; // the value of errno if call failed, 0 otherwise 00089 }; 00090 00091 00092 class VInstant; 00093 class VFSNode; 00094 class VBinaryIOStream; 00095 00096 class VDirectoryIterationCallback { 00097 public: 00098 00099 VDirectoryIterationCallback() {} 00100 virtual ~VDirectoryIterationCallback() {} 00101 00107 virtual bool handleNextNode(const VFSNode& node) = 0; 00108 }; 00109 00114 typedef std::vector<VFSNode> VFSNodeVector; 00115 00120 class VFSNode { 00121 public: 00122 00133 static VString normalizePath(const VString& path); 00141 static VString denormalizePath(const VString& path); 00142 00149 typedef enum { 00150 USER_HOME_DIRECTORY, 00151 LOG_FILES_DIRECTORY, 00152 USER_PREFERENCES_DIRECTORY, 00153 CACHED_DATA_DIRECTORY, 00154 APPLICATION_DATA_DIRECTORY, 00155 CURRENT_WORKING_DIRECTORY, 00156 EXECUTABLE_DIRECTORY 00157 } KnownDirectoryIdentifier; 00223 static VFSNode getKnownDirectoryNode(KnownDirectoryIdentifier id, const VString& companyName, const VString& appName); 00230 static VFSNode getCurrentWorkingDirectory(); 00237 static VFSNode getExecutableDirectory(); 00243 static VFSNode getExecutable(); 00244 00258 static void safelyOverwriteFile(const VFSNode& target, Vs64 dataLength, VBinaryIOStream& dataStream); 00259 00264 VFSNode(); 00268 VFSNode(const VFSNode& rhs); 00273 VFSNode(const VString& path); 00279 VFSNode(const VFSNode& directory, const VString& childName); 00283 virtual ~VFSNode() {} 00284 00289 VFSNode& operator=(const VFSNode& other); 00290 00295 void setPath(const VString& path); 00300 void getPath(VString& path) const; 00305 const VString& getPath() const; 00311 void getName(VString& name) const; 00317 VString getName() const; 00324 void setName(const VString& name); 00329 void getParentPath(VString& parentPath) const; 00334 void getParentNode(VFSNode& parent) const; 00340 void getChildPath(const VString& childName, VString& childPath) const; 00346 void getChildNode(const VString& childName, VFSNode& child) const; 00347 00352 void mkdirs() const; 00356 void mkdir() const; 00362 bool rm() const; 00369 bool rmDirContents() const; 00374 bool exists() const; 00379 VInstant creationDate() const; 00384 VInstant modificationDate() const; 00390 VFSize size() const; 00395 bool isFile() const; 00400 bool isDirectory() const; 00401 00410 void renameToPath(const VString& newPath) const; 00421 void renameToName(const VString& newName) const; 00434 void renameToName(const VString& newName, VFSNode& nodeToUpdate) const; 00442 void renameToNode(const VFSNode& newNode) const; 00443 00449 void list(VStringVector& children) const; 00455 void list(VFSNodeVector& children) const; 00464 void iterate(VDirectoryIterationCallback& callback) const; 00476 bool find(const VString& name, VFSNode& node) const; 00477 00493 void readAll(VString& s, bool includeLineEndings = true); 00501 void readAll(VStringVector& lines); 00510 static VString readTextFile(const VString& path, bool includeLineEndings = true); 00517 static void readTextFile(const VString& path, VStringVector& lines); 00518 00519 // The < operator is needed for to support STL sort(); others provided for completeness. 00520 // The purpose here is to allow a sorting a directory listing by name, 00521 // after calling VFSNode::list(). This is reasonable for many cases, but 00522 // note that the comparision is ultimately performed by strcmp(). 00523 friend inline bool operator< (const VFSNode& lhs, const VFSNode& rhs); 00524 friend inline bool operator<=(const VFSNode& lhs, const VFSNode& rhs); 00525 friend inline bool operator>=(const VFSNode& lhs, const VFSNode& rhs); 00526 friend inline bool operator> (const VFSNode& lhs, const VFSNode& rhs); 00527 00528 private: 00529 00530 // These are the key functions that typically peculiar to each platform, 00531 // and are implemented in the platform-specific vfsnode_platform.cpp 00532 // files. 00533 00541 static VString _platform_normalizePath(const VString& path); 00547 static VString _platform_denormalizePath(const VString& path); 00555 static VFSNode _platform_getKnownDirectoryNode(KnownDirectoryIdentifier id, const VString& companyName, const VString& appName); 00561 static VFSNode _platform_getExecutable(); 00562 00572 bool _platform_getNodeInfo(VFSNodeInfo& info) const; 00577 void _platform_createDirectory() const; 00585 bool _platform_removeDirectory() const; 00593 bool _platform_removeFile() const; 00599 void _platform_renameNode(const VString& newPath) const; 00600 00609 void _platform_directoryIterate(VDirectoryIterationCallback& callback) const; 00610 00611 VString mPath; 00612 00613 }; 00614 00615 inline bool operator< (const VFSNode& lhs, const VFSNode& rhs) { return lhs.mPath < rhs.mPath; } 00616 inline bool operator<=(const VFSNode& lhs, const VFSNode& rhs) { return !operator>(lhs, rhs); } 00617 inline bool operator>=(const VFSNode& lhs, const VFSNode& rhs) { return !operator<(lhs, rhs); } 00618 inline bool operator> (const VFSNode& lhs, const VFSNode& rhs) { return operator<(rhs, lhs); } 00619 00620 #endif /* vfsnode_h */ 00621