FileSystem.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. #include "HashSet.h"
  26. /// Return files.
  27. static const unsigned SCAN_FILES = 0x1;
  28. /// Return directories.
  29. static const unsigned SCAN_DIRS = 0x2;
  30. /// Return also hidden files.
  31. static const unsigned SCAN_HIDDEN = 0x4;
  32. /// Subsystem for file and directory operations and access control.
  33. class FileSystem : public Object
  34. {
  35. OBJECT(FileSystem);
  36. public:
  37. /// Construct.
  38. FileSystem(Context* context);
  39. /// Destruct.
  40. ~FileSystem();
  41. /// %Set the current working directory.
  42. bool SetCurrentDir(const String& pathName);
  43. /// Create a directory.
  44. bool CreateDir(const String& pathName);
  45. /// Run a program using the command interpreter, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  46. int SystemCommand(const String& commandLine);
  47. /// Run a specific program, block until it exists and return the exit code. Will fail if any allowed paths are defined.
  48. int SystemRun(const String& fileName, const Vector<String>& arguments);
  49. /// Open a file in an external program, with mode such as "edit" optionally specified. Will fail if any allowed paths are defined.
  50. bool SystemOpen(const String& fileName, const String& mode = String());
  51. /// Copy a file. Return true if successful.
  52. bool Copy(const String& srcFileName, const String& destFileName);
  53. /// Rename a file. Return true if successful.
  54. bool Rename(const String& srcFileName, const String& destFileName);
  55. /// Delete a file. Return true if successful.
  56. bool Delete(const String& fileName);
  57. /// Register a path as allowed to access. If no paths are registered, all are allowed.
  58. void RegisterPath(const String& pathName);
  59. /// Return the absolute current working directory.
  60. String GetCurrentDir() const;
  61. /// Return whether paths have been registered.
  62. bool HasRegisteredPaths() const { return allowedPaths_.Size() > 0; }
  63. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  64. bool CheckAccess(const String& pathName) const;
  65. /// Returns the file's last modified time as seconds since 1.1.1970, or 0 if can not be accessed.
  66. unsigned GetLastModifiedTime(const String& fileName) const;
  67. /// Check if a file exists.
  68. bool FileExists(const String& fileName) const;
  69. /// Check if a directory exists.
  70. bool DirExists(const String& pathName) const;
  71. /// Scan a directory for specified files.
  72. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  73. /// Return the program's directory.
  74. String GetProgramDir() const;
  75. /// Return the user documents directory.
  76. String GetUserDocumentsDir() const;
  77. private:
  78. /// Scan directory, called internally.
  79. void ScanDirInternal(Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  80. /// Allowed directories.
  81. HashSet<String> allowedPaths_;
  82. };
  83. /// Split a full path to path, filename and extension. The extension will be converted to lowercase.
  84. void SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension);
  85. /// Return the path from a full path.
  86. String GetPath(const String& fullPath);
  87. /// Return the filename from a full path.
  88. String GetFileName(const String& fullPath);
  89. /// Return the extension from a full path, converted to lowercase.
  90. String GetExtension(const String& fullPath);
  91. /// Return the filename and extension from a full path. The extension will be converted to lowercase.
  92. String GetFileNameAndExtension(const String& fullPath);
  93. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  94. String AddTrailingSlash(const String& pathName);
  95. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  96. String RemoveTrailingSlash(const String& pathName);
  97. /// Return the parent path, or the path itself if not available.
  98. String GetParentPath(const String& pathName);
  99. /// Convert a path to internal format (use slashes.)
  100. String GetInternalPath(const String& pathName);
  101. /// Convert a path to the format required by the operating system.
  102. String GetNativePath(const String& pathName);
  103. /// Convert a path to the format required by the operating system in wide characters
  104. WString GetWideNativePath(const String& pathName);