Refactoring and introducing a ManagedType to be called from c#
This commit is contained in:
parent
7ed1c3e8bb
commit
cd1f8a7fb9
|
@ -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 <std::string, std::string> stdBindList;
|
||||
std::map <std::string , std::string>::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<std::string,std::string>(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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
111
main.cpp
111
main.cpp
|
@ -3,116 +3,29 @@
|
|||
#include <augeas.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#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 <std::string, std::string> stdBindList;
|
||||
std::map <std::string , std::string>::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<std::string,std::string>(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;
|
||||
}
|
Loading…
Reference in New Issue