claugeas/main.cpp

118 lines
2.7 KiB
C++
Raw Normal View History

2022-11-03 22:30:21 +03:00
#include "iostream"
#include "augeas.h"
#include "include/AugSettings.h"
#include "map"
#include "main.h"
2022-11-03 22:30:21 +03:00
extern "C" {
2022-11-02 18:25:09 +03:00
2022-11-03 22:30:21 +03:00
RMDEF int32_t getFour() {
2022-11-02 21:27:10 +03:00
2022-11-03 22:30:21 +03:00
return 4;
2022-11-02 19:27:57 +03:00
2022-11-03 22:30:21 +03:00
}
2022-11-03 22:30:21 +03:00
// Testing interop
RMDEF int32_t getThree() {
2022-11-03 22:30:21 +03:00
return 3333;
2022-11-03 22:30:21 +03:00
}
2022-11-03 22:30:21 +03:00
// Testing interop
RMDEF void printStringExample(char* someString) {
std::cout << someString << std::endl;
}
2022-11-02 22:13:01 +03:00
2022-11-03 22:30:21 +03:00
RMDEF 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);
}
// 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;
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);
}
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);
}
}