]>
Commit | Line | Data |
---|---|---|
b644367b | 1 | |
0da10ea1 | 2 | /* |
2b6662ba | 3 | * $Id: String.cc,v 1.9 2001/01/12 00:37:14 wessels Exp $ |
0da10ea1 | 4 | * |
49d44054 | 5 | * DEBUG: section 67 String |
0da10ea1 | 6 | * AUTHOR: Duane Wessels |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
0da10ea1 | 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. | |
0da10ea1 | 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. | |
24 | * | |
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. | |
29 | * | |
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 | * |
0da10ea1 | 34 | */ |
35 | ||
36 | #include "squid.h" | |
37 | ||
f21997a0 | 38 | static void |
b644367b | 39 | stringInitBuf(String * s, size_t sz) |
f21997a0 | 40 | { |
41 | s->buf = memAllocBuf(sz, &sz); | |
42 | assert(sz < 65536); | |
43 | s->size = sz; | |
44 | } | |
0da10ea1 | 45 | |
46 | void | |
b644367b | 47 | stringInit(String * s, const char *str) |
0da10ea1 | 48 | { |
49 | assert(s); | |
50 | if (str) | |
51 | stringLimitInit(s, str, strlen(str)); | |
52 | else | |
53 | *s = StringNull; | |
54 | } | |
55 | ||
56 | void | |
b644367b | 57 | stringLimitInit(String * s, const char *str, int len) |
0da10ea1 | 58 | { |
0da10ea1 | 59 | assert(s && str); |
b644367b | 60 | stringInitBuf(s, len + 1); |
0da10ea1 | 61 | s->len = len; |
0da10ea1 | 62 | xmemcpy(s->buf, str, len); |
63 | s->buf[len] = '\0'; | |
64 | } | |
65 | ||
66 | String | |
b644367b | 67 | stringDup(const String * s) |
0da10ea1 | 68 | { |
69 | String dup; | |
70 | assert(s); | |
71 | stringInit(&dup, s->buf); | |
72 | return dup; | |
73 | } | |
74 | ||
75 | void | |
b644367b | 76 | stringClean(String * s) |
0da10ea1 | 77 | { |
78 | assert(s); | |
79 | if (s->buf) | |
80 | memFreeBuf(s->size, s->buf); | |
81 | *s = StringNull; | |
82 | } | |
83 | ||
84 | void | |
b644367b | 85 | stringReset(String * s, const char *str) |
0da10ea1 | 86 | { |
87 | stringClean(s); | |
88 | stringInit(s, str); | |
89 | } | |
f21997a0 | 90 | |
91 | void | |
b644367b | 92 | stringAppend(String * s, const char *str, int len) |
f21997a0 | 93 | { |
99edd1c3 | 94 | assert(s); |
95 | assert(str && len >= 0); | |
f21997a0 | 96 | if (s->len + len < s->size) { |
97 | strncat(s->buf, str, len); | |
98 | s->len += len; | |
99 | } else { | |
100 | String snew = StringNull; | |
101 | snew.len = s->len + len; | |
102 | stringInitBuf(&snew, snew.len + 1); | |
99edd1c3 | 103 | if (s->buf) |
104 | xmemcpy(snew.buf, s->buf, s->len); | |
105 | if (len) | |
106 | xmemcpy(snew.buf + s->len, str, len); | |
f21997a0 | 107 | snew.buf[snew.len] = '\0'; |
108 | stringClean(s); | |
109 | *s = snew; | |
110 | } | |
111 | } |