From d9be07670c2a7e3d54b6defb5ce5334b41ae1d90 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 8 Dec 2014 11:26:03 -0800 Subject: Don't ignore when we can't parse a file We are quite inconsistent when it comes to reporting back errors. One case where this caused somewhat unexpected behavior was when the user would try to open a .csv file by passing it as command line argument. The file was silently ignored, but treated as if it had been opened successfully. Now we issue a somewhat reasonable error message. Signed-off-by: Dirk Hohndel --- dive.h | 2 +- file.c | 35 ++++++++++++++++++++--------------- parse-xml.c | 11 ++++++----- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/dive.h b/dive.h index 95f4d6f68..f67c7368b 100644 --- a/dive.h +++ b/dive.h @@ -619,7 +619,7 @@ struct dive *find_dive_n_near(timestamp_t when, int n, timestamp_t offset); extern int match_one_dc(struct divecomputer *a, struct divecomputer *b); extern void parse_xml_init(void); -extern void parse_xml_buffer(const char *url, const char *buf, int size, struct dive_table *table, const char **params); +extern int parse_xml_buffer(const char *url, const char *buf, int size, struct dive_table *table, const char **params); extern void parse_xml_exit(void); extern void set_filename(const char *filename, bool force); diff --git a/file.c b/file.c index d85661080..358397675 100644 --- a/file.c +++ b/file.c @@ -76,7 +76,7 @@ static void zip_read(struct zip_file *file, const char *filename) mem = realloc(mem, size); } mem[read] = 0; - parse_xml_buffer(filename, mem, read, &dive_table, NULL); + (void) parse_xml_buffer(filename, mem, read, &dive_table, NULL); free(mem); } @@ -370,7 +370,7 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo /* CSV files */ if (!strcasecmp(fmt, "CSV")) - return 1; + return report_error("Cannot open CSV file %s; please use Import log file dialog", filename); /* Truly nasty intentionally obfuscated Cochran Anal software */ if (!strcasecmp(fmt, "CAN")) return try_to_open_cochran(filename, mem); @@ -387,16 +387,17 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo return 0; } -static void parse_file_buffer(const char *filename, struct memblock *mem) +static int parse_file_buffer(const char *filename, struct memblock *mem) { + int ret; char *fmt = strrchr(filename, '.'); - if (fmt && open_by_filename(filename, fmt + 1, mem)) - return; + if (fmt && (ret = open_by_filename(filename, fmt + 1, mem)) != 0) + return ret; if (!mem->size || !mem->buffer) - return; + return report_error("Out of memory parsing file %s\n", filename); - parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL); + return parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL); } int parse_file(const char *filename) @@ -405,6 +406,7 @@ int parse_file(const char *filename) const char *branch; struct memblock mem; char *fmt; + int ret; git = is_git_repository(filename, &branch); if (git && !git_load_dives(git, branch)) @@ -426,9 +428,9 @@ int parse_file(const char *filename) } } - parse_file_buffer(filename, &mem); + ret = parse_file_buffer(filename, &mem); free(mem.buffer); - return 0; + return ret; } #define MATCH(buffer, pattern) \ @@ -822,6 +824,7 @@ void init_csv_file_parsing(char **params, char *timebuf, char *depthbuf, char *t int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int sepidx, const char *csvtemplate, int unitidx) { + int ret; struct memblock mem; char *params[27]; char timebuf[MAXCOLDIGITS]; @@ -852,13 +855,14 @@ int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int p if (try_to_xslt_open_csv(filename, &mem, csvtemplate)) return -1; - parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); free(mem.buffer); - return 0; + return ret; } int parse_seabear_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int sepidx, const char *csvtemplate, int unitidx) { + int ret; struct memblock mem; char *params[27]; char timebuf[MAXCOLDIGITS]; @@ -938,9 +942,9 @@ int parse_seabear_csv_file(const char *filename, int timef, int depthf, int temp if (try_to_xslt_open_csv(filename, &mem, csvtemplate)) return -1; - parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); free(mem.buffer); - return 0; + return ret; } int parse_manual_file(const char *filename, int sepidx, int units, int dateformat, int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf, int buddyf, int notesf, int weightf, int tagsf) @@ -967,6 +971,7 @@ int parse_manual_file(const char *filename, int sepidx, int units, int dateforma struct tm *timep; char curdate[9]; char curtime[6]; + int ret; if (numberf >= MAXCOLS || datef >= MAXCOLS || timef >= MAXCOLS || durationf >= MAXCOLS || locationf >= MAXCOLS || gpsf >= MAXCOLS || maxdepthf >= MAXCOLS || meandepthf >= MAXCOLS || buddyf >= MAXCOLS || notesf >= MAXCOLS || weightf >= MAXCOLS || tagsf >= MAXCOLS) return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS); @@ -1037,7 +1042,7 @@ int parse_manual_file(const char *filename, int sepidx, int units, int dateforma if (try_to_xslt_open_csv(filename, &mem, "manualCSV")) return -1; - parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); free(mem.buffer); - return 0; + return ret; } diff --git a/parse-xml.c b/parse-xml.c index 80cd2021a..f1b439e28 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -1745,7 +1745,7 @@ const char *preprocess_divelog_de(const char *buffer) return buffer; } -void parse_xml_buffer(const char *url, const char *buffer, int size, +int parse_xml_buffer(const char *url, const char *buffer, int size, struct dive_table *table, const char **params) { xmlDoc *doc; @@ -1756,10 +1756,9 @@ void parse_xml_buffer(const char *url, const char *buffer, int size, if (res != buffer) free((char *)res); - if (!doc) { - report_error(translate("gettextFromC", "Failed to parse '%s'"), url); - return; - } + if (!doc) + return report_error(translate("gettextFromC", "Failed to parse '%s'"), url); + set_save_userid_local(false); set_userid(""); reset_all(); @@ -1768,6 +1767,8 @@ void parse_xml_buffer(const char *url, const char *buffer, int size, traverse(xmlDocGetRootElement(doc)); dive_end(); xmlFreeDoc(doc); + + return 0; } void parse_mkvi_buffer(struct membuffer *txt, struct membuffer *csv, const char *starttime) -- cgit v1.2.3-70-g09d2