1 /* This file is part of SDL_him - SDL Hildon Input Method addon
2 * Copyright (C) 2010 Javier S. Pedro
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
24 /* This could be better. */
26 #define CAPACITY_MARGIN 10
28 void ds_clear(dstring_t *ds)
31 ds->capacity = CAPACITY_MARGIN;
32 ds->str = malloc(ds->capacity + 1);
34 ds->str[ds->len] = '\0';
37 void ds_set(dstring_t *ds, const char *str)
40 /* Clean the string, but don't reallocate capacity. */
42 if (ds->capacity > 0) {
48 const size_t len = strlen(str);
50 if (ds->capacity < len) {
52 ds->capacity = len + CAPACITY_MARGIN;
53 ds->str = malloc(ds->capacity + 1);
60 void ds_append(dstring_t *ds, const char *str)
63 const size_t len = strlen(str);
64 const size_t new_len = ds->len + len;
66 if (ds->capacity < new_len) {
67 ds->capacity = new_len + CAPACITY_MARGIN;
68 ds->str = realloc(ds->str, ds->capacity + 1);
71 strcpy(&ds->str[ds->len], str);