mirror of
https://github.com/labwc/labwc.git
synced 2026-02-15 22:05:25 -05:00
src/config/rcxml.c: parse xml from buffer
Avoid unit tests writing to/from files by using xmlParseMemory() instead of xmlReadFile().
This commit is contained in:
parent
40c0b169ef
commit
bc51e0ad2f
10 changed files with 86 additions and 9 deletions
23
src/common/buf.c
Normal file
23
src/common/buf.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
3
src/common/meson.build
Normal file
3
src/common/meson.build
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
labwc_sources += files(
|
||||
'buf.c',
|
||||
)
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -11,5 +11,6 @@ labwc_sources = files(
|
|||
'xwl.c',
|
||||
)
|
||||
|
||||
subdir('common')
|
||||
subdir('config')
|
||||
subdir('debug')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue