FileSystem.h 5.7 KB

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