Skip to content

Commit

Permalink
enforce exclusivity among sort options (GPSBabel#970)
Browse files Browse the repository at this point in the history
* enforce exclusive sort options.

* use default brace inits for sort members.
  • Loading branch information
tsteven4 authored Jan 2, 2023
1 parent 0ccae9f commit 7b321cd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 78 deletions.
130 changes: 68 additions & 62 deletions sort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,93 +70,99 @@ bool SortFilter::sort_comp_rh_by_number(const route_head* a, const route_head* b

void SortFilter::process()
{
if (wpt_sort_mode != SortModeWpt::none) {
switch (wpt_sort_mode) {
case SortModeWpt::description:
waypt_sort(sort_comp_wpt_by_description);
break;
case SortModeWpt::gcid:
waypt_sort(sort_comp_wpt_by_gcid);
break;
case SortModeWpt::shortname:
waypt_sort(sort_comp_wpt_by_shortname);
break;
case SortModeWpt::time:
waypt_sort(sort_comp_wpt_by_time);
break;
default:
fatal(MYNAME ": unknown waypoint sort mode.");
}
switch (wpt_sort_mode) {
case SortModeWpt::none:
break;
case SortModeWpt::description:
waypt_sort(sort_comp_wpt_by_description);
break;
case SortModeWpt::gcid:
waypt_sort(sort_comp_wpt_by_gcid);
break;
case SortModeWpt::shortname:
waypt_sort(sort_comp_wpt_by_shortname);
break;
case SortModeWpt::time:
waypt_sort(sort_comp_wpt_by_time);
break;
default:
fatal(MYNAME ": unknown waypoint sort mode.");
}

if (rte_sort_mode != SortModeRteHd::none) {
switch (rte_sort_mode) {
case SortModeRteHd::description:
route_sort(sort_comp_rh_by_description);
break;
case SortModeRteHd::name:
route_sort(sort_comp_rh_by_name);
break;
case SortModeRteHd::number:
route_sort(sort_comp_rh_by_number);
break;
default:
fatal(MYNAME ": unknown route sort mode.");
}
switch (rte_sort_mode) {
case SortModeRteHd::none:
break;
case SortModeRteHd::description:
route_sort(sort_comp_rh_by_description);
break;
case SortModeRteHd::name:
route_sort(sort_comp_rh_by_name);
break;
case SortModeRteHd::number:
route_sort(sort_comp_rh_by_number);
break;
default:
fatal(MYNAME ": unknown route sort mode.");
}
if (trk_sort_mode != SortModeRteHd::none) {
switch (trk_sort_mode) {
case SortModeRteHd::description:
track_sort(sort_comp_rh_by_description);
break;
case SortModeRteHd::name:
track_sort(sort_comp_rh_by_name);
break;
case SortModeRteHd::number:
track_sort(sort_comp_rh_by_number);
break;
default:
fatal(MYNAME ": unknown track sort mode.");
}

switch (trk_sort_mode) {
case SortModeRteHd::none:
break;
case SortModeRteHd::description:
track_sort(sort_comp_rh_by_description);
break;
case SortModeRteHd::name:
track_sort(sort_comp_rh_by_name);
break;
case SortModeRteHd::number:
track_sort(sort_comp_rh_by_number);
break;
default:
fatal(MYNAME ": unknown track sort mode.");
}
}

void SortFilter::init()
{
// sort waypts by
if (opt_sm_description) {
if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
wpt_sort_mode = SortModeWpt::none;
} else if (opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
wpt_sort_mode = SortModeWpt::description;
}
if (opt_sm_gcid) {
} else if (!opt_sm_description && opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
wpt_sort_mode = SortModeWpt::gcid;
}
if (opt_sm_shortname) {
} else if (!opt_sm_description && !opt_sm_gcid && opt_sm_shortname && !opt_sm_time) {
wpt_sort_mode = SortModeWpt::shortname;
}
if (opt_sm_time) {
} else if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && opt_sm_time) {
wpt_sort_mode = SortModeWpt::time;
} else {
fatal(MYNAME ": At most one of the options description, gcid, shortname and time may be selected.");
}

// sort routes by
if (opt_sm_rtedesc) {
if (!opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) {
rte_sort_mode = SortModeRteHd::none;
} else if (opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) {
rte_sort_mode = SortModeRteHd::description;
}
if (opt_sm_rtename) {
} else if (!opt_sm_rtedesc && opt_sm_rtename && !opt_sm_rtenum) {
rte_sort_mode = SortModeRteHd::name;
}
if (opt_sm_rtenum) {
} else if (!opt_sm_rtedesc && !opt_sm_rtename && opt_sm_rtenum) {
rte_sort_mode = SortModeRteHd::number;
} else {
fatal(MYNAME ": At most one of the options rtedesc, rtename and rtenum may be selected.");
}

// sort tracks by
if (opt_sm_trkdesc) {
if (!opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) {
trk_sort_mode = SortModeRteHd::none;
} else if (opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) {
trk_sort_mode = SortModeRteHd::description;
}
if (opt_sm_trkname) {
} else if (!opt_sm_trkdesc && opt_sm_trkname && !opt_sm_trknum) {
trk_sort_mode = SortModeRteHd::name;
}
if (opt_sm_trknum) {
} else if (!opt_sm_trkdesc && !opt_sm_trkname && opt_sm_trknum) {
trk_sort_mode = SortModeRteHd::number;
} else {
fatal(MYNAME ": At most one of the options trkdesc, trkname and trknum may be selected.");
}
}

Expand Down
49 changes: 33 additions & 16 deletions sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
#include <QString> // for QString
#include <QVector> // for QVector

#include "defs.h" // for ARGTYPE_BOOL, ARG_NOMINMAX, arglist_t, ARG_TERMI...
#include "defs.h" // for arglist_t, ARGTYPE_BOOL, ARG_NOMINMAX, Waypoint
#include "filter.h" // for Filter


#if FILTERS_ENABLED

class SortFilter:public Filter
{
public:

/* Member Functions */

QVector<arglist_t>* get_args() override
{
return &args;
Expand All @@ -41,6 +45,9 @@ class SortFilter:public Filter
void process() override;

private:

/* Types */

enum class SortModeWpt {
none,
description,
Expand All @@ -49,21 +56,39 @@ class SortFilter:public Filter
time
};

SortModeWpt wpt_sort_mode = SortModeWpt::none; /* How are we sorting these? */

enum class SortModeRteHd {
none,
description,
name,
number
};

SortModeRteHd rte_sort_mode = SortModeRteHd::none; /* How are we sorting these? */
SortModeRteHd trk_sort_mode = SortModeRteHd::none; /* How are we sorting these? */
/* Member Functions */

static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b);
static bool sort_comp_rh_by_description(const route_head* a, const route_head* b);
static bool sort_comp_rh_by_name(const route_head* a, const route_head* b);
static bool sort_comp_rh_by_number(const route_head* a, const route_head* b);

/* Data Members */

char* opt_sm_gcid{}, *opt_sm_shortname{}, *opt_sm_description{}, *opt_sm_time{};
char* opt_sm_rtenum{}, *opt_sm_rtename{}, *opt_sm_rtedesc{};
char* opt_sm_trknum{}, *opt_sm_trkname{}, *opt_sm_trkdesc{};
SortModeWpt wpt_sort_mode{SortModeWpt::none}; /* How are we sorting these? */
SortModeRteHd rte_sort_mode{SortModeRteHd::none}; /* How are we sorting these? */
SortModeRteHd trk_sort_mode{SortModeRteHd::none}; /* How are we sorting these? */

char* opt_sm_gcid{};
char* opt_sm_shortname{};
char* opt_sm_description{};
char* opt_sm_time{};
char* opt_sm_rtenum{};
char* opt_sm_rtename{};
char* opt_sm_rtedesc{};
char* opt_sm_trknum{};
char* opt_sm_trkname{};
char* opt_sm_trkdesc{};

QVector<arglist_t> args = {
{
Expand Down Expand Up @@ -108,14 +133,6 @@ class SortFilter:public Filter
},
};

static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b);
static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b);
static bool sort_comp_rh_by_description(const route_head* a, const route_head* b);
static bool sort_comp_rh_by_name(const route_head* a, const route_head* b);
static bool sort_comp_rh_by_number(const route_head* a, const route_head* b);

};
#endif // FILTERS_ENABLED
#endif // SORT_H_INCLUDED_

0 comments on commit 7b321cd

Please sign in to comment.