code cleanup: add empty table structures

It seemed to make sense to combine all three types in one commit.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2020-01-08 21:25:02 -08:00
parent 70cabb968c
commit e1cd055111
13 changed files with 47 additions and 41 deletions

View file

@ -469,10 +469,10 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
// this only matters if undoit were called before redoit // this only matters if undoit were called before redoit
currentDive = nullptr; currentDive = nullptr;
struct dive_table dives_to_add = { 0, 0, 0 }; struct dive_table dives_to_add = empty_dive_table;
struct dive_table dives_to_remove = { 0, 0, 0 }; struct dive_table dives_to_remove = empty_dive_table;
struct trip_table trips_to_add = { 0, 0, 0 }; struct trip_table trips_to_add = empty_trip_table;
struct dive_site_table sites_to_add = { 0, 0, 0 }; struct dive_site_table sites_to_add = empty_dive_site_table;
process_imported_dives(dives, trips, sites, flags, &dives_to_add, &dives_to_remove, &trips_to_add, &sites_to_add); process_imported_dives(dives, trips, sites, flags, &dives_to_add, &dives_to_remove, &trips_to_add, &sites_to_add);
// Add trips to the divesToAdd.trips structure // Add trips to the divesToAdd.trips structure

View file

@ -133,6 +133,8 @@ typedef struct dive_table {
struct dive **dives; struct dive **dives;
} dive_table_t; } dive_table_t;
static const dive_table_t empty_dive_table = { 0, 0, (struct dive **)0 };
struct picture; struct picture;
struct dive_site; struct dive_site;
struct dive_site_table; struct dive_site_table;

View file

@ -992,10 +992,10 @@ static bool merge_dive_tables(struct dive_table *dives_from, struct dive_table *
void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, struct dive_site_table *import_sites_table, int flags) void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table, struct dive_site_table *import_sites_table, int flags)
{ {
int i, idx; int i, idx;
struct dive_table dives_to_add = { 0 }; struct dive_table dives_to_add = empty_dive_table;
struct dive_table dives_to_remove = { 0 }; struct dive_table dives_to_remove = empty_dive_table;
struct trip_table trips_to_add = { 0 }; struct trip_table trips_to_add = empty_trip_table;
struct dive_site_table dive_sites_to_add = { 0 }; struct dive_site_table dive_sites_to_add = empty_dive_site_table;
/* Process imported dives and generate lists of dives /* Process imported dives and generate lists of dives
* to-be-added and to-be-removed */ * to-be-added and to-be-removed */
@ -1121,7 +1121,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
/* If the caller didn't pass an import_trip_table because all /* If the caller didn't pass an import_trip_table because all
* dives are tripless, provide a local table. This may be * dives are tripless, provide a local table. This may be
* necessary if the trips are autogrouped */ * necessary if the trips are autogrouped */
struct trip_table local_trip_table = { 0 }; struct trip_table local_trip_table = empty_trip_table;
if (!import_trip_table) if (!import_trip_table)
import_trip_table = &local_trip_table; import_trip_table = &local_trip_table;

View file

@ -31,6 +31,8 @@ typedef struct dive_site_table {
struct dive_site **dive_sites; struct dive_site **dive_sites;
} dive_site_table_t; } dive_site_table_t;
static const dive_site_table_t empty_dive_site_table = { 0, 0, (struct dive_site **)0 };
extern struct dive_site_table dive_site_table; extern struct dive_site_table dive_site_table;
static inline struct dive_site *get_dive_site(int nr, struct dive_site_table *ds_table) static inline struct dive_site *get_dive_site(int nr, struct dive_site_table *ds_table)

View file

@ -24,6 +24,8 @@ typedef struct trip_table {
struct dive_trip **trips; struct dive_trip **trips;
} trip_table_t; } trip_table_t;
static const trip_table_t empty_trip_table = { 0, 0, (struct dive_trip **)0 };
extern void add_dive_to_trip(struct dive *, dive_trip_t *); extern void add_dive_to_trip(struct dive *, dive_trip_t *);
extern struct dive_trip *unregister_dive_from_trip(struct dive *dive); extern struct dive_trip *unregister_dive_from_trip(struct dive *dive);
extern void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg); extern void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg);

View file

