From 1f30b63db9aeab2332d269939c46c2fd9a0f0f8d Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 8 May 2018 03:32:44 +0300 Subject: Some work on lists --- CMakeLists.txt | 14 +++++ src/actionhelper.c | 11 ++++ src/actionhelper.h | 10 ++++ src/actions.c | 38 ++++++++----- src/ck.c | 26 +++++++++ src/cklist.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cklist.h | 56 +++++++++++++++++++ 7 files changed, 298 insertions(+), 15 deletions(-) create mode 100644 src/actionhelper.c create mode 100644 src/actionhelper.h create mode 100644 src/cklist.c create mode 100644 src/cklist.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e38e8e2..fe0934c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,13 @@ +## actionparser.c - Action parser for ck -------------------------------*- C -*- +## +## This file is part of ck, the config keeper +## +## ----------------------------------------------------------------------------- +## +## Copyright (C) 2018 Anastasis Grammenos +## GPLv3 (see LICENCE for the full notice) +## +## -------------------------------------------------------------------------- */ cmake_minimum_required (VERSION 3.5.6) project(ck C) @@ -22,19 +32,23 @@ set(ckLib_src ${SRC_DIR}/ck.c ${SRC_DIR}/actionparser.c ${SRC_DIR}/actions.c + ${SRC_DIR}/actionhelper.c ${SRC_DIR}/confparser.c ${SRC_DIR}/dblayer.c ${SRC_DIR}/dbhelper.c ${SRC_DIR}/ckutil.c + ${SRC_DIR}/cklist.c ${SRC_DIR}/engine.c ) set(ckLib_hdr ${SRC_DIR}/actionparser.h ${SRC_DIR}/actions.h + ${SRC_DIR}/actionhelper.h ${SRC_DIR}/confparser.h ${SRC_DIR}/dblayer.h ${SRC_DIR}/dbhelper.h ${SRC_DIR}/ckutil.h + ${SRC_DIR}/cklist.h ${SRC_DIR}/engine.h ) diff --git a/src/actionhelper.c b/src/actionhelper.c new file mode 100644 index 0000000..55f0326 --- /dev/null +++ b/src/actionhelper.c @@ -0,0 +1,11 @@ +#include "actionhelper.h" + +/* Holds the suggestions */ +char _sug[2][STR_L]; + +edit_rc +edit_get_config_or_suggestions(const int argc, + char **argv, + char *ret) { + return ERC_ERR; +} diff --git a/src/actionhelper.h b/src/actionhelper.h new file mode 100644 index 0000000..75ad0a7 --- /dev/null +++ b/src/actionhelper.h @@ -0,0 +1,10 @@ +#include "ckutil.h" + +typedef enum edit_opt_return_code edit_rc; +enum edit_opt_return_code { + ERC_OK, + ERC_SUGGESTIONS, + ERC_ERR +}; + +extern edit_rc edit_get_config_or_suggestions(const int argc, char **argv, char *ret); diff --git a/src/actions.c b/src/actions.c index 3a4d8ac..e72e2b5 100644 --- a/src/actions.c +++ b/src/actions.c @@ -9,6 +9,7 @@ * * -------------------------------------------------------------------------- */ #include "actions.h" +#include "actionhelper.h" #include "dblayer.h" #include "ckutil.h" #include "engine.h" @@ -57,21 +58,16 @@ AddOpt make_add_options(const int argc, char **argv) { return addOpt; } } else if (argc == 4) { - if (strcmp(argv[2], "-s") == 0) { - addOpt.secret = 1; - } else if (strcmp(argv[2], "-p") == 0) { - addOpt.prime = 1; - } else { - addOpt.err = ADD_ERR_WRONG_FLAGS; - return addOpt; - } - if (strcmp(argv[3], "-s") == 0) { - addOpt.secret = 1; - } else if (strcmp(argv[3], "-p") == 0) { - addOpt.prime = 1; - } else { - addOpt.err = ADD_ERR_WRONG_FLAGS; - return addOpt; + int i; + for (i = 2; i < 4; i++) { + if (strcmp(argv[i], "-s") == 0) { + addOpt.secret = 1; + } else if (strcmp(argv[i], "-p") == 0) { + addOpt.prime = 1; + } else { + addOpt.err = ADD_ERR_WRONG_FLAGS; + return addOpt; + } } } return addOpt; @@ -151,6 +147,18 @@ int run_EDIT(UserOpt *opt, Conf *conf) { close_DB(&db); return 0; } + } else { + close_DB(&db); + char confName[STR_L]; + switch (edit_get_config_or_suggestions(opt->argc, + opt->argv, + confName)) { + case ERC_OK: + return 0; + case ERC_ERR: + return 1; + } + return 1; } close_DB(&db); diff --git a/src/ck.c b/src/ck.c index 4861c06..972dc7e 100644 --- a/src/ck.c +++ b/src/ck.c @@ -30,6 +30,7 @@ #include "actions.h" #include "dblayer.h" +#include "cklist.h" #include "ckutil.h" void free_res(UserOpt *opt, Conf *conf) { @@ -42,6 +43,31 @@ void free_res(UserOpt *opt, Conf *conf) { } int main(int argc, char *argv[]) { + + cklist *p; + p = list_make_and_add("0"); + list_add(p, "1"); + list_add(p, "2"); + list_add(p, "3"); + list_add(p, "4"); + list_add(p, "5"); + list_add(p, "6"); + list_add(p, "7"); + list_add(p, "8"); + list_print_lisp(p); + cklist *po = list_move(p); + cklist *po_l = list_copy_until(po, 4); + list_print_lisp(po_l); + cklist *po_r = list_copy_from(po, 5); + list_print_lisp(po_r); + cklist *po_part = list_copy_part(po, 2, 4); + list_print_lisp(po_part); + list_print(po); + list_free(po_part); + list_free(po); + list_free(po_l); + list_free(po_r); + UserOpt opt; /* get user opt */ switch(parse_action(argc, argv, &opt)) { 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; isize; i++) { + free(ckl->arr[i]); + } + free(ckl->arr); + free(ckl); +} diff --git a/src/cklist.h b/src/cklist.h new file mode 100644 index 0000000..9c883b5 --- /dev/null +++ b/src/cklist.h @@ -0,0 +1,56 @@ +/* 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) + * + * ----------------------------------------------------------------------------- + * + * Ck needs a list of strings. Assists with finding the correct config file + * to edit when using ck edit and provides a backend to the ck list action. + * + * -------------------------------------------------------------------------- */ + +typedef struct cklist_st cklist; +struct cklist_st { + int size; + int pos; + char **arr; +}; + +extern cklist* list_make_new(); +extern void list_add(cklist *ckl, const char *str); +extern cklist* list_make_and_add(const char *str); +extern void list_rewind(cklist *ckl); + +extern int list_next(cklist *ckl); +extern char* list_get(cklist *ckl); + +extern void list_size(cklist *ckl); + +/* rewinds */ +extern cklist* list_duplicate(cklist *ckl); +/* rewinds */ +extern cklist* list_move(cklist *ckl); +/* rewinds + * copy from index (>=) to the end */ +extern cklist* list_copy_from(cklist *ckl, int index); +/* rewinds + * copy from the start until (<) index*/ +extern cklist* list_copy_until(cklist *ckl, int index); + +/* rewinds + * copy from (>=) until (<) */ +extern cklist* list_copy_part(cklist *ckl, int from, int until); + +/* rewinds */ +extern void list_print_lisp(cklist *ckl); +/* rewinds */ +extern void list_print(cklist *ckl); + +/* Deallocate resources */ +extern void list_free(cklist *ckl); + -- cgit v1.2.3