]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Packer.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / Packer.cc
CommitLineData
2ac76861 1
cb69b4c7 2/*
262a0e14 3 * $Id$
cb69b4c7 4 *
123abbe1 5 * DEBUG: section 60 Packer: A uniform interface to store-like modules
cb69b4c7 6 * AUTHOR: Alex Rousskov
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
cb69b4c7 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
cb69b4c7 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
26ac0430 24 *
cb69b4c7 25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
26ac0430 29 *
cb69b4c7 30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
cb69b4c7 34 */
35
adba4a64 36/*
2ac76861 37 * Rationale:
38 * ----------
26ac0430 39 *
2ac76861 40 * OK, we have to major interfaces comm.c and store.c.
26ac0430 41 *
2ac76861 42 * Store.c has a nice storeAppend[Printf] capability which makes "storing"
26ac0430
AJ
43 * things easy and painless.
44 *
2ac76861 45 * Comm.c lacks commAppend[Printf] because comm does not handle its own
46 * buffers (no mem_obj equivalent for comm.c).
26ac0430 47 *
ec41b64c 48 * Thus, if one wants to be able to store _and_ Comm::Write an object, s/he
2ac76861 49 * has to implement two almost identical functions.
26ac0430 50 *
2ac76861 51 * Packer
52 * ------
26ac0430 53 *
2ac76861 54 * Packer provides for a more uniform interface to store and comm modules.
55 * Packer has its own append and printf routines that "know" where to send
56 * incoming data. In case of store interface, Packer sends data to
57 * storeAppend. Otherwise, Packer uses a MemBuf that can be flushed later to
ec41b64c 58 * Comm::Write.
26ac0430 59 *
2ac76861 60 * Thus, one can write just one function that will either "pack" things for
ec41b64c 61 * Comm::Write or "append" things to store, depending on actual packer
2ac76861 62 * supplied.
26ac0430 63 *
eb139d08 64 * It is amazing how much work a tiny object can save. :)
26ac0430 65 *
2ac76861 66 */
adba4a64 67
cb69b4c7 68/*
69 * To-Do:
70 */
71
582c2af2 72#include "squid.h"
e6ccf245 73#include "Store.h"
0eb49b6d 74#include "MemBuf.h"
cb69b4c7 75
76/* local types */
77
78/* local routines */
79
80/* local constants and vars */
81
82/*
83 * We do have one potential problem here. Both append_f and vprintf_f types
84 * cannot match real functions precisely (at least because of the difference in
85 * the type of the first parameter). Thus, we have to use type cast. If somebody
86 * changes the prototypes of real functions, Packer will not notice that because
87 * of the type cast.
88 *
89 * Solution: we use the constants below to *hard code* current prototypes of
90 * real functions. If real prototypes change, these constants will produce a
91 * warning (e.g., "warning: assignment from incompatible pointer type").
92 */
93
2fe7eff9 94static void
95memBufAppend(MemBuf *mb, const char *buf, mb_size_t len)
96{
97 mb->append(buf, len);
98}
99
100static void
101memBufVPrintf(MemBuf * mb, const char *fmt, va_list vargs)
102{
103 mb->vPrintf(fmt, vargs);
104}
105
3900307b 106static void
107storeEntryAppend(StoreEntry *e, const char *buf, int len)
108{
109 e->append(buf, len);
110}
111
cb69b4c7 112/* append()'s */
3900307b 113static void (*const store_append) (StoreEntry *, const char *, int) = &storeEntryAppend;
2ac76861 114static void (*const memBuf_append) (MemBuf *, const char *, mb_size_t) = &memBufAppend;
cb69b4c7 115
116/* vprintf()'s */
2ac76861 117static void (*const store_vprintf) (StoreEntry *, const char *, va_list ap) = &storeAppendVPrintf;
118static void (*const memBuf_vprintf) (MemBuf *, const char *, va_list ap) = &memBufVPrintf;
cb69b4c7 119
adba4a64 120/* init/clean */
121
122/* init with this to forward data to StoreEntry */
cb69b4c7 123void
2ac76861 124packerToStoreInit(Packer * p, StoreEntry * e)
cb69b4c7 125{
126 assert(p && e);
2ac76861 127 p->append = (append_f) store_append;
866be921 128 p->packer_vprintf = (vprintf_f) store_vprintf;
cb69b4c7 129 p->real_handler = e;
3900307b 130 e->buffer();
cb69b4c7 131}
132
adba4a64 133/* init with this to accumulate data in MemBuf */
cb69b4c7 134void
2ac76861 135packerToMemInit(Packer * p, MemBuf * mb)
cb69b4c7 136{
137 assert(p && mb);
2ac76861 138 p->append = (append_f) memBuf_append;
866be921 139 p->packer_vprintf = (vprintf_f) memBuf_vprintf;
cb69b4c7 140 p->real_handler = mb;
141}
142
adba4a64 143/* call this when you are done */
cb69b4c7 144void
2ac76861 145packerClean(Packer * p)
cb69b4c7 146{
2ac76861 147 assert(p);
b89401f3 148
149 if (p->append == (append_f) store_append && p->real_handler)
3900307b 150 static_cast<StoreEntry*>(p->real_handler)->flush();
b89401f3 151
2ac76861 152 /* it is not really necessary to do this, but, just in case... */
153 p->append = NULL;
866be921 154 p->packer_vprintf = NULL;
2ac76861 155 p->real_handler = NULL;
cb69b4c7 156}
157
158void
2ac76861 159packerAppend(Packer * p, const char *buf, int sz)
cb69b4c7 160{
161 assert(p);
162 assert(p->real_handler && p->append);
163 p->append(p->real_handler, buf, sz);
164}
165
cb69b4c7 166void
2ac76861 167packerPrintf(Packer * p, const char *fmt,...)
cb69b4c7 168{
169 va_list args;
170 va_start(args, fmt);
62e76326 171
cb69b4c7 172 assert(p);
866be921 173 assert(p->real_handler && p->packer_vprintf);
174 p->packer_vprintf(p->real_handler, fmt, args);
cb69b4c7 175 va_end(args);
176}