15 static int readfile(const char *filename, struct memblock *mem)
17 int ret, fd = open(filename, O_RDONLY);
23 fd = open(filename, O_RDONLY);
30 if (!S_ISREG(st.st_mode))
35 mem->buffer = malloc(st.st_size);
40 mem->size = st.st_size;
41 ret = read(fd, mem->buffer, mem->size);
60 static void suunto_read(struct zip_file *file, GError **error)
62 int size = 1024, n, read = 0;
63 char *mem = malloc(size);
65 while ((n = zip_fread(file, mem+read, size-read)) > 0) {
68 mem = realloc(mem, size);
70 parse_xml_buffer("SDE file", mem, read, error);
75 static int try_to_open_suunto(const char *filename, GError **error)
79 struct zip *zip = zip_open(filename, ZIP_CHECKCONS, NULL);
83 for (index = 0; ;index++) {
84 struct zip_file *file = zip_fopen_index(zip, index, 0);
87 suunto_read(file, error);
97 static int open_by_filename(const char *filename, const char *fmt, GError **error)
99 /* Suunto Dive Manager files: SDE */
100 if (!strcasecmp(fmt, "SDE"))
101 return try_to_open_suunto(filename, error);
106 void parse_file(const char *filename, GError **error)
111 fmt = strrchr(filename, '.');
112 if (fmt && open_by_filename(filename, fmt+1, error))
115 if (readfile(filename, &mem) < 0) {
116 fprintf(stderr, "Failed to read '%s'.\n", filename);
118 *error = g_error_new(g_quark_from_string("subsurface"),
120 "Failed to read '%s'",
126 parse_xml_buffer(filename, mem.buffer, mem.size, error);