summaryrefslogtreecommitdiffstats
path: root/file.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-03-14 11:26:07 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-03-14 12:36:06 -0700
commit609715ab698489588b32aa69d98fa2c5b233ef8f (patch)
treed9bfef1748deea912ce894e22c53f7920a62afec /file.c
parentec33a95ad04099d428275de2c6b36e7098cc943e (diff)
downloadsubsurface-609715ab698489588b32aa69d98fa2c5b233ef8f.tar.gz
Convert other users of showError() to the new world order
The "report_error()" interface is a lot simpler, although some of the C++ code uses QStrings which make them a bit annoying, especially for the varargs model. Still, even with the explicit conversion to UTF8 and "char *", the report_error() model is much nicer. This also just makes refreshDisplay() do the error reporting in the UI automatically, so a number of error paths don't even have to worry. And the multi-line model of error reporting means that it all automatically does the right thing, and reports errors for each file rather than just for the last file that failed to open. So this removes closer to a hundred lines of cruft, while being a simpler interface and doing better error reporting. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'file.c')
-rw-r--r--file.c105
1 files changed, 42 insertions, 63 deletions
diff --git a/file.c b/file.c
index 8facdf8d5..2c0b9f939 100644
--- a/file.c
+++ b/file.c
@@ -62,7 +62,7 @@ out:
}
-static void zip_read(struct zip_file *file, char **error, const char *filename)
+static void zip_read(struct zip_file *file, const char *filename)
{
int size = 1024, n, read = 0;
char *mem = malloc(size);
@@ -73,11 +73,11 @@ static void zip_read(struct zip_file *file, char **error, const char *filename)
mem = realloc(mem, size);
}
mem[read] = 0;
- parse_xml_buffer(filename, mem, read, &dive_table, NULL, error);
+ parse_xml_buffer(filename, mem, read, &dive_table, NULL);
free(mem);
}
-static int try_to_open_zip(const char *filename, struct memblock *mem, char **error)
+static int try_to_open_zip(const char *filename, struct memblock *mem)
{
int success = 0;
/* Grr. libzip needs to re-open the file, it can't take a buffer */
@@ -89,7 +89,7 @@ static int try_to_open_zip(const char *filename, struct memblock *mem, char **er
struct zip_file *file = zip_fopen_index(zip, index, 0);
if (!file)
break;
- zip_read(file, error, filename);
+ zip_read(file, filename);
zip_fclose(file);
success++;
}
@@ -98,19 +98,12 @@ static int try_to_open_zip(const char *filename, struct memblock *mem, char **er
return success;
}
-static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, char **error, const char *tag)
+static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, const char *tag)
{
char *buf;
- if (readfile(filename, mem) < 0) {
- if (error) {
- int len = strlen(translate("gettextFromC", "Failed to read '%s'")) + strlen(filename);
- *error = malloc(len);
- snprintf(*error, len, translate("gettextFromC", "Failed to read '%s'"), filename);
- }
-
- return 1;
- }
+ if (readfile(filename, mem) < 0)
+ return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
/* Surround the CSV file content with XML tags to enable XSLT
* parsing
@@ -132,8 +125,7 @@ static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, char
free(starttag);
free(endtag);
free(buf);
- *error = strdup("Memory allocation failed in __func__\n");
- return 1;
+ return report_error("Memory allocation failed in %s", __func__);
}
sprintf(starttag, "<%s>", tag);
@@ -148,10 +140,8 @@ static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, char
free(starttag);
free(endtag);
} else {
- /* we can atleast try to strdup a error... */
- *error = strdup("realloc failed in __func__\n");
free(mem->buffer);
- return 1;
+ return report_error("realloc failed in %s", __func__);
}
return 0;
@@ -163,7 +153,7 @@ int db_test_func(void *param, int columns, char **data, char **column)
}
-static int try_to_open_db(const char *filename, struct memblock *mem, char **error)
+static int try_to_open_db(const char *filename, struct memblock *mem)
{
sqlite3 *handle;
char dm4_test[] = "select count(*) from sqlite_master where type='table' and name='Dive' and sql like '%ProfileBlob%'";
@@ -180,7 +170,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, char **err
/* Testing if DB schema resembles Suunto DM4 database format */
retval = sqlite3_exec(handle, dm4_test, &db_test_func, 0, NULL);
if (!retval) {
- retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, &dive_table, error);
+ retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
sqlite3_close(handle);
return retval;
}
@@ -188,7 +178,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, char **err
/* Testing if DB schema resembles Shearwater database format */
retval = sqlite3_exec(handle, shearwater_test, &db_test_func, 0, NULL);
if (!retval) {
- retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, &dive_table, error);
+ retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, &dive_table);
sqlite3_close(handle);
return retval;
}
@@ -321,11 +311,11 @@ static int try_to_open_csv(const char *filename, struct memblock *mem, enum csv_
return 1;
}
-static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem, char **error)
+static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem)
{
/* Suunto Dive Manager files: SDE, ZIP; divelogs.de files: DLD */
if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD"))
- return try_to_open_zip(filename, mem, error);
+ return try_to_open_zip(filename, mem);
/* CSV files */
if (!strcasecmp(fmt, "CSV"))
@@ -334,7 +324,7 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo
#if ONCE_COCHRAN_IS_SUPPORTED
/* Truly nasty intentionally obfuscated Cochran Anal software */
if (!strcasecmp(fmt, "CAN"))
- return try_to_open_cochran(filename, mem, error);
+ return try_to_open_cochran(filename, mem);
#endif
/* Cochran export comma-separated-value files */
@@ -348,19 +338,19 @@ 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, char **error)
+static void parse_file_buffer(const char *filename, struct memblock *mem)
{
char *fmt = strrchr(filename, '.');
- if (fmt && open_by_filename(filename, fmt + 1, mem, error))
+ if (fmt && open_by_filename(filename, fmt + 1, mem))
return;
if (!mem->size || !mem->buffer)
return;
- parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL, error);
+ parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL);
}
-void parse_file(const char *filename, char **error)
+int parse_file(const char *filename)
{
struct git_repository *git;
const char *branch;
@@ -369,37 +359,32 @@ void parse_file(const char *filename, char **error)
git = is_git_repository(filename, &branch);
if (git && !git_load_dives(git, branch))
- return;
+ return 0;
if (readfile(filename, &mem) < 0) {
/* we don't want to display an error if this was the default file */
if (prefs.default_filename && !strcmp(filename, prefs.default_filename))
- return;
-
- if (error) {
- int len = strlen(translate("gettextFromC", "Failed to read '%s'")) + strlen(filename);
- *error = malloc(len);
- snprintf(*error, len, translate("gettextFromC", "Failed to read '%s'"), filename);
- }
+ return 0;
- return;
+ return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
}
fmt = strrchr(filename, '.');
if (fmt && (!strcasecmp(fmt + 1, "DB") || !strcasecmp(fmt + 1, "BAK"))) {
- if (!try_to_open_db(filename, &mem, error)) {
+ if (!try_to_open_db(filename, &mem)) {
free(mem.buffer);
- return;
+ return 0;
}
}
- parse_file_buffer(filename, &mem, error);
+ parse_file_buffer(filename, &mem);
free(mem.buffer);
+ return 0;
}
#define MAXCOLDIGITS 3
#define MAXCOLS 100
-void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int unitidx, char **error)
+int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int cnsf, int stopdepthf, int sepidx, const char *csvtemplate, int unitidx)
{
struct memblock mem;
int pnr = 0;
@@ -417,13 +402,9 @@ void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int
char curdate[9];
char curtime[6];
- if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS || po2f >= MAXCOLS || cnsf >= MAXCOLS || stopdepthf >= MAXCOLS) {
- int len = strlen(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d")) + MAXCOLDIGITS;
- *error = malloc(len);
- snprintf(*error, len, translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
+ if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS || po2f >= MAXCOLS || cnsf >= MAXCOLS || stopdepthf >= MAXCOLS)
+ return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);
- return;
- }
snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
snprintf(depthbuf, MAXCOLDIGITS, "%d", depthf);
snprintf(tempbuf, MAXCOLDIGITS, "%d", tempf);
@@ -463,16 +444,17 @@ void parse_csv_file(const char *filename, int timef, int depthf, int tempf, int
params[pnr++] = NULL;
if (filename == NULL)
- return;
+ return report_error("No CSV filename");
- if (try_to_xslt_open_csv(filename, &mem, error, csvtemplate))
- return;
+ if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
+ return -1;
- parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params, error);
+ parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
free(mem.buffer);
+ return 0;
}
-void parse_manual_file(const char *filename, int sepidx, int units, int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf, int buddyf, int notesf, int weightf, int tagsf, char **error)
+int parse_manual_file(const char *filename, int sepidx, int units, int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf, int buddyf, int notesf, int weightf, int tagsf)
{
struct memblock mem;
int pnr = 0;
@@ -496,13 +478,9 @@ void parse_manual_file(const char *filename, int sepidx, int units, int numberf,
char curdate[9];
char curtime[6];
- 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) {
- int len = strlen(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d")) + MAXCOLDIGITS;
- *error = malloc(len);
- snprintf(*error, len, translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), 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;
- }
snprintf(numberbuf, MAXCOLDIGITS, "%d", numberf);
snprintf(datebuf, MAXCOLDIGITS, "%d", datef);
snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
@@ -560,11 +538,12 @@ void parse_manual_file(const char *filename, int sepidx, int units, int numberf,
params[pnr++] = NULL;
if (filename == NULL)
- return;
+ return report_error("No manual CSV filename");
- if (try_to_xslt_open_csv(filename, &mem, error, "manualCSV"))
- return;
+ if (try_to_xslt_open_csv(filename, &mem, "manualCSV"))
+ return -1;
- parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params, error);
+ parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
free(mem.buffer);
+ return 0;
}