]> git.ipfire.org Git - thirdparty/squid.git/blame - src/StoreEntryStream.h
Prep for 3.3.12 and 3.4.4
[thirdparty/squid.git] / src / StoreEntryStream.h
CommitLineData
c21ad0f5 1/*
c21ad0f5 2 *
3 * SQUID Web Proxy Cache http://www.squid-cache.org/
4 * ----------------------------------------------------------
5 *
6 * Squid is the result of efforts by numerous individuals from
7 * the Internet community; see the CONTRIBUTORS file for full
8 * details. Many organizations have provided support for Squid's
9 * development; see the SPONSORS file for full details. Squid is
10 * Copyrighted (C) 2001 by the Regents of the University of
11 * California; see the COPYRIGHT file for full details. Squid
12 * incorporates software developed and/or copyrighted by other
13 * sources; see the CREDITS file for full details.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
26ac0430 19 *
c21ad0f5 20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26ac0430 24 *
c21ad0f5 25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
28 *
29 */
30
31#ifndef SQUID_STORE_ENTRY_STREAM_H
32#define SQUID_STORE_ENTRY_STREAM_H
33
34#include "Store.h"
35
36#include <ostream>
37
38/*
39 * This class provides a streambuf interface for writing
40 * to StoreEntries. Typical use is via a StoreEntryStream
41 * rather than direct manipulation
42 */
43
44class StoreEntryStreamBuf : public std::streambuf
45{
46
47public:
26ac0430 48 StoreEntryStreamBuf(StoreEntry *anEntry) : theEntry(anEntry) {
1bfe9ade 49 theEntry->lock("StoreEntryStreamBuf");
4e87141f 50 theEntry->buffer();
c21ad0f5 51 }
52
26ac0430 53 ~StoreEntryStreamBuf() {
1bfe9ade 54 theEntry->unlock("StoreEntryStreamBuf");
c21ad0f5 55 }
56
57protected:
58 /* flush the current buffer and the character that is overflowing
59 * to the store entry.
60 */
26ac0430 61 virtual int_type overflow(int_type aChar = traits_type::eof()) {
c21ad0f5 62 std::streamsize pending(pptr() - pbase());
63
64 if (pending && sync ())
65 return traits_type::eof();
66
67 if (aChar != traits_type::eof()) {
a203dec7
AJ
68 // NP: cast because GCC promotes int_type to 32-bit type
69 // std::basic_streambuf<char>::int_type {aka int}
70 // despite the definition with 8-bit type value.
71 char chars[1] = {char(aChar)};
c21ad0f5 72
73 if (aChar != traits_type::eof())
4e87141f 74 theEntry->append(chars, 1);
c21ad0f5 75 }
76
77 pbump (-pending); // Reset pptr().
78 return aChar;
79 }
80
81 /* push the buffer to the store */
26ac0430 82 virtual int sync() {
c21ad0f5 83 std::streamsize pending(pptr() - pbase());
84
85 if (pending)
4e87141f 86 theEntry->append(pbase(), pending);
c21ad0f5 87
4e87141f 88 theEntry->flush();
c21ad0f5 89
90 return 0;
91 }
92
93 /* write multiple characters to the store entry
94 * - this is an optimisation method.
95 */
26ac0430 96 virtual std::streamsize xsputn(const char * chars, std::streamsize number) {
c21ad0f5 97 if (number)
4e87141f 98 theEntry->append(chars, number);
c21ad0f5 99
100 return number;
101 }
102
103private:
4e87141f 104 StoreEntry *theEntry;
c21ad0f5 105
106};
107
108class StoreEntryStream : public std::ostream
109{
110
111public:
4e87141f 112 /* create a stream for writing text etc into theEntry */
1ca48d39 113 // See http://www.codecomments.com/archive292-2005-2-396222.html
114 StoreEntryStream(StoreEntry *entry): std::ostream(0), theBuffer(entry) {
115 rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
116 clear(); //clear badbit set by calling init(0)
117 }
c21ad0f5 118
119private:
4e87141f 120 StoreEntryStreamBuf theBuffer;
c21ad0f5 121};
122
123#endif /* SQUID_STORE_ENTRY_STREAM_H */