summaryrefslogtreecommitdiff
path: root/keyword.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-01 11:22:59 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-01 11:25:02 -0300
commit6de2da7f73135fcff28d33845e3b73f293125f60 (patch)
tree9fda547f42e9f2fb68ad7a083cb671c2cb07d542 /keyword.c
parent05c0cd0db19063cd3a4043a5401c00b874ededa7 (diff)
git, keyword: Move `parse_key_value_file()` to `keyword.{c,h}`
Diffstat (limited to 'keyword.c')
-rw-r--r--keyword.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/keyword.c b/keyword.c
index d7bd550..8127440 100644
--- a/keyword.c
+++ b/keyword.c
@@ -2,8 +2,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <syslog.h>
#include <time.h>
#include "keyword.h"
+#include "macros.h"
#include "misc.h"
void *get_keyword_offset_ptr(const keyword *key, void *ptr) {
@@ -87,3 +89,52 @@ int parse_keywords(const keyword **keys, char *key, char *value, void *ret, void
}
return error;
}
+
+int parse_key_value_file(void *ret, void *ctx, const keyword **keywords, char *buf, const char *delm, parse_callback *parse_cb) {
+ if (buf != NULL) {
+ int ret_val = 0;
+ for (;;) {
+ char *lhs, *rhs;
+ /* Find the keyword before the delimiter(s). */
+ lhs = strtok_r(skip_whitespace(buf), delm, &buf);
+ /* Remove any whitespace ahead of us. */
+ lhs = strtok_r(lhs, " \t\v\r\n", &rhs);
+
+ /* Did we hit EOF? */
+ if (is_empty(lhs) || is_empty(buf)) {
+ break;
+ } else {
+ int error;
+ char *tmp = skip_whitespace(buf);
+
+ /* Does the right hand side start with a double, or single quote? */
+ if (*tmp == '\"' || *tmp == '\'') {
+ const char *delm = (*delm == '\"') ? "\"" : "\'";
+ /* Get the string in between the start, and end qoute. */
+ rhs = get_str_delm_range(tmp, delm, delm, &buf);
+ } else {
+ /* Get the rest of the line. */
+ rhs = strtok_r(tmp, "\n", &buf);
+ }
+
+ /* Did we fail to parse the keyword? */
+ if (error = parse_keywords(keywords, lhs, rhs, ret, ctx)) {
+ log(LOG_WARNING, "Failed to parse keyword \"%s\". Error code: %i", lhs, error);
+ ret_val = error;
+ }
+ }
+
+ /* Do we have a parse callback? */
+ if (parse_cb != NULL) {
+ /* Did the callback return an error. */
+ if (parse_cb(&ret, ctx, buf) < 0) {
+ ret_val = 7;
+ break;
+ }
+ }
+ }
+ return ret_val;
+ } else {
+ return 8;
+ }
+}