claugeas/main.cpp

77 lines
1.7 KiB
C++

#include <iostream>
#include <exception>
#include <augeas.h>
// 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";
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", &s);
std::cout << "The value read from node: " << s << std::endl;
aug_close(aug);
}
static void testAugPreview() {
struct augeas *aug;
int r;
char *s;
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);
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);
}
int main(int argc, char *argv[]) {
testGet();
testAugPreview();
return 0;
}