ImfStdIO.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_STD_IO_H
  6. #define INCLUDED_IMF_STD_IO_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Low-level file input and output for OpenEXR
  10. // based on C++ standard iostreams.
  11. //
  12. //-----------------------------------------------------------------------------
  13. #include "ImfExport.h"
  14. #include "ImfNamespace.h"
  15. #include "ImfIO.h"
  16. #include <fstream>
  17. #include <sstream>
  18. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  19. //-------------------------------------------
  20. // class StdIFStream -- an implementation of
  21. // class OPENEXR_IMF_INTERNAL_NAMESPACE::IStream based on class std::ifstream
  22. //-------------------------------------------
  23. class IMF_EXPORT_TYPE StdIFStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::IStream
  24. {
  25. public:
  26. //-------------------------------------------------------
  27. // A constructor that opens the file with the given name.
  28. // The destructor will close the file.
  29. //-------------------------------------------------------
  30. IMF_EXPORT StdIFStream (const char fileName[]);
  31. //---------------------------------------------------------
  32. // A constructor that uses a std::ifstream that has already
  33. // been opened by the caller. The StdIFStream's destructor
  34. // will not close the std::ifstream.
  35. //---------------------------------------------------------
  36. IMF_EXPORT StdIFStream (std::ifstream &is, const char fileName[]);
  37. IMF_EXPORT virtual ~StdIFStream ();
  38. StdIFStream (const StdIFStream &) = delete;
  39. StdIFStream (StdIFStream &&) = delete;
  40. StdIFStream &operator=(const StdIFStream &) = delete;
  41. StdIFStream &operator=(StdIFStream &&) = delete;
  42. IMF_EXPORT virtual bool read (char c[/*n*/], int n);
  43. IMF_EXPORT virtual uint64_t tellg ();
  44. IMF_EXPORT virtual void seekg (uint64_t pos);
  45. IMF_EXPORT virtual void clear ();
  46. private:
  47. std::ifstream * _is;
  48. bool _deleteStream;
  49. };
  50. //------------------------------------------------
  51. // class StdISStream -- an implementation of class
  52. // OPENEXR_IMF_INTERNAL_NAMESPACE::IStream, based on class std::istringstream
  53. //------------------------------------------------
  54. class IMF_EXPORT_TYPE StdISStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::IStream
  55. {
  56. public:
  57. IMF_EXPORT StdISStream ();
  58. IMF_EXPORT ~StdISStream ();
  59. StdISStream (const StdISStream &) = delete;
  60. StdISStream (StdISStream &&) = delete;
  61. StdISStream &operator=(const StdISStream &) = delete;
  62. StdISStream &operator=(StdISStream &&) = delete;
  63. IMF_EXPORT virtual bool read (char c[/*n*/], int n);
  64. IMF_EXPORT virtual uint64_t tellg ();
  65. IMF_EXPORT virtual void seekg (uint64_t pos);
  66. IMF_EXPORT virtual void clear ();
  67. IMF_EXPORT std::string str () const;
  68. IMF_EXPORT void str (const std::string &s);
  69. private:
  70. std::istringstream _is;
  71. };
  72. //-------------------------------------------
  73. // class StdOFStream -- an implementation of
  74. // class OPENEXR_IMF_INTERNAL_NAMESPACE::OStream based on class std::ofstream
  75. //-------------------------------------------
  76. class IMF_EXPORT_TYPE StdOFStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::OStream
  77. {
  78. public:
  79. //-------------------------------------------------------
  80. // A constructor that opens the file with the given name.
  81. // The destructor will close the file.
  82. //-------------------------------------------------------
  83. IMF_EXPORT StdOFStream (const char fileName[]);
  84. //---------------------------------------------------------
  85. // A constructor that uses a std::ofstream that has already
  86. // been opened by the caller. The StdOFStream's destructor
  87. // will not close the std::ofstream.
  88. //---------------------------------------------------------
  89. IMF_EXPORT StdOFStream (std::ofstream &os, const char fileName[]);
  90. IMF_EXPORT virtual ~StdOFStream ();
  91. StdOFStream (const StdOFStream &) = delete;
  92. StdOFStream (StdOFStream &&) = delete;
  93. StdOFStream &operator=(const StdOFStream &) = delete;
  94. StdOFStream &operator=(StdOFStream &&) = delete;
  95. IMF_EXPORT virtual void write (const char c[/*n*/], int n);
  96. IMF_EXPORT virtual uint64_t tellp ();
  97. IMF_EXPORT virtual void seekp (uint64_t pos);
  98. private:
  99. std::ofstream * _os;
  100. bool _deleteStream;
  101. };
  102. //------------------------------------------------
  103. // class StdOSStream -- an implementation of class
  104. // OPENEXR_IMF_INTERNAL_NAMESPACE::OStream, based on class std::ostringstream
  105. //------------------------------------------------
  106. class IMF_EXPORT_TYPE StdOSStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::OStream
  107. {
  108. public:
  109. IMF_EXPORT StdOSStream ();
  110. IMF_EXPORT ~StdOSStream ();
  111. StdOSStream (const StdOSStream &) = delete;
  112. StdOSStream (StdOSStream &&) = delete;
  113. StdOSStream &operator=(const StdOSStream &) = delete;
  114. StdOSStream &operator=(StdOSStream &&) = delete;
  115. IMF_EXPORT virtual void write (const char c[/*n*/], int n);
  116. IMF_EXPORT virtual uint64_t tellp ();
  117. IMF_EXPORT virtual void seekp (uint64_t pos);
  118. IMF_EXPORT std::string str () const;
  119. private:
  120. std::ostringstream _os;
  121. };
  122. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  123. #endif