ImfStdIO.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_STD_IO_H
  35. #define INCLUDED_IMF_STD_IO_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Low-level file input and output for OpenEXR
  39. // based on C++ standard iostreams.
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include "ImfIO.h"
  43. #include "ImfNamespace.h"
  44. #include "ImfExport.h"
  45. #include <fstream>
  46. #include <sstream>
  47. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  48. //-------------------------------------------
  49. // class StdIFStream -- an implementation of
  50. // class OPENEXR_IMF_INTERNAL_NAMESPACE::IStream based on class std::ifstream
  51. //-------------------------------------------
  52. class StdIFStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::IStream
  53. {
  54. public:
  55. //-------------------------------------------------------
  56. // A constructor that opens the file with the given name.
  57. // The destructor will close the file.
  58. //-------------------------------------------------------
  59. IMF_EXPORT
  60. StdIFStream (const char fileName[]);
  61. //---------------------------------------------------------
  62. // A constructor that uses a std::ifstream that has already
  63. // been opened by the caller. The StdIFStream's destructor
  64. // will not close the std::ifstream.
  65. //---------------------------------------------------------
  66. IMF_EXPORT
  67. StdIFStream (std::ifstream &is, const char fileName[]);
  68. IMF_EXPORT
  69. virtual ~StdIFStream ();
  70. IMF_EXPORT
  71. virtual bool read (char c[/*n*/], int n);
  72. IMF_EXPORT
  73. virtual Int64 tellg ();
  74. IMF_EXPORT
  75. virtual void seekg (Int64 pos);
  76. IMF_EXPORT
  77. virtual void clear ();
  78. private:
  79. std::ifstream * _is;
  80. bool _deleteStream;
  81. };
  82. //-------------------------------------------
  83. // class StdOFStream -- an implementation of
  84. // class OPENEXR_IMF_INTERNAL_NAMESPACE::OStream based on class std::ofstream
  85. //-------------------------------------------
  86. class StdOFStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::OStream
  87. {
  88. public:
  89. //-------------------------------------------------------
  90. // A constructor that opens the file with the given name.
  91. // The destructor will close the file.
  92. //-------------------------------------------------------
  93. IMF_EXPORT
  94. StdOFStream (const char fileName[]);
  95. //---------------------------------------------------------
  96. // A constructor that uses a std::ofstream that has already
  97. // been opened by the caller. The StdOFStream's destructor
  98. // will not close the std::ofstream.
  99. //---------------------------------------------------------
  100. IMF_EXPORT
  101. StdOFStream (std::ofstream &os, const char fileName[]);
  102. IMF_EXPORT
  103. virtual ~StdOFStream ();
  104. IMF_EXPORT
  105. virtual void write (const char c[/*n*/], int n);
  106. IMF_EXPORT
  107. virtual Int64 tellp ();
  108. IMF_EXPORT
  109. virtual void seekp (Int64 pos);
  110. private:
  111. std::ofstream * _os;
  112. bool _deleteStream;
  113. };
  114. //------------------------------------------------
  115. // class StdOSStream -- an implementation of class
  116. // OPENEXR_IMF_INTERNAL_NAMESPACE::OStream, based on class std::ostringstream
  117. //------------------------------------------------
  118. class StdOSStream: public OPENEXR_IMF_INTERNAL_NAMESPACE::OStream
  119. {
  120. public:
  121. IMF_EXPORT
  122. StdOSStream ();
  123. IMF_EXPORT
  124. virtual void write (const char c[/*n*/], int n);
  125. IMF_EXPORT
  126. virtual Int64 tellp ();
  127. IMF_EXPORT
  128. virtual void seekp (Int64 pos);
  129. IMF_EXPORT
  130. std::string str () const {return _os.str();}
  131. private:
  132. std::ostringstream _os;
  133. };
  134. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  135. #endif