expose more functions

This commit is contained in:
Wvader 2022-11-05 02:08:50 +00:00
parent d2cdf81418
commit 401226f094
3 changed files with 88 additions and 15 deletions

View File

@ -11,7 +11,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
add_library(clAugeas SHARED
include/augSettings.cpp
include/augSettings.h
main.cpp main.h)
main.cpp main.h include/pair.cpp)
target_link_libraries(clAugeas augeas)

View File

@ -1,5 +1,6 @@
#include "main.h"
#include <sys/stat.h>
#include <sstream>
extern "C" {
@ -28,7 +29,7 @@ bool load_file(void* aug, const char* filePath) {
}
CLAPI void print_preview(augeas* aug, const char* matchPath, const char* filePath) {
CLAPI void print_preview(augeas* aug, const char* matchPath) {
if(aug == nullptr) {
std::cout << "error: augeas is not initialized." << std::endl;
@ -54,8 +55,7 @@ CLAPI void print_preview(augeas* aug, const char* matchPath, const char* filePat
// Print tree of the matchPath
CLAPI void print_tree(
augeas *aug,
const char* matchPath,
const char* filePath
const char* matchPath
) {
if(aug == nullptr) {
@ -110,6 +110,79 @@ CLAPI void print_tree(
}
}
CLAPI const char* get_tree(
augeas *aug,
const char* matchPath
) {
if(aug == nullptr) {
std::cout << "error: augeas is not initialized." << std::endl;
return "";
}
int r;
FILE *out = tmpfile();
r = aug_print(aug, out,matchPath);
if(r != 0) {
std::cout << "error: aug_print failure." << std::endl;
return "";
}
std::map <std::string, std::string> stdBindList;
std::map <std::string ,std::string>::iterator pos;
std::stringstream rstream;
char line[256];
rewind(out);
while (fgets(line, 256, out) != nullptr) {
// remove end of line
line[strlen(line) - 1] = '\0';
std::string str_matchPath = matchPath;
std::string s = line;
// skip comments
if (s.find("#comment") != std::string::npos)
continue;
s = s.substr(str_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);
rstream << ";K;" << key << ";VAL;" << value << ";ENDL;";
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;
}
return strdup(rstream.str().c_str());
}
CLAPI void free_str(char* str) {
if(str != nullptr) {
free(str);
}
}
CLAPI const char* get_node(augeas* aug, char* path ) {
@ -130,11 +203,11 @@ CLAPI const char* get_node(augeas* aug, char* path ) {
if (nullptr != value) {
return value;
} else {
return "NIL";
return "";
}
} else {
return "NIL";
return "";
}
}

15
main.h
View File

@ -15,19 +15,18 @@ extern "C" {
CLAPI void *init_aug (augSettings settings, int flags);
CLAPI void print_preview (augeas* aug,
const char* matchPath,
const char* filePath);
CLAPI bool load_file (void *aug, const char *filePath);
CLAPI void print_tree (augeas* aug,
const char* matchPath,
const char* filePath);
CLAPI void print_preview (augeas *aug,const char *matchPath);
CLAPI void print_tree (augeas *aug,const char *matchPath);
CLAPI const char *get_tree (augeas *aug,const char *matchPath);
CLAPI const char *get_node (augeas *aug, char *path);
CLAPI void close_aug (void *aug);
CLAPI void free_str (char *str);
}