Skip to content

Commit

Permalink
convert tpg to dynamic Format class. (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 authored Nov 18, 2023
1 parent 0656a45 commit f888ec5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 52 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ set(HEADERS
skytraq.h
subrip.h
text.h
tpg.h
tpo.h
unicsv.h
units.h
Expand Down
75 changes: 26 additions & 49 deletions tpg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@
*/

#include "tpg.h"

#include <cctype> // for isalnum
#include <cstring> // for memcmp

#include <QChar> // for QChar
#include <QString> // for QString
#include <QVector> // for QVector

#include "defs.h"
#include "gbfile.h" // for gbfwrite, gbfgetint16, gbfputint16, gbfclose
#include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index, GPS_Math_Known_Da...
#include "gbfile.h" // for gbfwrite, gbfgetint16, gbfputint16, gbfclose, gbfgetdbl, gbfgetpstr, gbfopen_le, gbfputdbl, gbfgetint32, gbfputc, gbfputpstr, gbfread
#include "jeeps/gpsmath.h" // for GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M, GPS_Math_WGS84_To_Known_Datum_M
#include "mkshort.h" // for MakeShort


Expand All @@ -40,21 +41,8 @@
#define MAXTPGSTRINGSIZE 256
#define MAXTPGOUTPUTPINS 65535

static gbfile* tpg_file_in;
static gbfile* tpg_file_out;
static MakeShort* mkshort_handle;
static char* tpg_datum_opt;
static int tpg_datum_idx;

static unsigned int waypt_out_count;

static
QVector<arglist_t> tpg_args = {
{"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX , nullptr},
};

static int
valid_tpg_header(char* header, int len)
int
TpgFormat::valid_tpg_header(char* header, int len)
{
unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D,
0x00, 0x43, 0x54, 0x6F, 0x70,
Expand All @@ -68,46 +56,46 @@ valid_tpg_header(char* header, int len)
return memcmp(header_bytes, header, len);
}

static void
tpg_common_init()
void
TpgFormat::tpg_common_init()
{
tpg_datum_idx = GPS_Lookup_Datum_Index(tpg_datum_opt);
if (tpg_datum_idx < 0) {
fatal(MYNAME ": Datum '%s' is not recognized.\n", tpg_datum_opt);
}
}

static void
tpg_rd_init(const QString& fname)
void
TpgFormat::rd_init(const QString& fname)
{
tpg_common_init();
tpg_file_in = gbfopen_le(fname, "rb", MYNAME);
}

static void
tpg_rd_deinit()
void
TpgFormat::rd_deinit()
{
gbfclose(tpg_file_in);
}

static void
tpg_wr_init(const QString& fname)
void
TpgFormat::wr_init(const QString& fname)
{
tpg_common_init();
tpg_file_out = gbfopen_le(fname, "wb", MYNAME);
mkshort_handle = new MakeShort;
waypt_out_count = 0;
}

static void
tpg_wr_deinit()
void
TpgFormat::wr_deinit()
{
delete mkshort_handle;
gbfclose(tpg_file_out);
}

static void
tpg_read()
void
TpgFormat::read()
{
char buff[MAXTPGSTRINGSIZE + 1];
double amt;
Expand Down Expand Up @@ -169,8 +157,8 @@ tpg_read()
}
}

static void
tpg_waypt_pr(const Waypoint* wpt)
void
TpgFormat::tpg_waypt_pr(const Waypoint* wpt)
{
double lon, lat;
double amt;
Expand Down Expand Up @@ -280,8 +268,8 @@ tpg_waypt_pr(const Waypoint* wpt)
}
}

static void
tpg_write()
void
TpgFormat::write()
{
unsigned char header_bytes[] = { 0xFF, 0xFF, 0x01, 0x00, 0x0D,
0x00, 0x43, 0x54, 0x6F, 0x70,
Expand All @@ -307,19 +295,8 @@ tpg_write()
/* write the rest of the header */
gbfwrite(header_bytes, 1, 19, tpg_file_out);

waypt_disp_all(tpg_waypt_pr);
auto tpg_waypt_pr_lambda = [this](const Waypoint* waypointp)->void {
tpg_waypt_pr(waypointp);
};
waypt_disp_all(tpg_waypt_pr_lambda);
}

ff_vecs_t tpg_vecs = {
ff_type_file,
FF_CAP_RW_WPT,
tpg_rd_init,
tpg_wr_init,
tpg_rd_deinit,
tpg_wr_deinit,
tpg_read,
tpg_write,
nullptr,
&tpg_args,
NULL_POS_OPS
};
84 changes: 84 additions & 0 deletions tpg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
National Geographic Topo! TPG file support (Waypoints/Routes)
Contributed to gpsbabel by Alex Mottram
For Topo! version 2.x. Routes are currently not implemented.
Copyright (C) 2002 Alex Mottram, geo_alexm at cox-internet.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TPG_H_INCLUDED_
#define TPG_H_INCLUDED_

#include <QString> // for QString
#include <QVector> // for QVector

#include "defs.h"
#include "format.h" // for Format
#include "gbfile.h" // for gbfile
#include "mkshort.h" // for MakeShort


class TpgFormat : public Format
{
public:
using Format::Format;

QVector<arglist_t>* get_args() override
{
return &tpg_args;
}

ff_type get_type() const override
{
return ff_type_file;
}

QVector<ff_cap> get_cap() const override
{
return FF_CAP_RW_WPT;
}

void rd_init(const QString& fname) override;
void read() override;
void rd_deinit() override;
void wr_init(const QString& fname) override;
void write() override;
void wr_deinit() override;

private:
/* Member Functions */

static int valid_tpg_header(char* header, int len);
void tpg_common_init();
void tpg_waypt_pr(const Waypoint* wpt);

/* Data Members */

gbfile* tpg_file_in{};
gbfile* tpg_file_out{};
MakeShort* mkshort_handle{};
char* tpg_datum_opt{};
int tpg_datum_idx{};

unsigned int waypt_out_count{};

QVector<arglist_t> tpg_args = {
{"datum", &tpg_datum_opt, "Datum (default=NAD27)", "N. America 1927 mean", ARGTYPE_STRING, ARG_NOMINMAX, nullptr},
};
};
#endif // TPG_H_INCLUDED_
6 changes: 3 additions & 3 deletions vecs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "src/core/logging.h" // for Warning, FatalMsg
#include "subrip.h" // for SubripFormat
#include "text.h" // for TextFormat
#include "tpg.h" // for TpgFormat
#include "tpo.h" // for Tpo2Format, Tpo3Format
#include "unicsv.h" // for UnicsvFormat
#include "v900.h" // for V900Format
Expand All @@ -81,7 +82,6 @@
extern ff_vecs_t geo_vecs;
extern ff_vecs_t ozi_vecs;
#if MAXIMAL_ENABLED
extern ff_vecs_t tpg_vecs;
extern ff_vecs_t gpl_vecs;
extern ff_vecs_t mtk_vecs;
extern ff_vecs_t mtk_fvecs;
Expand Down Expand Up @@ -121,7 +121,6 @@ struct Vecs::Impl {
KmlFormat kml_fmt;
#if MAXIMAL_ENABLED
LowranceusrFormat lowranceusr_fmt;
LegacyFormat tpg_fmt {tpg_vecs};
Tpo2Format tpo2_fmt;
Tpo3Format tpo3_fmt;
#if SHAPELIB_ENABLED
Expand Down Expand Up @@ -233,11 +232,12 @@ struct Vecs::Impl {
nullptr,
},
{
&tpg_fmt,
nullptr,
"tpg",
"National Geographic Topo .tpg (waypoints)",
"tpg",
nullptr,
&fmtfactory<TpgFormat>
},
{
&tpo2_fmt,
Expand Down

0 comments on commit f888ec5

Please sign in to comment.