Skip to content

Commit

Permalink
use Qt for os independent environmental variable access. (GPSBabel#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 authored Feb 24, 2022
1 parent 0a64f53 commit 4a3aaa1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 38 deletions.
3 changes: 0 additions & 3 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,6 @@ FILE* xfopen(const char* fname, const char* type, const char* errtxt);
// Thin wrapper around fopen() that supports Unicode fname on all platforms.
FILE* ufopen(const QString& fname, const char* mode);

// OS-abstracting wrapper for getting Unicode environment variables.
QString ugetenv(const char* env_var);

// FIXME: case_ignore_strcmp() and case_ignore_strncmp() should probably
// just be replaced at the call sites. These shims are just here to make
// them more accommodating of QString input.
Expand Down
9 changes: 0 additions & 9 deletions gpsbabel-sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ Prec = 6

[ gdb ]
via = 1

;------------------------------------------------------------------
[ tiger ]
snlen=7

;------------------------------------------------------------------
[pathaway]
# dbname = The Last Trip
deficon = Golf Course
13 changes: 6 additions & 7 deletions inifile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "defs.h" // for fatal, ugetenv, warning
#include "defs.h" // for fatal, warning
#include "inifile.h"
#include "src/core/file.h" // for File
#include <QByteArray> // for QByteArray
Expand All @@ -29,8 +29,7 @@
#include <QHash> // for QHash
#include <QIODevice> // for QIODevice::ReadOnly, QIODevice
#include <QTextStream> // for QTextStream
#include <Qt> // for CaseInsensitive
#include <QtGlobal> // for qPrintable
#include <QtGlobal> // for qEnvironmentVariable, qPrintable, QT_VERSION, QT_VERSION_CHECK
#include <utility>

#define MYNAME "inifile"
Expand Down Expand Up @@ -66,7 +65,7 @@ open_gpsbabel_inifile()
{
QString res;

QString envstr = ugetenv("GPSBABELINI");
QString envstr = qEnvironmentVariable("GPSBABELINI");
if (!envstr.isNull()) {
if (QFile(envstr).open(QIODevice::ReadOnly)) {
return envstr;
Expand All @@ -79,9 +78,9 @@ open_gpsbabel_inifile()
#ifdef __WIN32__
// Use &&'s early-out behaviour to try successive file locations: first
// %APPDATA%, then %WINDIR%, then %SYSTEMROOT%.
(name = find_gpsbabel_inifile(ugetenv("APPDATA"))).isNull()
&& (name = find_gpsbabel_inifile(ugetenv("WINDIR"))).isNull()
&& (name = find_gpsbabel_inifile(ugetenv("SYSTEMROOT"))).isNull();
(name = find_gpsbabel_inifile(qEnvironmentVariable("APPDATA"))).isNull()
&& (name = find_gpsbabel_inifile(qEnvironmentVariable("WINDIR"))).isNull()
&& (name = find_gpsbabel_inifile(qEnvironmentVariable("SYSTEMROOT"))).isNull();
#else
// Use &&'s early-out behaviour to try successive file locations: first
// ~/.gpsbabel, then /usr/local/etc, then /etc.
Expand Down
1 change: 0 additions & 1 deletion inifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#define HAVE_INIFILE_H

#include <QHash> // for QHash
#include <QList> // for QList
#include <QString> // for QString

class InifileSection;
Expand Down
21 changes: 3 additions & 18 deletions util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <cstdarg> // for va_list, va_end, va_start, va_copy
#include <cstdio> // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout
#include <cstdint> // for uint32_t
#include <cstdlib> // for abs, getenv, calloc, free, malloc, realloc
#include <cstdlib> // for abs, calloc, free, malloc, realloc
#include <cstring> // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror
#include <ctime> // for mktime, localtime

Expand All @@ -44,7 +44,7 @@
#include <QXmlStreamAttributes> // for QXmlStreamAttributes
#include <Qt> // for CaseInsensitive
#include <QTimeZone> // for QTimeZone
#include <QtGlobal> // for qAsConst, QAddConst<>::Type, qPrintable
#include <QtGlobal> // for qAsConst, qEnvironmentVariableIsSet, QAddConst<>::Type, qPrintable

#include "defs.h"
#include "src/core/datetime.h" // for DateTime
Expand Down Expand Up @@ -206,21 +206,6 @@ ufopen(const QString& fname, const char* mode)
#endif
}

/*
* OS-abstracting wrapper for getting Unicode environment variables.
*/
QString ugetenv(const char* env_var)
{
#ifdef __WIN32__
// Use QString to convert 8-bit env_var argument to wchar_t* for _wgetenv().
return QString::fromWCharArray(
_wgetenv((const wchar_t*) QString(env_var).utf16()));
#else
// Everyone else uses UTF-8 or some other locale-specific 8-bit encoding.
return QString::fromLocal8Bit(std::getenv(env_var));
#endif
}

/*
* Allocate a string using a format list with optional arguments
* Returns -1 on error.
Expand Down Expand Up @@ -721,7 +706,7 @@ mklocaltime(struct tm* t)
bool
gpsbabel_testmode()
{
static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr;
static bool testmode = qEnvironmentVariableIsSet("GPSBABEL_FREEZE_TIME");
return testmode;
}

Expand Down

0 comments on commit 4a3aaa1

Please sign in to comment.