Skip to content

Commit

Permalink
convert garmin_fs functions from char* -> QString (#1202)
Browse files Browse the repository at this point in the history
* convert garmin_fs functions from char* -> QString.

* update comment

* make tag_type a class enum

* update garmin_fs includes
  • Loading branch information
tsteven4 authored Oct 30, 2023
1 parent ab78834 commit 1fcdf28
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 442 deletions.
138 changes: 22 additions & 116 deletions garmin_fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
*/

#include <cstdio> // for snprintf, sscanf
#include <cstdlib> // for strtod
#include "garmin_fs.h"

#include <QString> // for QString, QStringLiteral
#include <Qt> // for CaseInsensitive
#include <QString> // for QString
#include <Qt> // for CaseInsensitive

#include "defs.h"
#include "garmin_fs.h"
#include "garmin_tables.h" // for gt_switch_display_mode_value, gt_display_mode_symbol, gt_display_mode_symbol_and_comment, gt_display_mode_symbol_and_name
#include "inifile.h" // for inifile_readstr
#include "src/core/xmlstreamwriter.h" // for XmlStreamWriter
#include "inifile.h" // for inifile_readstr


#define MYNAME "garmin_fs"
Expand Down Expand Up @@ -76,130 +72,40 @@ garmin_fs_t::~garmin_fs_t()
}
}

/* GPX - out */

void
garmin_fs_xml_convert(const int base_tag, int tag, const QString& qstr, Waypoint* waypt)
bool
garmin_fs_convert_category(const QString& category_name, uint16_t* category)
{
// FIXME: eliminate C string copy/use here:
const char* cdatastr = xstrdup(qstr);
garmin_fs_t* gmsd = garmin_fs_t::find(waypt);
if (gmsd == nullptr) {
gmsd = garmin_fs_alloc(-1);
waypt->fs.FsChainAdd(gmsd);
}

tag -= base_tag;
/*
tt_garmin_waypt_extension, -> 0
tt_garmin_proximity, -> 1
tt_garmin_temperature,-> 2
tt_garmin_depth, -> 3
tt_garmin_display_mode, -> 4
tt_garmin_categories, -> 5
tt_garmin_category, -> 6
tt_garmin_addr, -> 7
tt_garmin_city, -> 8
tt_garmin_state, -> 9
tt_garmin_country, -> 10
tt_garmin_postal_code, -> 11
tt_garmin_phone_nr, -> 12
*/
switch (tag) {
case 1:
if (*cdatastr) {
waypt->set_proximity(strtod(cdatastr, nullptr));
}
break;
case 2:
if (*cdatastr) {
waypt->set_temperature(strtod(cdatastr, nullptr));
}
break;
case 3:
if (*cdatastr) {
waypt->set_depth(strtod(cdatastr, nullptr));
}
break;
case 4:
if (case_ignore_strcmp(cdatastr, "SymbolOnly") == 0) {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol);
} else if (case_ignore_strcmp(cdatastr, "SymbolAndDescription") == 0) {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol_and_comment);
} else {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol_and_name);
}
break;
case 6:
if (! garmin_fs_merge_category(cdatastr, waypt)) {
// There's nothing a user can really do about this (well, they could
// create a gpsbabel.ini that mapped them to garmin category numbers
// but that feature is so obscure and used in so few outputs that
// there's no reason to alarm the user. Just silently disregard
// category names that don't map cleanly.
// warning(MYNAME ": Unable to convert category \"%s\"!\n", cdatastr);
// Is the name "Category" followed by a number? Use that number.
if (category_name.startsWith(u"Category ", Qt::CaseInsensitive)) {
bool ok;
int i = category_name.mid(9).toInt(&ok);
if (ok && (i >= 1) && (i <= 16)) {
*category = (1 << --i);
return true;
}
break;
case 7:
garmin_fs_t::set_addr(gmsd, cdatastr);
break;
case 8:
garmin_fs_t::set_city(gmsd, cdatastr);
break;
case 9:
garmin_fs_t::set_state(gmsd, cdatastr);
break;
case 10:
garmin_fs_t::set_country(gmsd, cdatastr);
break;
case 11:
garmin_fs_t::set_postal_code(gmsd, cdatastr);
break;
case 12:
garmin_fs_t::set_phone_nr(gmsd, cdatastr);
break;
}
xfree(cdatastr);
}

