FileSystem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "Vector.h"
  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 exists 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());
  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 being allowed to access
  59. void RegisterPath(const String& pathName);
  60. /// Return the absolute current working directory
  61. String GetCurrentDir();
  62. /// Check if a path is allowed to be accessed. If no paths defined, all are allowed
  63. bool CheckAccess(const String& pathName);
  64. /// Check if a file exists
  65. bool FileExists(const String& fileName);
  66. /// Check if a directory exists
  67. bool DirExists(const String& pathName);
  68. /// Scan a directory for specified files
  69. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive);
  70. /// Return the program's directory
  71. String GetProgramDir();
  72. /// Return the user documents directory
  73. String GetUserDocumentsDir();
  74. /// Return the system fonts directory
  75. String GetSystemFontDir();
  76. private:
  77. /// Scan directory, called internally
  78. void ScanDirInternal(Vector<String>& result, String path, const String& startPath,
  79. const String& filter, unsigned flags, bool recursive);
  80. /// Allowed directories
  81. Set<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. /// Fix a path so that it contains a slash in the end, and convert backslashes to slashes
  94. String AddTrailingSlash(const String& pathName);
  95. /// Remove the slash or backslash from the end of a path if exists
  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 the format required by the operating system
  100. String GetNativePath(const String& pathName, bool forNativeApi = false);