#include "main.h" #include #include extern "C" { inline bool path_exists (const std::string& name) { struct stat buffer; return (stat (name.c_str(), &buffer) == 0); } CLAPI void* init_aug(augSettings settings, int flags) { if(!path_exists(std::string(settings.root))) { std::cout << "error: root path is invalid." << settings.root << std::endl; return nullptr; } return aug_init(settings.root, settings.loadPath, flags); } CLAPI void close_aug(void* aug) { aug_close((augeas *) aug); } bool load_file(void* aug, const char* filePath) { int r = aug_load_file((augeas *) aug,filePath); return r == 0; } CLAPI void print_preview(augeas* aug, const char* matchPath) { if(aug == nullptr) { std::cout << "error: augeas is not initialized." << std::endl; return; } int r; char *s; r = aug_preview(aug,matchPath,&s); if(r != 0) { std::cout << "Failure previewing." << std::endl; return; } std::cout << s << std::endl; free(s); } // Print tree of the matchPath CLAPI void print_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 stdBindList; std::map ::iterator pos; 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); stdBindList.insert(std::pair(key,value)); // 2 } fclose(out); for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) { std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; } } 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 stdBindList; std::map ::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(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 ) { const char *value; /* * Return 1 if there is exactly one node matching PATH, * 0 if there is none, and a negative value * if there is more than one node matching PATH, * or if PATH is not a legal path expression. * * The string *value must not be freed by the caller, * and is valid as long as its node remains unchanged. */ int rc = aug_get(aug, path, &value); if (1 == rc) { if (nullptr != value) { return value; } else { return ""; } } else { return ""; } } }