claugeas/main.cpp

77 lines
1.7 KiB
C++
Raw Normal View History

2022-11-02 18:25:09 +03:00
#include <iostream>
#include <exception>
#include <augeas.h>
2022-11-02 19:27:57 +03:00
// This file is currently for testing and training using augeas with C++
// https://github.com/hercules-team/augeas/blob/master/tests/test-api.c
2022-11-02 18:25:09 +03:00
2022-11-02 19:27:57 +03:00
static const char *abs_top_srcdir;
2022-11-02 18:25:09 +03:00
2022-11-02 19:27:57 +03:00
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");
2022-11-02 18:25:09 +03:00
2022-11-02 19:27:57 +03:00
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);
2022-11-02 18:25:09 +03:00
}
2022-11-02 19:27:57 +03:00
free(etc_hosts_fn);
2022-11-02 18:25:09 +03:00
}
2022-11-02 19:27:57 +03:00
aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD);
2022-11-02 18:25:09 +03:00
2022-11-02 19:27:57 +03:00
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);
2022-11-02 18:25:09 +03:00
}
2022-11-02 19:27:57 +03:00
int main(int argc, char *argv[]) {
testGet();
testAugPreview();
2022-11-02 18:25:09 +03:00
return 0;
}