Skip to content

Commit

Permalink
convert tpo formats to Format class (#1225)
Browse files Browse the repository at this point in the history
* convert tpo to Format class.

* capture comments
  • Loading branch information
tsteven4 authored Nov 18, 2023
1 parent 1d74ab7 commit 840e884
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 97 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ set(HEADERS
skytraq.h
subrip.h
text.h
tpo.h
unicsv.h
units.h
vcf.h
Expand Down
123 changes: 30 additions & 93 deletions tpo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,36 @@
3.x "recreation"
*/

#include <cmath> // for abs
#include "tpo.h"

#include <cstdint> // for uint8_t
#include <cstdio> // for printf, SEEK_CUR, SEEK_SET
#include <cmath> // for abs
#include <cstring> // for strlen, strncmp
#include <vector> // for vector

#include <QByteArray> // for QByteArray
#include <QScopedArrayPointer> // for QScopedArrayPointer
#include <QString> // for QString
#include <QStringLiteral> // for qMakeStringPrivate, QStringLiteral
#include <QVector> // for QVector
#include <QtGlobal> // for qPrintable, Q_UNUSED

#include "defs.h"
#include "gbfile.h" // for gbfread, gbfgetc, gbfgetint32, gbfreadbuf, gbfseek, gbfgetdbl, gbfgetint16, gbfclose, gbfgetnativecstr, gbfgetuint16, gbfopen_le, gbfile
#include "defs.h" // for Waypoint, fatal, route_head, le_read32, waypt_add, track_add_wpt, track_add_head, xfree, xmalloc, doing_rtes, doing_wpts, gb_color, route_add_head, route_add_wpt, unknown_alt, doing_trks
#include "gbfile.h" // for gbfread, gbfgetc, gbfgetint32, gbfreadbuf, gbfseek, gbfgetdbl, gbfgetint16, gbfclose, gbfgetnativecstr, gbfgetuint16, gbfopen_le
#include "jeeps/gpsmath.h" // for GPS_Math_Known_Datum_To_WGS84_M


#define MYNAME "TPO"

static char* dumpheader = nullptr;

static
QVector<arglist_t> tpo2_args = {
};

static
QVector<arglist_t> tpo3_args = {
};


static gbfile* tpo_file_in;
static double track_length;

/*******************************************************************************/
/* READ */
/*******************************************************************************/

/* Define a global here that we can query from multiple places */
static float tpo_version = 0.0;

/* tpo_check_version_string()
Check the first bytes of the file for a version 3.0 header. */
static void
tpo_check_version_string()
void
TpoFormatBase::tpo_check_version_string()
{

unsigned char string_size;
Expand Down Expand Up @@ -142,11 +127,11 @@ tpo_check_version_string()
}
}

static void
/* tpo_dump_header_bytes(int header_size)
Write the first header_size bytes of the file to standard output
as a C array definition. */
tpo_dump_header_bytes(int header_size)
void
TpoFormatBase::tpo_dump_header_bytes(int header_size)
{
QByteArray buffer = gbfreadbuf(header_size, tpo_file_in);

Expand All @@ -172,8 +157,8 @@ tpo_dump_header_bytes(int header_size)
Keep reading bytes from the file until the section name is encountered,
then go seek_bytes forwards (+) or backwards (-) to the start of
the section data. */
static void
tpo_read_until_section(const char* section_name, int seek_bytes)
void
TpoFormatBase::tpo_read_until_section(const char* section_name, int seek_bytes)
{
char byte;
unsigned int match_index = 0;
Expand Down Expand Up @@ -220,7 +205,7 @@ tpo_read_until_section(const char* section_name, int seek_bytes)
// that is the only type of data available in the version 2.x TPO
// files.
//
static void tpo_read_2_x()
void TpoFormatBase::tpo_read_2_x()
{
char buff[16];

Expand Down Expand Up @@ -335,7 +320,7 @@ static void tpo_read_2_x()
//
// For version 3.x files.
//
static int tpo_read_int()
int TpoFormatBase::tpo_read_int()
{
constexpr int debug = 0;

Expand Down Expand Up @@ -381,7 +366,7 @@ static int tpo_read_int()
//
// For version 3.x/4.x files.
//
static int tpo_find_block(unsigned int block_desired)
int TpoFormatBase::tpo_find_block(unsigned int block_desired)
{
unsigned int block_type;
constexpr int debug = 0;
Expand Down Expand Up @@ -419,7 +404,7 @@ static int tpo_find_block(unsigned int block_desired)
//
// For version 3.x files.
//
static Waypoint* tpo_convert_ll(int lat, int lon)
Waypoint* TpoFormatBase::tpo_convert_ll(int lat, int lon)
{
auto* waypoint_temp = new Waypoint;

Expand Down Expand Up @@ -453,19 +438,9 @@ static Waypoint* tpo_convert_ll(int lat, int lon)
return (waypoint_temp);
}

#define TRACKNAMELENGTH 255
class StyleInfo
{
public:
QString name;
uint8_t color[3] {0, 0, 0}; // keep R/G/B values separate because line_color needs BGR
uint8_t wide{0};
uint8_t dash{0};
};

// Track decoder for version 3.x/4.x files.
// This block contains tracks (called "freehand routes" in Topo).
static void tpo_process_tracks()
void TpoFormatBase::tpo_process_tracks()
{
constexpr int debug = 0; // 0-4 for increasingly verbose output in this subroutine)

Expand Down Expand Up @@ -611,7 +586,7 @@ static void tpo_process_tracks()
track_style -= 1; // STARTS AT 1, whereas style arrays start at 0

// Can be 8/16/32-bit value - never used? length in meters?
track_length = tpo_read_int();
double track_length = tpo_read_int();

//UNKNOWN DATA LENGTH
unsigned int name_length = tpo_read_int();
Expand Down Expand Up @@ -1039,17 +1014,9 @@ static void tpo_process_tracks()
} // end of tpo_process_tracks


// Global index to waypoints, needed for routes, filled in by
// tpo_process_waypoints.
//
// For version 3.x files.
//
static Waypoint** tpo_wp_index;
static unsigned int tpo_index_ptr;

// Waypoint decoder for version 3.x files.
//
static void tpo_process_waypoints()
void TpoFormatBase::tpo_process_waypoints()
{
//printf("Processing Waypoints...\n");

Expand Down Expand Up @@ -1157,7 +1124,7 @@ static void tpo_process_waypoints()

// Map Notes decoder for version 3.x files.
//
static void tpo_process_map_notes()
void TpoFormatBase::tpo_process_map_notes()
{
//printf("Processing Map Notes...\n");

Expand Down Expand Up @@ -1255,7 +1222,7 @@ static void tpo_process_map_notes()

// Symbols decoder for version 3.x files.
//
static void tpo_process_symbols()
void TpoFormatBase::tpo_process_symbols()
{
//printf("Processing Symbols...\n");

Expand Down Expand Up @@ -1298,7 +1265,7 @@ static void tpo_process_symbols()

// Text Labels decoder for version 3.x files.
//
static void tpo_process_text_labels()
void TpoFormatBase::tpo_process_text_labels()
{
//printf("Processing Text Labels...\n");

Expand Down Expand Up @@ -1362,7 +1329,7 @@ static void tpo_process_text_labels()
// with pointers to waypoint objects by tpo_process_waypoints()
// function above.
//
static void tpo_process_routes()
void TpoFormatBase::tpo_process_routes()
{
//printf("Processing Routes...\n");

Expand Down Expand Up @@ -1446,7 +1413,7 @@ static void tpo_process_routes()
#ifdef DEAD_CODE_IS_REBORN
// Compass decoder for version 3.x files.
//
static void tpo_process_compass()
void TpoFormatBase::tpo_process_compass()
{

// Not implemented yet
Expand All @@ -1461,7 +1428,7 @@ static void tpo_process_compass()
// (called "freehand routes" or just "routes" in Topo), "waypoints",
// and "gps-routes". We intend to read all three types.
//
static void tpo_read_3_x()
void TpoFormatBase::tpo_read_3_x()
{

if (doing_trks) {
Expand Down Expand Up @@ -1517,8 +1484,8 @@ static void tpo_read_3_x()



static void
tpo_rd_init(const QString& fname)
void
TpoFormatBase::tpo_rd_init(const QString& fname)
{

// prepare for an attempt to deallocate memory that may or may not get allocated
Expand Down Expand Up @@ -1547,8 +1514,8 @@ tpo_rd_init(const QString& fname)
}
}

static void
tpo_rd_deinit()
void
TpoFormatBase::tpo_rd_deinit()
{
// Free the waypoint index, we don't need it anymore.
for (unsigned int i = 0; i < tpo_index_ptr; i++) {
Expand All @@ -1565,8 +1532,8 @@ tpo_rd_deinit()
gbfclose(tpo_file_in);
}

static void
tpo_read()
void
TpoFormatBase::tpo_read()
{

if (tpo_version == 2.0) {
Expand All @@ -1579,33 +1546,3 @@ tpo_read()
fatal(MYNAME ": gpsbabel can only read TPO versions through 3.x.x\n");
}
}

/* TPO 2.x format can read tracks only */
ff_vecs_t tpo2_vecs = {
ff_type_file, /* ff_type_internal */
{ ff_cap_none, ff_cap_read, ff_cap_none },
tpo_rd_init,
nullptr,
tpo_rd_deinit,
nullptr,
tpo_read,
nullptr,
nullptr,
&tpo2_args,
NULL_POS_OPS
};

/* TPO 3.x format can read waypoints/tracks/routes */
ff_vecs_t tpo3_vecs = {
ff_type_file, /* ff_type_internal */
{ ff_cap_read, ff_cap_read, ff_cap_read },
tpo_rd_init,
nullptr,
tpo_rd_deinit,
nullptr,
tpo_read,
nullptr,
nullptr,
&tpo3_args,
NULL_POS_OPS
};
Loading

0 comments on commit 840e884

Please sign in to comment.