jcs ratpoison hax
1/* Functions for handling string buffers.
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
3 *
4 * This file is part of ratpoison.
5 *
6 * ratpoison is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * ratpoison is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this software; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307 USA
20 */
21
22#include <string.h>
23
24#include "ratpoison.h"
25#include "sbuf.h"
26
27struct sbuf *
28sbuf_new (size_t initsz)
29{
30 struct sbuf *b = xmalloc (sizeof (struct sbuf));
31
32 if (initsz < 1)
33 initsz = 1;
34
35 b->data = xmalloc (initsz);
36 b->maxsz = initsz;
37
38 b->data[0] = '\0';
39 b->len = 0;
40
41 return b;
42}
43
44void
45sbuf_free (struct sbuf *b)
46{
47 if (b != NULL)
48 {
49 free (b->data);
50 free (b);
51 }
52}
53
54/* Free the structure but return the string. */
55char *
56sbuf_free_struct (struct sbuf *b)
57{
58 if (b != NULL)
59 {
60 char *tmp;
61 tmp = b->data;
62 free (b);
63 return tmp;
64 }
65
66 return NULL;
67}
68
69char *
70sbuf_nconcat (struct sbuf *b, const char *str, int len)
71{
72 size_t minsz = b->len + len + 1;
73
74 if (b->maxsz < minsz)
75 {
76 b->data = xrealloc (b->data, minsz);
77 b->maxsz = minsz;
78 }
79
80 memcpy (b->data + b->len, str, minsz - b->len - 1);
81 b->len = minsz - 1;
82 *(b->data + b->len) = 0;
83
84 return b->data;
85}
86
87
88char *
89sbuf_concat (struct sbuf *b, const char *str)
90{
91 return sbuf_nconcat (b, str, strlen (str));
92}
93
94char *
95sbuf_copy (struct sbuf *b, const char *str)
96{
97 b->len = 0;
98 return sbuf_concat (b, str);
99}
100
101char *
102sbuf_clear (struct sbuf *b)
103{
104 b->len = 0;
105 b->data[0] = '\0';
106 return b->data;
107}
108
109char *
110sbuf_get (struct sbuf *b)
111{
112 return b->data;
113}
114
115char *
116sbuf_printf (struct sbuf *b, char *fmt, ...)
117{
118 va_list ap;
119
120 free (b->data);
121
122 va_start (ap, fmt);
123 b->data = xvsprintf (fmt, ap);
124 va_end (ap);
125
126 b->len = strlen (b->data);
127 b->maxsz = b->len + 1;
128
129 return b->data;
130}
131
132char *
133sbuf_printf_concat (struct sbuf *b, char *fmt, ...)
134{
135 char *buffer;
136 va_list ap;
137
138 va_start (ap, fmt);
139 buffer = xvsprintf (fmt, ap);
140 va_end (ap);
141
142 sbuf_concat (b, buffer);
143 free (buffer);
144
145 return b->data;
146}
147
148/* if width >= 0 then limit the width of s to width chars. */
149char *
150sbuf_utf8_nconcat (struct sbuf *b, const char *s, int width)
151{
152 if (width >= 0)
153 {
154 int len, nchars;
155
156 len = nchars = 0;
157 while (s[len] != '\0' && nchars < width)
158 {
159 if (isu8start (s[len]))
160 do
161 len++;
162 while (isu8cont (s[len]));
163 else
164 len++;
165 nchars++;
166 }
167 sbuf_printf_concat (b, "%.*s", len, s);
168 }
169 else
170 sbuf_concat (b, s);
171
172 return b->data;
173}
174
175void
176sbuf_chop (struct sbuf *b)
177{
178 if (b->len)
179 {
180 b->data[--(b->len)] = '\0';
181 }
182}