| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* mowgli_string.c: Immutable string buffers with cheap manipulation. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* Copyright (c) 2007 Pippijn van Steenhoven <pippijn -at- one09.net> |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice is present in all copies. |
| 11 |
* |
| 12 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 16 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 20 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 21 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 |
* POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "mowgli.h" |
| 26 |
|
| 27 |
mowgli_string_t * |
| 28 |
mowgli_string_create(void) |
| 29 |
{ |
| 30 |
mowgli_string_t *self = mowgli_alloc(sizeof *self); |
| 31 |
|
| 32 |
self->size = 64; |
| 33 |
self->pos = 0; |
| 34 |
self->str = mowgli_alloc(self->size); |
| 35 |
|
| 36 |
self->append = &mowgli_string_append; |
| 37 |
self->append_char = &mowgli_string_append_char; |
| 38 |
self->reset = &mowgli_string_reset; |
| 39 |
self->destroy = &mowgli_string_destroy; |
| 40 |
|
| 41 |
return self; |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
mowgli_string_reset(mowgli_string_t *self) |
| 46 |
{ |
| 47 |
return_if_fail(self != NULL); |
| 48 |
|
| 49 |
self->str[0] = self->pos = 0; |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
mowgli_string_destroy(mowgli_string_t *self) |
| 54 |
{ |
| 55 |
return_if_fail(self != NULL); |
| 56 |
|
| 57 |
mowgli_free(self->str); |
| 58 |
mowgli_free(self); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
mowgli_string_append(mowgli_string_t *self, const char *src, size_t n) |
| 63 |
{ |
| 64 |
if (self->size - self->pos <= n) |
| 65 |
{ |
| 66 |
char *new_ptr; |
| 67 |
|
| 68 |
self->size = MAX(self->size * 2, self->pos + n + 8); |
| 69 |
|
| 70 |
new_ptr = mowgli_alloc(self->size); |
| 71 |
mowgli_strlcpy(new_ptr, self->str, self->size); |
| 72 |
|
| 73 |
mowgli_free(self->str); |
| 74 |
self->str = new_ptr; |
| 75 |
} |
| 76 |
|
| 77 |
memcpy(self->str + self->pos, src, n); |
| 78 |
self->pos += n; |
| 79 |
self->str[self->pos] = 0; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
mowgli_string_append_char(mowgli_string_t *self, const char c) |
| 84 |
{ |
| 85 |
if (self->size - self->pos <= 1) |
| 86 |
{ |
| 87 |
char *new_ptr; |
| 88 |
|
| 89 |
self->size = MAX(self->size * 2, self->pos + 9); |
| 90 |
|
| 91 |
new_ptr = mowgli_alloc(self->size); |
| 92 |
mowgli_strlcpy(new_ptr, self->str, self->size); |
| 93 |
|
| 94 |
mowgli_free(self->str); |
| 95 |
self->str = new_ptr; |
| 96 |
} |
| 97 |
|
| 98 |
self->str[self->pos++] = c; |
| 99 |
self->str[self->pos] = 0; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* Copyright (c) 1998 Todd C. Miller <[email protected]> |
| 104 |
* |
| 105 |
* Permission to use, copy, modify, and distribute this software for any |
| 106 |
* purpose with or without fee is hereby granted, provided that the above |
| 107 |
* copyright notice and this permission notice appear in all copies. |
| 108 |
* |
| 109 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 110 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 111 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 112 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 113 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 114 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 115 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 116 |
*/ |
| 117 |
|
| 118 |
size_t |
| 119 |
mowgli_strlcat(char *dst, const char *src, size_t siz) |
| 120 |
{ |
| 121 |
char *d = dst; |
| 122 |
const char *s = src; |
| 123 |
size_t n = siz; |
| 124 |
size_t dlen; |
| 125 |
|
| 126 |
/* Find the end of dst and adjust bytes left but don't go past end */ |
| 127 |
while (n-- != 0 && *d != '\0') |
| 128 |
{ |
| 129 |
d++; |
| 130 |
} |
| 131 |
|
| 132 |
dlen = d - dst; |
| 133 |
n = siz - dlen; |
| 134 |
|
| 135 |
if (n == 0) |
| 136 |
return dlen + strlen(s); |
| 137 |
|
| 138 |
while (*s != '\0') |
| 139 |
{ |
| 140 |
if (n != 1) |
| 141 |
{ |
| 142 |
*d++ = *s; |
| 143 |
n--; |
| 144 |
} |
| 145 |
|
| 146 |
s++; |
| 147 |
} |
| 148 |
|
| 149 |
*d = '\0'; |
| 150 |
|
| 151 |
return dlen + (s - src);/* count does not include NUL */ |
| 152 |
} |
| 153 |
|
| 154 |
size_t |
| 155 |
mowgli_strlcpy(char *dst, const char *src, size_t siz) |
| 156 |
{ |
| 157 |
char *d = dst; |
| 158 |
const char *s = src; |
| 159 |
size_t n = siz; |
| 160 |
|
| 161 |
/* Copy as many bytes as will fit */ |
| 162 |
if (n != 0) |
| 163 |
while (--n != 0) |
| 164 |
{ |
| 165 |
if ((*d++ = *s++) == '\0') |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
/* Not enough room in dst, add NUL and traverse rest of src */ |
| 170 |
|
| 171 |
if (n == 0) |
| 172 |
{ |
| 173 |
if (siz != 0) |
| 174 |
*d = '\0'; /* NUL-terminate dst */ |
| 175 |
|
| 176 |
while (*s++) |
| 177 |
{ } |
| 178 |
} |
| 179 |
|
| 180 |
return s - src - 1; /* count does not include NUL */ |
| 181 |
} |
| 182 |
|
| 183 |
ssize_t |
| 184 |
mowgli_writef(mowgli_descriptor_t fd, const char *fmt, ...) |
| 185 |
{ |
| 186 |
size_t len; |
| 187 |
va_list va; |
| 188 |
char buf[16384]; |
| 189 |
|
| 190 |
return_val_if_fail(fmt != NULL, -1); |
| 191 |
|
| 192 |
va_start(va, fmt); |
| 193 |
len = vsnprintf(buf, sizeof buf, fmt, va); |
| 194 |
va_end(va); |
| 195 |
|
| 196 |
#ifdef _WIN32 |
| 197 |
return send(fd, buf, len, 0); |
| 198 |
#else |
| 199 |
return write(fd, buf, len); |
| 200 |
#endif |
| 201 |
} |