FileSystem.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Set.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();
  61. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  62. bool CheckAccess(const String& pathName);
  63. /// Check if a file exists.
  64. bool FileExists(const String& fileName);
  65. /// Check if a directory exists.
  66. bool DirExists(const String& pathName);
  67. /// Scan a directory for specified files.
  68. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive);
  69. /// Return the program's directory.
  70. String GetProgramDir();
  71. /// Return the user documents directory.
  72. String GetUserDocumentsDir();
  73. private:
  74. /// Scan directory, called internally.
  75. void ScanDirInternal(Vector<String>& result, String path, const String& startPath,
  76. const String& filter, unsigned flags, bool recursive);
  77. /// Allowed directories.
  78. Set<String> allowedPaths_;
  79. };
  80. /// Split a full path to path, filename and extension. The extension will be converted to lowercase.
  81. void SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension);
  82. /// Return the path from a full path.
  83. String GetPath(const String& fullPath);
  84. /// Return the filename from a full path.
  85. String GetFileName(const String& fullPath);
  86. /// Return the extension from a full path, converted to lowercase.
  87. String GetExtension(const String& fullPath);
  88. /// Return the filename and extension from a full path. The extension will be converted to lowercase.
  89. String GetFileNameAndExtension(const String& fullPath);
  90. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  91. String AddTrailingSlash(const String& pathName);
  92. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  93. String RemoveTrailingSlash(const String& pathName);
  94. /// Return the parent path, or the path itself if not available.
  95. String GetParentPath(const String& pathName);
  96. /// Convert a path to internal format (use slashes.)
  97. String GetInternalPath(const String& pathName);
  98. /// Convert a path to the format required by the operating system.
  99. String GetNativePath(const String& pathName);