]>
Commit | Line | Data |
---|---|---|
412da427 | 1 | /* |
ef57eb7b | 2 | * Copyright (C) 1996-2016 The Squid Software Foundation and contributors |
412da427 | 3 | * |
bbc27441 AJ |
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. | |
412da427 FC |
7 | */ |
8 | ||
9 | #ifndef SQUID_SBUFSTREAM_H | |
10 | #define SQUID_SBUFSTREAM_H | |
11 | ||
12 | #include "SBuf.h" | |
13 | ||
412da427 | 14 | #include <ostream> |
412da427 | 15 | |
496a9e97 | 16 | /** streambuf class for a SBuf-backed stream interface. |
412da427 | 17 | * |
496a9e97 FC |
18 | * Auxiliary class to be able to leverage an ostream generating SBuf's |
19 | * analogous to std::ostrstream. | |
412da427 FC |
20 | */ |
21 | class SBufStreamBuf : public std::streambuf | |
22 | { | |
23 | public: | |
24 | /// initialize streambuf; use supplied SBuf as backing store | |
496a9e97 | 25 | explicit SBufStreamBuf(SBuf aBuf) : theBuf(aBuf) {} |
412da427 FC |
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()) | |
7cbd3256 | 48 | theBuf.append(chars, 1); |
412da427 FC |
49 | } |
50 | ||
51 | pbump(-pending); // Reset pptr(). | |
52 | return aChar; | |
53 | } | |
54 | ||
496a9e97 | 55 | /// push the streambuf to the backing SBuf |
412da427 FC |
56 | virtual int sync() { |
57 | std::streamsize pending(pptr() - pbase()); | |
58 | ||
59 | if (pending) | |
7cbd3256 | 60 | theBuf.append(pbase(), pending); |
412da427 FC |
61 | |
62 | return 0; | |
63 | } | |
64 | ||
496a9e97 FC |
65 | /** write multiple characters to the store entry |
66 | * \note this is an optimisation consistent with std::streambuf API | |
412da427 FC |
67 | */ |
68 | virtual std::streamsize xsputn(const char * chars, std::streamsize number) { | |
69 | if (number) | |
7cbd3256 | 70 | theBuf.append(chars, number); |
412da427 FC |
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: | |
496a9e97 | 88 | /** Create a SBufStream preinitialized with the contents of a SBuf |
412da427 | 89 | * |
496a9e97 FC |
90 | * The supplied SBuf copied: in order to retrieve the written-to contents |
91 | * they must be later fetched using the buf() class method. | |
412da427 FC |
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() { | |
a452bc8b | 106 | flush(); |
412da427 FC |
107 | return theBuffer.getBuf(); |
108 | } | |
109 | ||
110 | /// Clear the stream's backing store | |
111 | SBufStream& clearBuf() { | |
a452bc8b | 112 | flush(); |
412da427 FC |
113 | theBuffer.clearBuf(); |
114 | return *this; | |
115 | } | |
116 | ||
117 | private: | |
118 | SBufStreamBuf theBuffer; | |
119 | }; | |
120 | ||
121 | #endif /* SQUID_SBUFSTREAM_H */ | |
f53969cc | 122 |