1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// -*- c++ -*-
// $Id: formatload.cpp,v 1.3 2009-11-02 20:38:02 robertl Exp $
//------------------------------------------------------------------------
//
// Copyright (C) 2009 S. Khai Mong <khai@mangrai.com>.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
//
//------------------------------------------------------------------------
#include "formatload.h"
#include <QApplication> // for QApplication
#include <QByteArray> // for QByteArray
#include <QChar> // for QChar, operator==
#include <QCoreApplication> // for QCoreApplication
#include <QMessageBox> // for QMessageBox
#include <QObject> // for QObject
#include <QProcess> // for QProcess
#include <QRegularExpression> // for QRegularExpression, QRegularExpressionMatch
#include <QString> // for QString, operator+
#include <QTextStream> // for QTextStream
#include <QVariant> // for QVariant
#include "appname.h" // for appNam
#ifdef GENERATE_CORE_STRINGS
extern QTextStream* generate_output_stream;
#endif
//------------------------------------------------------------------------
static QString xlt(const QString& f)
{
#ifdef GENERATE_CORE_STRINGS
*generate_output_stream << "QT_TRANSLATE_NOOP(\"core\",\"" << f << "\")" << Qt::endl;
#endif
return QCoreApplication::translate("core", f.toUtf8().constData());
}
//------------------------------------------------------------------------
bool FormatLoad::skipToValidLine()
{
static const QRegularExpression regex("^file|serial");
while ((currentLine_ < lines_.size()) && !regex.match(lines_[currentLine_]).hasMatch()) {
currentLine_++;
}
return (currentLine_<lines_.size());
}
//------------------------------------------------------------------------
bool FormatLoad::processFormat(Format& format)
{
QStringList hfields = lines_[currentLine_++].split("\t");
if (hfields.size() < 5) {
return false;
}
QString htmlPage = lines_[currentLine_++].trimmed();
QList <FormatOption> optionList;
while ((currentLine_ < lines_.size()) && lines_[currentLine_].startsWith("option")) {
QStringList ofields = lines_[currentLine_].split("\t");
if (ofields.size() < 9) {
return false;
}
QString name = ofields[2];
QString description = ofields[3];
QString optionType = ofields[4];
QString optionDef = ofields[5];
QString optionMin = ofields[6];
QString optionMax = ofields[7];
QString optionHtml = ofields[8];
FormatOption::optionType type = FormatOption::OPTbool;
if (optionType == "boolean") {
type = FormatOption::OPTbool;
} else if (optionType == "string") {
type = FormatOption::OPTstring;
} else if (optionType == "integer") {
type = (optionMax != "" && optionMin != "") ? FormatOption::OPTboundedInt : FormatOption::OPTint;
if (optionMax == "") {
optionMax = "2147483647";
}
if (optionMin == "") {
optionMin = "-2147483647";
}
} else if (optionType == "float") {
type = FormatOption::OPTfloat;
if (optionMax == "") {
optionMax = "1.0E308";
}
if (optionMin == "") {
optionMin = "-1.0E308";
}
} else if (optionType == "file") {
type = FormatOption::OPTinFile;
} else if (optionType == "outfile") {
type = FormatOption::OPToutFile;
} else {
type = FormatOption::OPTstring;
}
optionList << FormatOption(name, xlt(description),
type, QVariant(optionDef), QVariant(optionMin),
QVariant(optionMax), optionHtml);
currentLine_++;
}
QList <FormatOption> optionList2 = optionList;
format = Format(hfields[2], xlt(hfields[4]),
hfields[1][0] == QChar('r'), hfields[1][2] == QChar('r'), hfields[1][4] == QChar('r'),
hfields[1][1] == QChar('w'), hfields[1][3] == QChar('w'), hfields[1][5] == QChar('w'),
hfields[0] == "file",
hfields[0] == "serial",
hfields[3].split('/'),
optionList,
optionList2, htmlPage);
#ifndef GENERATE_CORE_STRINGS
if (htmlPage.length() > 0 && Format::getHtmlBase().length() == 0) {
QString base = htmlPage;
static const QRegularExpression re("/[^/]+$");
base.replace(re, "/");
Format::setHtmlBase(base);
}
#endif
return true;
}
//------------------------------------------------------------------------
bool FormatLoad::getFormats(QList<Format>& formatList)
{
formatList.clear();
QProcess babel;
babel.start(QApplication::applicationDirPath() + "/gpsbabel", QStringList() << "-^3");
if (!babel.waitForStarted()) {
return false;
}
babel.closeWriteChannel();
if (!babel.waitForFinished()) {
return false;
}
if (babel.exitCode() != 0) {
return false;
}
QTextStream tstream(babel.readAll());
QList<int>lineList;
int k=0;
while (!tstream.atEnd()) {
QString l = tstream.readLine();
k++;
if (!l.trimmed().isEmpty()) {
lines_ << l;
lineList<<k;
}
}
currentLine_ = 0;
for (bool dataPresent = skipToValidLine(); dataPresent; dataPresent=skipToValidLine()) {
Format format;
if (!processFormat(format)) {
QMessageBox::information
(nullptr, appName,
QObject::tr("Error processing formats from running process \"gpsbabel -^3\" at line %1").arg(lineList[currentLine_]));
} else {
formatList << format;
}
}
return true;
}
|