src/config/rcxml.c: optionally write nodenames to buffer

This commit is contained in:
Johan Malm 2020-06-09 22:01:19 +01:00
parent bc51e0ad2f
commit 1f5d8c3812
4 changed files with 19 additions and 9 deletions

View file

@ -15,6 +15,6 @@ extern struct rcxml rc;
void rcxml_init(struct rcxml *rc); void rcxml_init(struct rcxml *rc);
void rcxml_parse_xml(struct buf *b); void rcxml_parse_xml(struct buf *b);
void rcxml_read(const char *filename); void rcxml_read(const char *filename);
void rcxml_set_verbose(void); void rcxml_get_nodenames(struct buf *b);
#endif /* RCXML_H */ #endif /* RCXML_H */

View file

@ -13,7 +13,8 @@
static bool in_keybind = false; static bool in_keybind = false;
static bool is_attribute = false; static bool is_attribute = false;
static bool verbose = false; static bool write_to_nodename_buffer = false;
static struct buf *nodename_buffer;
static void rstrip(char *buf, const char *pattern) static void rstrip(char *buf, const char *pattern)
{ {
@ -47,10 +48,13 @@ static void entry(xmlNode *node, char *nodename, char *content)
if (!nodename) if (!nodename)
return; return;
rstrip(nodename, ".openbox_config"); rstrip(nodename, ".openbox_config");
if (verbose) { if (write_to_nodename_buffer) {
if (is_attribute) if (is_attribute)
printf("@"); buf_add(nodename_buffer, "@");
printf("%s: %s\n", nodename, content); buf_add(nodename_buffer, nodename);
buf_add(nodename_buffer, ": ");
buf_add(nodename_buffer, content);
buf_add(nodename_buffer, "\n");
} }
if (!content) if (!content)
return; return;
@ -188,7 +192,8 @@ void rcxml_read(const char *filename)
free(b.buf); free(b.buf);
} }
void rcxml_set_verbose(void) void rcxml_get_nodenames(struct buf *b)
{ {
verbose = true; write_to_nodename_buffer = true;
nodename_buffer = b;
} }

View file

@ -26,7 +26,6 @@ int main(int argc, char *argv[])
} }
rcxml_init(&rc); rcxml_init(&rc);
rcxml_set_verbose();
rcxml_read("data/rc.xml"); rcxml_read("data/rc.xml");
/* Wayland requires XDG_RUNTIME_DIR to be set */ /* Wayland requires XDG_RUNTIME_DIR to be set */

View file

@ -3,16 +3,22 @@
#include <unistd.h> #include <unistd.h>
#include "rcxml.h" #include "rcxml.h"
#include "buf.h"
struct rcxml rc = { 0 }; struct rcxml rc = { 0 };
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct buf b;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "usage: %s <rc.xml file>\n", argv[0]); fprintf(stderr, "usage: %s <rc.xml file>\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
buf_init(&b);
rcxml_init(&rc); rcxml_init(&rc);
rcxml_set_verbose(); rcxml_get_nodenames(&b);
rcxml_read(argv[1]); rcxml_read(argv[1]);
printf("%s", b.buf);
free(b.buf);
} }