str.c revision 1.1 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.1 1994/12/04 17:11:04 christos Exp $";
34 #endif
35
36 /*
37 * Counted strings
38 */
39 #include <stdlib.h>
40
41 #include "util.h"
42 #include "str.h"
43
44 /*
45 * str_init(): Initialize string
46 */
47 void
48 str_init(s)
49 struct string *s;
50 {
51 s->s_str = NULL;
52 s->s_len = 0;
53 }
54
55
56 /*
57 * str_append(): Append string allocating buffer as necessary
58 */
59 void
60 str_append(buf, str, del)
61 struct string *buf;
62 const char *str;
63 int del;
64 {
65 size_t len = strlen(str) + 1;
66
67 if (buf->s_str == NULL)
68 buf->s_str = emalloc(len);
69 else {
70 buf->s_str = erealloc(buf->s_str, buf->s_len + len +
71 (del ? 2 : 1));
72 if (del)
73 buf->s_str[buf->s_len++] = del;
74 }
75
76 memcpy(&buf->s_str[buf->s_len], str, len);
77 buf->s_len += len - 1;
78 }
79
80 /*
81 * str_prepend(): Prepend string allocating buffer as necessary
82 */
83 void
84 str_prepend(buf, str, del)
85 struct string *buf;
86 const char *str;
87 int del;
88 {
89 char *ptr, *sptr;
90 size_t len = strlen(str) + 1;
91
92 sptr = ptr = emalloc(buf->s_len + len + (del ? 2 : 1));
93
94 if (del)
95 *ptr++ = del;
96
97 memcpy(ptr, str, len);
98
99 if (buf->s_str) {
100 memcpy(&ptr[len - 1], buf->s_str, buf->s_len + 1);
101 free(buf->s_str);
102 }
103
104 buf->s_str = sptr;
105 buf->s_len += del ? len : len - 1;
106 }
107
108 /*
109 * str_free(): Free a string
110 */
111 void
112 str_free(s)
113 struct string *s;
114 {
115 free(s->s_str);
116 s->s_str = NULL;
117 s->s_len = 0;
118 }
119