~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to src/String.cc

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2009-09-24 14:51:06 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (20.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090924145106-38jgrzmj0d73pha5
Tags: 3.1.0.13-1
* Upload to experimental

* New upstream release
  - Fixes Follow-X-Forwarded-For support (Closes: #523943)
  - Adds IPv6 support (Closes: #432351)

* debian/rules
  - Removed obsolete configuration options
  - Enable db and radius basic authentication modules

* debian/patches/01-cf.data.debian
  - Adapted to new upstream version

* debian/patches/02-makefile-defaults
  - Adapted to new upstream version

* debian/{squid.postinst,squid.rc,README.Debian,watch}
  - Updated references to squid 3.1

* debian/squid3.install
  - Install CSS file for error pages
  - Install manual pages for new authentication modules

* debian/squid3-common.install
  - Install documented version of configuration file in /usr/share/doc/squid3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
 
 * $Id: String.cc,v 1.26 2007/05/29 13:31:38 amosjeffries Exp $
 
3
 * $Id$
4
4
 *
5
5
 * DEBUG: section 67    String
6
6
 * AUTHOR: Duane Wessels
21
21
 *  it under the terms of the GNU General Public License as published by
22
22
 *  the Free Software Foundation; either version 2 of the License, or
23
23
 *  (at your option) any later version.
24
 
 *  
 
24
 *
25
25
 *  This program is distributed in the hope that it will be useful,
26
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
28
 *  GNU General Public License for more details.
29
 
 *  
 
29
 *
30
30
 *  You should have received a copy of the GNU General Public License
31
31
 *  along with this program; if not, write to the Free Software
32
32
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35
35
 
36
36
#include "squid.h"
37
37
#include "Store.h"
38
 
 
 
38
#include "TextException.h"
 
39
 
 
40
int
 
41
String::psize() const
 
42
{
 
43
    Must(size() < INT_MAX);
 
44
    return size();
 
45
}
 
46
 
 
47
 
 
48
// low-level buffer allocation,
 
49
// does not free old buffer and does not adjust or look at len_
39
50
void
40
 
String::initBuf(size_t sz)
 
51
String::allocBuffer(String::size_type sz)
41
52
{
42
53
    PROF_start(StringInitBuf);
43
 
    buf((char *)memAllocString(sz, &sz));
44
 
    assert(sz < 65536);
45
 
    size_ = sz;
 
54
    assert (undefined());
 
55
    char *newBuffer = (char*)memAllocString(sz, &sz);
 
56
    setBuffer(newBuffer, sz);
46
57
    PROF_stop(StringInitBuf);
47
58
}
48
59
 
 
60
// low-level buffer assignment
 
61
// does not free old buffer and does not adjust or look at len_
49
62
void
50
 
String::init(char const *str)
 
63
String::setBuffer(char *aBuf, String::size_type aSize)
51
64
{
52
 
    assert(this);
53
 
 
54
 
    PROF_start(StringInit);
55
 
    if (str)
56
 
        limitInit(str, strlen(str));
57
 
    else
58
 
        clean();
59
 
    PROF_stop(StringInit);
 
65
    assert(undefined());
 
66
    assert(aSize < 65536);
 
67
    buf_ = aBuf;
 
68
    size_ = aSize;
60
69
}
61
70
 
62
71
String::String (char const *aString) : size_(0), len_(0), buf_(NULL)
63
72
{
64
 
    init (aString);
 
73
    if (aString)
 
74
        allocAndFill(aString, strlen(aString));
65
75
#if DEBUGSTRINGS
66
76
 
67
77
    StringRegistry::Instance().add(this);
71
81
String &
72
82
String::operator =(char const *aString)
73
83
{
74
 
    clean();
75
 
    init (aString);
 
84
    reset(aString);
76
85
    return *this;
77
86
}
78
87
 
79
88
String &
80
89
String::operator = (String const &old)
81
90
{
82
 
    clean ();
83
 
 
84
 
    if (old.len_)
85
 
        limitInit (old.buf(), old.len_);
86
 
 
 
91
    clean(); // TODO: optimize to avoid cleaning the buffer we can use
 
92
    if (old.size() > 0)
 
93
        allocAndFill(old.rawBuf(), old.size());
87
94
    return *this;
88
95
}
89
96
 
105
112
    return true;
106
113
}
107
114
 
 
115
// public interface, makes sure that we clean the old buffer first
108
116
void
109
117
String::limitInit(const char *str, int len)
110
118
{
111
 
    PROF_start(StringLimitInit);
 
119
    clean(); // TODO: optimize to avoid cleaning the buffer we can use
 
120
    allocAndFill(str, len);
 
121
}
 
122
 
 
123
// Allocates the buffer to fit the supplied string and fills it.
 
124
// Does not clean.
 
125
void
 
126
String::allocAndFill(const char *str, int len)
 
127
{
 
128
    PROF_start(StringAllocAndFill);
112
129
    assert(this && str);
113
 
    initBuf(len + 1);
 
130
    allocBuffer(len + 1);
114
131
    len_ = len;
115
132
    xmemcpy(buf_, str, len);
116
133
    buf_[len] = '\0';
117
 
    PROF_stop(StringLimitInit);
 
134
    PROF_stop(StringAllocAndFill);
118
135
}
119
136
 
120
137
String::String (String const &old) : size_(0), len_(0), buf_(NULL)
121
138
{
122
 
    init (old.buf());
 
139
    if (old.size() > 0)
 
140
        allocAndFill(old.rawBuf(), old.size());
123
141
#if DEBUGSTRINGS
124
142
 
125
143
    StringRegistry::Instance().add(this);
132
150
    PROF_start(StringClean);
133
151
    assert(this);
134
152
 
135
 
    if (buf())
 
153
    /* TODO if mempools has already closed this will FAIL!! */
 
154
    if (defined())
136
155
        memFreeString(size_, buf_);
137
156
 
138
157
    len_ = 0;
156
175
String::reset(const char *str)
157
176
{
158
177
    PROF_start(StringReset);
159
 
    clean();
160
 
    init(str);
 
178
    clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
 
179
    if (str)
 
180
        allocAndFill(str, strlen(str));
161
181
    PROF_stop(StringReset);
162
182
}
163
183
 
172
192
        strncat(buf_, str, len);
173
193
        len_ += len;
174
194
    } else {
 
195
        // Create a temporary string and absorb it later.
175
196
        String snew;
176
197
        assert(len_ + len < 65536); // otherwise snew.len_ overflows below
177
198
        snew.len_ = len_ + len;
178
 
        snew.initBuf(snew.len_ + 1);
 
199
        snew.allocBuffer(snew.len_ + 1);
179
200
 
180
 
        if (buf_)
181
 
            xmemcpy(snew.buf_, buf(), len_);
 
201
        if (len_)
 
202
            xmemcpy(snew.buf_, rawBuf(), len_);
182
203
 
183
204
        if (len)
184
205
            xmemcpy(snew.buf_ + len_, str, len);
209
230
void
210
231
String::append(String const &old)
211
232
{
212
 
    append (old.buf(), old.len_);
 
233
    append (old.rawBuf(), old.len_);
213
234
}
214
235
 
215
236
void
216
237
String::absorb(String &old)
217
238
{
218
239
    clean();
219
 
    size_ = old.size_;
220
 
    buf (old.buf_);
 
240
    setBuffer(old.buf_, old.size_);
221
241
    len_ = old.len_;
222
242
    old.size_ = 0;
223
243
    old.buf_ = NULL;
224
244
    old.len_ = 0;
225
245
}
226
246
 
227
 
void
228
 
String::buf(char *newBuf)
 
247
String
 
248
String::substr(String::size_type from, String::size_type to) const
229
249
{
230
 
    assert (buf_ == NULL);
231
 
    buf_ = newBuf;
 
250
    Must(from >= 0 && from < size());
 
251
    Must(to > 0 && to <= size());
 
252
    Must(to > from);
 
253
 
 
254
    String rv;
 
255
    rv.limitInit(rawBuf()+from,to-from);
 
256
    return rv;
232
257
}
233
258
 
 
259
 
234
260
#if DEBUGSTRINGS
235
261
void
236
262
String::stat(StoreEntry *entry) const
237
263
{
238
 
    storeAppendPrintf(entry, "%p : %d/%d \"%s\"\n",this,len_, size_, buf());
 
264
    storeAppendPrintf(entry, "%p : %d/%d \"%.*s\"\n",this,len_, size_, size(), rawBuf());
239
265
}
240
266
 
241
267
StringRegistry &
251
277
    return lhs - rhs;
252
278
}
253
279
 
254
 
void
255
 
StringRegistry::registerWithCacheManager(CacheManager & manager)
 
280
StringRegistry::StringRegistry()
256
281
{
257
 
    manager.registerAction("strings",
258
 
                           "Strings in use in squid", Stat, 0, 1);
 
282
#if DEBUGSTRINGS
 
283
    CacheManager::GetInstance()->registerAction("strings",
 
284
            "Strings in use in squid", Stat, 0, 1);
 
285
#endif
259
286
}
260
287
 
261
288
void
262
289
 
263
290
StringRegistry::add
264
 
    (String const *entry)
 
291
(String const *entry)
265
292
{
266
293
    entries.insert(entry, ptrcmp);
267
294
}
269
296
void
270
297
 
271
298
StringRegistry::remove
272
 
    (String const *entry)
 
299
(String const *entry)
273
300
{
274
301
    entries.remove(entry, ptrcmp);
275
302
}
276
303
 
277
304
StringRegistry StringRegistry::Instance_;
278
305
 
279
 
extern size_t memStringCount();
 
306
extern String::size_type memStringCount();
280
307
 
281
308
void
282
309
StringRegistry::Stat(StoreEntry *entry)
409
436
    return p ? p : "(NULL)";
410
437
}
411
438
 
 
439
const char *
 
440
String::pos(char const *aString) const
 
441
{
 
442
    if (undefined())
 
443
        return NULL;
 
444
    return strstr(termedBuf(), aString);
 
445
}
 
446
 
 
447
const char *
 
448
String::pos(char const ch) const
 
449
{
 
450
    if (undefined())
 
451
        return NULL;
 
452
    return strchr(termedBuf(), ch);
 
453
}
 
454
 
 
455
const char *
 
456
String::rpos(char const ch) const
 
457
{
 
458
    if (undefined())
 
459
        return NULL;
 
460
    return strrchr(termedBuf(), (ch));
 
461
}
 
462
 
 
463
String::size_type
 
464
String::find(char const ch) const
 
465
{
 
466
    const char *c;
 
467
    c=pos(ch);
 
468
    if (c==NULL)
 
469
        return npos;
 
470
    return c-rawBuf();
 
471
}
 
472
 
 
473
String::size_type
 
474
String::find(char const *aString) const
 
475
{
 
476
    const char *c;
 
477
    c=pos(aString);
 
478
    if (c==NULL)
 
479
        return npos;
 
480
    return c-rawBuf();
 
481
}
 
482
 
 
483
String::size_type
 
484
String::rfind(char const ch) const
 
485
{
 
486
    const char *c;
 
487
    c=rpos(ch);
 
488
    if (c==NULL)
 
489
        return npos;
 
490
    return c-rawBuf();
 
491
}
 
492
 
 
493
 
 
494
 
412
495
#ifndef _USE_INLINE_
413
496
#include "String.cci"
414
497
#endif