Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
opt_ref_t  -> cmdl_ref_t
opt_enum_t -> cmdl_enum_t
cmdl_prase()  -> cmdlgrp_parse()
cmdl_help()   -> cmdlgrp_print_help()
cmdl_result() -> cmdlgrp_print_result()
  • Loading branch information
gitgjogh committed Jan 30, 2017
1 parent 36af342 commit 92a7e64
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 71 deletions.
76 changes: 38 additions & 38 deletions libsim/sim_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ int arg_parse_xlevel(int i, int argc, char *argv[])
return i;
}

const opt_ref_t* sim_name_2_ref(const opt_ref_t *refs, const char *name)
const cmdl_ref_t* sim_name_2_ref(const cmdl_ref_t *refs, const char *name)
{
if (refs == 0 || name == 0) { return 0; }

for (const opt_ref_t *p = refs; p->name; ++p) {
for (const cmdl_ref_t *p = refs; p->name; ++p) {
if (0==strcmp(p->name, name)) {
return p;
}
Expand All @@ -274,11 +274,11 @@ const opt_ref_t* sim_name_2_ref(const opt_ref_t *refs, const char *name)
}

/** string val to idx */
const opt_ref_t* sim_sval_2_ref(const opt_ref_t *refs, const char* sval)
const cmdl_ref_t* sim_sval_2_ref(const cmdl_ref_t *refs, const char* sval)
{
if (refs == 0 || sval == 0) { return 0; }

for (const opt_ref_t *p = refs; p->name; ++p) {
for (const cmdl_ref_t *p = refs; p->name; ++p) {
if (0==strcmp(p->val, sval)) {
return p;
}
Expand All @@ -287,7 +287,7 @@ const opt_ref_t* sim_sval_2_ref(const opt_ref_t *refs, const char* sval)
}

/** int val to idx */
const opt_ref_t* ref_ival_2_idx(opt_ref_t *refs, int val)
const cmdl_ref_t* ref_ival_2_idx(cmdl_ref_t *refs, int val)
{
char s[256] = {0};

Expand All @@ -301,31 +301,31 @@ const opt_ref_t* ref_ival_2_idx(opt_ref_t *refs, int val)
return sim_sval_2_ref(refs, s);
}

const opt_enum_t* sim_ival_2_enum(const opt_enum_t* enums, int val)
const cmdl_enum_t* sim_ival_2_enum(const cmdl_enum_t* enums, int val)
{
if (enums == 0) { return 0; }

for (const opt_enum_t *p = enums; p->name; ++p) {
for (const cmdl_enum_t *p = enums; p->name; ++p) {
if (p->val == val) {
return p;
}
}
return 0;
}

const opt_enum_t* sim_sval_2_enum(const opt_enum_t* enums, const char *sval)
const cmdl_enum_t* sim_sval_2_enum(const cmdl_enum_t* enums, const char *sval)
{
if (enums == 0 || sval == 0) { return 0; }

int ival = atoi(sval);
return sim_ival_2_enum(enums, ival);
}

const opt_enum_t* sim_name_2_enum(const opt_enum_t* enums, const char *name)
const cmdl_enum_t* sim_name_2_enum(const cmdl_enum_t* enums, const char *name)
{
if (enums == 0 || name == 0) { return 0; }

for (const opt_enum_t *p = enums; p->name; ++p) {
for (const cmdl_enum_t *p = enums; p->name; ++p) {
if (0 == strcmp(p->name, name)) {
return p;
}
Expand Down Expand Up @@ -449,12 +449,12 @@ int cmdl_parse_range (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *

return 2;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
int *range = (int *)dst;
xprint("'%d~%d';\n", range[0], range[1]);
}
else if (act == CMDL_ACT_HELP)
else if (act == CMDL_PRI_HELP)
{
xlprint(iter->layer+1, "`$start[~$last]` or `[$start]~$last`\n");
xlprint(iter->layer+1, "`$start[+$count]` or `[$start]+$count`\n");
Expand All @@ -477,11 +477,11 @@ int cmdl_parse_str (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
*((char **)dst) = arg;
return 2;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
xprint("'%s'", *((char **)dst));
}
else if (act == CMDL_ACT_ARGFMT) {
else if (act == CMDL_PRI_ARGFMT) {
xprint("<%%s>");
}

Expand All @@ -502,11 +502,11 @@ int cmdl_parse_strcpy(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
strcpy(*((char **)dst), arg);
return 2;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
xprint("'%s'", *((char **)dst));
}
else if (act == CMDL_ACT_ARGFMT) {
else if (act == CMDL_PRI_ARGFMT) {
xprint("<%%s>");
}

Expand All @@ -531,11 +531,11 @@ int cmdl_parse_int (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
int b_err = str_2_int(arg, dst);
return b_err ? CMDL_RET_ERROR : 2;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
xprint("'%d'", *((int*)dst));
}
else if (act == CMDL_ACT_ARGFMT)
else if (act == CMDL_PRI_ARGFMT)
{
xprint("<%%d>");
}
Expand All @@ -558,13 +558,13 @@ int cmdl_parse_ints (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
}
return opt->narg + 1;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
for (i=0; i<opt->narg; ++i) {
xprint("%s'%d'", i?"":",", ((int*)dst)[i]);
}
}
else if (act == CMDL_ACT_ARGFMT) {
else if (act == CMDL_PRI_ARGFMT) {
xprint("<%%d,%%d,...>");
}

Expand Down Expand Up @@ -600,11 +600,11 @@ int cmdl_parse_xlevel(cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *

return CMDL_RET_ERROR;
}
else if (act == CMDL_ACT_RESULT)
else if (act == CMDL_PRI_RESULT)
{
xprint("'%d'", xlog_get_level());
}
else if (act == CMDL_ACT_HELP)
else if (act == CMDL_PRI_HELP)
{
xlprint(iter->layer+1, "-xall, log all\n");
xlprint(iter->layer+1, "-xnon, log non\n");
Expand All @@ -625,7 +625,7 @@ int cmdl_parse_help (cmdl_iter_t *iter, void* dst, CMDL_ACT_e act, cmdl_opt_t *
}

/* Get opt by ref_name: user type "-%ref_name" in cmdl */
cmdl_opt_t* cmdl_getdesc_byref (cmdl_opt_t optv[], const char *ref_name)
cmdl_opt_t* cmdl_ref_2_opt (cmdl_opt_t optv[], const char *ref_name)
{
for (cmdl_opt_t* opt = optv; opt->names; ++opt)
{
Expand All @@ -637,7 +637,7 @@ cmdl_opt_t* cmdl_getdesc_byref (cmdl_opt_t optv[], const char *ref_name)
return 0;
}

cmdl_opt_t* cmdl_getdesc_byname(cmdl_opt_t optv[], const char *opt_name)
cmdl_opt_t* cmdl_name_2_opt(cmdl_opt_t optv[], const char *opt_name)
{
for (cmdl_opt_t* opt = optv; opt->names; ++opt) {
//xdbg("%d: %s, %s\n", i, name, optv[i].names);
Expand All @@ -648,18 +648,18 @@ cmdl_opt_t* cmdl_getdesc_byname(cmdl_opt_t optv[], const char *opt_name)
return 0;
}

cmdl_opt_t* cmdl_set_ref(cmdl_opt_t optv[], const char *opt_name, const opt_ref_t *refs)
cmdl_opt_t* cmdl_set_ref(cmdl_opt_t optv[], const char *opt_name, const cmdl_ref_t *refs)
{
cmdl_opt_t* opt = cmdl_getdesc_byname(optv, opt_name);
cmdl_opt_t* opt = cmdl_name_2_opt(optv, opt_name);
if (opt) {
opt->refs = refs;
}
return opt;
}

cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const opt_enum_t *enums)
cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const cmdl_enum_t *enums)
{
cmdl_opt_t* opt = cmdl_getdesc_byname(optv, opt_name);
cmdl_opt_t* opt = cmdl_name_2_opt(optv, opt_name);
if (opt) {
opt->enums = enums;
}
Expand Down Expand Up @@ -751,7 +751,7 @@ int cmdl_init(cmdl_opt_t optv[])
return n_err;
}

int cmdl_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
int cmdlgrp_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
{
ENTER_FUNC();

Expand Down Expand Up @@ -782,9 +782,9 @@ int cmdl_parse(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
cmdl_iter_t _iter2 = cmdl_iter_init(2, ref_expand, 0);

if (arg[1] == '%') { /* -%ref or -%enum */
p_opt = cmdl_getdesc_byref(optv, &arg[2]);
p_opt = cmdl_ref_2_opt(optv, &arg[2]);
} else { /* -opt arg ... */
p_opt = cmdl_getdesc_byname(optv, &arg[1]);
p_opt = cmdl_name_2_opt(optv, &arg[1]);
}
if (p_opt == 0) {
xlog_cmdl("argv[%d] (%s) unrecognized\n", cmdl_iter_idx(iter), arg);
Expand Down Expand Up @@ -881,28 +881,28 @@ void cmdl_iter_dbg (cmdl_iter_t *_iter)
/**
* @param iter just for layer trace
*/
int cmdl_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[])
int cmdlgrp_print_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[])
{
ENTER_FUNC();

++ iter->layer;
for (cmdl_opt_t *opt = optv; opt->names; ++opt)
{
xlprint(iter->layer, "-%s ",opt->names);
opt->parse(iter, 0, CMDL_ACT_ARGFMT, opt);
opt->parse(iter, 0, CMDL_PRI_ARGFMT, opt);
if (opt->default_val) {
xprint(", [%s]", opt->default_val);
}
xprint(", \t%s\n", SAFE_STR(opt->help, ""));
opt->parse(iter, 0, CMDL_ACT_HELP, opt);
opt->parse(iter, 0, CMDL_PRI_HELP, opt);

if (opt->refs) {
for (const opt_ref_t *r = opt->refs; r->name; ++r) {
for (const cmdl_ref_t *r = opt->refs; r->name; ++r) {
xlprint(iter->layer+1, "%%%-10s \t `%s`\n", r->name, r->val);
}
}
if (opt->enums) {
for (const opt_enum_t *e = opt->enums; e->name; ++e) {
for (const cmdl_enum_t *e = opt->enums; e->name; ++e) {
xlprint(iter->layer+1, "%%%-10s \t `%d`\n", e->name, e->val);
}
}
Expand All @@ -917,7 +917,7 @@ int cmdl_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[])
/**
* @param iter just for layer trace
*/
int cmdl_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
int cmdlgrp_print_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
{
ENTER_FUNC();

Expand All @@ -926,7 +926,7 @@ int cmdl_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[])
for (cmdl_opt_t *opt = optv; opt->names; ++opt)
{
xlprint(iter->layer, "-%s = ", opt->names);
opt->parse(iter, (char *)dst + opt->dst_offset, CMDL_ACT_RESULT, opt);
opt->parse(iter, (char *)dst + opt->dst_offset, CMDL_PRI_RESULT, opt);

//FIXME: The following way to get ref.name or enum.name make no sense when:
// 1. optv[] was defined in local scope.
Expand Down
48 changes: 24 additions & 24 deletions libsim/sim_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ int opt_parse_ints(int i, int argc, char *argv[], int n, int *p[]);
int arg_parse_xlevel(int i, int argc, char *argv[]);


typedef struct opt_reference {
typedef struct cmdl_reference {
char *name;
char *val;
} opt_ref_t;
} cmdl_ref_t;

typedef struct opt_enum {
typedef struct cmdl_enum {
char *name;
int val;
} opt_enum_t;
} cmdl_enum_t;

/* null end (name for) refs[] or enums[] */
const opt_ref_t* sim_name_2_ref(const opt_ref_t refs[], const char *name);
const opt_ref_t* sim_sval_2_ref(const opt_ref_t refs[], const char *sval);
const opt_ref_t* sim_ival_2_ref(const opt_ref_t refs[], int ival);
const cmdl_ref_t* sim_name_2_ref(const cmdl_ref_t refs[], const char *name);
const cmdl_ref_t* sim_sval_2_ref(const cmdl_ref_t refs[], const char *sval);
const cmdl_ref_t* sim_ival_2_ref(const cmdl_ref_t refs[], int ival);
#define sim_val_2_ref sim_sval_2_ref
const opt_enum_t* sim_name_2_enum(const opt_enum_t enums[], const char *name);
const opt_enum_t* sim_sval_2_enum(const opt_enum_t enums[], const char *sval);
const opt_enum_t* sim_ival_2_enum(const opt_enum_t enums[], int ival);
const cmdl_enum_t* sim_name_2_enum(const cmdl_enum_t enums[], const char *name);
const cmdl_enum_t* sim_sval_2_enum(const cmdl_enum_t enums[], const char *sval);
const cmdl_enum_t* sim_ival_2_enum(const cmdl_enum_t enums[], int ival);
#define sim_val_2_enum sim_sval_2_enum

typedef struct cmdl_iter {
Expand All @@ -110,9 +110,9 @@ void cmdl_iter_dbg (cmdl_iter_t *iter);

typedef enum {
CMDL_ACT_PARSE = 0,
CMDL_ACT_HELP, ///< print help
CMDL_ACT_ARGFMT, ///< print arg format
CMDL_ACT_RESULT, ///< print parsing result
CMDL_PRI_HELP, ///< print help
CMDL_PRI_ARGFMT, ///< print arg format
CMDL_PRI_RESULT, ///< print parsing result
} CMDL_ACT_e;

typedef enum {
Expand Down Expand Up @@ -154,26 +154,26 @@ typedef struct cmdl_option_description {
char* default_val;
char* help;

const opt_ref_t* refs;
const opt_enum_t* enums;
const cmdl_ref_t* refs;
const cmdl_enum_t* enums;

///< private props:
int n_parse;
int argvIdx;
int b_default;
const opt_ref_t* p_ref;
const opt_enum_t* p_enum;
const cmdl_ref_t* p_ref;
const cmdl_enum_t* p_enum;
} cmdl_opt_t;

cmdl_opt_t* cmdl_getdesc_byref (cmdl_opt_t optv[], const char *ref_name);
cmdl_opt_t* cmdl_getdesc_byname(cmdl_opt_t optv[], const char *opt_name);
cmdl_opt_t* cmdl_set_ref (cmdl_opt_t optv[], const char *opt_name, const opt_ref_t *refs);
cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const opt_enum_t *enums);
cmdl_opt_t* cmdl_ref_2_opt (cmdl_opt_t optv[], const char *ref_name);
cmdl_opt_t* cmdl_name_2_opt(cmdl_opt_t optv[], const char *opt_name);
cmdl_opt_t* cmdl_set_ref (cmdl_opt_t optv[], const char *opt_name, const cmdl_ref_t *refs);
cmdl_opt_t* cmdl_set_enum(cmdl_opt_t optv[], const char *opt_name, const cmdl_enum_t *enums);


int cmdl_parse(cmdl_iter_t *iter, void *dst, cmdl_opt_t optv[]);
int cmdl_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[]);
int cmdl_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[]);
int cmdlgrp_parse(cmdl_iter_t *iter, void *dst, cmdl_opt_t optv[]);
int cmdlgrp_print_help(cmdl_iter_t *iter, void* null, cmdl_opt_t optv[]);
int cmdlgrp_print_result(cmdl_iter_t *iter, void* dst, cmdl_opt_t optv[]);


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 92a7e64

Please sign in to comment.