summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--dive.h4
-rw-r--r--file.c73
-rw-r--r--gtk-gui.c4
-rw-r--r--main.c2
-rw-r--r--parse-xml.c71
6 files changed, 88 insertions, 71 deletions
diff --git a/Makefile b/Makefile
index 8a5096a16..39e1cadb0 100644
--- a/Makefile
+++ b/Makefile
@@ -115,7 +115,7 @@ LIBS = $(LIBXML2) $(LIBXSLT) $(LIBGTK) $(LIBGCONF2) $(LIBDIVECOMPUTER) $(EXTRALI
OBJS = main.o dive.o profile.o info.o equipment.o divelist.o \
parse-xml.o save-xml.o libdivecomputer.o print.o uemis.o \
- gtk-gui.o statistics.o $(OSSUPPORT).o $(RESFILE)
+ gtk-gui.o statistics.o file.o $(OSSUPPORT).o $(RESFILE)
$(NAME): $(OBJS)
$(CC) $(LDFLAGS) -o $(NAME) $(OBJS) $(LIBS)
@@ -147,6 +147,9 @@ install-macosx: $(NAME)
$(INSTALL) $(ICONFILE) $(MACOSXINSTALL)/Contents/Resources/
$(INSTALL) $(MACOSXFILES)/Subsurface.icns $(MACOSXINSTALL)/Contents/Resources/
+file.o: file.c dive.h
+ $(CC) $(CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) $(XSLT) -c file.c
+
parse-xml.o: parse-xml.c dive.h
$(CC) $(CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) $(XSLT) -c parse-xml.c
diff --git a/dive.h b/dive.h
index 52158ad78..befa0cdcf 100644
--- a/dive.h
+++ b/dive.h
@@ -254,9 +254,11 @@ static inline struct dive *get_dive(unsigned int nr)
}
extern void parse_xml_init(void);
-extern void parse_xml_file(const char *filename, GError **error);
+extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
extern void set_filename(const char *filename);
+extern void parse_file(const char *filename, GError **error);
+
#ifdef XSLT
extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
#endif
diff --git a/file.c b/file.c
new file mode 100644
index 000000000..5db36a849
--- /dev/null
+++ b/file.c
@@ -0,0 +1,73 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "dive.h"
+
+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_file(const char *filename, GError **error)
+{
+ 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;
+ }
+
+ parse_xml_buffer(filename, mem.buffer, mem.size, error);
+ free(mem.buffer);
+}
diff --git a/gtk-gui.c b/gtk-gui.c
index 45185c016..5b892653f 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -114,7 +114,7 @@ static void file_open(GtkWidget *w, gpointer data)
GError *error = NULL;
while(filenames != NULL) {
filename = filenames->data;
- parse_xml_file(filename, &error);
+ parse_file(filename, &error);
if (error != NULL)
{
report_error(error);
@@ -961,7 +961,7 @@ static GtkWidget *xml_file_selector(GtkWidget *vbox, GtkWidget *main_dialog)
static void do_import_file(gpointer data, gpointer user_data)
{
GError *error = NULL;
- parse_xml_file(data, &error);
+ parse_file(data, &error);
if (error != NULL)
{
diff --git a/main.c b/main.c
index 67e19b02b..eae4ee239 100644
--- a/main.c
+++ b/main.c
@@ -226,7 +226,7 @@ int main(int argc, char **argv)
continue;
}
GError *error = NULL;
- parse_xml_file(a, &error);
+ parse_file(a, &error);
if (error != NULL)
{
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