Skip to content

Commit 8466bfb

Browse files
authored
refactor option handling (#1351)
* rough in Option class * testo passes * rough in OptionBool * more OptionBool use * more OptionBool * fix isEmpty * store qstrings * dont return const * rename * convert a few lost ARGTYPE_BOOLs to OptionBool. * utilize avalable QString for options. * obsolete argvalptr * Option is abstract class * eliminate special handling of ARGTYPE_BOOL without default in vecs. * fix #982 ... garmin, ozi, xcsv mkshort options whitespace_ok, must_unique broken since 2006. * test to make sure OptionBool iff ARGTYPE_BOOL * use OptionBool for all ARGTYPE_BOOL options. * correct OptionBool type. * delete unused parameterized Option ctors. * format and comment option.h * nodiscard for class Option. * vecs nullptr checks * have Vecs::validate_args check argval isn't nullptr. * catch up includes * clarify comment * fix introduced bug caught by tidy.
1 parent f32f4da commit 8466bfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+837
-578
lines changed

arcdist.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cmath> // for round
2525
#include <cstdio> // for printf, sscanf
2626
#include <cstdlib> // for strtod
27+
#include <tuple> // for tie, tuple
2728

2829
#include <QByteArray> // for QByteArray
2930
#include <QString> // for QString
@@ -112,7 +113,7 @@ void ArcDistanceFilter::process()
112113
QString line;
113114

114115
gpsbabel::TextStream stream;
115-
stream.open(arcfileopt, QIODevice::ReadOnly, MYNAME);
116+
stream.open(arcfileopt.get(), QIODevice::ReadOnly, MYNAME);
116117

117118
auto* arcpt1 = new Waypoint;
118119
auto* arcpt2 = new Waypoint;
@@ -157,7 +158,7 @@ void ArcDistanceFilter::process()
157158
if (wp->extra_data) {
158159
auto* ed = (extra_data*) wp->extra_data;
159160
wp->extra_data = nullptr;
160-
if ((ed->distance >= pos_dist) == (exclopt == nullptr)) {
161+
if ((ed->distance >= pos_dist) == !exclopt) {
161162
wp->wpt_flags.marked_for_deletion = 1;
162163
removed++;
163164
} else if (projectopt) {

arcdist.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
#ifndef ARCDIST_H_INCLUDED_
2323
#define ARCDIST_H_INCLUDED_
2424

25-
#include <QVector> // for QVector
25+
#include <QList> // for QList
26+
#include <QString> // for QString
27+
#include <QVector> // for QVector
2628

2729
#include "defs.h" // for ARG_NOMINMAX, ARGTYPE_BOOL, Waypoint (ptr only)
2830
#include "filter.h" // for Filter
31+
#include "option.h" // for OptionBool, OptionCString
2932

3033
#if FILTERS_ENABLED
3134

@@ -58,13 +61,13 @@ class ArcDistanceFilter:public Filter
5861
/* Data Members */
5962

6063
double pos_dist{};
61-
char* distopt = nullptr;
62-
char* arcfileopt = nullptr;
63-
char* rteopt = nullptr;
64-
char* trkopt = nullptr;
65-
char* exclopt = nullptr;
66-
char* ptsopt = nullptr;
67-
char* projectopt = nullptr;
64+
OptionCString distopt;
65+
OptionCString arcfileopt;
66+
OptionBool rteopt;
67+
OptionBool trkopt;
68+
OptionBool exclopt;
69+
OptionBool ptsopt;
70+
OptionBool projectopt;
6871

6972
QVector<arglist_t> args = {
7073
{

bend.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
#ifndef BEND_H_INCLUDED_
2424
#define BEND_H_INCLUDED_
2525

26-
#include <QVector> // for QVector
26+
#include <QList> // for QList
27+
#include <QString> // for QString
28+
#include <QVector> // for QVector
2729

2830
#include "defs.h" // for route_head (ptr only), ARGTYPE_FLOAT, ARG_NOMINMAX
2931
#include "filter.h" // for Filter
32+
#include "option.h" // for OptionCString
3033

3134
#if FILTERS_ENABLED
3235

@@ -42,8 +45,8 @@ class BendFilter:public Filter
4245
void deinit() override;
4346

4447
private:
45-
char* distopt = nullptr;
46-
char* minangleopt = nullptr;
48+
OptionCString distopt;
49+
OptionCString minangleopt;
4750

4851
double maxDist{};
4952
double minAngle{};

defs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "geocache.h" // for Geocache
4747
#include "formspec.h" // for FormatSpecificData
4848
#include "inifile.h" // for inifile_t
49+
#include "option.h"
4950
#include "session.h" // for session_t
5051
#include "src/core/datetime.h" // for DateTime
5152

@@ -873,13 +874,13 @@ extern posn_status tracking_status;
873874

874875
struct arglist_t {
875876
const QString argstring;
876-
char** const argval{nullptr};
877+
Option* const argval{nullptr};
877878
const QString helpstring;
878879
const QString defaultvalue;
879880
const uint32_t argtype{ARGTYPE_UNKNOWN};
880881
const QString minvalue; /* minimum value for numeric options */
881882
const QString maxvalue; /* maximum value for numeric options */
882-
char* argvalptr{nullptr}; /* !!! internal helper. Not used in definitions !!! */
883+
char* unused{nullptr}; /* TODO: delete from initialization lists */
883884
};
884885

885886
enum ff_type {

dg-100.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,12 @@ Dg100Format::rd_deinit()
692692
void
693693
Dg100Format::read()
694694
{
695-
if (*erase_only == '1') {
695+
if (erase_only) {
696696
dg100_erase();
697697
return;
698698
}
699699
dg100_getfiles();
700-
if (*erase == '1') {
700+
if (erase) {
701701
dg100_erase();
702702
}
703703
}

dg-100.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@
3030
#ifndef DG100_H_INCLUDED_
3131
#define DG100_H_INCLUDED_
3232

33-
#include <cstdint> // for uint8_t, int16_t, uint16_t
34-
#include <cstdio> // for size_t
33+
#include <cstdint> // for uint8_t, int16_t, uint16_t
34+
#include <cstdio> // for size_t
3535

36-
#include <QDateTime> // for QDateTime
37-
#include <QList> // for QList
38-
#include <QString> // for QString
39-
#include <QVector> // for QVector
36+
#include <QDateTime> // for QDateTime
37+
#include <QList> // for QList
38+
#include <QString> // for QString
39+
#include <QVector> // for QVector
4040

4141
#include "defs.h"
42-
#include "format.h" // for Format
43-
#include "gbfile.h" // for gbfile
42+
#include "format.h" // for Format
43+
#include "gbfile.h" // for gbfile
44+
#include "option.h" // for OptionBool
4445

4546

4647
class Dg100Format : public Format
@@ -141,8 +142,8 @@ class Dg100Format : public Format
141142

142143
/* GPSBabel integration */
143144

144-
char* erase{nullptr};
145-
char* erase_only{nullptr};
145+
OptionBool erase;
146+
OptionBool erase_only;
146147

147148
QVector<arglist_t> dg100_args = {
148149
{

discard.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,25 @@ void DiscardFilter::init()
157157
}
158158

159159
if (nameopt) {
160-
name_regex = generateRegExp(nameopt);
160+
name_regex = generateRegExp(nameopt.get());
161161
if (!name_regex.isValid()) {
162162
fatal(FatalMsg() << "discard: matchname option is an invalid expression.");
163163
}
164164
}
165165
if (descopt) {
166-
desc_regex = generateRegExp(descopt);
166+
desc_regex = generateRegExp(descopt.get());
167167
if (!desc_regex.isValid()) {
168168
fatal(FatalMsg() << "discard: matchdesc option is an invalid expression.");
169169
}
170170
}
171171
if (cmtopt) {
172-
cmt_regex = generateRegExp(cmtopt);
172+
cmt_regex = generateRegExp(cmtopt.get());
173173
if (!cmt_regex.isValid()) {
174174
fatal(FatalMsg() << "discard: matchcmt option is an invalid expression.");
175175
}
176176
}
177177
if (iconopt) {
178-
icon_regex = generateRegExp(iconopt);
178+
icon_regex = generateRegExp(iconopt.get());
179179
if (!icon_regex.isValid()) {
180180
fatal(FatalMsg() << "discard: matchicon option is an invalid expression.");
181181
}

discard.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
#ifndef DISCARD_H_INCLUDED_
2323
#define DISCARD_H_INCLUDED_
2424

25+
#include <QList> // for QList
2526
#include <QRegularExpression> // for QRegularExpression
2627
#include <QString> // for QString
2728
#include <QVector> // for QVector
2829

2930
#include "defs.h" // for arglist_t, ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_STRING, ARGTYPE_BOOL, ARGTYPE_INT, ARGTYPE_FLOAT, route_head, ARGTYPE_END_REQ, Waypoint, gpsdata_type
3031
#include "filter.h" // for Filter
32+
#include "option.h" // for OptionCString, OptionBool
3133

3234
#if FILTERS_ENABLED
3335
class DiscardFilter:public Filter
@@ -48,21 +50,21 @@ class DiscardFilter:public Filter
4850

4951
/* Data Members */
5052

51-
char* hdopopt = nullptr;
52-
char* vdopopt = nullptr;
53-
char* andopt = nullptr;
54-
char* satopt = nullptr;
55-
char* fixnoneopt = nullptr;
56-
char* fixunknownopt = nullptr;
57-
char* eleminopt = nullptr;
58-
char* elemaxopt = nullptr;
59-
char* nameopt = nullptr;
53+
OptionCString hdopopt;
54+
OptionCString vdopopt;
55+
OptionBool andopt;
56+
OptionCString satopt;
57+
OptionBool fixnoneopt;
58+
OptionBool fixunknownopt;
59+
OptionCString eleminopt;
60+
OptionCString elemaxopt;
61+
OptionCString nameopt;
6062
QRegularExpression name_regex;
61-
char* descopt = nullptr;
63+
OptionCString descopt;
6264
QRegularExpression desc_regex;
63-
char* cmtopt = nullptr;
65+
OptionCString cmtopt;
6466
QRegularExpression cmt_regex;
65-
char* iconopt = nullptr;
67+
OptionCString iconopt;
6668
QRegularExpression icon_regex;
6769

6870
double hdopf{};

duplicate.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#ifndef DUPLICATE_H_INCLUDED_
2323
#define DUPLICATE_H_INCLUDED_
2424

25+
#include <QList> // for QList
2526
#include <QString> // for QString
2627
#include <QVector> // for QVector
2728

2829
#include "defs.h" // for ARGTYPE_BOOL, ARG_NOMINMAX, Waypoint (ptr only)
2930
#include "filter.h" // for Filter
31+
#include "option.h" // for OptionBool
3032

3133
#if FILTERS_ENABLED
3234

@@ -41,10 +43,10 @@ class DuplicateFilter:public Filter
4143
void process() override;
4244

4345
private:
44-
char* snopt = nullptr;
45-
char* lcopt = nullptr;
46-
char* purge_duplicates = nullptr;
47-
char* correct_coords = nullptr;
46+
OptionBool snopt;
47+
OptionBool lcopt;
48+
OptionBool purge_duplicates;
49+
OptionBool correct_coords;
4850

4951
QVector<arglist_t> args = {
5052
{

exif.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ ExifFormat::exif_find_wpt_by_name(const Waypoint* wpt)
11781178
{
11791179
if (exif_wpt_ref != nullptr) {
11801180
return;
1181-
} else if ((wpt->shortname != nullptr) && (case_ignore_strcmp(wpt->shortname, opt_name) == 0)) {
1181+
} else if ((wpt->shortname != nullptr) && (case_ignore_strcmp(wpt->shortname, opt_name.get()) == 0)) {
11821182
exif_wpt_ref = wpt;
11831183
}
11841184
}
@@ -1502,7 +1502,7 @@ ExifFormat::wr_deinit()
15021502
gbfclose(fout_);
15031503

15041504
if (exif_success) {
1505-
if (*opt_overwrite == '1') {
1505+
if (opt_overwrite) {
15061506
QFile::remove(exif_fout_name);
15071507
QFile::rename(tmpname, exif_fout_name);
15081508
}
@@ -1530,7 +1530,7 @@ ExifFormat::write()
15301530
track_disp_all(nullptr, nullptr, exif_find_wpt_by_name_lambda);
15311531
}
15321532
if (exif_wpt_ref == nullptr) {
1533-
warning(MYNAME ": No matching point with name \"%s\" found.\n", opt_name);
1533+
warning(MYNAME ": No matching point with name \"%s\" found.\n", qPrintable(opt_name.get()));
15341534
}
15351535
} else {
15361536
auto exif_find_wpt_by_time_lambda = [this](const Waypoint* waypointp)->void {

exif.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@
3636
#ifndef EXIF_H_INCLUDED_
3737
#define EXIF_H_INCLUDED_
3838

39-
#include <QDate> // for QDate
40-
#include <QDateTime> // for QDateTime
41-
#include <QList> // for QList
42-
#include <QString> // for QString
43-
#include <QTime> // for QTime
44-
#include <QVariant> // for QVariant
45-
#include <QVector> // for QVector
39+
#include <QByteArray> // for QByteArray
40+
#include <QDate> // for QDate
41+
#include <QDateTime> // for QDateTime
42+
#include <QList> // for QList
43+
#include <QString> // for QString
44+
#include <QTime> // for QTime
45+
#include <QVariant> // for QVariant
46+
#include <QVector> // for QVector
4647

47-
#include <cstdint> // for uint32_t, uint16_t, uint8_t, int16_t, int32_t
48+
#include <cstdint> // for uint32_t, uint16_t, uint8_t, int16_t, int32_t
4849

49-
#include "defs.h" // for arglist_t, ff_cap, Waypoint, ARG_NOMINMAX, ARGTYPE_BOOL, ff_cap_none, ARGTYPE_INT, ARGTYPE_STRING, ff_cap_read, ff_cap_write, ff_type, ff_type_file
50-
#include "format.h" // for Format
51-
#include "gbfile.h" // for gbfile, gbsize_t
50+
#include "defs.h" // for arglist_t, ff_cap, Waypoint, ARG_NOMINMAX, ARGTYPE_BOOL, ff_cap_none, ARGTYPE_INT, ARGTYPE_STRING, ff_cap_read, ff_cap_write, ff_type, ff_type_file
51+
#include "format.h" // for Format
52+
#include "gbfile.h" // for gbfile, gbsize_t
53+
#include "option.h" // for OptionCString, OptionBool
5254

5355

5456
class ExifFormat : public Format
@@ -201,11 +203,11 @@ class ExifFormat : public Format
201203
char exif_success{};
202204
QString exif_fout_name;
203205

204-
char* opt_filename{};
205-
char* opt_overwrite{};
206-
char* opt_frame{};
207-
char* opt_name{};
208-
char* opt_offsettime{};
206+
OptionBool opt_filename;
207+
OptionBool opt_overwrite;
208+
OptionCString opt_frame;
209+
OptionCString opt_name;
210+
OptionCString opt_offsettime;
209211

210212
QVector<arglist_t> exif_args = {
211213
{ "filename", &opt_filename, "Set waypoint name to source filename", "Y", ARGTYPE_BOOL, ARG_NOMINMAX, nullptr },

filter_vecs.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <algorithm> // for sort
3232
#include <cassert> // for assert
3333
#include <cstdio> // for printf
34+
#include <type_traits> // for is_base_of
3435

3536
#include "arcdist.h" // for ArcDistanceFilter
3637
#include "bend.h" // for BendFilter
@@ -216,9 +217,9 @@ void FilterVecs::prepare_filter(const fltinfo_t& fltdata)
216217
qtemp = inifile_readstr(global_opts.inifile, "Common filter settings", arg.argstring);
217218
}
218219
if (qtemp.isNull()) {
219-
Vecs::assign_option(fltdata.fltname, &arg, arg.defaultvalue);
220+
Vecs::assign_option(fltdata.fltname, arg, arg.defaultvalue);
220221
} else {
221-
Vecs::assign_option(fltdata.fltname, &arg, qtemp);
222+
Vecs::assign_option(fltdata.fltname, arg, qtemp);
222223
}
223224
}
224225
}
@@ -230,7 +231,7 @@ void FilterVecs::prepare_filter(const fltinfo_t& fltdata)
230231
for (auto& arg : *args) {
231232
const QString opt = Vecs::get_option(fltdata.options, arg.argstring);
232233
if (!opt.isNull()) {
233-
Vecs::assign_option(fltdata.fltname, &arg, opt);
234+
Vecs::assign_option(fltdata.fltname, arg, opt);
234235
}
235236
}
236237
}
@@ -274,9 +275,6 @@ void FilterVecs::init_filter_vec(Filter* flt)
274275
QVector<arglist_t>* args = flt->get_args();
275276
if (args && !args->isEmpty()) {
276277
assert(args->isDetached());
277-
for (auto& arg : *args) {
278-
arg.argvalptr = nullptr;
279-
}
280278
}
281279
}
282280

0 commit comments

Comments
 (0)