Skip to content

Commit

Permalink
convert garmin_txt to OptionString
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 committed Oct 25, 2024
1 parent d85365f commit 27e016f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
37 changes: 13 additions & 24 deletions garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,15 @@ bool GarminTxtFormat::is_valid_alt(double alt)

/* helpers */

const char*
GarminTxtFormat::get_option_val(const char* option, const char* def)
{
const char* c = (option != nullptr) ? option : def;
return c;
}

void
GarminTxtFormat::init_date_and_time_format()
{
// This is old, and weird, code.. date_time_format is a global that's
// explicitly malloced and freed elsewhere. This isn't very C++ at all,
// but this format is on its deathbead for deprecation.
const char* d = get_option_val(opt_date_format, kDefaultDateFormat);
QString d1 = convert_human_date_format(d);
QString d1 = convert_human_date_format(opt_date_format);

const char* t = get_option_val(opt_time_format, kDefaultTimeFormat);
QString t1 = convert_human_time_format(t);
QString t1 = convert_human_time_format(opt_time_format);

date_time_format = QStringLiteral("%1 %2").arg(d1, t1);
}
Expand Down Expand Up @@ -625,11 +616,11 @@ GarminTxtFormat::track_disp_wpt_cb(const Waypoint* wpt)
void
GarminTxtFormat::garmin_txt_utc_option()
{
if (opt_utc != nullptr) {
if (case_ignore_strcmp(opt_utc.get(), "utc") == 0) {
if (opt_utc) {
if (case_ignore_strcmp(opt_utc, "utc") == 0) {
utc_offs = 0;
} else {
utc_offs = xstrtoi(opt_utc, nullptr, 10);
utc_offs = opt_utc.toInt();
}
utc_offs *= (60 * 60);
gtxt_flags.utc = 1;
Expand All @@ -652,27 +643,25 @@ GarminTxtFormat::wr_init(const QString& fname)
fout = new gpsbabel::TextStream;
fout->open(fname, QIODevice::WriteOnly, MYNAME, "windows-1252");

gtxt_flags.metric = (toupper(*get_option_val(opt_dist, "m")) == 'M');
gtxt_flags.celsius = (toupper(*get_option_val(opt_temp, "c")) == 'C');
gtxt_flags.metric = opt_dist.get().startsWith("m", Qt::CaseInsensitive);
gtxt_flags.celsius = opt_temp.get().startsWith("c", Qt::CaseInsensitive);
init_date_and_time_format();
if (opt_precision) {
precision = xstrtoi(opt_precision, nullptr, 10);
precision = opt_precision.toInt();
if (precision < 0) {
fatal(MYNAME ": Invalid precision (%s)!", qPrintable(opt_precision.get()));
fatal(MYNAME ": Invalid precision (%s)!", qPrintable(opt_precision));
}
}

QString datum_str = get_option_val(opt_datum, nullptr);
QString grid_str = get_option_val(opt_grid, nullptr);

grid_index = grid_lat_lon_dmm;
if (!grid_str.isEmpty()) {
if (QString grid_str = opt_grid; !grid_str.isEmpty()) {
// we don't use OptionString::toInt because we want the conversion status
bool ok;

if (int i = grid_str.toInt(&ok); ok) {
grid_index = (grid_type) i;
if ((grid_index < GRID_INDEX_MIN) || (grid_index > GRID_INDEX_MAX))
fatal(MYNAME ": Grid index out of range (%d..%d)!",
fatal(MYNAME ": Grid index out of range (%d..%d)!\n",
(int)GRID_INDEX_MIN, (int)GRID_INDEX_MAX);
} else {
grid_index = gt_lookup_grid_type(grid_str, MYNAME);
Expand All @@ -687,7 +676,7 @@ GarminTxtFormat::wr_init(const QString& fname)
datum_index = kDatumWGS84;
break;
default:
datum_index = gt_lookup_datum_index(datum_str, MYNAME);
datum_index = gt_lookup_datum_index(opt_datum, MYNAME);
}

garmin_txt_utc_option();
Expand Down
25 changes: 11 additions & 14 deletions garmin_txt.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#include "defs.h"
#include "format.h" // for Format
#include "option.h" // for OptionCString
#include "option.h" // for OptionString
#include "src/core/textstream.h" // for TextStream


Expand Down Expand Up @@ -69,8 +69,6 @@ class GarminTxtFormat : public Format
/* Constants */

static constexpr double kGarminUnknownAlt = 1.0e25;
static constexpr char kDefaultDateFormat[] = "dd/mm/yyyy";
static constexpr char kDefaultTimeFormat[] = "HH:mm:ss";

static const QVector<QString> headers;

Expand Down Expand Up @@ -111,7 +109,6 @@ class GarminTxtFormat : public Format
/* Member Functions */

static bool is_valid_alt(double alt);
static const char* get_option_val(const char* option, const char* def);
void init_date_and_time_format();
void convert_datum(const Waypoint* wpt, double* dest_lat, double* dest_lon) const;
void enum_waypt_cb(const Waypoint* wpt);
Expand Down Expand Up @@ -172,23 +169,23 @@ class GarminTxtFormat : public Format
std::array<QList<std::pair<QString, int>>, unknown_header> header_mapping_info;
QStringList header_column_names;

OptionCString opt_datum;
OptionCString opt_dist;
OptionCString opt_temp;
OptionCString opt_date_format;
OptionCString opt_time_format;
OptionCString opt_precision;
OptionCString opt_utc;
OptionCString opt_grid;
OptionString opt_datum;
OptionString opt_dist;
OptionString opt_temp;
OptionString opt_date_format;
OptionString opt_time_format;
OptionString opt_precision;
OptionString opt_utc;
OptionString opt_grid;

QVector<arglist_t> garmin_txt_args = {
{"date", &opt_date_format, "Read/Write date format (i.e. yyyy/mm/dd)", nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"date", &opt_date_format, "Read/Write date format (i.e. yyyy/mm/dd)", "dd/mm/yyyy", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"datum", &opt_datum, "GPS datum (def. WGS 84)", "WGS 84", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"dist", &opt_dist, "Distance unit [m=metric, s=statute]", "m", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"grid", &opt_grid, "Write position using this grid.", nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"prec", &opt_precision, "Precision of coordinates", "3", ARGTYPE_INT, ARG_NOMINMAX, nullptr},
{"temp", &opt_temp, "Temperature unit [c=Celsius, f=Fahrenheit]", "c", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"time", &opt_time_format, "Read/Write time format (i.e. HH:mm:ss xx)", nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"time", &opt_time_format, "Read/Write time format (i.e. HH:mm:ss xx)", "HH:mm:ss", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
{"utc", &opt_utc, "Write timestamps with offset x to UTC time", nullptr, ARGTYPE_INT, "-23", "+23", nullptr},
};

Expand Down
4 changes: 2 additions & 2 deletions reference/format3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ option gdb roadbook Include major turn points (with description) from calculated

file rwrwrw garmin_txt txt Garmin MapSource - txt (tab delimited) garmin_txt
https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html
option garmin_txt date Read/Write date format (i.e. yyyy/mm/dd) string https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_date
option garmin_txt date Read/Write date format (i.e. yyyy/mm/dd) string dd/mm/yyyy https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_date

option garmin_txt datum GPS datum (def. WGS 84) string WGS 84 https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_datum

Expand All @@ -222,7 +222,7 @@ option garmin_txt prec Precision of coordinates integer 3 https://www.gpsbabel

option garmin_txt temp Temperature unit [c=Celsius, f=Fahrenheit] string c https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_temp

option garmin_txt time Read/Write time format (i.e. HH:mm:ss xx) string https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_time
option garmin_txt time Read/Write time format (i.e. HH:mm:ss xx) string HH:mm:ss https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_time

option garmin_txt utc Write timestamps with offset x to UTC time integer -23 +23 https://www.gpsbabel.org/WEB_DOC_DIR/fmt_garmin_txt.html#fmt_garmin_txt_o_utc

Expand Down

0 comments on commit 27e016f

Please sign in to comment.