File.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef COMMON_FILE_H
  24. #define COMMON_FILE_H
  25. #include "Deserializer.h"
  26. #include "Serializer.h"
  27. #include "SharedPtr.h"
  28. #include <cstdio>
  29. #include <set>
  30. //! File open mode
  31. enum FileMode
  32. {
  33. FILE_READ = 0,
  34. FILE_WRITE,
  35. FILE_READWRITE
  36. };
  37. //! Return files
  38. static const unsigned SCAN_FILES = 1;
  39. //! Return directories
  40. static const unsigned SCAN_DIRECTORIES = 2;
  41. //! Return also hidden files
  42. static const unsigned SCAN_HIDDEN = 4;
  43. class PackageFile;
  44. //! A file opened either through the filesystem or from within a package file
  45. class File : public RefCounted, public Deserializer, public Serializer
  46. {
  47. public:
  48. //! Construct and open the file with the specified name and open mode
  49. File(const std::string& fileName, FileMode mode = FILE_READ);
  50. //! Construct by specifying a package file source
  51. File(const PackageFile& package, const std::string& fileName);
  52. //! Destruct. Close the file if open
  53. virtual ~File();
  54. //! Read bytes from the file
  55. virtual void read(void* dest, unsigned size);
  56. //! Set position from the beginning of the file
  57. virtual unsigned seek(unsigned position);
  58. //! Write bytes to the file
  59. virtual void write(const void* data, unsigned size);
  60. //! Return the file name
  61. virtual const std::string& getName() const { return mFileName; }
  62. //! Close the file. Can not be re-opened without constructing another File
  63. void close();
  64. //! Change the file name. Used by the resource system
  65. void setName(const std::string& name);
  66. //! Return the open mode
  67. FileMode getMode() const { return mMode; }
  68. //! Return a checksum of the file contents, using the SDBM hash algorithm
  69. unsigned getChecksum();
  70. //! Return the file handle
  71. FILE* getHandle() const { return mHandle; }
  72. private:
  73. //! File handle
  74. FILE* mHandle;
  75. //! File name
  76. std::string mFileName;
  77. //! Open mode
  78. FileMode mMode;
  79. //! Start position within a package file, 0 for regular files
  80. unsigned mOffset;
  81. //! Content checksum
  82. unsigned mChecksum;
  83. };
  84. //! Check if a file exists
  85. bool fileExists(const std::string& fileName);
  86. //! Check if a directory exists
  87. bool directoryExists(const std::string& pathName);
  88. //! Scan a directory for specified files
  89. void scanDirectory(std::vector<std::string>& result, const std::string& pathName, const std::string& filter, unsigned flags, bool recursive);
  90. //! Return the absolute current directory
  91. std::string getCurrentDirectory();
  92. //! Set the current directory
  93. bool setCurrentDirectory(const std::string& pathName);
  94. //! Create a directory
  95. bool createDirectory(const std::string& pathName);
  96. //! Register a path as being allowed to access
  97. void registerDirectory(const std::string& pathName);
  98. //! Check if a path is allowed to be accessed. If no paths defined, all are allowed
  99. bool checkDirectoryAccess(const std::string& pathName);
  100. //! Split a full path to path, filename and extension. Optionally convert the extension to lowercase
  101. void splitPath(const std::string& fullPath, std::string& pathName, std::string& fileName, std::string& extension, bool lowerCaseExtension = true);
  102. //! Return the path from a full path
  103. std::string getPath(const std::string& fullPath);
  104. //! Return the filename from a full path
  105. std::string getFileName(const std::string& fullPath);
  106. //! Return the extension from a full path and optionally convert to lowercase
  107. std::string getExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  108. //! Return the filename and extension from a full path. Optionally convert the extension to lowercase
  109. std::string getFileNameAndExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  110. //! Fix a path so that it contains a slash in the end, and convert backslashes to slashes
  111. std::string fixPath(const std::string& pathName);
  112. //! Remove the slash or backslash from the end of a path if exists
  113. std::string unfixPath(const std::string& pathName);
  114. //! Convert a path to the format required by the operating system
  115. std::string getOSPath(const std::string& pathName, bool forNativeApi = false);
  116. #endif // COMMON_FILE_H