Interop working

This commit is contained in:
Wvader 2022-11-03 19:30:21 +00:00
parent 02d5784f5b
commit b64a17d77f
8 changed files with 147 additions and 164 deletions

View File

@ -1,105 +0,0 @@
#include "AugSettings.h"
#include "augeas.h"
#include "map"
#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

View File

@ -1,30 +0,0 @@
#include "AugFuncs.h"
#include <string>
#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

View File

@ -1,8 +0,0 @@
#ifndef BLUEWEST_AUGEAS_AUGSETTINGS_H
#define BLUEWEST_AUGEAS_AUGSETTINGS_H
struct AugSettings {
const char *root;
const char *loadPath;
};
#endif //BLUEWEST_AUGEAS_AUGSETTINGS_H

View File

@ -1,12 +1,22 @@
cmake_minimum_required(VERSION 3.21)
project(BlueWest.Augeas)
project(BlueAug VERSION 1.0.1 DESCRIPTION "BlueAug description")
include_directories(/opt/homebrew/opt/augeas/include/) # Headers para auto-complete?
link_directories(/opt/homebrew/opt/augeas/lib)
# Os binarios com as libraries, como é mac .dylib, linux: .SO, etc
# tive que usar HOMEBREW_NO_INSTALL_CLEANUP=1 "brew install augeas --build-from-source"
# para ele armazenar o codigo compilado.
set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-fdeclspec")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
add_library(BlueAug SHARED include/AugSettings.cpp main.cpp main.h)
target_link_libraries(BlueAug augeas) # Se não ele não sabe que é para compilar para ARM64
#add_executable(BlueAug main.cpp AugFuncs.h AugManaged.h AugSettings.h)
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

0
include/AugSettings.cpp Normal file
View File

4
include/AugSettings.h Normal file
View File

@ -0,0 +1,4 @@
struct AugSettings {
const char *root;
const char *loadPath;
};

126
main.cpp
View File

@ -1,27 +1,117 @@
#include <iostream>
#include "AugSettings.h"
#include "AugFuncs.h"
#include "iostream"
#include "augeas.h"
#include "include/AugSettings.h"
#include "map"
#include "main.h"
// Testing and training using augeas with C++
// https://github.com/hercules-team/augeas/blob/master/tests/test-api.c
extern "C" {
RMDEF int32_t getFour() {
return 4;
}
// Testing interop
RMDEF int32_t getThree() {
return 3333;
}
// Testing interop
RMDEF void printStringExample(char* someString) {
std::cout << someString << std::endl;
}
int main(int argc, char *argv[]) {
RMDEF void testSource(const AugSettings& settings) {
int r;
struct augeas *aug;
char *s;
//testGet();
aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD);
auto testSettings = AugSettings {
.root = "../root",
.loadPath = "/opt/homebrew/share/augeas/lenses/dist",
};
r = aug_load_file(aug, "/etc/hosts");
r = aug_source(aug, "/files/etc/hosts/1/ipaddr", nullptr);
auto instance = AugFuncs();
std::cout << "The value read from node: " << s << std::endl;
instance.printPreview(testSettings, "/files/etc/hosts/1", "/etc/hosts");
//printPreview("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config");
aug_close(aug);
}
// This prints the actual file
RMDEF 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;
instance.printAugTree(testSettings, "/files/etc/hosts/*", "/etc/hosts");
instance.printAugTree(testSettings, "/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config");
aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD);
return 0;
}
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);
}
RMDEF 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);
}
}

22
main.h Normal file
View File

@ -0,0 +1,22 @@
#if defined(_WIN32)
#define RMDEF __declspec(dllexport) extern "C" inline
#else
#define RMDEF
#endif
extern "C" {
RMDEF int getThree ();
RMDEF int getFour ();
RMDEF void testSource (const AugSettings& settings);
RMDEF void printPreview (const AugSettings& settings,
const std::string& matchPath,
const std::string& filePath);
RMDEF void printStringExample (char* someString);
RMDEF void printAugTree (const AugSettings& settings,
const std::string& matchPath,
const std::string& filePath);
}