diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2010-08-09 18:52:23 -0700 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2010-08-09 18:52:23 -0700 |
commit | 722f67e407b5a1cf8aa060a40355982176870ede (patch) | |
tree | 6c381dafc98faa74d86d1bb0481fe6da077db16d | |
parent | 97bf9794e381317ca89d15009b6827dc6ff9d78c (diff) | |
download | vyatta-cfg-722f67e407b5a1cf8aa060a40355982176870ede.tar.gz vyatta-cfg-722f67e407b5a1cf8aa060a40355982176870ede.zip |
add function to C API
-rw-r--r-- | src/cstore/cstore-c.cpp | 58 | ||||
-rw-r--r-- | src/cstore/cstore-c.h | 4 |
2 files changed, 47 insertions, 15 deletions
diff --git a/src/cstore/cstore-c.cpp b/src/cstore/cstore-c.cpp index 0e25b3c..95f052b 100644 --- a/src/cstore/cstore-c.cpp +++ b/src/cstore/cstore-c.cpp @@ -21,6 +21,14 @@ #include <cstore/cstore-c.h> #include <cstore/unionfs/cstore-unionfs.hpp> +static void +_get_str_vec(vector<string>& vec, const char *strs[], int num_strs) +{ + for (int i = 0; i < num_strs; i++) { + vec.push_back(strs[i]); + } +} + void * cstore_init(void) { @@ -41,9 +49,7 @@ cstore_validate_tmpl_path(void *handle, const char *path_comps[], { if (handle) { vector<string> vs; - for (int i = 0; i < num_comps; i++) { - vs.push_back(path_comps[i]); - } + _get_str_vec(vs, path_comps, num_comps); Cstore *cs = (Cstore *) handle; return (cs->validateTmplPath(vs, validate_tags) ? 1 : 0); } @@ -56,9 +62,7 @@ cstore_validate_tmpl_path_d(void *handle, const char *path_comps[], { if (handle) { vector<string> vs; - for (int i = 0; i < num_comps; i++) { - vs.push_back(path_comps[i]); - } + _get_str_vec(vs, path_comps, num_comps); Cstore *cs = (Cstore *) handle; return (cs->validateTmplPath(vs, validate_tags, *def) ? 1 : 0); } @@ -70,9 +74,7 @@ cstore_cfg_path_exists(void *handle, const char *path_comps[], int num_comps) { if (handle) { vector<string> vs; - for (int i = 0; i < num_comps; i++) { - vs.push_back(path_comps[i]); - } + _get_str_vec(vs, path_comps, num_comps); Cstore *cs = (Cstore *) handle; return (cs->cfgPathExists(vs) ? 1 : 0); } @@ -108,15 +110,39 @@ cstore_cfg_path_deactivated(void *handle, const char *path_comps[], { if (handle) { vector<string> vs; - for (int i = 0; i < num_comps; i++) { - vs.push_back(path_comps[i]); - } + _get_str_vec(vs, path_comps, num_comps); Cstore *cs = (Cstore *) handle; return (cs->cfgPathDeactivated(vs, in_active) ? 1 : 0); } return 0; } +char * +cstore_cfg_path_get_effective_value(void *handle, const char *path_comps[], + int num_comps) +{ + if (handle) { + vector<string> vs; + _get_str_vec(vs, path_comps, num_comps); + Cstore *cs = (Cstore *) handle; + string val; + if (!cs->cfgPathGetEffectiveValue(vs, val)) { + return NULL; + } + + int vsize = val.length(); + char *buf = (char *) malloc(vsize + 1); + if (!buf) { + return NULL; + } + strncpy(buf, val.c_str(), vsize); + buf[vsize] = 0; + + return buf; + } + return NULL; +} + char ** cstore_path_string_to_path_comps(const char *path_str, int *num_comps) { @@ -141,10 +167,12 @@ cstore_path_string_to_path_comps(const char *path_str, int *num_comps) vec.push_back(start); } char **ret = (char **) malloc(sizeof(char *) * vec.size()); - for (unsigned int i = 0; i < vec.size(); i++) { - ret[i] = strdup(vec[i].c_str()); + if (ret) { + for (unsigned int i = 0; i < vec.size(); i++) { + ret[i] = strdup(vec[i].c_str()); + } + *num_comps = vec.size(); } - *num_comps = vec.size(); free(pstr); return ret; } diff --git a/src/cstore/cstore-c.h b/src/cstore/cstore-c.h index c2c5fa0..a2ad844 100644 --- a/src/cstore/cstore-c.h +++ b/src/cstore/cstore-c.h @@ -34,6 +34,10 @@ int cstore_cfg_path_exists(void *handle, const char *path_comps[], int cstore_cfg_path_deactivated(void *handle, const char *path_comps[], int num_comps, int in_active); +char *cstore_cfg_path_get_effective_value(void *handle, + const char *path_comps[], + int num_comps); + /* the following are internal APIs for the library. they can only be used * during cstore operations since they operate on "current" paths constructed * by the operations. |