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.

geo.cc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright (C) 2002 Robert Lipe, robertlipe+source@gpsbabel.org
  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 <QtCore/QXmlStreamReader>
  17. #include <QtCore/QXmlStreamWriter>
  18. #include <QtCore/QDebug>
  19. #include "src/core/file.h"
  20. static char* deficon = NULL;
  21. static char* nuke_placer;
  22. static gbfile* ofd;
  23. static QString ostring;
  24. static QXmlStreamWriter writer(&ostring);
  25. static
  26. arglist_t geo_args[] = {
  27. {"deficon", &deficon, "Default icon name", NULL, ARGTYPE_STRING, ARG_NOMINMAX },
  28. {"nuke_placer", &nuke_placer, "Omit Placer name", NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
  29. ARG_TERMINATOR
  30. };
  31. #define MYNAME "geo"
  32. // This really should be class-local...
  33. QXmlStreamReader reader;
  34. QString geo_read_fname;
  35. geocache_container wpt_container(const QString&);
  36. void GeoReadLoc()
  37. {
  38. Waypoint* wpt = NULL;
  39. QString current_tag;
  40. while (!reader.atEnd()) {
  41. QStringRef tag_name = reader.name();
  42. if (reader.tokenType()==QXmlStreamReader::StartElement) {
  43. current_tag.append("/");
  44. current_tag.append(tag_name);
  45. if (current_tag == "/loc/waypoint") {
  46. wpt = new Waypoint;
  47. wpt->AllocGCData();
  48. // There is no 'unknown' alt value and so many reference files have
  49. // leaked it that we just paper over that here.
  50. wpt->altitude = 0;
  51. } else if (current_tag == "/loc/waypoint/name") {
  52. QXmlStreamAttributes a = reader.attributes();
  53. wpt->shortname = a.value("id").toString();
  54. wpt->description = reader.readElementText();
  55. } else if (current_tag == "/loc/waypoint/coord") {
  56. QXmlStreamAttributes a = reader.attributes();
  57. wpt->latitude = a.value("lat").toString().toDouble();
  58. wpt->longitude = a.value("lon").toString().toDouble();
  59. } else if (current_tag == "/loc/waypoint/type") {
  60. wpt->icon_descr = reader.readElementText();
  61. } else if (current_tag == "/loc/waypoint/link") {
  62. QXmlStreamAttributes a = reader.attributes();
  63. waypt_add_url(wpt,
  64. reader.readElementText(), a.value("text").toString());
  65. } else if (current_tag == "/loc/waypoint/difficulty") {
  66. wpt->gc_data->diff = reader.readElementText().toInt() * 10;
  67. } else if (current_tag == "/loc/waypoint/terrain") {
  68. wpt->gc_data->terr = reader.readElementText().toInt() * 10;
  69. } else if (current_tag == "/loc/waypoint/container") {
  70. wpt->gc_data->container = wpt_container(reader.readElementText());
  71. }
  72. }
  73. // The tokenType may have changed to EndElement as a result of readElementText.
  74. if (reader.tokenType() == QXmlStreamReader::EndElement) {
  75. if (current_tag == "/loc/waypoint") {
  76. waypt_add(wpt);
  77. }
  78. current_tag.chop(tag_name.length() + 1);
  79. }
  80. reader.readNext();
  81. }
  82. }
  83. static void
  84. geo_rd_init(const QString& fname)
  85. {
  86. geo_read_fname = fname;
  87. }
  88. static void
  89. geo_read(void)
  90. {
  91. gpsbabel::File file(geo_read_fname);
  92. file.open(QIODevice::ReadOnly);
  93. reader.setDevice(&file);
  94. GeoReadLoc();
  95. if (reader.hasError()) {
  96. fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
  97. qPrintable(reader.errorString()),
  98. qPrintable(file.fileName()),
  99. (long) reader.lineNumber(),
  100. (long) reader.columnNumber());
  101. }
  102. }
  103. geocache_container wpt_container(const QString& args)
  104. {
  105. geocache_container v;
  106. switch (args.toInt()) {
  107. case 1:
  108. v = gc_unknown;
  109. break;
  110. case 2:
  111. v = gc_micro;
  112. break;
  113. case 3:
  114. v = gc_regular;
  115. break;
  116. case 4:
  117. v = gc_large;
  118. break;
  119. case 5:
  120. v = gc_virtual;
  121. break;
  122. case 6:
  123. v = gc_other;
  124. break;
  125. case 8:
  126. v = gc_small;
  127. break;
  128. default:
  129. v = gc_unknown;
  130. break;
  131. }
  132. return v;
  133. }
  134. static void
  135. geo_rd_deinit(void)
  136. {
  137. }
  138. static void
  139. geo_wr_init(const QString& fname)
  140. {
  141. ofd = gbfopen(fname, "w", MYNAME);
  142. //writer.setAutoFormatting(true);
  143. writer.setAutoFormattingIndent(0);
  144. writer.writeStartDocument();
  145. }
  146. static void
  147. geo_wr_deinit(void)
  148. {
  149. writer.writeEndDocument();
  150. gbfputs(ostring,ofd);
  151. gbfclose(ofd);
  152. ofd = NULL;
  153. }
  154. static void
  155. geo_waypt_pr(const Waypoint* waypointp)
  156. {
  157. writer.writeStartElement("waypoint");
  158. writer.writeStartElement("name");
  159. writer.writeAttribute("id", waypointp->shortname);
  160. // TODO: this could be writeCharacters, but it's here for compat with pre
  161. // Qt writer.
  162. writer.writeCDATA(waypointp->description);
  163. writer.writeEndElement();
  164. writer.writeStartElement("coord");
  165. writer.writeAttribute("lat", QString::number(waypointp->latitude, 'f'));
  166. writer.writeAttribute("lon", QString::number(waypointp->longitude, 'f'));
  167. writer.writeEndElement();
  168. writer.writeTextElement("type", deficon ? deficon : waypointp->icon_descr);
  169. if (waypointp->HasUrlLink()) {
  170. writer.writeStartElement("link");
  171. writer.writeAttribute("text ", "Cache Details");
  172. UrlLink link = waypointp->GetUrlLink();
  173. writer.writeCharacters(link.url_);
  174. writer.writeEndElement();
  175. }
  176. if (waypointp->gc_data && waypointp->gc_data->diff) {
  177. writer.writeTextElement("difficulty",
  178. QString::number(waypointp->gc_data->diff/10));
  179. writer.writeTextElement("terrain",
  180. QString::number(waypointp->gc_data->terr/10));
  181. int v = 1;
  182. switch (waypointp->gc_data->container) {
  183. case gc_unknown:
  184. v = 1;
  185. break;
  186. case gc_micro:
  187. v = 2;
  188. break;
  189. case gc_regular:
  190. v = 3;
  191. break;
  192. case gc_large:
  193. v = 4;
  194. break;
  195. case gc_virtual:
  196. v = 5;
  197. break;
  198. case gc_other:
  199. v = 6;
  200. break;
  201. case gc_small:
  202. v = 8;
  203. break;
  204. default:
  205. v = 1;
  206. break;
  207. }
  208. writer.writeTextElement("container",
  209. QString::number(v));
  210. }
  211. writer.writeEndElement();
  212. }
  213. static void
  214. geo_write(void)
  215. {
  216. writer.writeStartElement("loc");
  217. writer.writeAttribute("version", "1.0");
  218. // TODO: This could be moved to wr_init, but the pre GPX version put the two
  219. // lines above this, so mimic that behaviour exactly.
  220. writer.setAutoFormatting(true);
  221. writer.writeAttribute("src", "EasyGPS");
  222. waypt_disp_all(geo_waypt_pr);
  223. writer.writeEndElement();
  224. }
  225. ff_vecs_t geo_vecs = {
  226. ff_type_file,
  227. { (ff_cap)(ff_cap_read | ff_cap_write), ff_cap_none, ff_cap_none },
  228. geo_rd_init,
  229. geo_wr_init,
  230. geo_rd_deinit,
  231. geo_wr_deinit,
  232. geo_read,
  233. geo_write,
  234. NULL,
  235. geo_args,
  236. CET_CHARSET_UTF8, 0 /* CET-REVIEW */
  237. };