From 7b9c0abcb0af8cf29b2700703477993f516f10ce Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 11 Mar 2009 16:28:54 -0700 Subject: [PATCH] cconf: can use substitution variables --- src/cconf.cc | 78 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/src/cconf.cc b/src/cconf.cc index 14f56940d9541..fde15c560316b 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -16,12 +16,80 @@ using namespace std; const char *id = NULL, *type = NULL; char *name, *alt_name; -char *post_process_val(char *val) +#define MAX_VAR_LEN 32 + + +static bool get_var(const char *str, int pos, char *var_name, int len, int *new_pos) +{ + int bracket = (str[pos] == '{'); + int out_pos = 0; + + if (bracket) { + pos++; + } + + while (str[pos] && + ((bracket && str[pos] != '}') || + isalnum(str[pos]))) { + var_name[out_pos] = str[pos]; + + out_pos ++; + if (out_pos == len) + return false; + pos++; + } + + var_name[out_pos] = '\0'; + + if (bracket && (str[pos] == '}')) + pos++; + + *new_pos = pos; + + return true; +} + +static const char *var_val(char *var_name) +{ + if (strcmp(var_name, "type")==0) + return type; + if (strcmp(var_name, "id")==0) + return id; + if (strcmp(var_name, "name")==0) + return name; + + return ""; +} + +#define MAX_LINE 256 + +static char *post_process_val(const char *val) { - return val; + char var_name[MAX_VAR_LEN]; + char buf[MAX_LINE]; + int i=0; + int out_pos = 0; + + while (val[i] && (out_pos < MAX_LINE - 1)) { + if (val[i] == '$') { + if (get_var(val, i+1, var_name, MAX_VAR_LEN, &i)) { + out_pos += snprintf(buf+out_pos, MAX_LINE-out_pos, "%s", var_val(var_name)); + } else { + ++i; + } + } else { + buf[out_pos] = val[i]; + ++out_pos; + ++i; + } + } + + buf[out_pos] = '\0'; + + return strdup(buf); } -void usage() +static void usage() { cerr << "usage: cconf <-c filename> [-t type] [-i id] [-l|--list_sections ] [-s
] [[-s section] ... ] [default]" << std::endl; exit(1); @@ -131,13 +199,13 @@ int main(int argc, const char **argv) cf.read(sections[i], key, (char **)&val, NULL); if (val) { - cout << val << std::endl; + cout << post_process_val(val) << std::endl; exit(0); } } if (defval) { - cout << defval << std::endl; + cout << post_process_val(defval) << std::endl; exit(0); } -- 2.39.5