ImfIO.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_IO_H
  6. #define INCLUDED_IMF_IO_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Low-level file input and output for OpenEXR.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "ImfForward.h"
  13. #include <string>
  14. #include <cstdint>
  15. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  16. //-----------------------------------------------------------
  17. // class IStream -- an abstract base class for input streams.
  18. //-----------------------------------------------------------
  19. class IMF_EXPORT_TYPE IStream
  20. {
  21. public:
  22. //-----------
  23. // Destructor
  24. //-----------
  25. IMF_EXPORT virtual ~IStream ();
  26. //-------------------------------------------------
  27. // Does this input stream support memory-mapped IO?
  28. //
  29. // Memory-mapped streams can avoid an extra copy;
  30. // memory-mapped read operations return a pointer
  31. // to an internal buffer instead of copying data
  32. // into a buffer supplied by the caller.
  33. //-------------------------------------------------
  34. IMF_EXPORT virtual bool isMemoryMapped () const;
  35. //------------------------------------------------------
  36. // Read from the stream:
  37. //
  38. // read(c,n) reads n bytes from the stream, and stores
  39. // them in array c. If the stream contains less than n
  40. // bytes, or if an I/O error occurs, read(c,n) throws
  41. // an exception. If read(c,n) reads the last byte from
  42. // the file it returns false, otherwise it returns true.
  43. //------------------------------------------------------
  44. virtual bool read (char c[/*n*/], int n) = 0;
  45. //---------------------------------------------------
  46. // Read from a memory-mapped stream:
  47. //
  48. // readMemoryMapped(n) reads n bytes from the stream
  49. // and returns a pointer to the first byte. The
  50. // returned pointer remains valid until the stream
  51. // is closed. If there are less than n byte left to
  52. // read in the stream or if the stream is not memory-
  53. // mapped, readMemoryMapped(n) throws an exception.
  54. //---------------------------------------------------
  55. IMF_EXPORT virtual char * readMemoryMapped (int n);
  56. //--------------------------------------------------------
  57. // Get the current reading position, in bytes from the
  58. // beginning of the file. If the next call to read() will
  59. // read the first byte in the file, tellg() returns 0.
  60. //--------------------------------------------------------
  61. virtual uint64_t tellg () = 0;
  62. //-------------------------------------------
  63. // Set the current reading position.
  64. // After calling seekg(i), tellg() returns i.
  65. //-------------------------------------------
  66. virtual void seekg (uint64_t pos) = 0;
  67. //------------------------------------------------------
  68. // Clear error conditions after an operation has failed.
  69. //------------------------------------------------------
  70. IMF_EXPORT virtual void clear ();
  71. //------------------------------------------------------
  72. // Get the name of the file associated with this stream.
  73. //------------------------------------------------------
  74. IMF_EXPORT const char * fileName () const;
  75. protected:
  76. IMF_EXPORT IStream (const char fileName[]);
  77. private:
  78. IStream (const IStream &) = delete;
  79. IStream & operator = (const IStream &) = delete;
  80. IStream (IStream &&) = delete;
  81. IStream & operator = (IStream &&) = delete;
  82. std::string _fileName;
  83. };
  84. //-----------------------------------------------------------
  85. // class OStream -- an abstract base class for output streams
  86. //-----------------------------------------------------------
  87. class IMF_EXPORT_TYPE OStream
  88. {
  89. public:
  90. //-----------
  91. // Destructor
  92. //-----------
  93. IMF_EXPORT virtual ~OStream ();
  94. //----------------------------------------------------------
  95. // Write to the stream:
  96. //
  97. // write(c,n) takes n bytes from array c, and stores them
  98. // in the stream. If an I/O error occurs, write(c,n) throws
  99. // an exception.
  100. //----------------------------------------------------------
  101. virtual void write (const char c[/*n*/], int n) = 0;
  102. //---------------------------------------------------------
  103. // Get the current writing position, in bytes from the
  104. // beginning of the file. If the next call to write() will
  105. // start writing at the beginning of the file, tellp()
  106. // returns 0.
  107. //---------------------------------------------------------
  108. virtual uint64_t tellp () = 0;
  109. //-------------------------------------------
  110. // Set the current writing position.
  111. // After calling seekp(i), tellp() returns i.
  112. //-------------------------------------------
  113. virtual void seekp (uint64_t pos) = 0;
  114. //------------------------------------------------------
  115. // Get the name of the file associated with this stream.
  116. //------------------------------------------------------
  117. IMF_EXPORT const char * fileName () const;
  118. protected:
  119. IMF_EXPORT OStream (const char fileName[]);
  120. private:
  121. OStream (const OStream &) = delete;
  122. OStream & operator = (const OStream &) = delete;
  123. OStream (OStream &&) = delete;
  124. OStream & operator = (OStream &&) = delete;
  125. std::string _fileName;
  126. };
  127. //-----------------------
  128. // Helper classes for Xdr
  129. //-----------------------
  130. struct StreamIO
  131. {
  132. static inline void
  133. writeChars (OStream &os, const char c[/*n*/], int n)
  134. {
  135. os.write (c, n);
  136. }
  137. static inline bool
  138. readChars (IStream &is, char c[/*n*/], int n)
  139. {
  140. return is.read (c, n);
  141. }
  142. };
  143. struct CharPtrIO
  144. {
  145. static inline void
  146. writeChars (char *&op, const char c[/*n*/], int n)
  147. {
  148. while (n--)
  149. *op++ = *c++;
  150. }
  151. static inline bool
  152. readChars (const char *&ip, char c[/*n*/], int n)
  153. {
  154. while (n--)
  155. *c++ = *ip++;
  156. return true;
  157. }
  158. };
  159. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  160. #endif