unsigned char
garmin_fs_convert_category(const char* category_name, uint16_t* category)
{
int i;
int cat = 0;

// Is the name "Category" followed by a number? Use that number.
if ((case_ignore_strncmp(category_name, "Category ", 9) == 0) &&
(1 == sscanf(category_name + 9, "%d", &i)) &&
(i >= 1) && (i <= 16)) {
cat = (1 << --i);
} else if (global_opts.inifile != nullptr) {
if (global_opts.inifile != nullptr) {
// Do we have a gpsbabel.ini that maps category names to category #'s?
for (i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++) {
QString key = QString::number(i + 1);
QString c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
if (c.compare(category_name, Qt::CaseInsensitive) == 0) {
cat = (1 << i);
break;
*category = (1 << i);
return true;
}
}
}
if (cat == 0) {
return 0;
} else {
*category = cat;
return 1;
}
return false;
}

unsigned char
garmin_fs_merge_category(const char* category_name, Waypoint* waypt)
bool
garmin_fs_merge_category(const QString& category_name, Waypoint* waypt)
{
uint16_t cat;

// Attempt to get a textual category name to a category number.
if (!garmin_fs_convert_category(category_name, &cat)) {
return 0;
return false;
}

garmin_fs_t* gmsd = garmin_fs_t::find(waypt);
Expand All @@ -210,5 +116,5 @@ garmin_fs_merge_category(const char* category_name, Waypoint* waypt)
waypt->fs.FsChainAdd(gmsd);
}
garmin_fs_t::set_category(gmsd, cat);
return 1;
return true;
}
20 changes: 7 additions & 13 deletions garmin_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
#ifndef GARMIN_FS_H
#define GARMIN_FS_H

#include <cstdint> // for int32_t, int16_t, uint16_t
#include <cstdint> // for int32_t, int16_t, uint16_t

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

#include "defs.h"
#include "formspec.h" // for FsChainFind, kFsGmsd, FormatSpecificData
#include "src/core/xmlstreamwriter.h" // for XmlStreamWriter
#include "formspec.h" // for FormatSpecificData, kFsGmsd, FormatSpecificDataList


/* this order is used by most devices */
Expand Down Expand Up @@ -219,16 +218,11 @@ garmin_fs_t* garmin_fs_alloc(int protocol);
void garmin_fs_destroy(void* fs);
void garmin_fs_copy(void** dest, const void* src);

/* for GPX */
void garmin_fs_xml_convert(int base_tag, int tag, const QString& qstr, Waypoint* waypt);
/* ..convert_category: returns true=OK; false=Unable to convert category */
bool garmin_fs_convert_category(const QString& category_name, uint16_t* category);

/* common garmin_fs utilities */

/* ..convert_category: returns 1=OK; 0=Unable to convert category */
unsigned char garmin_fs_convert_category(const char* category_name, uint16_t* category);

/* ..merge_category: returns 1=OK; 0=Unable to convert category */
unsigned char garmin_fs_merge_category(const char* category_name, Waypoint* waypt);
/* ..merge_category: returns true=OK; false=Unable to convert category */
bool garmin_fs_merge_category(const QString& category_name, Waypoint* waypt);

#define GMSD_SECTION_CATEGORIES "Garmin Categories"

Expand Down
2 changes: 1 addition & 1 deletion garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ parse_categories(const QString& str)
QString cin = catstring.trimmed();
if (!cin.isEmpty()) {
uint16_t val;
if (!garmin_fs_convert_category(CSTR(cin), &val)) {
if (!garmin_fs_convert_category(cin, &val)) {
warning(MYNAME ": Unable to convert category \"%s\" at line %d!\n", qPrintable(cin), current_line);
} else {
res = res | val;
Expand Down
Loading

0 comments on commit 1fcdf28

Please sign in to comment.