From cd1f8a7fb9b740a6e6071229670fdcda6a2ebb52 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:03:08 +0000 Subject: [PATCH] Refactoring and introducing a ManagedType to be called from c# --- AugFuncs.h | 104 +++++++++++++++++++++++++++++++++++++++++++++ AugManaged.h | 29 +++++++++++++ AugSettings.h | 8 ++++ CMakeLists.txt | 2 +- main.cpp | 111 ++++++------------------------------------------- 5 files changed, 154 insertions(+), 100 deletions(-) create mode 100644 AugFuncs.h create mode 100644 AugManaged.h create mode 100644 AugSettings.h diff --git a/AugFuncs.h b/AugFuncs.h new file mode 100644 index 0000000..b923732 --- /dev/null +++ b/AugFuncs.h @@ -0,0 +1,104 @@ +#include "AugSettings.h" +#include "augeas.h" + +#ifndef BLUEWEST_AUGEAS_BLUEAUG_H +#define BLUEWEST_AUGEAS_BLUEAUG_H + +class AugFuncs { + + static void testSource(const AugSettings& settings) { + int r; + struct augeas *aug; + char *s; + + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + + r = aug_load_file(aug, "/etc/hosts"); + r = aug_source(aug, "/files/etc/hosts/1/ipaddr", nullptr); + + std::cout << "The value read from node: " << s << std::endl; + + aug_close(aug); + } + + +/// Get the tree of the /etc/hosts file printed in the console +/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784 + +public: +// This prints the actual file + void printPreview(const AugSettings& settings, const std::string& matchPath, const std::string& filePath) { + struct augeas *aug; + int r; + char *s; + char *etc_hosts_fn = nullptr; + FILE *hosts_fp = nullptr; + char *hosts_txt = nullptr; + + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + + r = aug_load_file(aug, filePath.c_str()); + r = aug_preview(aug, matchPath.c_str(), &s); + + std::cout << s << std::endl; + + free(hosts_txt); + free(s); + aug_close(aug); + } + + void printAugTree( + const AugSettings& settings, + const std::string& matchPath, + const std::string& filePath + ) { + struct augeas *aug; + int r; + FILE *out = tmpfile(); + + aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + + r = aug_load_file(aug, filePath.c_str()); + r = aug_print(aug, out,matchPath.c_str()); + + 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 s = line; + + // skip comments + if (s.find("#comment") != std::string::npos) + continue; + s = s.substr(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); + 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; + } + + aug_close(aug); + } +}; + + +#endif //BLUEWEST_AUGEAS_BLUEAUG_H diff --git a/AugManaged.h b/AugManaged.h new file mode 100644 index 0000000..2735c93 --- /dev/null +++ b/AugManaged.h @@ -0,0 +1,29 @@ +#include "AugFuncs.h" + +#ifndef BLUEWEST_AUGEAS_MANAGEDAUG_H +#define BLUEWEST_AUGEAS_MANAGEDAUG_H + + + // Managed type to expose the public library to other languages + + ref class AugManaged +{ + AugFuncs* NativePtr; + +public: + AugManaged() : NativePtr(new AugFuncs()) {} + ~AugManaged() { delete NativePtr; } + + void TestAugTree() + { + auto testSettings = AugSettings { + .root = "../root", + .loadPath = "/opt/homebrew/share/augeas/lenses/dist", + }; + + NativePtr->printPreview(testSettings, "/files/etc/hosts/1", "/etc/hosts"); + } +}; + + +#endif //BLUEWEST_AUGEAS_MANAGEDAUG_H diff --git a/AugSettings.h b/AugSettings.h new file mode 100644 index 0000000..00c031f --- /dev/null +++ b/AugSettings.h @@ -0,0 +1,8 @@ +#ifndef BLUEWEST_AUGEAS_AUGSETTINGS_H +#define BLUEWEST_AUGEAS_AUGSETTINGS_H + +struct AugSettings { + const char *root; + const char *loadPath; +}; +#endif //BLUEWEST_AUGEAS_AUGSETTINGS_H diff --git a/CMakeLists.txt b/CMakeLists.txt index ba2a42d..c6969f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,5 +8,5 @@ link_directories(/opt/homebrew/opt/augeas/lib) set(CMAKE_CXX_STANDARD 14) -add_executable(BlueWest.Augeas main.cpp) +add_executable(BlueWest.Augeas main.cpp AugFuncs.h AugManaged.h AugSettings.h) target_link_libraries(BlueWest.Augeas augeas) # Se não ele não sabe que é para compilar para ARM64 diff --git a/main.cpp b/main.cpp index 8efb66a..7cfcd37 100644 --- a/main.cpp +++ b/main.cpp @@ -3,116 +3,29 @@ #include #include #include - +#include "AugSettings.h" +#include "AugFuncs.h" // Testing and training using augeas with C++ // https://github.com/hercules-team/augeas/blob/master/tests/test-api.c -static const char *abs_top_srcdir; -static const char *root = "../root"; -static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist"; - - -static void testGet() { - int r; - const char *value; - const char *label; - struct augeas *aug; - char *s; - - aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, "/etc/hosts"); - r = aug_source(aug, "/files/etc/hosts/1/ipaddr", NULL); - - std::cout << "The value read from node: " << s << std::endl; - - aug_close(aug); -} - -// This prints the actual file -static void printPreview(const std::string& matchPath, const std::string& filePath) { - struct augeas *aug; - int r; - char *s; - char *etc_hosts_fn = NULL; - FILE *hosts_fp = NULL; - char *hosts_txt = NULL; - - aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, filePath.c_str()); - r = aug_preview(aug, matchPath.c_str(), &s); - - std::cout << s << std::endl; - - free(hosts_txt); - free(s); - aug_close(aug); -} - - -/// Get the tree of the /etc/hosts file printed in the console -/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784 - -static void printAugTree(const std::string& matchPath, const std::string& filePath) { - struct augeas *aug; - int r; - FILE *out = tmpfile(); - - aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); - - r = aug_load_file(aug, filePath.c_str()); - r = aug_print(aug, out,matchPath.c_str()); - - std::map stdBindList; - std::map ::iterator pos; - - char line[256]; - rewind(out); - while (fgets(line, 256, out) != NULL) { - // remove end of line - line[strlen(line) - 1] = '\0'; - std::string s = line; - ; - // skip comments - if (s.find("#comment") != std::string::npos) - continue; - s = s.substr(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); - 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; - } - - aug_close(aug); -} - int main(int argc, char *argv[]) { //testGet(); - printPreview("/files/etc/hosts/1", "/etc/hosts"); + auto testSettings = AugSettings { + .root = "../root", + .loadPath = "/opt/homebrew/share/augeas/lenses/dist", + }; + + auto instance = AugFuncs(); + + instance.printPreview(testSettings, "/files/etc/hosts/1", "/etc/hosts"); //printPreview("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config"); - printAugTree("/files/etc/hosts/*", "/etc/hosts"); - printAugTree("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config"); + instance.printAugTree(testSettings, "/files/etc/hosts/*", "/etc/hosts"); + instance.printAugTree(testSettings, "/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config"); return 0; } \ No newline at end of file