File.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/String.h>
  7. #include <AnKi/Util/Enum.h>
  8. #include <cstdio>
  9. namespace anki
  10. {
  11. /// @addtogroup util_file
  12. /// @{
  13. /// Open mode
  14. /// @memberof File
  15. enum class FileOpenFlag : U8
  16. {
  17. NONE = 0,
  18. READ = 1 << 0,
  19. WRITE = 1 << 1,
  20. APPEND = WRITE | (1 << 3),
  21. BINARY = 1 << 4,
  22. ENDIAN_LITTLE = 1 << 5, ///< The default
  23. ENDIAN_BIG = 1 << 6,
  24. SPECIAL = 1 << 7, ///< Android package file.
  25. };
  26. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(FileOpenFlag)
  27. /// Passed to seek function
  28. /// @memberof File
  29. enum class FileSeekOrigin
  30. {
  31. BEGINNING = SEEK_SET,
  32. CURRENT = SEEK_CUR,
  33. END = SEEK_END
  34. };
  35. /// An abstraction over typical files and files in ziped archives. This class can read from regular C files, zip files
  36. /// and on Android from the packed asset files.
  37. /// To identify the file:
  38. /// - If the filename starts with '$' it will try to load a system specific file. For Android this is a file in the .apk
  39. /// - If the above are false then try to load a regular C file
  40. class File
  41. {
  42. public:
  43. /// Default constructor
  44. File() = default;
  45. File(const File&) = delete; // Non-copyable
  46. File& operator=(const File&) = delete; // Non-copyable
  47. /// Move
  48. File(File&& b)
  49. {
  50. *this = std::move(b);
  51. }
  52. /// Closes the file if it's open
  53. ~File();
  54. /// Move
  55. File& operator=(File&& b);
  56. /// Open a file.
  57. /// @param[in] filename The file to open
  58. /// @param[in] openMask The open flags. It's a combination of FileOpenFlag enum
  59. ANKI_USE_RESULT Error open(const CString& filename, FileOpenFlag openMask);
  60. /// Return true if the file is oppen
  61. Bool isOpen() const
  62. {
  63. return m_file != nullptr;
  64. }
  65. /// Close the file
  66. void close();
  67. /// Flush pending operations
  68. ANKI_USE_RESULT Error flush();
  69. /// Read data from the file
  70. ANKI_USE_RESULT Error read(void* buff, PtrSize size);
  71. /// Read all the contents of a text file
  72. /// If the file is not rewined it will probably fail
  73. ANKI_USE_RESULT Error readAllText(GenericMemoryPoolAllocator<U8> alloc, String& out);
  74. /// Read all the contents of a text file. If the file is not rewined it will probably fail.
  75. ANKI_USE_RESULT Error readAllText(StringAuto& out);
  76. /// Read 32bit unsigned integer. Set the endianness if the file's endianness is different from the machine's.
  77. ANKI_USE_RESULT Error readU32(U32& u);
  78. /// Read 32bit float. Set the endianness if the file's endianness is different from the machine's.
  79. ANKI_USE_RESULT Error readF32(F32& f);
  80. /// Write data to the file
  81. ANKI_USE_RESULT Error write(const void* buff, PtrSize size);
  82. /// Write formated text
  83. ANKI_USE_RESULT Error writeText(CString format, ...);
  84. /// Set the position indicator to a new position.
  85. /// @param offset Number of bytes to offset from origin
  86. /// @param origin Position used as reference for the offset
  87. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin);
  88. /// Return the position indicator inside the file.
  89. PtrSize tell();
  90. /// The the size of the file.
  91. PtrSize getSize() const
  92. {
  93. ANKI_ASSERT(!(m_flags & FileOpenFlag::WRITE));
  94. return m_size;
  95. }
  96. private:
  97. void* m_file = nullptr; ///< A native file type
  98. FileOpenFlag m_flags = FileOpenFlag::NONE; ///< All the flags. Set on open
  99. PtrSize m_size = 0;
  100. /// Get the current machine's endianness
  101. static FileOpenFlag getMachineEndianness();
  102. /// Open a C file
  103. ANKI_USE_RESULT Error openCFile(const CString& filename, FileOpenFlag flags);
  104. #if ANKI_OS_ANDROID
  105. /// Open an Android file
  106. ANKI_USE_RESULT Error openAndroidFile(const CString& filename, FileOpenFlag flags);
  107. #endif
  108. void zero()
  109. {
  110. m_file = nullptr;
  111. m_flags = FileOpenFlag::NONE;
  112. m_size = 0;
  113. }
  114. };
  115. /// @}
  116. } // end namespace anki