hdstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_HDSTREAM_H
30 #define CXXTOOLS_HDSTREAM_H
31 
32 #include <iostream>
33 #include <string>
34 #include <sstream>
35 
36 namespace cxxtools
37 {
38 
39 class Hdstreambuf : public std::streambuf
40 {
41  static const unsigned BUFFERSIZE = 16;
42 
43  std::streambuf::int_type overflow(std::streambuf::int_type ch);
44  std::streambuf::int_type underflow();
45  int sync();
46 
47  char Buffer[BUFFERSIZE];
48  std::streambuf* Dest;
49  unsigned offset;
50 
51  public:
52  Hdstreambuf(std::streambuf* dest)
53  : Dest(dest),
54  offset(0)
55  {
56  setp(Buffer, Buffer + BUFFERSIZE);
57  }
58 
59  unsigned getOffset() const { return offset; }
60  void setOffset(unsigned offset_) { offset = offset_; }
61 };
62 
68 class Hdostream : public std::ostream
69 {
70  typedef std::ostream base_class;
71  Hdstreambuf streambuf;
72 
73  public:
75  : base_class(0),
76  streambuf(std::cout.rdbuf())
77  {
78  init(&streambuf);
79  }
80  Hdostream(std::ostream& out)
81  : base_class(0),
82  streambuf(out.rdbuf())
83  {
84  init(&streambuf);
85  }
86 
87  unsigned getOffset() const { return streambuf.getOffset(); }
88  void setOffset(unsigned offset_) { streambuf.setOffset(offset_); }
89 };
90 
91 template <typename T>
92 void hexDump(std::ostream& out, const T& t)
93 {
94  Hdostream hd(out);
95  hd << t << std::flush;
96 }
97 
98 template <typename T>
99 std::string hexDump(const T& t)
100 {
101  std::ostringstream out;
102  Hdostream hd(out);
103  hd << t << std::flush;
104  return out.str();
105 }
106 
107 inline std::string hexDump(const char* p, unsigned n)
108 {
109  std::ostringstream out;
110  Hdostream hd(out);
111  hd.write(p, n);
112  hd.flush();
113  return out.str();
114 }
115 
122 class HexDump
123 {
124  friend std::ostream& operator<< (std::ostream& out, const HexDump& hd);
125  const char* _p;
126  unsigned _n;
127 
128 public:
129  HexDump(const char* p, unsigned n)
130  : _p(p),
131  _n(n)
132  { }
133 };
134 
146 inline std::ostream& operator<< (std::ostream& out, const HexDump& hd)
147 {
148  Hdstreambuf buf(out.rdbuf());
149  if (buf.sputn(hd._p, hd._n) == hd._n)
150  buf.pubsync();
151  else
152  out.setstate(std::ios::failbit);
153  return out;
154 }
155 
156 }
157 
158 #endif // CXXTOOLS_HDSTREAM_H