11 static int readfile(const char *filename, struct memblock *mem)
13 int ret, fd = open(filename, O_RDONLY);
20 fd = open(filename, O_RDONLY);
27 if (!S_ISREG(st.st_mode))
32 buf = malloc(st.st_size+1);
38 mem->size = st.st_size;
39 ret = read(fd, buf, mem->size);
59 static void suunto_read(struct zip_file *file, GError **error)
61 int size = 1024, n, read = 0;
62 char *mem = malloc(size);
64 while ((n = zip_fread(file, mem+read, size-read)) > 0) {
67 mem = realloc(mem, size);
69 parse_xml_buffer("SDE file", mem, read, error);
74 static int try_to_open_suunto(const char *filename, struct memblock *mem, GError **error)
78 /* Grr. libzip needs to re-open the file, it can't take a buffer */
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, struct memblock *mem, GError **error)
99 /* Suunto Dive Manager files: SDE */
100 if (!strcasecmp(fmt, "SDE"))
101 return try_to_open_suunto(filename, mem, error);
103 /* Truly nasty intentionally obfuscated Cochran Anal software */
104 if (!strcasecmp(fmt, "CAN"))
105 return try_to_open_cochran(filename, mem, error);
110 static void parse_file_buffer(const char *filename, struct memblock *mem, GError **error)
112 char *fmt = strrchr(filename, '.');
113 if (fmt && open_by_filename(filename, fmt+1, mem, error))
116 parse_xml_buffer(filename, mem->buffer, mem->size, error);
119 void parse_file(const char *filename, GError **error)
123 if (readfile(filename, &mem) < 0) {
124 fprintf(stderr, "Failed to read '%s'.\n", filename);
126 *error = g_error_new(g_quark_from_string("subsurface"),
128 "Failed to read '%s'",
134 parse_file_buffer(filename, &mem, error);