@ -904,9 +904,9 @@ int DiveLogImportDialog::parseTxtHeader(QString fileName, char **params, int pnr
void DiveLogImportDialog::on_buttonBox_accepted() void DiveLogImportDialog::on_buttonBox_accepted()
{ {
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
QStringList r = resultModel->result(); QStringList r = resultModel->result();
if (ui->knownImports->currentText() != "Manual import") { if (ui->knownImports->currentText() != "Manual import") {
for (int i = 0; i < fileNames.size(); ++i) { for (int i = 0; i < fileNames.size(); ++i) {

View file

@ -65,7 +65,7 @@ void DivesiteImportDialog::on_cancel_clicked()
void DivesiteImportDialog::on_ok_clicked() void DivesiteImportDialog::on_ok_clicked()
{ {
// delete non-selected dive sites // delete non-selected dive sites
struct dive_site_table selectedSites = { 0 }; struct dive_site_table selectedSites = empty_dive_site_table;
for (int i = 0; i < importedSites.nr; i++) for (int i = 0; i < importedSites.nr; i++)
if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) { if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) {
struct dive_site *newSite = alloc_dive_site(); struct dive_site *newSite = alloc_dive_site();

View file

@ -1662,9 +1662,9 @@ void MainWindow::importFiles(const QStringList fileNames)
return; return;
QByteArray fileNamePtr; QByteArray fileNamePtr;
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
for (int i = 0; i < fileNames.size(); ++i) { for (int i = 0; i < fileNames.size(); ++i) {
fileNamePtr = QFile::encodeName(fileNames.at(i)); fileNamePtr = QFile::encodeName(fileNames.at(i));
@ -1757,9 +1757,9 @@ void MainWindow::on_actionImportDiveSites_triggered()
return; return;
updateLastUsedDir(QFileInfo(fileNames[0]).dir().path()); updateLastUsedDir(QFileInfo(fileNames[0]).dir().path());
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
for (const QString &s: fileNames) { for (const QString &s: fileNames) {
QByteArray fileNamePtr = QFile::encodeName(s); QByteArray fileNamePtr = QFile::encodeName(s);

View file

@ -453,9 +453,9 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
break; break;
} }
/* parse file and import dives */ /* parse file and import dives */
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips, &sites); parse_file(QFile::encodeName(zipFile.fileName()), &table, &trips, &sites);
Command::importDives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de")); Command::importDives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));

View file

@ -380,9 +380,9 @@ void QMLManager::updateAllGlobalLists()
void QMLManager::mergeLocalRepo() void QMLManager::mergeLocalRepo()
{ {
char *filename = NOCLOUD_LOCALSTORAGE; char *filename = NOCLOUD_LOCALSTORAGE;
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
parse_file(filename, &table, &trips, &sites); parse_file(filename, &table, &trips, &sites);
add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
} }

View file

@ -3,8 +3,8 @@
#include "core/divelist.h" #include "core/divelist.h"
DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o), DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o),
diveTable({ 0 }), diveTable(empty_dive_table),
sitesTable({ 0 }) sitesTable(empty_dive_site_table)
{ {
connect(&thread, &QThread::finished, this, &DiveImportedModel::downloadThreadFinished); connect(&thread, &QThread::finished, this, &DiveImportedModel::downloadThreadFinished);
} }
@ -147,8 +147,8 @@ std::pair<struct dive_table, struct dive_site_table> DiveImportedModel::consumeT
beginResetModel(); beginResetModel();
// Move tables to result // Move tables to result
struct dive_table dives = { 0 }; struct dive_table dives = empty_dive_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
move_dive_table(&diveTable, &dives); move_dive_table(&diveTable, &dives);
move_dive_site_table(&sitesTable, &sites); move_dive_site_table(&sitesTable, &sites);

View file

@ -21,9 +21,9 @@ void TestMerge::testMergeEmpty()
/* /*
* check that we correctly merge mixed cylinder dives * check that we correctly merge mixed cylinder dives
*/ */
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips, &sites), 0);
add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips, &sites), 0);
@ -47,9 +47,9 @@ void TestMerge::testMergeBackwards()
/* /*
* check that we correctly merge mixed cylinder dives * check that we correctly merge mixed cylinder dives
*/ */
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test48.xml", &table, &trips, &sites), 0);
add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47.xml", &table, &trips, &sites), 0);

View file

@ -13,9 +13,9 @@ void TestRenumber::setup()
void TestRenumber::testMerge() void TestRenumber::testMerge()
{ {
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47b.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47b.xml", &table, &trips, &sites), 0);
add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(dive_table.nr, 1); QCOMPARE(dive_table.nr, 1);
@ -25,9 +25,9 @@ void TestRenumber::testMerge()
void TestRenumber::testMergeAndAppend() void TestRenumber::testMergeAndAppend()
{ {
struct dive_table table = { 0 }; struct dive_table table = empty_dive_table;
struct trip_table trips = { 0 }; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = { 0 }; struct dive_site_table sites = empty_dive_site_table;
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47c.xml", &table, &trips, &sites), 0); QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/test47c.xml", &table, &trips, &sites), 0);
add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
QCOMPARE(dive_table.nr, 2); QCOMPARE(dive_table.nr, 2);