]> git.ipfire.org Git - thirdparty/squid.git/blob - src/StoreEntryStream.h
Tightened StoreEntry locking. Fixed entry touching and synchronization code:
[thirdparty/squid.git] / src / StoreEntryStream.h
1 /*
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.
19 *
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.
24 *
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 #if HAVE_OSTREAM
37 #include <ostream>
38 #endif
39
40 /*
41 * This class provides a streambuf interface for writing
42 * to StoreEntries. Typical use is via a StoreEntryStream
43 * rather than direct manipulation
44 */
45
46 class StoreEntryStreamBuf : public std::streambuf
47 {
48
49 public:
50 StoreEntryStreamBuf(StoreEntry *anEntry) : theEntry(anEntry) {
51 theEntry->lock("StoreEntryStreamBuf");
52 theEntry->buffer();
53 }
54
55 ~StoreEntryStreamBuf() {
56 theEntry->unlock("StoreEntryStreamBuf");
57 }
58
59 protected:
60 /* flush the current buffer and the character that is overflowing
61 * to the store entry.
62 */
63 virtual int_type overflow(int_type aChar = traits_type::eof()) {
64 std::streamsize pending(pptr() - pbase());
65
66 if (pending && sync ())
67 return traits_type::eof();
68
69 if (aChar != traits_type::eof()) {
70 // NP: cast because GCC promotes int_type to 32-bit type
71 // std::basic_streambuf<char>::int_type {aka int}
72 // despite the definition with 8-bit type value.
73 char chars[1] = {char(aChar)};
74
75 if (aChar != traits_type::eof())
76 theEntry->append(chars, 1);
77 }
78
79 pbump (-pending); // Reset pptr().
80 return aChar;
81 }
82
83 /* push the buffer to the store */
84 virtual int sync() {
85 std::streamsize pending(pptr() - pbase());
86
87 if (pending)
88 theEntry->append(pbase(), pending);
89
90 theEntry->flush();
91
92 return 0;
93 }
94
95 /* write multiple characters to the store entry
96 * - this is an optimisation method.
97 */
98 virtual std::streamsize xsputn(const char * chars, std::streamsize number) {
99 if (number)
100 theEntry->append(chars, number);
101
102 return number;
103 }
104
105 private:
106 StoreEntry *theEntry;
107
108 };
109
110 class StoreEntryStream : public std::ostream
111 {
112
113 public:
114 /* create a stream for writing text etc into theEntry */
115 // See http://www.codecomments.com/archive292-2005-2-396222.html
116 StoreEntryStream(StoreEntry *entry): std::ostream(0), theBuffer(entry) {
117 rdbuf(&theBuffer); // set the buffer to now-initialized theBuffer
118 clear(); //clear badbit set by calling init(0)
119 }
120
121 private:
122 StoreEntryStreamBuf theBuffer;
123 };
124
125 #endif /* SQUID_STORE_ENTRY_STREAM_H */