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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-12-08 11:26:03 -08:00
parent 77664e7150
commit d9be07670c
3 changed files with 27 additions and 21 deletions

2
dive.h
View file

@ -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 int match_one_dc(struct divecomputer *a, struct divecomputer *b);
extern void parse_xml_init(void); 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 parse_xml_exit(void);
extern void set_filename(const char *filename, bool force); extern void set_filename(const char *filename, bool force);

35
file.c
View file

@ -76,7 +76,7 @@ static void zip_read(struct zip_file *file, const char *filename)
mem = realloc(mem, size); mem = realloc(mem, size);
} }
mem[read] = 0; mem[read] = 0;
parse_xml_buffer(filename, mem, read, &dive_table, NULL); (void) parse_xml_buffer(filename, mem, read, &dive_table, NULL);
free(mem); free(mem);
} }
@ -370,7 +370,7 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo
/* CSV files */ /* CSV files */
if (!strcasecmp(fmt, "CSV")) 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 */ /* Truly nasty intentionally obfuscated Cochran Anal software */
if (!strcasecmp(fmt, "CAN")) if (!strcasecmp(fmt, "CAN"))
return try_to_open_cochran(filename, mem); 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; 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, '.'); char *fmt = strrchr(filename, '.');
if (fmt && open_by_filename(filename, fmt + 1, mem)) if (fmt && (ret = open_by_filename(filename, fmt + 1, mem)) != 0)
return; return ret;
if (!mem->size || !mem->buffer) 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) int parse_file(const char *filename)
@ -405,6 +406,7 @@ int parse_file(const char *filename)
const char *branch; const char *branch;
struct memblock mem; struct memblock mem;
char *fmt; char *fmt;
int ret;
git = is_git_repository(filename, &branch); git = is_git_repository(filename, &branch);
if (git && !git_load_dives(git, 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); free(mem.buffer);
return 0; return ret;
} }
#define MATCH(buffer, pattern) \ #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 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; struct memblock mem;
char *params[27]; char *params[27];
char timebuf[MAXCOLDIGITS]; 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)) if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
return -1; 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); 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 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; struct memblock mem;
char *params[27]; char *params[27];
char timebuf[MAXCOLDIGITS]; 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)) if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
return -1; 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); 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) 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; struct tm *timep;
char curdate[9]; char curdate[9];
char curtime[6]; 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) 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); 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")) if (try_to_xslt_open_csv(filename, &mem, "manualCSV"))
return -1; 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); free(mem.buffer);
return 0; return ret;
} }

View file

@ -1745,7 +1745,7 @@ const char *preprocess_divelog_de(const char *buffer)
return 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) struct dive_table *table, const char **params)
{ {
xmlDoc *doc; xmlDoc *doc;
@ -1756,10 +1756,9 @@ void parse_xml_buffer(const char *url, const char *buffer, int size,
if (res != buffer) if (res != buffer)
free((char *)res); free((char *)res);
if (!doc) { if (!doc)
report_error(translate("gettextFromC", "Failed to parse '%s'"), url); return report_error(translate("gettextFromC", "Failed to parse '%s'"), url);
return;
}
set_save_userid_local(false); set_save_userid_local(false);
set_userid(""); set_userid("");
reset_all(); reset_all();
@ -1768,6 +1767,8 @@ void parse_xml_buffer(const char *url, const char *buffer, int size,
traverse(xmlDocGetRootElement(doc)); traverse(xmlDocGetRootElement(doc));
dive_end(); dive_end();
xmlFreeDoc(doc); xmlFreeDoc(doc);
return 0;
} }
void parse_mkvi_buffer(struct membuffer *txt, struct membuffer *csv, const char *starttime) void parse_mkvi_buffer(struct membuffer *txt, struct membuffer *csv, const char *starttime)