]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/config/rcxml.c: parse xml from buffer
authorJohan Malm <jgm323@gmail.com>
Tue, 9 Jun 2020 20:40:46 +0000 (21:40 +0100)
committerJohan Malm <jgm323@gmail.com>
Tue, 9 Jun 2020 20:40:46 +0000 (21:40 +0100)
Avoid unit tests writing to/from files by using xmlParseMemory() instead
of xmlReadFile().

data/rc.xml
include/buf.h [new file with mode: 0644]
include/rcxml.h
src/common/buf.c [new file with mode: 0644]
src/common/meson.build [new file with mode: 0644]
src/config/rcxml.c
src/meson.build
tests/meson.build
tests/t1000-rcxml-simple-parse.c
tools/rcxml/Makefile

index 49975ca2b12b03bcc1c4e6774c887436e0db69d4..da9244bfe24d4283c9ba4da8ba2acb7f82ae614f 100644 (file)
@@ -1,8 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
 <!-- Use <openbox_config> as root element for openbox compatibility -->
-<openbox_config xmlns="http://openbox.org/3.4/rc"
-               xmlns:xi="http://www.w3.org/2001/XInclude">
+<openbox_config>
 
 <!-- labwc specific settings - additional to openbox -->
 <lab>
diff --git a/include/buf.h b/include/buf.h
new file mode 100644 (file)
index 0000000..c718488
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Very simple C buffer implementation
+ *
+ * Copyright Johan Malm 2020
+ */
+
+#ifndef BUF_H
+#define BUF_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct buf {
+       char *buf;
+       int alloc;
+       int len;
+};
+
+void buf_init(struct buf *s);
+void buf_add(struct buf *s, const char *data);
+
+#endif /* BUF_H */
index 4e0c643e18d15d2c49d0fdd40804cbd52328278b..5370a7968a61e5f2e4d469c3267aa577a050b152 100644 (file)
@@ -4,6 +4,8 @@
 #include <stdio.h>
 #include <stdbool.h>
 
+#include "buf.h"
+
 struct rcxml {
        bool client_side_decorations;
 };
@@ -11,6 +13,7 @@ struct rcxml {
 extern struct rcxml rc;
 
 void rcxml_init(struct rcxml *rc);
+void rcxml_parse_xml(struct buf *b);
 void rcxml_read(const char *filename);
 void rcxml_set_verbose(void);
 
diff --git a/src/common/buf.c b/src/common/buf.c
new file mode 100644 (file)
index 0000000..87c0d30
--- /dev/null
@@ -0,0 +1,23 @@
+#include "buf.h"
+
+void buf_init(struct buf *s)
+{
+       s->alloc = 256;
+       s->buf = malloc(s->alloc);
+       s->buf[0] = '\0';
+       s->len = 0;
+}
+
+void buf_add(struct buf *s, const char *data)
+{
+       if (!data || data[0] == '\0')
+               return;
+       int len = strlen(data);
+       if (s->alloc <= s->len + len + 1) {
+               s->alloc = s->alloc + len;
+               s->buf = realloc(s->buf, s->alloc);
+       }
+       memcpy(s->buf + s->len, data, len);
+       s->len += len;
+       s->buf[s->len] = 0;
+}
diff --git a/src/common/meson.build b/src/common/meson.build
new file mode 100644 (file)
index 0000000..d14bba6
--- /dev/null
@@ -0,0 +1,3 @@
+labwc_sources += files(
+  'buf.c',
+)
index fc7b677e36df8377459966f6bce0583289d9631e..7caab18cddb3f68e901462fe2fe03aa6ac461f08 100644 (file)
@@ -1,4 +1,4 @@
-#define _POSIX_C_SOURCE 200112L
+#define _POSIX_C_SOURCE 200809L
 #include <stdio.h>
 #include <string.h>
 #include <strings.h>
@@ -143,11 +143,12 @@ static void xml_tree_walk(xmlNode *node)
        }
 }
 
-static void parse_xml(const char *filename)
+/* Exposed in header file to allow unit tests to parse buffers */
+void rcxml_parse_xml(struct buf *b)
 {
-       xmlDoc *d = xmlReadFile(filename, NULL, 0);
+       xmlDoc *d = xmlParseMemory(b->buf, b->len);
        if (!d) {
-               fprintf(stderr, "fatal: error reading file '%s'\n", filename);
+               fprintf(stderr, "fatal: xmlParseMemory()\n");
                exit(EXIT_FAILURE);
        }
        xml_tree_walk(xmlDocGetRootElement(d));
@@ -162,7 +163,29 @@ void rcxml_init(struct rcxml *rc)
 
 void rcxml_read(const char *filename)
 {
-       parse_xml(filename);
+       FILE *stream;
+       char *line = NULL;
+       size_t len = 0;
+       ssize_t n_read;
+       struct buf b;
+
+       /* Read <filename> into buffer and then call rcxml_parse_xml() */
+       stream = fopen(filename, "r");
+       if (!stream) {
+               fprintf(stderr, "warn: cannot read '%s'\n", filename);
+               return;
+       }
+       buf_init(&b);
+       while ((n_read = getline(&line, &len, stream) != -1)) {
+               char *p = strrchr(line, '\n');
+               if (p)
+                       *p = '\0';
+               buf_add(&b, line);
+       }
+       free(line);
+       fclose(stream);
+       rcxml_parse_xml(&b);
+       free(b.buf);
 }
 
 void rcxml_set_verbose(void)
index 478f34ff532c243cdcb6d2e5652ba59259822e15..e76b4fb4b305755160c64cea27597511de90e5ca 100644 (file)
@@ -11,5 +11,6 @@ labwc_sources = files(
   'xwl.c',
 )
 
+subdir('common')
 subdir('config')
 subdir('debug')
index aa0c5a36a63c3c55bf2ebd20f3a7fbaeae802af5..3d4383752fbc148edb75dc232a23a4c40a5d51c0 100644 (file)
@@ -1,6 +1,6 @@
 rcxml_lib = static_library(
   'rcxml',
-  sources: ['../src/config/rcxml.c'],
+  sources: files('../src/config/rcxml.c', '../src/common/buf.c'),
   dependencies: xml2,
   include_directories: [labwc_inc],
   link_with: library('libxml-2.0'),
index d3f98c0fad5639ebd10d996069516ffd42b81ff8..c57cb74cec1bfa545dfe673010055136b911cfa1 100644 (file)
@@ -33,4 +33,6 @@ int main(int argc, char **argv)
 
        diag("Simple parse rc.xml");
        ok1(rc.client_side_decorations);
+
+       return exit_status();
 }
index 40940aa2a74a6a5c12fc43c25eec9ff2326eedd9..d8a9da5fae9d22f45b8800ea98fedf19aebc53e0 100644 (file)
@@ -11,7 +11,7 @@ PROGS = rcxml-print-nodenames
 all: $(PROGS)
 
 rcxml-print-nodenames: rcxml-print-nodenames.c
-       $(CC) $(CFLAGS) -o $@ $^ ../../src/config/rcxml.c $(LDFLAGS)
+       $(CC) $(CFLAGS) -o $@ $^ ../../src/config/rcxml.c ../../src/common/buf.c $(LDFLAGS)
 
 clean:
        rm -f $(PROGS)