]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SBufStream.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / SBufStream.h
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_SBUFSTREAM_H
10 #define SQUID_SBUFSTREAM_H
11
12 #include "SBuf.h"
13
14 #include <ostream>
15
16 /** streambuf class for a SBuf-backed stream interface.
17 *
18 * Auxiliary class to be able to leverage an ostream generating SBuf's
19 * analogous to std::ostrstream.
20 */
21 class SBufStreamBuf : public std::streambuf
22 {
23 public:
24 /// initialize streambuf; use supplied SBuf as backing store
25 explicit SBufStreamBuf(SBuf aBuf) : theBuf(aBuf) {}
26
27 /// get a copy of the stream's contents
28 SBuf getBuf() {
29 return theBuf;
30 }
31
32 /// clear the stream's store
33 void clearBuf() {
34 theBuf.clear();
35 }
36
37 protected:
38 virtual int_type overflow(int_type aChar = traits_type::eof()) {
39 std::streamsize pending(pptr() - pbase());
40
41 if (pending && sync())
42 return traits_type::eof();
43
44 if (aChar != traits_type::eof()) {
45 char chars[1] = {static_cast<char>(aChar)};
46
47 if (aChar != traits_type::eof())
48 theBuf.append(chars, 1);
49 }
50
51 pbump(-pending); // Reset pptr().
52 return aChar;
53 }
54
55 /// push the streambuf to the backing SBuf
56 virtual int sync() {
57 std::streamsize pending(pptr() - pbase());
58
59 if (pending)
60 theBuf.append(pbase(), pending);
61
62 return 0;
63 }
64
65 /** write multiple characters to the store entry
66 * \note this is an optimisation consistent with std::streambuf API
67 */
68 virtual std::streamsize xsputn(const char * chars, std::streamsize number) {
69 if (number)
70 theBuf.append(chars, number);
71
72 return number;
73 }
74
75 private:
76 SBuf theBuf;
77 SBufStreamBuf(); // no default constructor
78 };
79
80 /** Stream interface to write to a SBuf.
81 *
82 * Data is appended using standard operator << semantics, and extracted
83 * using the buf() method, in analogy with std::strstream .
84 */
85 class SBufStream : public std::ostream
86 {
87 public:
88 /** Create a SBufStream preinitialized with the contents of a SBuf
89 *
90 * The supplied SBuf copied: in order to retrieve the written-to contents
91 * they must be later fetched using the buf() class method.
92 */
93 SBufStream(SBuf aBuf): std::ostream(0), theBuffer(aBuf) {
94 rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
95 clear(); //clear badbit set by calling init(0)
96 }
97
98 /// Create an empty SBufStream
99 SBufStream(): std::ostream(0), theBuffer(SBuf()) {
100 rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
101 clear(); //clear badbit set by calling init(0)
102 }
103
104 /// Retrieve a copy of the current stream status
105 SBuf buf() {
106 flush();
107 return theBuffer.getBuf();
108 }
109
110 /// Clear the stream's backing store
111 SBufStream& clearBuf() {
112 flush();
113 theBuffer.clearBuf();
114 return *this;
115 }
116
117 private:
118 SBufStreamBuf theBuffer;
119 };
120
121 #endif /* SQUID_SBUFSTREAM_H */