diff options
author | Miika Turkia <miika.turkia@gmail.com> | 2013-09-29 15:44:38 +0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-10-02 08:49:52 -0700 |
commit | ba5b5c3952f7de974f75162c99de51b5f572a078 (patch) | |
tree | 6e6bdf18216e221719b0e5c026c905f8d34b7bc7 /file.c | |
parent | adefc8805a7474e6f0baca476df33f7ab8ea38c8 (diff) | |
download | subsurface-ba5b5c3952f7de974f75162c99de51b5f572a078.tar.gz |
Initial code to import CSV log files
This patch implements basic functionality to import CSV formatted log
profiles to Subsurface. The import includes time, depth and temperature
from AP Logviewer based on one sample log file I have received. It is
assumed that dive time is the first parameter and depth second.
Temperature is given as a parameter from C source (hard coded currently
to field 15) but we should have a GUI implemented for selecting the
wanted fields.
The two different sample logs of CSV dive log export I have received use
tabulator as field separator. I assume the possible GUI should have
option for the FS as well to be given as parameter to the XSLT.
[Dirk Hohndel: small fix to the error string malloc]
Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 39 |
1 files changed, 37 insertions, 2 deletions
@@ -97,6 +97,36 @@ 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) +{ + char *buf; + + if (readfile(filename, mem) < 0) { + if (error) { + int len = strlen(_("Failed to read '%s'")) + strlen(filename); + *error = malloc(len); + snprintf(*error, len, _("Failed to read '%s'"), filename); + } + + return 1; + } + + /* Surround the CSV file content with XML tags to enable XSLT + * parsing + */ + buf = realloc(mem->buffer, mem->size + strlen("<csv></csv>")); + if (buf != NULL) { + memmove(buf + 5, mem->buffer, mem->size); + memcpy(buf, "<csv>", 5); + memcpy(mem->buffer + mem->size + 5, "</csv>", 7); + mem->buffer = buf; + mem->size += strlen("<csv></csv>"); + } else + return 1; + + return 0; +} + static int try_to_open_db(const char *filename, struct memblock *mem, char **error) { return parse_dm4_buffer(filename, mem->buffer, mem->size, &dive_table, error); @@ -229,6 +259,10 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD")) return try_to_open_zip(filename, mem, error); + /* CSV files */ + if (!strcasecmp(fmt, "CSV")) + return try_to_xslt_open_csv(filename, mem, error); + #if ONCE_COCHRAN_IS_SUPPORTED /* Truly nasty intentionally obfuscated Cochran Anal software */ if (!strcasecmp(fmt, "CAN")) @@ -266,8 +300,9 @@ void parse_file(const char *filename, char **error) return; if (error) { - *error = malloc(1024); - snprintf(*error, 1024, _("Failed to read '%s'"), filename); + int len = strlen(_("Failed to read '%s'")) + strlen(filename); + *error = malloc(len); + snprintf(*error, len, _("Failed to read '%s'"), filename); } return; |