diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-09-28 13:59:01 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-09-28 15:06:00 -0700 |
commit | 32a4ce61694bfe8364227440ea5716df7cc61fd1 (patch) | |
tree | 257ac4f9204182b20d7e4bda7bc80911bc6303a6 | |
parent | a5db03c2b32115b14e4b83501a0771b186d87913 (diff) | |
download | subsurface-32a4ce61694bfe8364227440ea5716df7cc61fd1.tar.gz |
Parser: parse text-based files into arbitrary table
In d815e0c9476ef62e6b84fb28ce48ab7cddefe77e a dive_table pointer
was added to the parsing functions to allow parsing into tables
other than the global dive table. This will be necessary for undo of
import and implementation a cleaner interface. A few cases, notably
CSV and proprietary formats were forgotten.
Implement parsing into arbitrary tables also for these cases.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/cochran.c | 9 | ||||
-rw-r--r-- | core/datatrak.c | 2 | ||||
-rw-r--r-- | core/file.c | 10 | ||||
-rw-r--r-- | core/file.h | 4 | ||||
-rw-r--r-- | core/import-csv.c | 34 | ||||
-rw-r--r-- | core/import-csv.h | 10 | ||||
-rw-r--r-- | core/liquivision.c | 8 | ||||
-rw-r--r-- | desktop-widgets/divelogimportdialog.cpp | 12 | ||||
-rw-r--r-- | desktop-widgets/mainwindow.cpp | 2 | ||||
-rw-r--r-- | tests/testparse.cpp | 12 |
10 files changed, 52 insertions, 51 deletions
diff --git a/core/cochran.c b/core/cochran.c index a8f008c4c..164b94bee 100644 --- a/core/cochran.c +++ b/core/cochran.c @@ -600,7 +600,8 @@ static void cochran_parse_samples(struct dive *dive, const unsigned char *log, } static void cochran_parse_dive(const unsigned char *decode, unsigned mod, - const unsigned char *in, unsigned size) + const unsigned char *in, unsigned size, + struct dive_table *table) { unsigned char *buf = malloc(size); struct dive *dive; @@ -785,13 +786,13 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod, } dive->downloaded = true; - record_dive(dive); + record_dive_to_table(dive, table); mark_divelist_changed(true); free(buf); } -int try_to_open_cochran(const char *filename, struct memblock *mem) +int try_to_open_cochran(const char *filename, struct memblock *mem, struct dive_table *table) { UNUSED(filename); unsigned int i; @@ -822,7 +823,7 @@ int try_to_open_cochran(const char *filename, struct memblock *mem) break; cochran_parse_dive(decode, mod, mem->buffer + dive1, - dive2 - dive1); + dive2 - dive1, table); } return 1; // no further processing needed diff --git a/core/datatrak.c b/core/datatrak.c index 1c728aa45..582fb512f 100644 --- a/core/datatrak.c +++ b/core/datatrak.c @@ -602,7 +602,7 @@ int datatrak_import(struct memblock *mem, struct dive_table *table) rc = 1; goto out; } else { - record_dive(ptdive); + record_dive_to_table(ptdive, table); } i++; } diff --git a/core/file.c b/core/file.c index abef20a7d..6f2edcf8d 100644 --- a/core/file.c +++ b/core/file.c @@ -220,16 +220,16 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo return report_error(translate("gettextFromC", csv_warning), filename); /* Truly nasty intentionally obfuscated Cochran Anal software */ if (!strcasecmp(fmt, "CAN")) - return try_to_open_cochran(filename, mem); + return try_to_open_cochran(filename, mem, table); /* Cochran export comma-separated-value files */ if (!strcasecmp(fmt, "DPT")) - return try_to_open_csv(mem, CSV_DEPTH); + return try_to_open_csv(mem, CSV_DEPTH, table); if (!strcasecmp(fmt, "LVD")) - return try_to_open_liquivision(filename, mem); + return try_to_open_liquivision(filename, mem, table); if (!strcasecmp(fmt, "TMP")) - return try_to_open_csv(mem, CSV_TEMP); + return try_to_open_csv(mem, CSV_TEMP, table); if (!strcasecmp(fmt, "HP1")) - return try_to_open_csv(mem, CSV_PRESSURE); + return try_to_open_csv(mem, CSV_PRESSURE, table); return 0; } diff --git a/core/file.h b/core/file.h index 7dbd07b48..e0033d94e 100644 --- a/core/file.h +++ b/core/file.h @@ -7,8 +7,8 @@ struct memblock { size_t size; }; -extern int try_to_open_cochran(const char *filename, struct memblock *mem); -extern int try_to_open_liquivision(const char *filename, struct memblock *mem); +extern int try_to_open_cochran(const char *filename, struct memblock *mem, struct dive_table *table); +extern int try_to_open_liquivision(const char *filename, struct memblock *mem, struct dive_table *table); extern int datatrak_import(struct memblock *mem, struct dive_table *table); extern void ostctools_import(const char *file, struct dive_table *table); diff --git a/core/import-csv.c b/core/import-csv.c index b3ff4261f..15255e191 100644 --- a/core/import-csv.c +++ b/core/import-csv.c @@ -101,7 +101,7 @@ static char *parse_dan_new_line(char *buf, const char *NL) } static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, const char *tag); -static int parse_dan_format(const char *filename, char **params, int pnr) +static int parse_dan_format(const char *filename, char **params, int pnr, struct dive_table *table) { int ret = 0, i; size_t end_ptr = 0; @@ -219,7 +219,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr) } } params[pnr_local] = NULL; - ret |= parse_xml_buffer(filename, "<csv></csv>", 11, &dive_table, (const char **)params); + ret |= parse_xml_buffer(filename, "<csv></csv>", 11, table, (const char **)params); continue; } @@ -276,7 +276,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr) if (try_to_xslt_open_csv(filename, &mem_csv, "csv")) return -1; - ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, &dive_table, (const char **)params); + ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, table, (const char **)params); end_ptr += ptr - (char *)mem_csv.buffer; free(mem_csv.buffer); } @@ -288,7 +288,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr) return ret; } -int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate) +int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, struct dive_table *table) { int ret, i; struct memblock mem; @@ -308,7 +308,7 @@ int parse_csv_file(const char *filename, char **params, int pnr, const char *csv mem.size = 0; if (!strcmp("DL7", csvtemplate)) { - return parse_dan_format(filename, params, pnr); + return parse_dan_format(filename, params, pnr, table); } else if (strcmp(params[0], "date")) { time(&now); timep = localtime(&now); @@ -343,7 +343,7 @@ int parse_csv_file(const char *filename, char **params, int pnr, const char *csv fprintf(stderr, "%s/xslt/csv2xml.xslt -\n", SUBSURFACE_SOURCE); } #endif - ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, (const char **)params); free(mem.buffer); for (i = 0; params[i]; i += 2) @@ -402,7 +402,7 @@ static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, cons return 0; } -int try_to_open_csv(struct memblock *mem, enum csv_format type) +int try_to_open_csv(struct memblock *mem, enum csv_format type, struct dive_table *table) { char *p = mem->buffer; char *header[8]; @@ -452,7 +452,7 @@ int try_to_open_csv(struct memblock *mem, enum csv_format type) break; p = end + 1; } - record_dive(dive); + record_dive_to_table(dive, table); return 1; } @@ -494,7 +494,7 @@ static char *next_mkvi_key(const char *haystack) return ret; } -int parse_txt_file(const char *filename, const char *csv) +int parse_txt_file(const char *filename, const char *csv, struct dive_table *table) { struct memblock memtxt, memcsv; @@ -773,7 +773,7 @@ int parse_txt_file(const char *filename, const char *csv) if (!lineptr || !*lineptr) break; } - record_dive(dive); + record_dive_to_table(dive, table); return 1; } else { return 0; @@ -786,15 +786,15 @@ int parse_txt_file(const char *filename, const char *csv) #define TIMESTR 6 #define SBPARAMS 40 -static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate); -int parse_seabear_log(const char *filename) +static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, struct dive_table *table); +int parse_seabear_log(const char *filename, struct dive_table *table) { char *params[SBPARAMS]; int pnr = 0; pnr = parse_seabear_header(filename, params, pnr); - if (parse_seabear_csv_file(filename, params, pnr, "csv") < 0) { + if (parse_seabear_csv_file(filename, params, pnr, "csv", table) < 0) { return -1; } @@ -802,7 +802,7 @@ int parse_seabear_log(const char *filename) } -static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate) +static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, struct dive_table *table) { int ret, i; struct memblock mem; @@ -921,7 +921,7 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, fprintf(stderr, "xslt/csv2xml.xslt\n"); } - ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, (const char **)params); free(mem.buffer); for (i = 0; params[i]; i += 2) free(params[i + 1]); @@ -929,7 +929,7 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, return ret; } -int parse_manual_file(const char *filename, char **params, int pnr) +int parse_manual_file(const char *filename, char **params, int pnr, struct dive_table *table) { struct memblock mem; time_t now; @@ -969,7 +969,7 @@ int parse_manual_file(const char *filename, char **params, int pnr) fprintf(stderr, "%s/xslt/manualcsv2xml.xslt -\n", SUBSURFACE_SOURCE); } #endif - ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, (const char **)params); free(mem.buffer); for (i = 0; i < pnr - 2; ++i) diff --git a/core/import-csv.h b/core/import-csv.h index 1f1d330e7..363daa291 100644 --- a/core/import-csv.h +++ b/core/import-csv.h @@ -21,12 +21,12 @@ enum csv_format { extern "C" { #endif -int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate); -int try_to_open_csv(struct memblock *mem, enum csv_format type); -int parse_txt_file(const char *filename, const char *csv); +int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, struct dive_table *table); +int try_to_open_csv(struct memblock *mem, enum csv_format type, struct dive_table *table); +int parse_txt_file(const char *filename, const char *csv, struct dive_table *table); -int parse_seabear_log(const char *filename); -int parse_manual_file(const char *filename, char **params, int pnr); +int parse_seabear_log(const char *filename, struct dive_table *table); +int parse_manual_file(const char *filename, char **params, int pnr, struct dive_table *table); #ifdef __cplusplus } diff --git a/core/liquivision.c b/core/liquivision.c index 28d571b3c..99641835c 100644 --- a/core/liquivision.c +++ b/core/liquivision.c @@ -132,7 +132,7 @@ static int handle_event_ver3(int code, const unsigned char *ps, unsigned int ps_ return skip; } -static void parse_dives (int log_version, const unsigned char *buf, unsigned int buf_size) +static void parse_dives (int log_version, const unsigned char *buf, unsigned int buf_size, struct dive_table *table) { unsigned int ptr = 0; unsigned char model; @@ -428,7 +428,7 @@ static void parse_dives (int log_version, const unsigned char *buf, unsigned int // End dive dive->downloaded = true; - record_dive(dive); + record_dive_to_table(dive, table); dive = NULL; mark_divelist_changed(true); @@ -442,7 +442,7 @@ static void parse_dives (int log_version, const unsigned char *buf, unsigned int free(dive); } -int try_to_open_liquivision(const char *filename, struct memblock *mem) +int try_to_open_liquivision(const char *filename, struct memblock *mem, struct dive_table *table) { UNUSED(filename); const unsigned char *buf = mem->buffer; @@ -466,7 +466,7 @@ int try_to_open_liquivision(const char *filename, struct memblock *mem) } ptr += 4; - parse_dives(log_version, buf + ptr, buf_size - ptr); + parse_dives(log_version, buf + ptr, buf_size - ptr, table); return 1; } diff --git a/desktop-widgets/divelogimportdialog.cpp b/desktop-widgets/divelogimportdialog.cpp index 967330277..4d092bb8c 100644 --- a/desktop-widgets/divelogimportdialog.cpp +++ b/desktop-widgets/divelogimportdialog.cpp @@ -844,9 +844,7 @@ void DiveLogImportDialog::on_buttonBox_accepted() if (ui->knownImports->currentText() != "Manual import") { for (int i = 0; i < fileNames.size(); ++i) { if (ui->knownImports->currentText() == "Seabear CSV") { - - parse_seabear_log(qPrintable(fileNames[i])); - + parse_seabear_log(qPrintable(fileNames[i]), &dive_table); } else { char *params[49]; int pnr = 0; @@ -862,7 +860,8 @@ void DiveLogImportDialog::on_buttonBox_accepted() } pnr = setup_csv_params(r, params, pnr); parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, - specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv"); + specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", + &dive_table); } } } else { @@ -926,7 +925,7 @@ void DiveLogImportDialog::on_buttonBox_accepted() params[pnr++] = intdup(r.indexOf(tr("Rating"))); params[pnr++] = NULL; - parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1); + parse_manual_file(qPrintable(fileNames[i]), params, pnr - 1, &dive_table); } else { char *params[51]; int pnr = 0; @@ -942,7 +941,8 @@ void DiveLogImportDialog::on_buttonBox_accepted() } pnr = setup_csv_params(r, params, pnr); parse_csv_file(qPrintable(fileNames[i]), params, pnr - 1, - specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv"); + specialCSV.contains(ui->knownImports->currentIndex()) ? qPrintable(CSVApps[ui->knownImports->currentIndex()].name) : "csv", + &dive_table); } } } diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 08b4a07b5..887b96230 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -1758,7 +1758,7 @@ void MainWindow::importTxtFiles(const QStringList fileNames) QFileInfo check_file(csv); if (check_file.exists() && check_file.isFile()) { - if (parse_txt_file(fileNamePtr.data(), csv) == 0) + if (parse_txt_file(fileNamePtr.data(), csv, &dive_table) == 0) csvFiles += fileNames.at(i); } else { csvFiles += fileNamePtr; diff --git a/tests/testparse.cpp b/tests/testparse.cpp index 9f88a19aa..ffda8f9ac 100644 --- a/tests/testparse.cpp +++ b/tests/testparse.cpp @@ -107,7 +107,7 @@ int TestParse::parseCSV(int units, std::string file) params[pnr++] = intdup(-1); params[pnr++] = NULL; - return parse_manual_file(file.c_str(), params, pnr - 1); + return parse_manual_file(file.c_str(), params, pnr - 1, &dive_table); } int TestParse::parseDivingLog() @@ -216,7 +216,7 @@ void TestParse::testParseHUDC() params[pnr++] = NULL; QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/TestDiveSeabearHUDC.csv", - params, pnr - 1, "csv"), + params, pnr - 1, "csv", &dive_table), 0); QCOMPARE(dive_table.nr, 1); @@ -261,7 +261,7 @@ void TestParse::testParseNewFormat() "/dives/") .append(files.at(i)) .toLatin1() - .data()), + .data(), &dive_table), 0); QCOMPARE(dive_table.nr, i + 1); } @@ -367,7 +367,7 @@ int TestParse::parseCSVmanual(int units, std::string file) params[pnr++] = intdup(units); params[pnr++] = NULL; - return parse_manual_file(file.c_str(), params, pnr - 1); + return parse_manual_file(file.c_str(), params, pnr - 1, &dive_table); } void TestParse::exportCSVDiveDetails() @@ -416,7 +416,7 @@ int TestParse::parseCSVprofile(int units, std::string file) params[pnr++] = intdup(units); params[pnr++] = NULL; - return parse_csv_file(file.c_str(), params, pnr - 1, "csv"); + return parse_csv_file(file.c_str(), params, pnr - 1, "csv", &dive_table); } void TestParse::exportCSVDiveProfile() @@ -510,7 +510,7 @@ void TestParse::parseDL7() clear_dive_file_data(); QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/DL7.zxu", - params, pnr - 1, "DL7"), + params, pnr - 1, "DL7", &dive_table), 0); QCOMPARE(dive_table.nr, 3); |