--- /dev/null
+/* This file is part of SDL_him - SDL Hildon Input Method addon
+ * Copyright (C) 2010 Javier S. Pedro
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "ds.h"
+
+/* This could be better. */
+
+#define CAPACITY_MARGIN 10
+
+void ds_clear(dstring_t *ds)
+{
+ free(ds->str);
+ ds->capacity = CAPACITY_MARGIN;
+ ds->str = malloc(ds->capacity + 1);
+ ds->len = 0;
+ ds->str[ds->len] = '\0';
+}
+
+void ds_set(dstring_t *ds, const char *str)
+{
+ if (!str) {
+ /* Clean the string, but don't reallocate capacity. */
+ ds->len = 0;
+ if (ds->capacity > 0) {
+ ds->str[0] = '\0';
+ }
+ return;
+ }
+
+ const size_t len = strlen(str);
+
+ if (ds->capacity < len) {
+ free(ds->str);
+ ds->capacity = len + CAPACITY_MARGIN;
+ ds->str = malloc(ds->capacity + 1);
+ }
+
+ strcpy(ds->str, str);
+ ds->len = len;
+}
+
+void ds_append(dstring_t *ds, const char *str)
+{
+ if (!str) return;
+ const size_t len = strlen(str);
+ const size_t new_len = ds->len + len;
+
+ if (ds->capacity < new_len) {
+ ds->capacity = new_len + CAPACITY_MARGIN;
+ ds->str = realloc(ds->str, ds->capacity + 1);
+ }
+
+ strcpy(&ds->str[ds->len], str);
+ ds->len = new_len;
+}
+