nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit cc7676f9c622a973aba1d56836df52d18b6d4b9a
parent 67ec8a3e31c6f100208231145ebfc32f4180558d
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 28 Nov 2019 17:52:27 -0500

nserver: ncmd.h : Add cmd_parts.

* nserver/src/ncmd.c (cmd_parts): New function definition.
* nserver/src/ncmd.h (cmd_parts): New function declaration.
* nserver/tests/ncmd_tests.c (test_cmd_parts): Test cmd_parts.
(all_tests): Add test_cmd_parts

Diffstat:
nserver/src/ncmd.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
nserver/src/ncmd.h | 3+++
nserver/tests/ncmd_tests.c | 32++++++++++++++++++++++++++++++++
3 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -45,3 +45,56 @@ char *check_cmd(char *cmd) err = "internal error\n"; return err; } + + +struct bstrList *cmd_parts(char *cmd) +{ + bstring bcmd = NULL; + struct bstrList *parts_tmp = NULL; + + bcmd = bfromcstr(cmd); + check(bcmd != NULL, "bstring creation failed"); + + parts_tmp = bsplit(bcmd, ' '); + check(parts_tmp != NULL, "cmp split failed"); + check(parts_tmp->qty > 0, "qty check failed"); + + struct bstrList *parts = bstrListCreate(); + check(parts != NULL, "parts create failed"); + + int rc = bstrListAlloc(parts, parts_tmp->qty); + check(rc == BSTR_OK, "parts alloc failed"); + check(parts->qty == 0, "qty check failed"); + + bstring part = NULL; + int index = 0; + for (int i = 0; i < parts_tmp->qty; i++) { + check(parts->qty <= parts->mlen, "parts capacity check failed"); + + part = parts_tmp->entry[i]; + + if (blength(part) == 0) { + continue; + } + + parts->entry[index++] = bstrcpy(part); + parts->qty++; + } + + // Clean up. + bdestroy(bcmd); + bstrListDestroy(parts_tmp); + + return parts; + error: + // Clean up. + if (bcmd) { + bdestroy(bcmd); + } + if (parts_tmp) { + bstrListDestroy(parts_tmp); + } + + return NULL; +} + diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -3,6 +3,7 @@ #include <string.h> +#include <bstrlib.h> #include <dbg.h> #define CMD_MIN_SIZE 5 @@ -11,4 +12,6 @@ int sanitize(char *cmd); char *check_cmd(char *cmd); +struct bstrList *cmd_parts(char *cmd); + #endif diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -105,12 +105,44 @@ char *test_check_cmd() return NULL; } +char *test_cmd_parts() +{ + struct bstrList *parts = NULL; + + bstring create = bfromcstr("/create"); + + char *bacon = "/create bacon"; + parts = cmd_parts(bacon); + mu_assert(parts != NULL, "cmd parts failed"); + mu_assert(parts->qty == 2, "qty check failed"); + mu_assert(bstricmp(parts->entry[0], create) == 0, + "equality check failed"); + + // Cleanup + bstrListDestroy(parts); + + char *ham = "/create ham"; + parts = cmd_parts(ham); + mu_assert(parts != NULL, "cmd parts failed"); + mu_assert(parts->qty == 2, "qty check failed"); + mu_assert(bstricmp(parts->entry[0], create) == 0, + "equality check failed"); + + + // Cleanup + bdestroy(create); + bstrListDestroy(parts); + + return NULL; +} + char *all_tests() { mu_suite_start(); mu_run_test(test_sanitize); mu_run_test(test_check_cmd); + mu_run_test(test_cmd_parts); return NULL; }