Skip to content

Commit

Permalink
mkshort becomes a class (#1173)
Browse files Browse the repository at this point in the history
* make mkshort a class

* add new file mkshort.h

* refactor names

* use bools with mkshort setters
  • Loading branch information
tsteven4 authored Sep 6, 2023
1 parent 1ccfdb6 commit 45268f7
Show file tree
Hide file tree
Showing 27 changed files with 349 additions and 368 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ set(HEADERS
legacyformat.h
igc.h
lowranceusr.h
mkshort.h
nmea.h
osm.h
qstarz_bl_1000.h
Expand Down
23 changes: 0 additions & 23 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,29 +838,6 @@ using ff_exit = void (*)();
using ff_writeposn = void (*)(Waypoint*);
using ff_readposn = Waypoint* (*)(posn_status*);

/*
* All shortname functions take a shortname handle as the first arg.
* This is an opaque pointer. Callers must not fondle the contents of it.
*/
// This is a crutch until the new C++ shorthandle goes in.

struct mkshort_handle_imp; // forward declare, definition in mkshort.cc
using short_handle = mkshort_handle_imp*;

QByteArray mkshort(short_handle, const QByteArray&, bool);
QString mkshort(short_handle, const QString&);
short_handle mkshort_new_handle();
QString mkshort_from_wpt(short_handle h, const Waypoint* wpt);
void mkshort_del_handle(short_handle* h);
void setshort_length(short_handle, int n);
void setshort_badchars(short_handle, const char*);
void setshort_goodchars(short_handle, const char*);
void setshort_mustupper(short_handle, int n);
void setshort_mustuniq(short_handle, int n);
void setshort_whitespace_ok(short_handle, int n);
void setshort_repeating_whitespace_ok(short_handle, int n);
void setshort_defname(short_handle, const char* s);

#define ARGTYPE_UNKNOWN 0x00000000U
#define ARGTYPE_INT 0x00000001U
#define ARGTYPE_FLOAT 0x00000002U
Expand Down
44 changes: 22 additions & 22 deletions garmin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@
#include "jeeps/gpsserial.h" // for DEFAULT_BAUD
#include "jeeps/gpsutil.h" // for GPS_User, GPS_Enable_Diagnose, GPS_E...
#include "src/core/datetime.h" // for DateTime
#include "mkshort.h" // for MakeShort


#define MYNAME "GARMIN"
static const char* portname;
static short_handle mkshort_handle;
static MakeShort* mkshort_handle;
static GPS_PWay* tx_waylist;
static GPS_PWay* tx_routelist;
static GPS_PWay* cur_tx_routelist_entry;
Expand All @@ -75,7 +76,7 @@ static char* baudopt = nullptr;
static char* opt_codec = nullptr;
static int baud = 0;
static int categorybits;
static int receiver_must_upper = 1;
static bool receiver_must_upper = true;
static QTextCodec* codec{nullptr};

#define MILITANT_VALID_WAYPT_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expand Down Expand Up @@ -155,11 +156,11 @@ write_char_string(char* dest, const char* source, size_t destsize)
static void
rw_init(const QString& fname)
{
receiver_must_upper = 1;
receiver_must_upper = true;
const char* receiver_charset = "US-ASCII";

if (!mkshort_handle) {
mkshort_handle = mkshort_new_handle();
mkshort_handle = new MakeShort;
}

if (global_opts.debug_level > 0) {
Expand Down Expand Up @@ -218,7 +219,7 @@ rw_init(const QString& fname)
}

/*
* Grope the unit we're talking to to set setshort_length to
* Grope the unit we're talking to to set set_length to
* 20 for the V,
* 10 for Street Pilot, (old) Rhino, 76
* 6 for the III, 12, emap, and etrex
Expand Down Expand Up @@ -260,7 +261,7 @@ rw_init(const QString& fname)
case 574: /* Geko 201 */
receiver_short_length = 6;
valid_waypt_chars = MILITANT_VALID_WAYPT_CHARS " +-";
setshort_badchars(mkshort_handle, "\"$.,'!");
mkshort_handle->set_badchars("\"$.,'!");
break;

case 155: /* Garmin V */
Expand All @@ -270,7 +271,7 @@ rw_init(const QString& fname)
break;
case 382: /* C320 */
receiver_short_length = 30;
receiver_must_upper = 0;
receiver_must_upper = false;
break;
case 292: /* (60|76)C[S]x series */
case 421: /* Vista|Legend Cx */
Expand All @@ -280,28 +281,28 @@ rw_init(const QString& fname)
case 957: /* Legend HC */
receiver_short_length = 14;
snwhiteopt = xstrdup("1");
receiver_must_upper = 0;
receiver_must_upper = false;
/* This might be 8859-1 */
receiver_charset = "windows-1252";
break;
case 291: /* GPSMAP 60CS, probably others */
case 1095: /* GPS 72H */
receiver_short_length = 10;
valid_waypt_chars = MILITANT_VALID_WAYPT_CHARS " +-";
setshort_badchars(mkshort_handle, "\"$.,'!");
mkshort_handle->set_badchars("\"$.,'!");
break;
case 231: /* Quest */
case 463: /* Quest 2 */
receiver_must_upper = 0;
receiver_must_upper = false;
receiver_short_length = 30;
receiver_charset = "windows-1252";
break;
case 577: // Rino 530HCx Version 2.50
receiver_must_upper = 0;
receiver_must_upper = false;
receiver_short_length = 14;
break;
case 429: // Streetpilot i3
receiver_must_upper = 0;
receiver_must_upper = false;
receiver_charset = "windows-1252";
receiver_short_length = 30;
break;
Expand Down Expand Up @@ -332,26 +333,26 @@ rw_init(const QString& fname)
* If the user provided a short_length, override the calculated value.
*/
if (snlen) {
setshort_length(mkshort_handle, xstrtoi(snlen, nullptr, 10));
mkshort_handle->set_length(xstrtoi(snlen, nullptr, 10));
} else {
setshort_length(mkshort_handle, receiver_short_length);
mkshort_handle->set_length(receiver_short_length);
}

if (snwhiteopt) {
setshort_whitespace_ok(mkshort_handle, xstrtoi(snwhiteopt, nullptr, 10));
mkshort_handle->set_whitespace_ok(xstrtoi(snwhiteopt, nullptr, 10));
}

/*
* Until Garmin documents how to determine valid character space
* for the new models, we just release this safety check manually.
*/
if (receiver_must_upper) {
setshort_goodchars(mkshort_handle, valid_waypt_chars);
mkshort_handle->set_goodchars(valid_waypt_chars);
} else {
setshort_badchars(mkshort_handle, "");
mkshort_handle->set_badchars("");
}

setshort_mustupper(mkshort_handle, receiver_must_upper);
mkshort_handle->set_mustupper(receiver_must_upper);

/*
* This used to mean something when we used cet, but these days this
Expand Down Expand Up @@ -398,9 +399,8 @@ rw_deinit()
}
}

if (mkshort_handle) {
mkshort_del_handle(&mkshort_handle);
}
delete mkshort_handle;
mkshort_handle = nullptr;

xfree(portname);
portname = nullptr;
Expand Down Expand Up @@ -888,7 +888,7 @@ waypoint_prepare()
* mkshort will do collision detection and namespace
* cleaning
*/
QByteArray ident = mkshort(mkshort_handle,
QByteArray ident = mkshort_handle->mkshort(
global_opts.synthesize_shortnames ?
str_from_unicode(src) :
str_from_unicode(wpt->shortname),
Expand Down
21 changes: 11 additions & 10 deletions garmin_gpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ GarminGPIFormat::enum_waypt_cb(const Waypoint* ref) const
auto* wpt = new Waypoint(*ref);

if (*opt_unique == '1') {
wpt->shortname = mkshort(short_h, wpt->shortname);
wpt->shortname = short_h->mkshort(wpt->shortname);
}

wdata_add_wpt(wdata, wpt);
Expand Down Expand Up @@ -1252,15 +1252,15 @@ GarminGPIFormat::wr_init(const QString& fname)

fout = gbfopen_le(fname, "wb", MYNAME);

short_h = mkshort_new_handle();
short_h = new MakeShort;

setshort_length(short_h, 1024);
setshort_badchars(short_h, "\r\n");
setshort_mustupper(short_h, 0);
setshort_mustuniq(short_h, 1);
setshort_whitespace_ok(short_h, 1);
setshort_repeating_whitespace_ok(short_h, 0);
setshort_defname(short_h, "POI");
short_h->set_length(1024);
short_h->set_badchars("\r\n");
short_h->set_mustupper(false);
short_h->set_mustuniq(true);
short_h->set_whitespace_ok(true);
short_h->set_repeating_whitespace_ok(false);
short_h->set_defname("POI");

codepage = 0;

Expand Down Expand Up @@ -1325,7 +1325,8 @@ void
GarminGPIFormat::wr_deinit()
{
wdata_free(wdata);
mkshort_del_handle(&short_h);
delete short_h;
short_h = nullptr;
gbfclose(fout);

if ((opt_sleep) && !gpsbabel_testmode()) { /* don't sleep during 'testo' */
Expand Down
5 changes: 3 additions & 2 deletions garmin_gpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
#include <cstdint> // for int32_t, int16_t, uint16_t
#include <ctime> // for time_t

#include "defs.h" // for arglist_t, ARG_NOMINMAX, ff_cap, Waypoint, ARGTYPE_BOOL, ARGTYPE_STRING, ff_cap_none, ARGTYPE_FILE, ARGTYPE_INT, bounds, ff_cap_read, ff_cap_write, ff_type, ff_type_file, short_handle
#include "defs.h" // for arglist_t, ARG_NOMINMAX, ff_cap, Waypoint, ARGTYPE_BOOL, ARGTYPE_STRING, ff_cap_none, ARGTYPE_FILE, ARGTYPE_INT, bounds, ff_cap_read, ff_cap_write, ff_type, ff_type_file
#include "format.h" // for Format
#include "garmin_fs.h" // for garmin_fs_t
#include "gbfile.h" // for gbfile
#include "mkshort.h" // for MakeShort


class GarminGPIFormat : public Format
Expand Down Expand Up @@ -377,7 +378,7 @@ class GarminGPIFormat : public Format
uint16_t codepage{}; /* code-page, e.g. 1252, 65001 */
reader_data_t* rdata{};
writer_data_t* wdata{};
short_handle short_h{};
MakeShort* short_h{};
char units{};
time_t gpi_timestamp = 0;
QTextCodec* codec{nullptr};
Expand Down
36 changes: 18 additions & 18 deletions gdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
#include <cstring> // for memset, strstr, strcmp
#include <iterator> // for next

#include "defs.h" // for Waypoint, warning, route_head, fatal, UrlLink, bounds, mkshort, UrlList, unknown_alt, xfree, waypt_add_to_bounds, waypt_init_bounds, xstrtoi, mkshort_del_handle, route_add_wpt, route_disp_all, waypt_bounds_valid, xmalloc, gb_color, WaypointList, find_wa...
#include "defs.h" // for Waypoint, warning, route_head, fatal, UrlLink, bounds, UrlList, unknown_alt, xfree, waypt_add_to_bounds, waypt_init_bounds, xstrtoi, route_add_wpt, route_disp_all, waypt_bounds_valid, xmalloc, gb_color, WaypointList, find_wa...
#include "formspec.h" // for FormatSpecificDataList
#include "garmin_fs.h" // for garmin_fs_t, garmin_ilink_t, garmin_fs_alloc
#include "garmin_tables.h" // for gt_waypt_class_map_point, gt_color_index_by_rgb, gt_color_value, gt_waypt_classes_e, gt_find_desc_from_icon_number, gt_find_icon_number_from_desc, gt_gdb_display_mode_symbol, gt_get_icao_country, gt_waypt_class_user_waypoint, GDB, gt_display_mode_symbol
#include "gbfile.h" // for gbfgetint32, gbfputint32, gbfgetc, gbfread, gbfwrite, gbfgetdbl, gbfputc, gbfgetcstr, gbfclose, gbfgetnativecstr, gbfopen_le, gbfputint16, gbfile, gbfcopyfrom, gbfputcstr, gbfrewind, gbfseek, gbftell, gbfgetcstr_old, gbfgetint16, gbfgetuint32, gbfputdbl
#include "grtcirc.h" // for RAD, gcdist, radtometers
#include "jeeps/gpsmath.h" // for GPS_Math_Deg_To_Semi, GPS_Math_Semi_To_Deg
#include "mkshort.h" // for MakeShort
#include "src/core/datetime.h" // for DateTime


Expand Down Expand Up @@ -1113,19 +1114,17 @@ GdbFormat::read()
void
GdbFormat::reset_short_handle(const char* defname)
{
if (short_h != nullptr) {
mkshort_del_handle(&short_h);
}
delete short_h;

short_h = mkshort_new_handle();
short_h = new MakeShort;

setshort_length(short_h, kGDBNameBufferLen);
setshort_badchars(short_h, "\r\n\t");
setshort_mustupper(short_h, 0);
setshort_mustuniq(short_h, 1);
setshort_whitespace_ok(short_h, 1);
setshort_repeating_whitespace_ok(short_h, 1);
setshort_defname(short_h, defname);
short_h->set_length(kGDBNameBufferLen);
short_h->set_badchars("\r\n\t");
short_h->set_mustupper(false);
short_h->set_mustuniq(true);
short_h->set_whitespace_ok(true);
short_h->set_repeating_whitespace_ok(true);
short_h->set_defname(defname);
}

/* ----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -1613,7 +1612,7 @@ GdbFormat::write_waypoint_cb(const Waypoint* refpt)
}
}

name = mkshort(short_h, name);
name = short_h->mkshort(name);
wpt->extra_data = new QString(name);
write_waypoint(wpt, name, gmsd, icon, display);

Expand All @@ -1630,9 +1629,9 @@ GdbFormat::write_route_cb(const route_head* rte)

QString name;
if (rte->rte_name.isNull()) {
name = mkshort(short_h, QString::asprintf("Route%04d", rte->rte_num));
name = short_h->mkshort(QString::asprintf("Route%04d", rte->rte_num));
} else {
name = mkshort(short_h, rte->rte_name);
name = short_h->mkshort(rte->rte_name);
}

rte_ct++; /* increase informational number of written routes */
Expand All @@ -1652,9 +1651,9 @@ GdbFormat::write_track_cb(const route_head* trk)

QString name;
if (trk->rte_name.isNull()) {
name = mkshort(short_h, QString::asprintf("Track%04d", trk->rte_num));
name = short_h->mkshort(QString::asprintf("Track%04d", trk->rte_num));
} else {
name = mkshort(short_h, trk->rte_name);
name = short_h->mkshort(trk->rte_name);
}

trk_ct++; /* increase informational number of written tracks */
Expand Down Expand Up @@ -1704,7 +1703,8 @@ GdbFormat::wr_deinit()
{
disp_summary(fout);
gdb_flush_waypt_queue(waypt_nameposn_out_hash);
mkshort_del_handle(&short_h);
delete short_h;
short_h = nullptr;
gbfclose(fout);
gbfclose(ftmp);
}
Expand Down
5 changes: 3 additions & 2 deletions gdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
#include <QVector> // for QVector
#include <QtGlobal> // for QT_VERSION, QT_VERSION_CHECK

#include "defs.h" // for arglist_t, Waypoint, route_head, ARGTYPE_BOOL, ARGTYPE_INT, ARG_NOMINMAX, bounds, FF_CAP_RW_ALL, ff_cap, ff_type, ff_type_file, short_handle
#include "defs.h" // for arglist_t, Waypoint, route_head, ARGTYPE_BOOL, ARGTYPE_INT, ARG_NOMINMAX, bounds, FF_CAP_RW_ALL, ff_cap, ff_type, ff_type_file
#include "format.h" // for Format
#include "garmin_fs.h" // for garmin_fs_t
#include "garmin_tables.h" // for gt_waypt_classes_e
#include "gbfile.h" // for gbfile
#include "mkshort.h" // for MakeShort


class GdbFormat : public Format
Expand Down Expand Up @@ -172,7 +173,7 @@ class GdbFormat : public Format
WptNamePosnHash waypt_nameposn_in_hidden_hash;
WptNameHash waypt_name_in_hidden_hash;
WptNamePosnHash waypt_nameposn_out_hash;
short_handle short_h{};
MakeShort* short_h{};

char* gdb_opt_category{};
char* gdb_opt_ver{};
Expand Down
Loading

0 comments on commit 45268f7

Please sign in to comment.