str.c revision 1.2 1 /*
2 * Copyright (c) 1994 Christos Zoulas
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christos Zoulas.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static char *rcsid = "$Id: str.c,v 1.2 1995/04/24 13:25:55 cgd Exp $";
34 #endif
35
36 /*
37 * Counted strings
38 */
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "util.h"
43 #include "str.h"
44
45 /*
46 * str_init(): Initialize string
47 */
48 void
49 str_init(s)
50 struct string *s;
51 {
52 s->s_str = NULL;
53 s->s_len = 0;
54 }
55
56
57 /*
58 * str_append(): Append string allocating buffer as necessary
59 */
60 void
61 str_append(buf, str, del)
62 struct string *buf;
63 const char *str;
64 int del;
65 {
66 size_t len = strlen(str) + 1;
67
68 if (buf->s_str == NULL)
69 buf->s_str = emalloc(len);
70 else {
71 buf->s_str = erealloc(buf->s_str, buf->s_len + len +
72 (del ? 2 : 1));
73 if (del)
74 buf->s_str[buf->s_len++] = del;
75 }
76
77 memcpy(&buf->s_str[buf->s_len], str, len);
78 buf->s_len += len - 1;
79 }
80
81 /*
82 * str_prepend(): Prepend string allocating buffer as necessary
83 */
84 void
85 str_prepend(buf, str, del)
86 struct string *buf;
87 const char *str;
88 int del;
89 {
90 char *ptr, *sptr;
91 size_t len = strlen(str) + 1;
92
93 sptr = ptr = emalloc(buf->s_len + len + (del ? 2 : 1));
94
95 if (del)
96 *ptr++ = del;
97
98 memcpy(ptr, str, len);
99
100 if (buf->s_str) {
101 memcpy(&ptr[len - 1], buf->s_str, buf->s_len + 1);
102 free(buf->s_str);
103 }
104
105 buf->s_str = sptr;
106 buf->s_len += del ? len : len - 1;
107 }
108
109 /*
110 * str_free(): Free a string
111 */
112 void
113 str_free(s)
114 struct string *s;
115 {
116 free(s->s_str);
117 s->s_str = NULL;
118 s->s_len = 0;
119 }
120