From b80df54b9af7d4b06a7fcf2a582982cc071d31e0 Mon Sep 17 00:00:00 2001 From: code liturgy Date: Fri, 11 Nov 2022 17:09:13 -0500 Subject: [PATCH] Improvements and bug fixing --- CMakeLists.txt | 5 +- main.cpp | 178 ++++++++++++++++++++++++++++--------------------- main.h | 34 +++++++--- utils.cpp | 7 ++ utils.h | 13 ++++ 5 files changed, 148 insertions(+), 89 deletions(-) create mode 100644 utils.cpp create mode 100644 utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d0b37a9..c1a1988 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,7 @@ cmake_minimum_required(VERSION 3.21) project(clAugeas VERSION 1.0.1 DESCRIPTION "clAugeas description") set(CMAKE_CXX_STANDARD 14) -if(WIN32) - message("TODO: Windows instructions") -elseif(APPLE) +if(APPLE) include_directories(/opt/homebrew/opt/augeas/include/) #link_directories(/opt/homebrew/opt/augeas/lib) # TODO: Check if works with this commente.d elseif(UNIX) @@ -25,6 +23,7 @@ find_library( add_library(clAugeas SHARED augSettings.cpp augSettings.h + utils.h utils.cpp main.cpp main.h) target_link_libraries(clAugeas xml2) diff --git a/main.cpp b/main.cpp index b722b87..1faa63d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,97 +1,109 @@ #include "main.h" -#include #include - #ifdef __linux__ + #include + #endif + + +template +constexpr size_t size(T (&)[N]) { return N; } + + extern "C" { -inline bool path_exists (const std::string& name) { - struct stat buffer; - return (stat (name.c_str(), &buffer) == 0); -} - -CLAPI void* init_aug(augSettings settings, int flags) { - - if(!path_exists(std::string(settings.root))) { - std::cout << "error: root path is invalid." << settings.root << std::endl; - return nullptr; - } +CLAPI void* +init_aug(augSettings settings, int flags) { + CHECK_RET_NULLPTR(!path_exists(std::string(settings.root)), "Root path is invalid."); return aug_init(settings.root, settings.loadPath, flags); } -CLAPI void close_aug(void* aug) { +CLAPI void +close_aug(void *aug) { + CHECK_RET_VOID(!aug, "Failed to close augeas."); aug_close((augeas *) aug); } -bool load_file(void* aug, const char* filePath) { - int r = aug_load_file((augeas *) aug,filePath); +CLAPI bool +load_file(void *aug, const char *filePath) { + CHECK_RET_BOOL(!aug, INVALID_STATE); + int r = aug_load_file((augeas *) aug, filePath); return r == 0; } - -CLAPI void print_preview(augeas* aug, const char* matchPath) { - - if(aug == nullptr) { - std::cout << "error: augeas is not initialized." << std::endl; - return; - } - +CLAPI void +print_preview(augeas *aug, const char *path) { + CHECK_RET_VOID(!aug, "Augeas is in invalid state"); int r; char *s; - - r = aug_preview(aug,matchPath,&s); - - if(r != 0) { - std::cout << "Failure previewing." << std::endl; - return; - } - + r = aug_preview(aug, path, &s); + CHECK_RET_VOID(r != 0, "Failed to preview.") std::cout << s << std::endl; - free(s); } +CLAPI int rm_node(augeas *aug, const char *path) { + CHECK_RET_INT(!aug, INVALID_STATE); + return aug_rm(aug, path); +} -// Print tree of the matchPath + + +CLAPI char* +get_preview(augeas *aug, const char *path) { + + CHECK_RET_NULLPTR(!aug, INVALID_STATE); + int r; + char *s; + + r = aug_preview(aug, path, &s); + + if (r != 0) { + std::cout << "Failure previewing." << std::endl; + return nullptr; + } + + // must be freed + return s; +} + + +// Print tree of the path CLAPI void print_tree( augeas *aug, - const char* matchPath + const char *path ) { - if(aug == nullptr) { - std::cout << "error: augeas is not initialized." << std::endl; - return; - } - + CHECK_RET_VOID(!aug, INVALID_STATE); + int r; - + FILE *out = tmpfile(); - r = aug_print(aug, out,matchPath); + r = aug_print(aug, out, path); - if(r != 0) { + if (r != 0) { std::cout << "error: aug_print failure." << std::endl; return; } - std::map stdBindList; - std::map ::iterator pos; + std::map stdBindList; + std::map::iterator pos; 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 str_path = path; std::string s = line; // skip comments if (s.find("#comment") != std::string::npos) continue; - s = s.substr(str_matchPath.length() - 1); + s = s.substr(str_path.length() - 1); // split by '=' sign size_t eqpos = s.find(" = "); if (eqpos == std::string::npos) @@ -102,55 +114,50 @@ CLAPI void print_tree( // remove '"' sign from around value value.erase(value.begin()); value.erase(value.end() - 1); - stdBindList.insert(std::pair(key,value)); // 2 + stdBindList.insert(std::pair(key, value)); // 2 } fclose(out); - for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) - { + for (pos = stdBindList.begin(); pos != stdBindList.end(); pos++) { std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; } } -CLAPI const char* get_tree( +CLAPI char *get_tree( augeas *aug, - const char* matchPath + const char *path ) { - if(aug == nullptr) { - std::cout << "error: augeas is not initialized." << std::endl; - return ""; - } + CHECK_RET_NULLPTR(!aug, INVALID_STATE); int r; FILE *out = tmpfile(); - r = aug_print(aug, out,matchPath); + r = aug_print(aug, out, path); - if(r != 0) { + if (r != 0) { std::cout << "error: aug_print failure." << std::endl; - return ""; + return nullptr; } - std::map ::iterator pos; + std::map::iterator pos; std::stringstream ss; - 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 str_path = path; std::string s = line; // skip comments if (s.find("#comment") != std::string::npos) continue; - s = s.substr(str_matchPath.length() - 1); + s = s.substr(str_path.length() - 1); // split by '=' sign size_t eqpos = s.find(" = "); if (eqpos == std::string::npos) @@ -170,16 +177,34 @@ CLAPI const char* get_tree( return strdup(ss.str().c_str()); } -CLAPI void free_str(char* str) { - if(str != nullptr) { +CLAPI void free_str(char *str) { + if (str != nullptr) { free(str); } } -CLAPI const char* get_node(augeas* aug, char* path ) { +CLAPI bool save(augeas *aug) { + CHECK_RET_BOOL(!aug, INVALID_STATE); + return aug_save(aug) == 0; +} + +CLAPI void get_arr( unsigned long &size, char** result) +{ + +} + +CLAPI int match(augeas *aug, const char *match_path, char*** matches) { + CHECK_RET_INT(!aug, INVALID_STATE); + return aug_match(aug, match_path, matches); +} + + + +CLAPI const char *get_node(augeas *aug, const char *path) { const char *value; + CHECK_RET_NULLPTR(!aug, INVALID_STATE); /* * Return 1 if there is exactly one node matching PATH, * 0 if there is none, and a negative value @@ -190,17 +215,18 @@ CLAPI const char* get_node(augeas* aug, char* path ) { * and is valid as long as its node remains unchanged. */ - int rc = aug_get(aug, path, &value); - if (1 == rc) { - if (nullptr != value) { - return value; - } else { - return ""; - } + aug_get(aug, path, &value); + return value; +} - } else { - return ""; - } +CLAPI bool set_node(augeas *aug, const char *path, const char *value) { + CHECK_RET_BOOL(!aug, INVALID_STATE); + return aug_set(aug, path, value) == 0; +} + +CLAPI bool insert_node(augeas *aug, const char *path, const char *label, int before) { + CHECK_RET_BOOL(!aug, INVALID_STATE); + return aug_insert(aug, path, label, before) == 0; } } diff --git a/main.h b/main.h index cc6aadf..66c8704 100644 --- a/main.h +++ b/main.h @@ -4,29 +4,43 @@ #include "map" #include #include "augSettings.h" +#include "utils.h" #if defined(_WIN32) -#define CLAPI __declspec(dllexport) +#define CLAPI __attribute__((unused)) __declspec(dllexport) #else -#define CLAPI +#define CLAPI __attribute__((unused)) #endif extern "C" { -CLAPI void *init_aug (augSettings settings, int flags); +// Object Life-cycle +CLAPI void *init_aug (augSettings settings, int flags); -CLAPI bool load_file (void *aug, const char *filePath); +CLAPI bool load_file (void *aug, const char *filePath); -CLAPI void print_preview (augeas *aug,const char *matchPath); +CLAPI void print_preview (augeas *aug, const char *path); -CLAPI void print_tree (augeas *aug,const char *matchPath); +CLAPI void close_aug (void *aug); -CLAPI const char *get_tree (augeas *aug,const char *matchPath); +CLAPI void free_str (char *str); -CLAPI const char *get_node (augeas *aug, char *path); +CLAPI void print_tree (augeas *aug, const char *path); -CLAPI void close_aug (void *aug); +CLAPI char *get_tree (augeas *aug, const char *path); -CLAPI void free_str (char *str); +CLAPI const char *get_node (augeas *aug, const char *path); +CLAPI bool set_node (augeas *aug, const char *path, const char *value); + +CLAPI bool insert_node (augeas *aug, const char *path, const char *label, int before); + +CLAPI char* get_preview (augeas *aug, const char *path); + +CLAPI int rm_node (augeas *aug, const char *path); + +CLAPI bool save (augeas *aug); + +CLAPI int match(augeas *aug, const char *match_path, char*** matches); +// internal static extern int match(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath, uint* size, char*** stringArray); } diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..96ec7f8 --- /dev/null +++ b/utils.cpp @@ -0,0 +1,7 @@ +#include +#include "utils.h" + +bool path_exists(const std::string &name) { + struct stat buffer; + return (stat(name.c_str(), &buffer) == 0); +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..0c24383 --- /dev/null +++ b/utils.h @@ -0,0 +1,13 @@ +#include "string" +#include + +// Some guards +#define CHECK_RET_VOID(bx, msg) if(bx) {std::cout << msg << std::endl; return;} +#define CHECK_RET_NULLPTR(bx, msg) if(bx) {std::cout << msg << std::endl; return nullptr;} +#define CHECK_RET_BOOL(bx, msg) if(bx) {std::cout << msg << std::endl; return false;} +#define CHECK_RET_INT(bx, msg) if(bx) {std::cout << msg << std::endl; return -1;} + +#define INVALID_STATE "Augeas is in invalid state." + +bool path_exists(const std::string &name); +