File.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. struct PackageEntry;
  38. //! A file opened either through the filesystem or from within a package file
  39. class File : public RefCounted, public Deserializer, public Serializer
  40. {
  41. public:
  42. //! Construct and open the file with the specified name and open mode
  43. File(const std::string& fileName, FileMode mode = FILE_READ);
  44. //! Construct by specifying an entry within a package file
  45. File(const std::string& fileName, File* packageFile, const PackageEntry& entry);
  46. //! Destruct. Close the file if open
  47. virtual ~File();
  48. //! Read bytes from the file
  49. virtual void read(void* dest, unsigned size);
  50. //! Set position from the beginning of the file
  51. virtual unsigned seek(unsigned position);
  52. //! Write bytes to the file
  53. virtual void write(const void* data, unsigned size);
  54. //! Return the file name
  55. virtual const std::string& getName() const { return mFileName; }
  56. //! Close the file. Can not be re-opened without constructing another File
  57. void close();
  58. //! Change the file name. Used by the resource system
  59. void setName(const std::string& name);
  60. //! Return the open mode
  61. FileMode getMode() const { return mMode; }
  62. //! Return a checksum of the file contents, using the SDBM hash algorithm
  63. unsigned getChecksum();
  64. //! Return the file handle
  65. FILE* getHandle() const { return mHandle; }
  66. private:
  67. //! File handle
  68. FILE* mHandle;
  69. //! Pointer to package file if opened from within a package
  70. WeakPtr<File> mPackageFile;
  71. //! File name
  72. std::string mFileName;
  73. //! Open mode
  74. FileMode mMode;
  75. //! Start position within a package file
  76. unsigned mOffset;
  77. //! Content checksum
  78. unsigned mChecksum;
  79. };
  80. //! Check if a file exists
  81. bool fileExists(const std::string& fileName);
  82. //! Create a directory
  83. void createDirectory(const std::string& pathName);
  84. //! Scan a directory for specified files
  85. std::vector<std::string> scanDirectory(const std::string& pathName, const std::string& filter, bool recursive);
  86. //! Register a path as being allowed to access
  87. void registerDirectory(const std::string& pathName);
  88. //! Check if a path is allowed to be accessed. If no paths defined, all are allowed
  89. bool checkDirectoryAccess(const std::string& pathName);
  90. //! Split a full path to path, filename and extension. Optionally convert the extension to lowercase
  91. void splitPath(const std::string& fullPath, std::string& pathName, std::string& fileName, std::string& extension, bool lowerCaseExtension = true);
  92. //! Return the path from a full path
  93. std::string getPath(const std::string& fullPath);
  94. //! Return the filename from a full path
  95. std::string getFileName(const std::string& fullPath);
  96. //! Return the extension from a full path and optionally convert to lowercase
  97. std::string getExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  98. //! Return the filename and extension from a full path. Optionally convert the extension to lowercase
  99. std::string getFileNameAndExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  100. //! Fix a path so that it contains a slash in the end, and convert backslashes to slashes
  101. std::string fixPath(const std::string& pathName);
  102. //! Convert a path to the format required by the operating system
  103. std::string getOSPath(const std::string& pathName, bool forNativeApi = false);
  104. #endif // COMMON_FILE_H