You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.1KB

  1. /*
  2. Copyright (C) 2014 Robert Lipe
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  14. */
  15. #include "defs.h"
  16. #include "src/core/file.h"
  17. #include "src/core/xmlstreamwriter.h"
  18. #include <QtCore/QXmlStreamReader>
  19. #include <QtCore/QXmlStreamWriter>
  20. #include <QtCore/QDebug>
  21. static gpsbabel::File* oqfile;
  22. static QXmlStreamWriter* writer;
  23. static
  24. arglist_t mapfactor_args[] = {
  25. ARG_TERMINATOR
  26. };
  27. #define MYNAME "mapfactor"
  28. // This really should be class-local...
  29. static QXmlStreamReader reader;
  30. static QString mapfactor_read_fname;
  31. static const double milliarcseconds = 60.0 * 60.0 * 1000.0;
  32. geocache_container wpt_container(const QString&);
  33. void MapfactorRead()
  34. {
  35. Waypoint* wpt = NULL;
  36. while (!reader.atEnd()) {
  37. QStringRef tag_name = reader.name();
  38. if (reader.tokenType()==QXmlStreamReader::StartElement) {
  39. if (tag_name == "item") {
  40. wpt = new Waypoint;
  41. QXmlStreamAttributes a = reader.attributes();
  42. wpt->shortname = a.value("name").toString();
  43. wpt->latitude = a.value("lat").toString().toDouble() / milliarcseconds;
  44. wpt->longitude = a.value("lon").toString().toDouble() / milliarcseconds;
  45. }
  46. }
  47. if (reader.tokenType() == QXmlStreamReader::EndElement) {
  48. if (wpt && reader.name() == "item") {
  49. waypt_add(wpt);
  50. }
  51. }
  52. reader.readNext();
  53. }
  54. }
  55. static void
  56. mapfactor_rd_init(const QString& fname)
  57. {
  58. mapfactor_read_fname = fname;
  59. }
  60. static void
  61. mapfactor_read(void)
  62. {
  63. gpsbabel::File file(mapfactor_read_fname);
  64. file.open(QIODevice::ReadOnly);
  65. reader.setDevice(&file);
  66. MapfactorRead();
  67. if (reader.hasError()) {
  68. fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
  69. qPrintable(reader.errorString()),
  70. qPrintable(file.fileName()),
  71. (long) reader.lineNumber(),
  72. (long) reader.columnNumber());
  73. }
  74. }
  75. static void
  76. mapfactor_rd_deinit(void)
  77. {
  78. }
  79. static void
  80. mapfactor_wr_init(const QString& fname)
  81. {
  82. oqfile = new gpsbabel::File(fname);
  83. oqfile->open(QIODevice::WriteOnly | QIODevice::Text);
  84. writer = new gpsbabel::XmlStreamWriter(oqfile);
  85. // Override the "UTF-8-XML" with ... the default.
  86. writer->setCodec("utf-8");
  87. writer->setAutoFormatting(true);
  88. writer->setAutoFormattingIndent(2);
  89. writer->writeStartDocument();
  90. }
  91. static void
  92. mapfactor_wr_deinit(void)
  93. {
  94. writer->writeEndDocument();
  95. delete writer;
  96. writer = NULL;
  97. oqfile->close();
  98. delete oqfile;
  99. oqfile = NULL;
  100. }
  101. static void
  102. mapfactor_waypt_pr(const Waypoint* waypointp)
  103. {
  104. writer->writeStartElement("item");
  105. writer->writeAttribute("name", waypointp->shortname);
  106. writer->writeAttribute("lat", QString::number(waypointp->latitude * milliarcseconds, 'f', 0));
  107. writer->writeAttribute("lon", QString::number(waypointp->longitude * milliarcseconds, 'f', 0));
  108. writer->writeEndElement();
  109. }
  110. static void
  111. mapfactor_write(void)
  112. {
  113. writer->writeStartElement("favourites");
  114. writer->writeAttribute("version", "1");
  115. // TODO: This could be moved to wr_init, but the pre GPX version put the two
  116. // lines above this, so mimic that behaviour exactly.
  117. writer->setAutoFormatting(true);
  118. waypt_disp_all(mapfactor_waypt_pr);
  119. writer->writeEndElement();
  120. }
  121. ff_vecs_t mapfactor_vecs = {
  122. ff_type_file,
  123. { (ff_cap)(ff_cap_read | ff_cap_write), ff_cap_none, ff_cap_none },
  124. mapfactor_rd_init,
  125. mapfactor_wr_init,
  126. mapfactor_rd_deinit,
  127. mapfactor_wr_deinit,
  128. mapfactor_read,
  129. mapfactor_write,
  130. NULL,
  131. mapfactor_args,
  132. CET_CHARSET_UTF8, 0 /* CET-REVIEW */
  133. };