2022-11-03 22:30:21 +03:00
|
|
|
#include "main.h"
|
2022-11-05 05:08:50 +03:00
|
|
|
#include <sstream>
|
2022-11-02 22:17:51 +03:00
|
|
|
|
2022-11-06 20:21:55 +03:00
|
|
|
#ifdef __linux__
|
2022-11-12 01:09:13 +03:00
|
|
|
|
2022-11-06 20:21:55 +03:00
|
|
|
#include <cstring>
|
2022-11-12 01:09:13 +03:00
|
|
|
|
2022-11-06 20:21:55 +03:00
|
|
|
#endif
|
2022-11-02 18:25:09 +03:00
|
|
|
|
2022-11-02 22:13:01 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
template<class T, size_t N>
|
|
|
|
constexpr size_t size(T (&)[N]) { return N; }
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
|
|
CLAPI void*
|
|
|
|
init_aug(augSettings settings, int flags) {
|
|
|
|
CHECK_RET_NULLPTR(!path_exists(std::string(settings.root)), "Root path is invalid.");
|
2022-11-05 00:10:00 +03:00
|
|
|
return aug_init(settings.root, settings.loadPath, flags);
|
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI void
|
|
|
|
close_aug(void *aug) {
|
|
|
|
CHECK_RET_VOID(!aug, "Failed to close augeas.");
|
2022-11-05 00:38:12 +03:00
|
|
|
aug_close((augeas *) aug);
|
2022-11-03 22:30:21 +03:00
|
|
|
}
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI bool
|
|
|
|
load_file(void *aug, const char *filePath) {
|
|
|
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
|
|
|
int r = aug_load_file((augeas *) aug, filePath);
|
2022-11-05 00:38:12 +03:00
|
|
|
return r == 0;
|
|
|
|
}
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI void
|
|
|
|
print_preview(augeas *aug, const char *path) {
|
|
|
|
CHECK_RET_VOID(!aug, "Augeas is in invalid state");
|
|
|
|
int r;
|
|
|
|
char *s;
|
|
|
|
r = aug_preview(aug, path, &s);
|
|
|
|
CHECK_RET_VOID(r != 0, "Failed to preview.")
|
|
|
|
std::cout << s << std::endl;
|
|
|
|
free(s);
|
|
|
|
}
|
2022-11-05 00:38:12 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI int rm_node(augeas *aug, const char *path) {
|
|
|
|
CHECK_RET_INT(!aug, INVALID_STATE);
|
|
|
|
return aug_rm(aug, path);
|
|
|
|
}
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-05 00:38:12 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
|
|
|
|
CLAPI char*
|
|
|
|
get_preview(augeas *aug, const char *path) {
|
|
|
|
|
|
|
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
2022-11-03 22:30:21 +03:00
|
|
|
int r;
|
|
|
|
char *s;
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
r = aug_preview(aug, path, &s);
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
if (r != 0) {
|
2022-11-05 00:10:00 +03:00
|
|
|
std::cout << "Failure previewing." << std::endl;
|
2022-11-12 01:09:13 +03:00
|
|
|
return nullptr;
|
2022-11-05 00:10:00 +03:00
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
// must be freed
|
|
|
|
return s;
|
2022-11-03 22:30:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
// Print tree of the path
|
2022-11-05 00:38:12 +03:00
|
|
|
CLAPI void print_tree(
|
2022-11-05 00:10:00 +03:00
|
|
|
augeas *aug,
|
2022-11-12 01:09:13 +03:00
|
|
|
const char *path
|
2022-11-03 22:30:21 +03:00
|
|
|
) {
|
2022-11-05 00:10:00 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CHECK_RET_VOID(!aug, INVALID_STATE);
|
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
int r;
|
2022-11-12 01:09:13 +03:00
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
FILE *out = tmpfile();
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
r = aug_print(aug, out, path);
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
if (r != 0) {
|
2022-11-05 00:10:00 +03:00
|
|
|
std::cout << "error: aug_print failure." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
std::map<std::string, std::string> stdBindList;
|
|
|
|
std::map<std::string, std::string>::iterator pos;
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
char line[256];
|
|
|
|
rewind(out);
|
|
|
|
while (fgets(line, 256, out) != nullptr) {
|
|
|
|
// remove end of line
|
|
|
|
line[strlen(line) - 1] = '\0';
|
2022-11-12 01:09:13 +03:00
|
|
|
std::string str_path = path;
|
2022-11-03 22:30:21 +03:00
|
|
|
std::string s = line;
|
|
|
|
// skip comments
|
|
|
|
if (s.find("#comment") != std::string::npos)
|
|
|
|
continue;
|
2022-11-12 01:09:13 +03:00
|
|
|
s = s.substr(str_path.length() - 1);
|
2022-11-03 22:30:21 +03:00
|
|
|
// 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);
|
2022-11-12 01:09:13 +03:00
|
|
|
stdBindList.insert(std::pair<std::string, std::string>(key, value)); // 2
|
2022-11-03 22:30:21 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
for (pos = stdBindList.begin(); pos != stdBindList.end(); pos++) {
|
2022-11-03 22:30:21 +03:00
|
|
|
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-11-05 00:10:00 +03:00
|
|
|
}
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI char *get_tree(
|
2022-11-05 05:08:50 +03:00
|
|
|
augeas *aug,
|
2022-11-12 01:09:13 +03:00
|
|
|
const char *path
|
2022-11-05 05:08:50 +03:00
|
|
|
) {
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
2022-11-05 05:08:50 +03:00
|
|
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
FILE *out = tmpfile();
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
r = aug_print(aug, out, path);
|
2022-11-05 05:08:50 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
if (r != 0) {
|
2022-11-05 05:08:50 +03:00
|
|
|
std::cout << "error: aug_print failure." << std::endl;
|
2022-11-12 01:09:13 +03:00
|
|
|
return nullptr;
|
2022-11-05 05:08:50 +03:00
|
|
|
}
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
std::map<std::string, std::string>::iterator pos;
|
2022-11-05 05:08:50 +03:00
|
|
|
|
2022-11-05 21:43:43 +03:00
|
|
|
std::stringstream ss;
|
2022-11-05 05:08:50 +03:00
|
|
|
|
|
|
|
char line[256];
|
|
|
|
rewind(out);
|
|
|
|
while (fgets(line, 256, out) != nullptr) {
|
|
|
|
// remove end of line
|
|
|
|
line[strlen(line) - 1] = '\0';
|
2022-11-12 01:09:13 +03:00
|
|
|
std::string str_path = path;
|
2022-11-05 05:08:50 +03:00
|
|
|
std::string s = line;
|
|
|
|
// skip comments
|
|
|
|
if (s.find("#comment") != std::string::npos)
|
|
|
|
continue;
|
2022-11-12 01:09:13 +03:00
|
|
|
s = s.substr(str_path.length() - 1);
|
2022-11-05 05:08:50 +03:00
|
|
|
// 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);
|
|
|
|
|
2022-11-05 21:43:43 +03:00
|
|
|
ss << ";K;" << key << ";VAL;" << value << ";ENDL;";
|
2022-11-05 05:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(out);
|
2022-11-05 21:43:43 +03:00
|
|
|
return strdup(ss.str().c_str());
|
2022-11-05 05:08:50 +03:00
|
|
|
}
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI void free_str(char *str) {
|
|
|
|
if (str != nullptr) {
|
2022-11-05 05:08:50 +03:00
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 22:30:21 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI bool save(augeas *aug) {
|
|
|
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
|
|
|
return aug_save(aug) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLAPI void get_arr( unsigned long &size, char** result)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CLAPI int match(augeas *aug, const char *match_path, char*** matches) {
|
|
|
|
CHECK_RET_INT(!aug, INVALID_STATE);
|
|
|
|
return aug_match(aug, match_path, matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CLAPI const char *get_node(augeas *aug, const char *path) {
|
2022-11-05 00:38:12 +03:00
|
|
|
|
|
|
|
const char *value;
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CHECK_RET_NULLPTR(!aug, INVALID_STATE);
|
2022-11-05 00:38:12 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
aug_get(aug, path, &value);
|
|
|
|
return value;
|
|
|
|
}
|
2022-11-05 00:38:12 +03:00
|
|
|
|
2022-11-12 01:09:13 +03:00
|
|
|
CLAPI bool set_node(augeas *aug, const char *path, const char *value) {
|
|
|
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
|
|
|
return aug_set(aug, path, value) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLAPI bool insert_node(augeas *aug, const char *path, const char *label, int before) {
|
|
|
|
CHECK_RET_BOOL(!aug, INVALID_STATE);
|
|
|
|
return aug_insert(aug, path, label, before) == 0;
|
2022-11-05 00:38:12 +03:00
|
|
|
}
|
|
|
|
|
2022-11-03 22:30:21 +03:00
|
|
|
}
|