diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-01-26 13:00:45 -0800 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-01-26 13:00:45 -0800 |
commit | 4d10bc017ad683ffa3e7e399b9bcb9dcf524d964 (patch) | |
tree | 6a574035a59ded9981471249dccb497cb999adf4 /parse-xml.c | |
parent | 6e0ea1ce618d9d28d9512e2afe587bde9ae76fda (diff) | |
download | subsurface-4d10bc017ad683ffa3e7e399b9bcb9dcf524d964.tar.gz |
Split up file reading from 'parse-xml.c' into 'file.c'
We're going to eventually import non-xml files too, so let's begin
splitting the logic up.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'parse-xml.c')
-rw-r--r-- | parse-xml.c | 71 |
1 files changed, 5 insertions, 66 deletions
diff --git a/parse-xml.c b/parse-xml.c index cc2d0076d..2877e3a99 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -4,8 +4,6 @@ #include <stdlib.h> #include <errno.h> #include <unistd.h> -#include <fcntl.h> -#include <sys/stat.h> #define __USE_XOPEN #include <time.h> #include <libxml/parser.h> @@ -1304,84 +1302,25 @@ static void reset_all(void) import_source = UNKNOWN; } -struct memblock { - void *buffer; - size_t size; -}; - -static int readfile(const char *filename, struct memblock *mem) -{ - int ret, fd = open(filename, O_RDONLY); - struct stat st; - - mem->buffer = NULL; - mem->size = 0; - - fd = open(filename, O_RDONLY); - if (fd < 0) - return fd; - ret = fstat(fd, &st); - if (ret < 0) - goto out; - ret = -EINVAL; - if (!S_ISREG(st.st_mode)) - goto out; - ret = 0; - if (!st.st_size) - goto out; - mem->buffer = malloc(st.st_size); - ret = -1; - errno = ENOMEM; - if (!mem->buffer) - goto out; - mem->size = st.st_size; - ret = read(fd, mem->buffer, mem->size); - if (ret < 0) - goto free; - if (ret == mem->size) - goto out; - errno = EIO; - ret = -1; -free: - free(mem->buffer); - mem->buffer = NULL; - mem->size = 0; -out: - close(fd); - return ret; -} - -void parse_xml_file(const char *filename, GError **error) +void parse_xml_buffer(const char *url, const char *buffer, int size, GError **error) { xmlDoc *doc; - struct memblock mem; - - if (readfile(filename, &mem) < 0) { - fprintf(stderr, "Failed to read '%s'.\n", filename); - if (error) { - *error = g_error_new(g_quark_from_string("subsurface"), - DIVE_ERROR_PARSE, - "Failed to read '%s'", - filename); - } - return; - } - doc = xmlReadMemory(mem.buffer, mem.size, filename, NULL, 0); + doc = xmlReadMemory(buffer, size, url, NULL, 0); if (!doc) { - fprintf(stderr, "Failed to parse '%s'.\n", filename); + fprintf(stderr, "Failed to parse '%s'.\n", url); if (error != NULL) { *error = g_error_new(g_quark_from_string("subsurface"), DIVE_ERROR_PARSE, "Failed to parse '%s'", - filename); + url); } return; } /* we assume that the last (or only) filename passed as argument is a * great filename to use as default when saving the dives */ - set_filename(filename); + set_filename(url); reset_all(); dive_start(); #ifdef XSLT |