gzbinstream.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef GZBINSTREAM_H
  2. #define GZBINSTREAM_H
  3. // ============================================================================
  4. // gzbinstream, C++ iostream classes for gzbinary in and output
  5. // Copyright (C) 2006 Frank Mattern
  6. //
  7. // This library is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 2.1 of the License, or (at your option) any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. // Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. // ============================================================================
  21. //
  22. // File : gzbinstream.h
  23. // Revision : $Revision: 1.2 $
  24. // Revision_date : $Date: 2009/05/28 11:36:30 $
  25. // Author(s) : Frank Mattern
  26. //
  27. // Standard streambuf implementation following Nicolai Josuttis, "The
  28. // Standard C++ Library".
  29. // ============================================================================
  30. #include "core/basics/binstream.h"
  31. // standard C++ with new header file names and std:: namespace
  32. #ifdef NICE_USELIB_ZLIB
  33. #ifdef NICE_USELIB_LINAL
  34. #include "LinAl/matrix.h"
  35. #endif
  36. #include "core/basics/gzstream.h"
  37. #include "core/basics/binstream.h"
  38. #include <iostream>
  39. #include <fstream>
  40. #include <zlib.h>
  41. namespace NICE{
  42. // ----------------------------------------------------------------------------
  43. // User classes. Use igzbinstream and ogzbinstream analogously to ifstream and
  44. // ofstream respectively. They read and write files based on the gz*
  45. // function interface of the zlib. Files are compatible with gzip compression.
  46. // ----------------------------------------------------------------------------
  47. /**
  48. * Input gzip binary stream class.
  49. * Supports reading of gzip binary files.
  50. *
  51. * @author Frank Mattern, Ferid Bajramovic
  52. */
  53. class igzbinstream : public NICE::ibinstream {
  54. private:
  55. igzstream stream;
  56. public:
  57. igzbinstream() {}
  58. igzbinstream(const char* name,
  59. std::ios_base::openmode mode = std::ios::in)
  60. : stream(name, mode) {}
  61. //gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
  62. virtual void read(char* data, unsigned int length) {
  63. stream.read(data, length);
  64. }
  65. inline void open(const char* name,
  66. std::ios_base::openmode mode = std::ios::out) {
  67. stream.open(name, mode);
  68. }
  69. inline void close() {
  70. stream.close();
  71. }
  72. inline bool good() const {
  73. return stream.good();
  74. }
  75. inline bool fail() const {
  76. return stream.fail();
  77. }
  78. };
  79. /**
  80. * Output gzip binary stream class.
  81. * Supports writing of binary gziped files.
  82. *
  83. * @note If the compiler cannot find the correct operator \c <<,
  84. * try casting your object of this class to \c (NICE::obinstream&).
  85. * However, this is NOT a good idea in combination with
  86. * \c NICE::IOStreamable (which is deprecated)!
  87. *
  88. * @author Frank Mattern, Ferid Bajramovic
  89. */
  90. class ogzbinstream : public NICE::obinstream {
  91. private:
  92. ogzstream stream;
  93. public:
  94. ogzbinstream() {}
  95. ogzbinstream( const char* name, int mode = std::ios::out)
  96. : stream(name, mode) {}
  97. //gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
  98. virtual void write(char* data, unsigned int length) {
  99. stream.write(data, length);
  100. }
  101. inline void open(const char* name,
  102. std::ios_base::openmode open_mode = std::ios::out) {
  103. stream.open(name, open_mode);
  104. }
  105. inline void close() {
  106. stream.close();
  107. }
  108. inline bool good() const {
  109. return stream.good();
  110. }
  111. inline bool fail() const {
  112. return stream.fail();
  113. }
  114. };
  115. } // namespace
  116. #else // NICE_USELIB_ZLIB
  117. namespace NICE{
  118. typedef ifbinstream igzbinstream;
  119. typedef ofbinstream ogzbinstream;
  120. } // namespace
  121. #endif // NICE_USELIB_ZLIB
  122. #endif // GZBINSTREAM_H