aboutsummaryrefslogtreecommitdiffstats
path: root/src/cklist.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-05-08 03:32:44 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-05-08 03:32:44 +0300
commit1f30b63db9aeab2332d269939c46c2fd9a0f0f8d (patch)
treea0ec8aae21f42be4716ecb6fa5b685583c01e2cd /src/cklist.c
parent3a3424774944a421e1b93cbaf533a3500a4d613c (diff)
downloadck-1f30b63db9aeab2332d269939c46c2fd9a0f0f8d.tar.gz
ck-1f30b63db9aeab2332d269939c46c2fd9a0f0f8d.tar.bz2
ck-1f30b63db9aeab2332d269939c46c2fd9a0f0f8d.zip
Some work on lists
Diffstat (limited to 'src/cklist.c')
-rw-r--r--src/cklist.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/cklist.c b/src/cklist.c
new file mode 100644
index 0000000..32352df
--- /dev/null
+++ b/src/cklist.c
@@ -0,0 +1,158 @@
+/* cklist.h - Custom list data structure for ck ------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
+
+#include "cklist.h"
+#include "ckutil.h"
+
+cklist* list_make_new() {
+ cklist *ckl = malloc(sizeof(cklist));
+ ckl->size = 0;
+ ckl->pos = 0;
+ ckl->arr = NULL;
+
+ return ckl;
+}
+
+void list_add(cklist *ckl, const char *str) {
+ ckl->size++;
+ ckl->arr = (char **)
+ realloc(ckl->arr, ckl->size*sizeof(char *));
+
+ if (!ckl->arr) {
+ return;
+ }
+
+ ckl->arr[ckl->size-1] = strdup(str);
+}
+
+cklist* list_make_and_add(const char *str) {
+ cklist *ckl = list_make_new();
+ list_add(ckl,str);
+ return ckl;
+}
+
+int list_next(cklist *ckl) {
+ if (ckl->size < 1) {
+ return 0;
+ }
+ if (ckl->pos >= ckl->size - 1) {
+ return 0;
+ }
+ ckl->pos++;
+ return 1;
+}
+
+char* list_get(cklist *ckl) {
+ if (ckl->pos == -1) {
+ return NULL;
+ }
+ if (ckl->pos >= ckl->size) {
+ return NULL;
+ }
+ return ckl->arr[ckl->pos];
+}
+
+void list_rewind(cklist *ckl) {
+ ckl->pos = 0;
+}
+
+cklist* list_duplicate(cklist *ckl) {
+ list_rewind(ckl);
+ cklist *_ckl = list_make_and_add(list_get(ckl));
+ while (list_next(ckl)) {
+ list_add(_ckl, list_get(ckl));
+ }
+ list_rewind(ckl);
+ return _ckl;
+}
+
+cklist* list_move(cklist *ckl) {
+ list_rewind(ckl);
+ cklist *_ckl = list_make_and_add(list_get(ckl));
+ while (list_next(ckl)) {
+ list_add(_ckl, list_get(ckl));
+ }
+ list_free(ckl);
+ return _ckl;
+}
+
+cklist* list_copy_from(cklist *ckl, int index) {
+ list_rewind(ckl);
+ cklist *_ckl = list_make_new();
+ if (ckl->pos >= index) {
+ list_add(_ckl, list_get(ckl));
+ }
+ while(list_next(ckl)) {
+ if (ckl->pos >= index) {
+ list_add(_ckl, list_get(ckl));
+ }
+ }
+ list_rewind(ckl);
+ return _ckl;
+}
+
+cklist* list_copy_until(cklist *ckl, int index) {
+ list_rewind(ckl);
+ cklist *_ckl = list_make_and_add(list_get(ckl));
+ while(list_next(ckl)) {
+ if (ckl->pos < index) {
+ list_add(_ckl, list_get(ckl));
+ }
+ }
+ list_rewind(ckl);
+ return _ckl;
+}
+
+cklist* list_copy_part(cklist *ckl, int from, int until) {
+ list_rewind(ckl);
+ cklist *_ckl = list_make_new();
+ if (ckl->pos >= from && ckl->pos < until) {
+ list_add(_ckl, list_get(ckl));
+ }
+ while(list_next(ckl)) {
+ if (ckl->pos >= from && ckl->pos < until) {
+ list_add(_ckl, list_get(ckl));
+ }
+ }
+ list_rewind(ckl);
+ return _ckl;
+}
+
+void list_print_lisp(cklist *ckl) {
+ list_rewind(ckl);
+ printf("( \"%s\"", list_get(ckl));
+ while (list_next(ckl)) {
+ if (ckl->pos == ckl->size-1) {
+ printf(", \"%s\" )\n", list_get(ckl));
+ }
+ else {
+ printf(", \"%s\"", list_get(ckl));
+ }
+ }
+ list_rewind(ckl);
+}
+
+void list_print(cklist *ckl) {
+ list_rewind(ckl);
+ while (list_next(ckl)) {
+ printf("%s\n", list_get(ckl));
+ }
+ list_rewind(ckl);
+}
+
+void list_free(cklist *ckl) {
+ int i;
+ for (i=0; i<ckl->size; i++) {
+ free(ckl->arr[i]);
+ }
+ free(ckl->arr);
+ free(ckl);
+}