Mercurial > code
comparison buf.c @ 141:5ed54050ae31
Added buf_cut, cut the buffer
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 02 May 2012 20:20:22 +0200 |
parents | cf738da3ad60 |
children | 8c7812fefe0f |
comparison
equal
deleted
inserted
replaced
140:09fb5267c906 | 141:5ed54050ae31 |
---|---|
128 */ | 128 */ |
129 | 129 |
130 int | 130 int |
131 buf_putc(struct buf *buf, int c) | 131 buf_putc(struct buf *buf, int c) |
132 { | 132 { |
133 return buf_printf(buf, "%c", c); | 133 char str[2] = { c, '\0' }; |
134 | |
135 return buf_ncat(buf, str, 1); | |
134 } | 136 } |
135 | 137 |
136 /* | 138 /* |
137 * Concatenate the printf(3) like call to the string buffer, this function | 139 * Concatenate the printf(3) like call to the string buffer, this function |
138 * returns -1 on fixed safe buffer, otherwise 0 is returned if there | 140 * returns -1 on fixed safe buffer, otherwise 0 is returned if there |
202 | 204 |
203 return 0; | 205 return 0; |
204 } | 206 } |
205 | 207 |
206 /* | 208 /* |
209 * Remove `n' characters from the buffer, a positive value will cut the string | |
210 * from beginning, negative value will cut from end. | |
211 */ | |
212 | |
213 void | |
214 buf_cut(struct buf *buf, int n) | |
215 { | |
216 int pos; | |
217 | |
218 if (n < 0 || (unsigned int)n >= buf->length) | |
219 return; | |
220 | |
221 if (n < 0 && buf->length - (-n) > 0) | |
222 pos = buf->length - (-n); | |
223 else if ((unsigned int)n < buf->length) | |
224 pos = n; | |
225 | |
226 buf->text[pos] = '\0'; | |
227 buf->length -= buf->length - pos; | |
228 } | |
229 | |
230 /* | |
207 * Clear the string buffer. | 231 * Clear the string buffer. |
208 */ | 232 */ |
209 | 233 |
210 void | 234 void |
211 buf_clear(struct buf *buf) | 235 buf_clear(struct buf *buf) |