File.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef ANKI_UTIL_FILE_H
  2. #define ANKI_UTIL_FILE_H
  3. #include "anki/util/StringList.h"
  4. #include <string>
  5. namespace anki {
  6. /// @addtogroup util
  7. /// @{
  8. /// @addtogroup filesystem
  9. /// @{
  10. /// An abstraction over typical files and files in ziped archives. This class
  11. /// can read from regular C files, zip files and on Android from the packed
  12. /// asset files.
  13. /// To identify the file:
  14. /// - If the path contains ".ankizip" (eg /path/to/arch.ankizip/path/file.ext)
  15. /// it tries to open the archive and read the file from there.
  16. /// - If the filename starts with '$' it will try to load a system specific
  17. /// file. For Android this is a file in the .apk
  18. /// - If the above are false then try to load a regular C file
  19. class File
  20. {
  21. private:
  22. /// Internal filetype
  23. enum FileType
  24. {
  25. FT_C = 1 << 0, ///< C file
  26. FT_ZIP = 1 << 1, ///< Ziped file
  27. FT_SPECIAL = 1 << 2 ///< For example file is located in the android apk
  28. };
  29. public:
  30. /// Open mode
  31. enum OpenFlag
  32. {
  33. OF_READ = 1 << 3,
  34. OF_WRITE = 1 << 4,
  35. OF_APPEND = OF_WRITE | (1 << 5),
  36. OF_BINARY = 1 << 6
  37. };
  38. /// The 2 available byte orders. Used in binary files.
  39. enum Endianness
  40. {
  41. E_LITTLE_ENDIAN = 1 << 7, ///< The default
  42. E_BIG_ENDIAN = 1 << 8
  43. };
  44. /// Passed to seek function
  45. enum SeekOrigin
  46. {
  47. SO_BEGINNING = SEEK_SET,
  48. SO_CURRENT = SEEK_CUR,
  49. SO_END = SEEK_END
  50. };
  51. /// Default constructor
  52. File()
  53. : file(nullptr), flags(0)
  54. {}
  55. /// Open file
  56. File(const char* filename, U16 openMask)
  57. : file(nullptr), flags(0)
  58. {
  59. open(filename, openMask);
  60. }
  61. /// Closes the file if it's open
  62. ~File();
  63. /// Open a file
  64. /// @param[in] filename The file to open
  65. /// @param[in] openMask The open flags. It's a combination of OpenFlag and
  66. /// Endianness enums
  67. void open(const char* filename, U16 openMask);
  68. /// Return true if the file is oppen
  69. Bool isOpen() const
  70. {
  71. return file != nullptr;
  72. }
  73. /// Close the file
  74. void close();
  75. /// @name Read methods
  76. /// @{
  77. /// Read data from the file
  78. void read(void* buff, PtrSize size);
  79. /// Read all the contents of a text file
  80. void readAllText(std::string& out);
  81. /// Read all the contents of a text file and return the result in lines
  82. void readAllTextLines(StringList& lines);
  83. /// Read 32bit unsigned integer. Set the endianness if the file's
  84. /// endianness is different from the machine's
  85. U32 readU32();
  86. /// Read 32bit float. Set the endianness if the file's endianness is
  87. /// different from the machine's
  88. F32 readF32();
  89. /// @}
  90. /// @name Write methods
  91. /// @{
  92. /// Write data to the file
  93. void write(void* buff, PtrSize size);
  94. /// Write text
  95. void writeText(const char* format, ...);
  96. /// @}
  97. /// Set the position indicator to a new position
  98. /// @param offset Number of bytes to offset from origin
  99. /// @param origin Position used as reference for the offset
  100. void seek(PtrSize offset, SeekOrigin origin);
  101. /// @name Public statics
  102. /// @{
  103. /// Get file extension
  104. /// @param[in] filename The file to open
  105. /// @return nullptr on failure and if the dot is the last character
  106. static const char* getFileExtension(const char* filename);
  107. /// File exists?
  108. static Bool fileExists(const char* filename);
  109. /// @}
  110. private:
  111. void* file; ///< A native type
  112. U16 flags; ///< All the flags. Initialy zero and set on open
  113. /// Get the current machine's endianness
  114. static Endianness getMachineEndianness();
  115. /// Get the type of the file
  116. FileType identifyFile(const char* filename,
  117. std::string* archive, std::string* filenameInArchive);
  118. /// Open a C file
  119. void openCFile(const char* filename, U16 flags);
  120. /// Open an archive and the file inside
  121. /// @param[in] archive The filename of the archive
  122. /// @param[in] archived The filename of the file inside the archive
  123. void openZipFile(const char* archive, const char* archived, U16 flags);
  124. #if ANKI_OS == ANKI_OS_ANDROID
  125. /// Open an Android file
  126. void openAndFile(const char* filename, U16 flags);
  127. #endif
  128. };
  129. /// Return true if directory exists?
  130. extern Bool directoryExists(const char* dir);
  131. /// Equivalent to: rm -rf dir
  132. extern void removeDirectory(const char* dir);
  133. /// Equivalent to: mkdir dir
  134. extern void createDirectory(const char* dir);
  135. /// @}
  136. /// @}
  137. } // end namespace anki
  138. #endif