reading tree example

This commit is contained in:
Wvader 2022-11-02 18:27:10 +00:00
parent b136e35b31
commit 646ba943a1
1 changed files with 64 additions and 26 deletions

View File

@ -1,15 +1,14 @@
#include <iostream>
#include <exception>
#include <augeas.h>
#include <stdio.h>
#include <vector>
#include <map>
// This file is currently for 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";
// Puth the Augeas lens path here
static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist";
@ -37,21 +36,6 @@ static void readHostsFile() {
char *etc_hosts_fn = NULL;
FILE *hosts_fp = NULL;
char *hosts_txt = NULL;
int readsz = 0;
/* Read the original contents of the etc/hosts file */
if (asprintf(&etc_hosts_fn, "%s/etc/hosts", root) >= 0) {
hosts_fp = fopen(etc_hosts_fn, "r");
if (hosts_fp) {
hosts_txt = static_cast<char *>(calloc(sizeof(char), 4096));
if (hosts_txt) {
readsz = fread(hosts_txt, sizeof(char), 4096, hosts_fp);
*(hosts_txt + readsz) = '\0';
}
fclose(hosts_fp);
}
free(etc_hosts_fn);
}
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
@ -65,31 +49,85 @@ static void readHostsFile() {
aug_close(aug);
}
static void readHostsTree() {
static void printHostsText() {
struct augeas *aug;
int r;
char *s;
char *etc_hosts_fn = NULL;
FILE *hosts_fp = NULL;
char *hosts_txt = NULL;
int readsz = 0;
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
r = aug_load_file(aug, "/etc/hosts");
r = aug_preview(aug, "/files/etc/hosts/1", &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 readHostsTree() {
struct augeas *aug;
int r;
FILE *out = tmpfile();
char *etc_hosts_fn = NULL;
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
const std::string matchPath = "/files/etc/hosts/*";
r = aug_load_file(aug, "/etc/hosts");
r = aug_print(aug, out,"/files/etc/hosts/*");
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();
readHostsFile();
readHostsTree();
return 0;
}