Parser: parse text-based files into arbitrary table

In d815e0c947 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>
This commit is contained in:
Berthold Stoeger 2018-09-28 13:59:01 +02:00 committed by Dirk Hohndel
parent a5db03c2b3
commit 32a4ce6169
10 changed files with 52 additions and 51 deletions

View file

@ -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

View file

@ -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++;
}

View file

@ -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;
}

View file

@ -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);

View file

@ -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)

View file

@ -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
}

View file

@ -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;
}

View file

@ -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);
}
}
}

View file

@ -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;

View file

@ -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);