FileSystem.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  27. {
  28. /// Return files.
  29. static const unsigned SCAN_FILES = 0x1;
  30. /// Return directories.
  31. static const unsigned SCAN_DIRS = 0x2;
  32. /// Return also hidden files.
  33. static const unsigned SCAN_HIDDEN = 0x4;
  34. /// Subsystem for file and directory operations and access control.
  35. class FileSystem : public Object
  36. {
  37. OBJECT(FileSystem);
  38. public:
  39. /// Construct.
  40. FileSystem(Context* context);
  41. /// Destruct.
  42. ~FileSystem();
  43. /// Set the current working directory.
  44. bool SetCurrentDir(const String& pathName);
  45. /// Create a directory.
  46. bool CreateDir(const String& pathName);
  47. /// Run a program using the command interpreter, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  48. int SystemCommand(const String& commandLine);
  49. /// Run a specific program, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  50. int SystemRun(const String& fileName, const Vector<String>& arguments);
  51. /// Open a file in an external program, with mode such as "edit" optionally specified. Will fail if any allowed paths are defined.
  52. bool SystemOpen(const String& fileName, const String& mode = String());
  53. /// Copy a file. Return true if successful.
  54. bool Copy(const String& srcFileName, const String& destFileName);
  55. /// Rename a file. Return true if successful.
  56. bool Rename(const String& srcFileName, const String& destFileName);
  57. /// Delete a file. Return true if successful.
  58. bool Delete(const String& fileName);
  59. /// Register a path as allowed to access. If no paths are registered, all are allowed.
  60. void RegisterPath(const String& pathName);
  61. /// Return the absolute current working directory.
  62. String GetCurrentDir() const;
  63. /// Return whether paths have been registered.
  64. bool HasRegisteredPaths() const { return allowedPaths_.Size() > 0; }
  65. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  66. bool CheckAccess(const String& pathName) const;
  67. /// Returns the file's last modified time as seconds since 1.1.1970, or 0 if can not be accessed.
  68. unsigned GetLastModifiedTime(const String& fileName) const;
  69. /// Check if a file exists.
  70. bool FileExists(const String& fileName) const;
  71. /// Check if a directory exists.
  72. bool DirExists(const String& pathName) const;
  73. /// Scan a directory for specified files.
  74. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  75. /// Return the program's directory.
  76. String GetProgramDir() const;
  77. /// Return the user documents directory.
  78. String GetUserDocumentsDir() const;
  79. private:
  80. /// Scan directory, called internally.
  81. void ScanDirInternal(Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  82. /// Allowed directories.
  83. HashSet<String> allowedPaths_;
  84. };
  85. /// Split a full path to path, filename and extension. The extension will be converted to lowercase.
  86. void SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension);
  87. /// Return the path from a full path.
  88. String GetPath(const String& fullPath);
  89. /// Return the filename from a full path.
  90. String GetFileName(const String& fullPath);
  91. /// Return the extension from a full path, converted to lowercase.
  92. String GetExtension(const String& fullPath);
  93. /// Return the filename and extension from a full path. The extension will be converted to lowercase.
  94. String GetFileNameAndExtension(const String& fullPath);
  95. /// Replace the extension of a file name with another.
  96. String ReplaceExtension(const String& fullPath, const String& newExtension);
  97. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  98. String AddTrailingSlash(const String& pathName);
  99. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  100. String RemoveTrailingSlash(const String& pathName);
  101. /// Return the parent path, or the path itself if not available.
  102. String GetParentPath(const String& pathName);
  103. /// Convert a path to internal format (use slashes.)
  104. String GetInternalPath(const String& pathName);
  105. /// Convert a path to the format required by the operating system.
  106. String GetNativePath(const String& pathName);
  107. /// Convert a path to the format required by the operating system in wide characters
  108. WString GetWideNativePath(const String& pathName);
  109. }