summaryrefslogtreecommitdiff
path: root/libgammu/misc/array.c
diff options
context:
space:
mode:
authorMichal Čihař <nijel@debian.org>2017-06-30 15:35:36 +0200
committerMichal Čihař <nijel@debian.org>2017-06-30 15:35:36 +0200
commitab91c30e54a279ee9f8f7c5a63a597860c99a705 (patch)
tree50375b85e12295b1becc4d5be6c3b14264b5e728 /libgammu/misc/array.c
parent6441db396f58e65ae6240cf3a8504012b2937ef4 (diff)
New upstream version 1.38.4
Diffstat (limited to 'libgammu/misc/array.c')
-rw-r--r--libgammu/misc/array.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/libgammu/misc/array.c b/libgammu/misc/array.c
new file mode 100644
index 0000000..529a031
--- /dev/null
+++ b/libgammu/misc/array.c
@@ -0,0 +1,62 @@
+/**
+ * \file array.h
+ * \author Michal Čihař
+ *
+ * String arrays handling code.
+ */
+
+#include "array.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+void GSM_StringArray_New(GSM_StringArray *array)
+{
+ array->used = 0;
+ array->allocated = 0;
+ array->data = NULL;
+}
+
+void GSM_StringArray_Free(GSM_StringArray *array)
+{
+ size_t i;
+ for (i = 0; i < array->used; i++) {
+ free(array->data[i]);
+ }
+ free(array->data);
+ GSM_StringArray_New(array);
+}
+
+gboolean GSM_StringArray_Add(GSM_StringArray *array, const char *string)
+{
+ char **newdata;
+
+ /* Allocate extra space if needed */
+ if (array->used + 1 > array->allocated) {
+ newdata = realloc(array->data, (array->allocated + 10) * sizeof(char *));
+ if (newdata == NULL) return FALSE;
+ array->allocated += 10;
+ array->data = newdata;
+ }
+
+ array->data[array->used] = strdup(string);
+ if (array->data[array->used] == NULL) return FALSE;
+
+ array->used++;
+
+ return TRUE;
+}
+
+gboolean GSM_StringArray_Find(GSM_StringArray *array, const char *string)
+{
+ size_t i;
+ for (i = 0; i < array->used; i++) {
+ if (strcmp(array->data[i], string) == 0) return TRUE;
+ }
+ return FALSE;
+}
+
+/* Editor configuration
+ * vim: noexpandtab sw=8 ts=8 sts=8 tw=72:
+ */
+