From d377ef65af27e5a0e48d8b4017cc76517e305dfc Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Wed, 1 Dec 2021 06:26:08 -0700 Subject: [PATCH] convert arcdist, polygon, and xcsv to use gpsbabel::TextStream instead of gbfgetstr (#778) * read style files with a textstream. * use gpsbabel::textstream instead of gbfile. --- arcdist.cc | 42 +++++++++++++++++++++--------------------- polygon.cc | 27 ++++++++++++++------------- xcsv.cc | 13 +++++++------ 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/arcdist.cc b/arcdist.cc index c4663f353..b5b541536 100644 --- a/arcdist.cc +++ b/arcdist.cc @@ -19,21 +19,20 @@ */ -#include // for round -#include // for printf, sscanf -#include // for strtod -#include // for strchr, strlen, strncmp, strspn +#include // for round +#include // for printf, sscanf +#include // for strtod -#include // for QByteArray -#include // for QString -#include // for foreach, qPrintable, qint64 +#include // for QByteArray +#include // for QString +#include // 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 @@ -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; @@ -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) { diff --git a/polygon.cc b/polygon.cc index d2eae29a9..002f856a9 100644 --- a/polygon.cc +++ b/polygon.cc @@ -19,14 +19,14 @@ */ -#include // for sscanf -#include // for strchr, strlen, strspn +#include // for sscanf -#include // for foreach +#include // for QString +#include // 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 @@ -225,9 +225,10 @@ 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; @@ -235,18 +236,18 @@ void PolygonFilter::process() 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); @@ -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; diff --git a/xcsv.cc b/xcsv.cc index 7a060d620..048bb0a14 100644 --- a/xcsv.cc +++ b/xcsv.cc @@ -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 @@ -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; }