X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_tk_space.c;h=8b26d99e8a1abf0a4c78cb499def323f914823b9;hb=fd55c75d4ece51751ae95d98706458615e5b4776;hp=20facf8a5c0e76599a9c3fd62b12a2d6184d4401;hpb=0ae8acfbbe4820ccf222c49d8c49aa45202f4f18;p=protos%2Flibecoli.git diff --git a/lib/ecoli_tk_space.c b/lib/ecoli_tk_space.c index 20facf8..8b26d99 100644 --- a/lib/ecoli_tk_space.c +++ b/lib/ecoli_tk_space.c @@ -30,37 +30,107 @@ #include #include +#include +#include #include +#include #include #include +struct ec_tk_space { + struct ec_tk gen; +}; + static struct ec_parsed_tk *ec_tk_space_parse(const struct ec_tk *gen_tk, - const char *str) + const struct ec_strvec *strvec) { - struct ec_parsed_tk *parsed_tk; + struct ec_parsed_tk *parsed_tk = NULL; + struct ec_strvec *match_strvec; + const char *str; size_t len = 0; - if (!isspace(str[0])) - return NULL; - - parsed_tk = ec_parsed_tk_new(gen_tk); + parsed_tk = ec_parsed_tk_new(); if (parsed_tk == NULL) - return NULL; + goto fail; + + if (ec_strvec_len(strvec) == 0) + return parsed_tk; + str = ec_strvec_val(strvec, 0); while (isspace(str[len])) len++; + if (len == 0 || len != strlen(str)) + return parsed_tk; - parsed_tk->str = ec_strndup(str, len); + match_strvec = ec_strvec_ndup(strvec, 0, 1); + if (match_strvec == NULL) + goto fail; + + ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec); return parsed_tk; + + fail: + ec_parsed_tk_free(parsed_tk); + return NULL; } -static struct ec_tk_ops ec_tk_space_ops = { +static struct ec_tk_type ec_tk_space_type = { + .name = "space", .parse = ec_tk_space_parse, + .complete = ec_tk_default_complete, }; +EC_TK_TYPE_REGISTER(ec_tk_space_type); + struct ec_tk *ec_tk_space_new(const char *id) { - return ec_tk_new(id, &ec_tk_space_ops, sizeof(struct ec_tk_space)); + return ec_tk_new(id, &ec_tk_space_type, sizeof(struct ec_tk_space)); } +static int ec_tk_space_testcase(void) +{ + struct ec_tk *tk; + int ret = 0; + + tk = ec_tk_space_new(NULL); + if (tk == NULL) { + ec_log(EC_LOG_ERR, "cannot create tk\n"); + return -1; + } + ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " "); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " ", "foo"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, ""); + ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " foo"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo "); + ec_tk_free(tk); + + /* test completion */ + tk = ec_tk_space_new(NULL); + if (tk == NULL) { + ec_log(EC_LOG_ERR, "cannot create tk\n"); + return -1; + } + ret |= EC_TEST_CHECK_TK_COMPLETE(tk, + "", EC_TK_ENDLIST, + EC_TK_ENDLIST, + ""); + ret |= EC_TEST_CHECK_TK_COMPLETE(tk, + " ", EC_TK_ENDLIST, + EC_TK_ENDLIST, + ""); + ret |= EC_TEST_CHECK_TK_COMPLETE(tk, + "foo", EC_TK_ENDLIST, + EC_TK_ENDLIST, + ""); + ec_tk_free(tk); + + return ret; +} + +static struct ec_test ec_tk_space_test = { + .name = "tk_space", + .test = ec_tk_space_testcase, +}; + +EC_TEST_REGISTER(ec_tk_space_test);