File: mapfactor.cc

package info (click to toggle)
gpsbabel 1.7.0%2Bds-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,408 kB
  • sloc: cpp: 104,725; xml: 14,055; sh: 4,699; ansic: 2,062; makefile: 960; perl: 681; tcl: 138; javascript: 9
file content (161 lines) | stat: -rw-r--r-- 4,307 bytes parent folder | download
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
/*
    Copyright (C) 2014 Robert Lipe

    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 "defs.h"
#include "src/core/file.h"
#include "src/core/xmlstreamwriter.h"
#include <QtCore/QDebug>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamWriter>

static gpsbabel::File* oqfile;
static QXmlStreamWriter* writer;

static
QVector<arglist_t> mapfactor_args = {
};

#define MYNAME "mapfactor"

// This really should be class-local...
static QXmlStreamReader reader;
static QString mapfactor_read_fname;
static  const double milliarcseconds = 60.0 * 60.0 * 1000.0;

geocache_container wpt_container(const QString&);

static void MapfactorRead()
{
  Waypoint* wpt = nullptr;

  while (!reader.atEnd()) {
    QStringRef tag_name = reader.name();
    if (reader.tokenType()==QXmlStreamReader::StartElement) {
      if (tag_name == "item") {
        wpt = new Waypoint;

        QXmlStreamAttributes a = reader.attributes();
        wpt->shortname = a.value("name").toString();
        wpt->latitude = a.value("lat").toString().toDouble() / milliarcseconds;
        wpt->longitude = a.value("lon").toString().toDouble() / milliarcseconds;
      }
    }

    if (reader.tokenType() == QXmlStreamReader::EndElement) {
      if (wpt && reader.name() == "item") {
        waypt_add(wpt);
      }
    }

    reader.readNext();
  }
}

static void
mapfactor_rd_init(const QString& fname)
{
  mapfactor_read_fname = fname;
}

static void
mapfactor_read()
{
  gpsbabel::File file(mapfactor_read_fname);
  file.open(QIODevice::ReadOnly);
  reader.setDevice(&file);

  MapfactorRead();
  if (reader.hasError())  {
    fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
          qPrintable(reader.errorString()),
          qPrintable(file.fileName()),
          (long) reader.lineNumber(),
          (long) reader.columnNumber());
  }
}


static void
mapfactor_rd_deinit()
{
}

static void
mapfactor_wr_init(const QString& fname)
{
  oqfile = new gpsbabel::File(fname);
  oqfile->open(QIODevice::WriteOnly | QIODevice::Text);
  writer = new gpsbabel::XmlStreamWriter(oqfile);

  // Override the "UTF-8-XML" with ... the default.
  writer->setCodec("utf-8");

  writer->setAutoFormatting(true);
  writer->setAutoFormattingIndent(2);
  writer->writeStartDocument();
}

static void
mapfactor_wr_deinit()
{
  writer->writeEndDocument();
  delete writer;
  writer = nullptr;
  oqfile->close();
  delete oqfile;
  oqfile = nullptr;
}

static void
mapfactor_waypt_pr(const Waypoint* waypointp)
{
  writer->writeStartElement(QStringLiteral("item"));

  writer->writeAttribute(QStringLiteral("name"), waypointp->shortname);
  writer->writeAttribute(QStringLiteral("lat"), QString::number(waypointp->latitude * milliarcseconds, 'f', 0));
  writer->writeAttribute(QStringLiteral("lon"), QString::number(waypointp->longitude * milliarcseconds, 'f', 0));
  writer->writeEndElement();
}

static void
mapfactor_write()
{
  writer->writeStartElement(QStringLiteral("favourites"));
  writer->writeAttribute(QStringLiteral("version"), QStringLiteral("1"));
  // TODO: This could be moved to wr_init, but the pre GPX version put the two
  // lines above this, so mimic that behaviour exactly.
  writer->setAutoFormatting(true);
  waypt_disp_all(mapfactor_waypt_pr);
  writer->writeEndElement();
}

ff_vecs_t mapfactor_vecs = {
  ff_type_file,
  { (ff_cap)(ff_cap_read | ff_cap_write), ff_cap_none, ff_cap_none },
  mapfactor_rd_init,
  mapfactor_wr_init,
  mapfactor_rd_deinit,
  mapfactor_wr_deinit,
  mapfactor_read,
  mapfactor_write,
  nullptr,
  &mapfactor_args,
  CET_CHARSET_UTF8, 0	/* CET-REVIEW */
  , NULL_POS_OPS,
  nullptr
};