Skip to content

Commit

Permalink
convert arcdist, polygon, and xcsv to use gpsbabel::TextStream instea…
Browse files Browse the repository at this point in the history
…d of gbfgetstr (GPSBabel#778)

* read style files with a textstream.

* use gpsbabel::textstream instead of gbfile.
  • Loading branch information
tsteven4 authored Dec 1, 2021
1 parent f2da18d commit d377ef6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
42 changes: 21 additions & 21 deletions arcdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
*/

#include <cmath> // for round
#include <cstdio> // for printf, sscanf
#include <cstdlib> // for strtod
#include <cstring> // for strchr, strlen, strncmp, strspn
#include <cmath> // for round
#include <cstdio> // for printf, sscanf
#include <cstdlib> // for strtod

#include <QByteArray> // for QByteArray
#include <QString> // for QString
#include <QtGlobal> // for foreach, qPrintable, qint64
#include <QByteArray> // for QByteArray
#include <QString> // for QString
#include <QtGlobal> // for foreach, qPrintable, qint64

#include "defs.h"
#include "arcdist.h"
#include "gbfile.h" // for gbfclose, gbfgetstr, gbfopen, gbfile
#include "grtcirc.h" // for RAD, gcdist, linedistprj, radtomi
#include "src/core/datetime.h" // for DateTime
#include "src/core/logging.h" // for Fatal
#include "grtcirc.h" // for RAD, gcdist, linedistprj, radtomi
#include "src/core/datetime.h" // for DateTime
#include "src/core/logging.h" // for Fatal
#include "src/core/textstream.h" // for TextStream


#if FILTERS_ENABLED
Expand Down Expand Up @@ -119,30 +118,31 @@ void ArcDistanceFilter::process()

if (arcfileopt) {
int fileline = 0;
char* line;
QString line;

gbfile* file_in = gbfopen(arcfileopt, "r", MYNAME);
gpsbabel::TextStream stream;
stream.open(arcfileopt, QIODevice::ReadOnly, MYNAME);

auto* arcpt1 = new Waypoint;
auto* arcpt2 = new Waypoint;
arcdist_arc_disp_hdr_cb(nullptr);

arcpt2->latitude = arcpt2->longitude = BADVAL;
while ((line = gbfgetstr(file_in))) {
while (stream.readLineInto(&line)) {
fileline++;

char* pound = strchr(line, '#');
if (pound) {
if (0 == strncmp(pound, "#break", 6)) {
auto pound = line.indexOf('#');
if (pound >= 0) {
if (line.mid(pound, 6) == u"#break") {
arcdist_arc_disp_hdr_cb(nullptr);
}
*pound = '\0';
line.truncate(pound);
}

arcpt2->latitude = arcpt2->longitude = BADVAL;
int argsfound = sscanf(line, "%lf %lf", &arcpt2->latitude, &arcpt2->longitude);
int argsfound = sscanf(CSTR(line), "%lf %lf", &arcpt2->latitude, &arcpt2->longitude);

if (argsfound != 2 && strspn(line, " \t\n") < strlen(line)) {
if ((argsfound != 2) && (line.trimmed().size() > 0)) {
warning(MYNAME ": Warning: Arc file contains unusable vertex on line %d.\n", fileline);
} else {
Waypoint* arcpttmp = arcpt1;
Expand All @@ -154,7 +154,7 @@ void ArcDistanceFilter::process()
delete arcpt1;
delete arcpt2;

gbfclose(file_in);
stream.close();
} else if (rteopt) {
route_disp_all(arcdist_arc_disp_hdr_cb_f, nullptr, arcdist_arc_disp_wpt_cb_f);
} else if (trkopt) {
Expand Down
27 changes: 14 additions & 13 deletions polygon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/

#include <cstdio> // for sscanf
#include <cstring> // for strchr, strlen, strspn
#include <cstdio> // for sscanf

#include <QtGlobal> // for foreach
#include <QString> // for QString
#include <QtGlobal> // for foreach

#include "defs.h"
#include "polygon.h"
#include "gbfile.h" // for gbfclose, gbfgetstr, gbfopen, gbfile
#include "src/core/textstream.h" // for TextStream


#if FILTERS_ENABLED
Expand Down Expand Up @@ -225,28 +225,29 @@ void PolygonFilter::process()
int fileline = 0;
int first = 1;
int last = 0;
char* line;
QString line;

gbfile* file_in = gbfopen(polyfileopt, "r", MYNAME);
gpsbabel::TextStream stream;
stream.open(polyfileopt, QIODevice::ReadOnly, MYNAME);

double olat = BADVAL;
double olon = BADVAL;
double lat1 = BADVAL;
double lon1 = BADVAL;
double lat2 = BADVAL;
double lon2 = BADVAL;
while ((line = gbfgetstr(file_in))) {
while (stream.readLineInto(&line)) {
fileline++;

char* pound = strchr(line, '#');
if (pound) {
*pound = '\0';
auto pound = line.indexOf('#');
if (pound >= 0) {
line.truncate(pound);
}

lat2 = lon2 = BADVAL;
int argsfound = sscanf(line, "%lf %lf", &lat2, &lon2);
int argsfound = sscanf(CSTR(line), "%lf %lf", &lat2, &lon2);

if (argsfound != 2 && strspn(line, " \t\n") < strlen(line)) {
if ((argsfound != 2) && (line.trimmed().size() > 0)) {
warning(MYNAME
": Warning: Polygon file contains unusable vertex on line %d.\n",
fileline);
Expand Down Expand Up @@ -294,7 +295,7 @@ void PolygonFilter::process()
lon1 = lon2;
}
}
gbfclose(file_in);
stream.close();

foreach (Waypoint* wp, *global_waypoint_list) {
ed = (extra_data*) wp->extra_data;
Expand Down
13 changes: 7 additions & 6 deletions xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#include "csv_util.h" // for csv_stringtrim, dec_to_human, csv_stringclean, human_to_dec, ddmmdir_to_degrees, dec_to_intdeg, decdir_to_dec, intdeg_to_dec, csv_linesplit
#include "formspec.h" // for FormatSpecificDataList
#include "garmin_fs.h" // for garmin_fs_t, garmin_fs_alloc
#include "gbfile.h" // for gbfgetstr, gbfclose, gbfopen, gbfile
#include "grtcirc.h" // for RAD, gcdist, radtometers
#include "jeeps/gpsmath.h" // for GPS_Math_WGS84_To_UTM_EN, GPS_Lookup_Datum_Index, GPS_Math_Known_Datum_To_WGS84_M, GPS_Math_UTM_EN_To_Known_Datum, GPS_Math_WGS84_To_Known_Datum_M, GPS_Math_WGS84_To_UKOSMap_M
#include "jeeps/gpsport.h" // for int32
Expand Down Expand Up @@ -1832,18 +1831,20 @@ XcsvStyle::xcsv_parse_style_buff(const char* sbuff)
XcsvStyle
XcsvStyle::xcsv_read_style(const char* fname)
{
gbfile* fp = gbfopen(fname, "rb", MYNAME);
XcsvStyle style;
for (QString sbuff = gbfgetstr(fp); !sbuff.isNull(); sbuff = gbfgetstr(fp)) {
sbuff = sbuff.trimmed();
xcsv_parse_style_line(&style, sbuff);

gpsbabel::TextStream stream;
stream.open(fname, QIODevice::ReadOnly, MYNAME);
QString sbuff;
while (stream.readLineInto(&sbuff)) {
xcsv_parse_style_line(&style, sbuff.trimmed());
}
stream.close();

/* if we have no output fields, use input fields as output fields */
if (style.ofields.isEmpty()) {
style.ofields = style.ifields;
}
gbfclose(fp);

return style;
}
Expand Down

0 comments on commit d377ef6

Please sign in to comment.