diff options
author | rsiddharth <s@ricketyspace.net> | 2019-11-28 18:29:17 -0500 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-04-17 20:56:35 -0400 |
commit | 63c5da4f5f646d4c989e7760be46de86e3b01efe (patch) | |
tree | 4b41d5b24d75a4b56e500ed45015b86237cd66d2 /nserver/src/ncmd.c | |
parent | df712efd0127a8f145fcec39b695485270367783 (diff) |
nserver: ncmd.h: Add find_function.
* nserver/src/ncmd.c (test_find_function): New function.
(all_tests): Add test_find_function.
* nserver/src/ncmd.h (FUNCTIONS): New enum.
(find_function): New function declaration.
* nserver/tests/ncmd_tests.c (find_function): New function
definition.
Diffstat (limited to 'nserver/src/ncmd.c')
-rw-r--r-- | nserver/src/ncmd.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c index 8c90831..ad57e2b 100644 --- a/nserver/src/ncmd.c +++ b/nserver/src/ncmd.c @@ -98,3 +98,56 @@ struct bstrList *cmd_parts(char *cmd) return NULL; } +int find_function(struct bstrList *cmd_parts) +{ + // functions. + struct tagbstring fcreate = bsStatic("/create"); + struct tagbstring fsample = bsStatic("/sample"); + struct tagbstring fmean = bsStatic("/mean"); + struct tagbstring fdump = bsStatic("/dump"); + struct tagbstring fdelete = bsStatic("/delete"); + struct tagbstring flist = bsStatic("/list"); + + check(cmd_parts != NULL, "cmd_parts is NULL"); + check(cmd_parts->qty > 0, "qty check failed"); + + bstring cmd_name = cmd_parts->entry[0]; + check(blength(cmd_name) > 0, "cmd_name check failed"); + + // trim cmd name + int rc = btrimws(cmd_name); + check(rc == BSTR_OK, "cmd name trim failed"); + + // find function for cmd_name + if (bstricmp(cmd_name, &fcreate) == 0) { + check(cmd_parts->qty == 2, "cmd invalid"); + + return NS_CREATE; + } else if (bstricmp(cmd_name, &fsample) == 0) { + check(cmd_parts->qty == 3, "cmd invalid"); + + return NS_SAMPLE; + } else if (bstricmp(cmd_name, &fmean) == 0) { + check(cmd_parts->qty == 2, "cmd invalid"); + + return NS_MEAN; + } else if (bstricmp(cmd_name, &fdump) == 0) { + check(cmd_parts->qty == 2, "cmd invalid"); + + return NS_DUMP; + } else if (bstricmp(cmd_name, &fdelete) == 0) { + check(cmd_parts->qty == 2, "cmd invalid"); + + return NS_DELETE; + } else if (bstricmp(cmd_name, &flist) == 0) { + check(cmd_parts->qty == 1, "cmd invalid"); + + return NS_LIST; + } else { + return NS_NOP; + } + + error: + return NS_NOP; +} + |