File.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class PackageFile;
  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 a package file source
  45. File(const PackageFile& package, const std::string& fileName);
  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. //! File name
  70. std::string mFileName;
  71. //! Open mode
  72. FileMode mMode;
  73. //! Start position within a package file, 0 for regular files
  74. unsigned mOffset;
  75. //! Content checksum
  76. unsigned mChecksum;
  77. };
  78. //! Check if a file exists
  79. bool fileExists(const std::string& fileName);
  80. //! Check if a directory exists
  81. bool directoryExists(const std::string& pathName);
  82. //! Create a directory
  83. void createDirectory(const std::string& pathName);
  84. //! Return the absolute working directory
  85. std::string getWorkingDirectory();
  86. //! Scan a directory for specified files
  87. std::vector<std::string> scanDirectory(const std::string& pathName, const std::string& filter, bool recursive, bool directories, bool hidden);
  88. //! Register a path as being allowed to access
  89. void registerDirectory(const std::string& pathName);
  90. //! Check if a path is allowed to be accessed. If no paths defined, all are allowed
  91. bool checkDirectoryAccess(const std::string& pathName);
  92. //! Split a full path to path, filename and extension. Optionally convert the extension to lowercase
  93. void splitPath(const std::string& fullPath, std::string& pathName, std::string& fileName, std::string& extension, bool lowerCaseExtension = true);
  94. //! Return the path from a full path
  95. std::string getPath(const std::string& fullPath);
  96. //! Return the filename from a full path
  97. std::string getFileName(const std::string& fullPath);
  98. //! Return the extension from a full path and optionally convert to lowercase
  99. std::string getExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  100. //! Return the filename and extension from a full path. Optionally convert the extension to lowercase
  101. std::string getFileNameAndExtension(const std::string& fullPath, bool lowerCaseExtension = true);
  102. //! Fix a path so that it contains a slash in the end, and convert backslashes to slashes
  103. std::string fixPath(const std::string& pathName);
  104. //! Remove the slash or backslash from the end of a path if exists
  105. std::string unfixPath(const std::string& pathName);
  106. //! Convert a path to the format required by the operating system
  107. std::string getOSPath(const std::string& pathName, bool forNativeApi = false);
  108. #endif // COMMON_FILE_H