From 401226f094a3ba9a7745378ecd04f26003e32c12 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Sat, 5 Nov 2022 02:08:50 +0000 Subject: [PATCH] expose more functions --- CMakeLists.txt | 3 +- main.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++--- main.h | 17 +++++------ 3 files changed, 88 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8881ec..1b11056 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") add_library(clAugeas SHARED include/augSettings.cpp include/augSettings.h - main.cpp main.h) + + main.cpp main.h include/pair.cpp) target_link_libraries(clAugeas augeas) diff --git a/main.cpp b/main.cpp index 4b9f806..67c23a3 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "main.h" #include +#include extern "C" { @@ -28,7 +29,7 @@ bool load_file(void* aug, const char* filePath) { } -CLAPI void print_preview(augeas* aug, const char* matchPath, const char* filePath) { +CLAPI void print_preview(augeas* aug, const char* matchPath) { if(aug == nullptr) { std::cout << "error: augeas is not initialized." << std::endl; @@ -54,8 +55,7 @@ CLAPI void print_preview(augeas* aug, const char* matchPath, const char* filePat // Print tree of the matchPath CLAPI void print_tree( augeas *aug, - const char* matchPath, - const char* filePath + const char* matchPath ) { if(aug == nullptr) { @@ -110,6 +110,79 @@ CLAPI void print_tree( } } +CLAPI const char* get_tree( + augeas *aug, + const char* matchPath +) { + + if(aug == nullptr) { + std::cout << "error: augeas is not initialized." << std::endl; + return ""; + } + + int r; + + FILE *out = tmpfile(); + + r = aug_print(aug, out,matchPath); + + if(r != 0) { + std::cout << "error: aug_print failure." << std::endl; + return ""; + } + + std::map stdBindList; + std::map ::iterator pos; + + std::stringstream rstream; + + + char line[256]; + rewind(out); + while (fgets(line, 256, out) != nullptr) { + // remove end of line + line[strlen(line) - 1] = '\0'; + std::string str_matchPath = matchPath; + std::string s = line; + // skip comments + if (s.find("#comment") != std::string::npos) + continue; + s = s.substr(str_matchPath.length() - 1); + // split by '=' sign + size_t eqpos = s.find(" = "); + if (eqpos == std::string::npos) + continue; + // extract key and value + std::string key = s.substr(0, eqpos); + std::string value = s.substr(eqpos + 3); + + // remove '"' sign from around value + value.erase(value.begin()); + value.erase(value.end() - 1); + + rstream << ";K;" << key << ";VAL;" << value << ";ENDL;"; + + stdBindList.insert(std::pair(key,value)); // 2 + + } + + fclose(out); + + + for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) + { + std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; + } + + return strdup(rstream.str().c_str()); + +} + +CLAPI void free_str(char* str) { + if(str != nullptr) { + free(str); + } +} CLAPI const char* get_node(augeas* aug, char* path ) { @@ -130,11 +203,11 @@ CLAPI const char* get_node(augeas* aug, char* path ) { if (nullptr != value) { return value; } else { - return "NIL"; + return ""; } } else { - return "NIL"; + return ""; } } diff --git a/main.h b/main.h index 486303b..f5e356a 100644 --- a/main.h +++ b/main.h @@ -13,21 +13,20 @@ extern "C" { -CLAPI void* init_aug (augSettings settings, int flags); +CLAPI void *init_aug (augSettings settings, int flags); -CLAPI void print_preview (augeas* aug, - const char* matchPath, - const char* filePath); +CLAPI bool load_file (void *aug, const char *filePath); -CLAPI void print_tree (augeas* aug, - const char* matchPath, - const char* filePath); +CLAPI void print_preview (augeas *aug,const char *matchPath); -CLAPI const char* get_node (augeas* aug, char* path ); +CLAPI void print_tree (augeas *aug,const char *matchPath); +CLAPI const char *get_tree (augeas *aug,const char *matchPath); +CLAPI const char *get_node (augeas *aug, char *path); -CLAPI void close_aug (void* aug); +CLAPI void close_aug (void *aug); +CLAPI void free_str (char *str); }