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.

dmtlog.cc 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. Support for TrackLogs digital mapping (.trl) files,
  3. Copyright (C) 2006,2007 Olaf Klein, o.b.klein@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include <QtCore/QXmlStreamAttributes>
  17. #include "defs.h"
  18. #include "jeeps/gpsmath.h"
  19. #include "xmlgeneric.h"
  20. #define MYNAME "dmtlog"
  21. #define DEFLATE_BUFF_SIZE 16384
  22. static gbfile* fin, *fout;
  23. static char* xmlbin;
  24. static Waypoint* xmlwpt;
  25. static route_head* xmltrk;
  26. static QString xmlgrid;
  27. static int xmldatum;
  28. static double xmlEasting, xmlNorthing;
  29. static double xmlLatitude, xmlLongitude;
  30. static double xmlAltitude;
  31. #if !ZLIB_INHIBITED
  32. static int xmlbinsize;
  33. #endif
  34. static char header_written;
  35. static char* opt_index;
  36. static int track_index, this_index;
  37. static
  38. arglist_t dmtlog_args[] = {
  39. {
  40. "index", &opt_index,
  41. "Index of track (if more than one in source)", "1", ARGTYPE_INT, "1", NULL
  42. },
  43. ARG_TERMINATOR
  44. };
  45. #if !ZLIB_INHIBITED
  46. static xg_callback tlog3a_xgcb_version, tlog3a_xgcb_length, tlog3a_xgcb_data;
  47. static xg_tag_mapping tlog3a_xgcb_map[] = {
  48. { tlog3a_xgcb_version, cb_cdata, "/CXMLSafe/Version" },
  49. { tlog3a_xgcb_length, cb_cdata, "/CXMLSafe/Length" },
  50. { tlog3a_xgcb_data, cb_cdata, "/CXMLSafe/Data" },
  51. { NULL, (xg_cb_type)0, NULL}
  52. };
  53. #endif
  54. static xg_callback tlog3b_xgcb_tfna, tlog3b_xgcb_tfdes;
  55. static xg_callback tlog3b_xgcb_wptst, tlog3b_xgcb_tptst;
  56. static xg_callback tlog3b_xgcb_tpten, tlog3b_xgcb_wpten;
  57. static xg_callback tlog3b_xgcb_wptid, tlog3b_xgcb_wptdt;
  58. static xg_callback tlog3b_xgcb_wptgr, tlog3b_xgcb_wptea;
  59. static xg_callback tlog3b_xgcb_wptno, tlog3b_xgcb_wptal;
  60. static xg_callback tlog3b_xgcb_tptdt;
  61. static xg_tag_mapping tlog3b_xgcb_map[] = {
  62. { tlog3b_xgcb_tfna, cb_cdata, "/CTrackFile/Name" },
  63. { tlog3b_xgcb_tfdes, cb_cdata, "/CTrackFile/Description" },
  64. { tlog3b_xgcb_wptst, cb_start, "/CTrackFile/CWayPoint" },
  65. { tlog3b_xgcb_wptid, cb_cdata, "/CTrackFile/CWayPoint/Id" },
  66. { tlog3b_xgcb_wptdt, cb_cdata, "/CTrackFile/CWayPoint/Datum" },
  67. { tlog3b_xgcb_wptgr, cb_cdata, "/CTrackFile/CWayPoint/Grid" },
  68. { tlog3b_xgcb_wptea, cb_cdata, "/CTrackFile/CWayPoint/Easting" },
  69. { tlog3b_xgcb_wptno, cb_cdata, "/CTrackFile/CWayPoint/Northing" },
  70. { tlog3b_xgcb_wptal, cb_cdata, "/CTrackFile/CWayPoint/Altitude" },
  71. { tlog3b_xgcb_wpten, cb_end, "/CTrackFile/CWayPoint" },
  72. { tlog3b_xgcb_tptst, cb_start, "/CTrackFile/CTrackPoint" },
  73. { tlog3b_xgcb_wptid, cb_cdata, "/CTrackFile/CTrackPoint/Id" },
  74. { tlog3b_xgcb_tptdt, cb_cdata, "/CTrackFile/CTrackPoint/Datum" },
  75. { tlog3b_xgcb_wptgr, cb_cdata, "/CTrackFile/CTrackPoint/Grid" },
  76. { tlog3b_xgcb_wptea, cb_cdata, "/CTrackFile/CTrackPoint/Easting" },
  77. { tlog3b_xgcb_wptno, cb_cdata, "/CTrackFile/CTrackPoint/Northing" },
  78. { tlog3b_xgcb_wptal, cb_cdata, "/CTrackFile/CTrackPoint/Altitude" },
  79. { tlog3b_xgcb_tpten, cb_end, "/CTrackFile/CTrackPoint" },
  80. { NULL, (xg_cb_type)0, NULL}
  81. };
  82. /* helpers */
  83. static void
  84. convert_datum(Waypoint* wpt, int datum)
  85. {
  86. if (datum != DATUM_WGS84) {
  87. double lat = wpt->latitude;
  88. double lon = wpt->longitude;
  89. double alt = wpt->altitude;
  90. GPS_Math_Known_Datum_To_WGS84_C(lat, lon, alt,
  91. &wpt->latitude, &wpt->longitude, &wpt->altitude,
  92. datum);
  93. }
  94. }
  95. static void
  96. finalize_pt(Waypoint* wpt)
  97. {
  98. if (xmlgrid == "BNG") {
  99. GPS_Math_NGENToAiry1830LatLon(xmlEasting, xmlNorthing,
  100. &wpt->latitude, &wpt->longitude);
  101. xmldatum = DATUM_OSGB36;
  102. } else {
  103. wpt->latitude = xmlLatitude;
  104. wpt->longitude = xmlLongitude;
  105. }
  106. /* NOTE:
  107. * Alan White reports this program actually subtracts a number
  108. * of meters ranging between 46 and 50 meters. It appears to be
  109. * constant for each location, but different without an obvious
  110. * correlation to ground altitude. We considered offsetting this
  111. * in GPSBabel, but concluded it wasn't worth the bother.
  112. * If we get complaints, probably all of our alt reading and writing
  113. * should offset an average of 46m or so.
  114. */
  115. wpt->altitude = xmlAltitude;
  116. convert_datum(wpt, xmldatum);
  117. }
  118. /* xml-reader callbacks */
  119. #if !ZLIB_INHIBITED
  120. static void
  121. tlog3a_xgcb_version(xg_string args, const QXmlStreamAttributes*)
  122. {
  123. if (args != "1") {
  124. fatal(MYNAME ": Unsupported file version '%s'!\n", qPrintable(args));
  125. }
  126. }
  127. static void
  128. tlog3a_xgcb_length(xg_string args, const QXmlStreamAttributes*)
  129. {
  130. }
  131. static void
  132. tlog3a_xgcb_data(xg_string args, const QXmlStreamAttributes*)
  133. {
  134. int len;
  135. char* bin;
  136. char* cin, *cout;
  137. char cl, ch;
  138. #if NEW_STRINGS
  139. // This function needs rethinking.
  140. len = args.length();
  141. #else
  142. len = strlen(args);
  143. #endif
  144. bin = (char*) xmalloc((len >> 1) + 1);
  145. #if NEW_STRINGS
  146. char* cincopy = xstrdup(args);
  147. cin = cincopy;
  148. #else
  149. cin = (char*)args;
  150. #endif
  151. cout = bin;
  152. cl = 0x10;
  153. while (*cin) {
  154. char c = *cin++;
  155. if (c == '\0') {
  156. break;
  157. } else if ((c >= 'A') && (c <= 'F')) {
  158. c -= 'A' - 10;
  159. } else if ((c >= 'a') && (c <= 'f')) {
  160. c -= 'a' - 10;
  161. } else if ((c >= '0') && (c <= '9')) {
  162. c -= '0';
  163. } else {
  164. continue;
  165. }
  166. if (cl == 0x10) {
  167. cl = c;
  168. } else {
  169. ch = (cl << 4) | c;
  170. *cout++ = ch;
  171. cl = 0x10;
  172. }
  173. }
  174. xmlbin = bin;
  175. xmlbinsize = (cout - bin);
  176. #if NEW_STRINGS
  177. xfree(cincopy);
  178. #endif
  179. }
  180. #endif
  181. static void
  182. tlog3b_xgcb_tfna(xg_string args, const QXmlStreamAttributes*)
  183. {
  184. if (xmltrk == NULL) {
  185. xmltrk = route_head_alloc();
  186. track_add_head(xmltrk);
  187. }
  188. xmltrk->rte_name = args;
  189. }
  190. static void
  191. tlog3b_xgcb_tfdes(xg_string args, const QXmlStreamAttributes*)
  192. {
  193. if (xmltrk == NULL) {
  194. xmltrk = route_head_alloc();
  195. track_add_head(xmltrk);
  196. }
  197. xmltrk->rte_desc = args;
  198. }
  199. static void
  200. tlog3b_xgcb_wptst(xg_string args, const QXmlStreamAttributes*)
  201. {
  202. xmlwpt = new Waypoint;
  203. xmldatum = DATUM_WGS84;
  204. }
  205. static void
  206. tlog3b_xgcb_tptst(xg_string args, const QXmlStreamAttributes*)
  207. {
  208. xmlwpt = new Waypoint;
  209. xmldatum = DATUM_WGS84;
  210. }
  211. static void
  212. tlog3b_xgcb_tpten(xg_string args, const QXmlStreamAttributes*)
  213. {
  214. finalize_pt(xmlwpt);
  215. if (xmltrk == NULL) {
  216. xmltrk = route_head_alloc();
  217. track_add_head(xmltrk);
  218. }
  219. track_add_wpt(xmltrk, xmlwpt);
  220. xmlwpt = NULL;
  221. }
  222. static void
  223. tlog3b_xgcb_wptid(xg_string args, const QXmlStreamAttributes*)
  224. {
  225. xmlwpt->shortname = args;
  226. }
  227. static void
  228. tlog3b_xgcb_wptdt(xg_string args, const QXmlStreamAttributes*)
  229. {
  230. xmldatum = GPS_Lookup_Datum_Index(args);
  231. }
  232. static void
  233. tlog3b_xgcb_wptgr(xg_string args, const QXmlStreamAttributes*)
  234. {
  235. xmlgrid = args;
  236. /*
  237. if (xmlgrid != NULL) {
  238. if (strcmp(xmlgrid, args) == 0) {
  239. return;
  240. }
  241. xfree(xmlgrid);
  242. }
  243. xmlgrid = xstrdup(args);
  244. */
  245. }
  246. static void
  247. tlog3b_xgcb_wptno(xg_string args, const QXmlStreamAttributes*)
  248. {
  249. xmlNorthing = args.toDouble();
  250. }
  251. static void
  252. tlog3b_xgcb_wptea(xg_string args, const QXmlStreamAttributes*)
  253. {
  254. xmlEasting = args.toDouble();
  255. }
  256. static void
  257. tlog3b_xgcb_wptal(xg_string args, const QXmlStreamAttributes*)
  258. {
  259. xmlAltitude = args.toDouble();
  260. }
  261. static void
  262. tlog3b_xgcb_tptdt(xg_string args, const QXmlStreamAttributes*)
  263. {
  264. xmldatum = GPS_Lookup_Datum_Index(args);
  265. }
  266. static void
  267. tlog3b_xgcb_wpten(xg_string args, const QXmlStreamAttributes*)
  268. {
  269. finalize_pt(xmlwpt);
  270. waypt_add(xmlwpt);
  271. xmlwpt = NULL;
  272. }
  273. static char*
  274. read_str(gbfile* f)
  275. {
  276. int i;
  277. char* res;
  278. i = gbfgetc(f);
  279. if (i == 0xff) {
  280. i = gbfgetint16(f);
  281. }
  282. res = (char*) xmalloc(i + 1);
  283. res[i] = '\0';
  284. if (i) {
  285. gbfread(res, 1, i, f);
  286. }
  287. return res;
  288. }
  289. static void
  290. write_str(const char* str, gbfile* f)
  291. {
  292. if (str && *str) {
  293. int len = strlen(str);
  294. if (len > 0xfe) {
  295. #if 0
  296. if (len > 0x7fff) {
  297. len = 0x7fff;
  298. }
  299. gbfputc((unsigned char) 0xff, f);
  300. gbfputint16(len, f);
  301. #else
  302. len = 0xfe;
  303. gbfputc(len, f);
  304. #endif
  305. } else {
  306. gbfputc(len, f);
  307. }
  308. gbfwrite(str, len, 1, f);
  309. } else {
  310. gbfputc(0, f);
  311. }
  312. }
  313. static void
  314. write_str(const QString& str, gbfile* f)
  315. {
  316. write_str(CSTR(str), f);
  317. }
  318. static int
  319. read_datum(gbfile* f)
  320. {
  321. int res;
  322. char* d, *g;
  323. d = read_str(f);
  324. g = read_str(f);
  325. res = GPS_Lookup_Datum_Index(d);
  326. if (*g && (strcmp(d, g) != 0)) {
  327. fatal(MYNAME ": Unsupported combination of datum '%s' and grid '%s'!\n",
  328. d, g);
  329. }
  330. xfree(d);
  331. xfree(g);
  332. return res;
  333. }
  334. static void
  335. read_CTrackFile(const int version)
  336. {
  337. char buf[128];
  338. int32_t ver;
  339. int32_t tcount, wcount;
  340. int16_t u1;
  341. int32_t ux;
  342. route_head* track;
  343. int i;
  344. u1 = gbfgetint16(fin);
  345. gbfread(buf, 1, 10, fin);
  346. if ((u1 != 0x0a) || (strncmp("CTrackFile", buf, 10) != 0)) {
  347. fatal(MYNAME ": Unknown or invalid track file.\n");
  348. }
  349. if (version == 8) {
  350. gbfseek(fin, 36, SEEK_CUR); /* skip unknown 36 bytes */
  351. }
  352. ver = gbfgetint32(fin);
  353. if (ver != version) {
  354. fatal(MYNAME ": Unknown or invalid track file (%d).\n", ver);
  355. }
  356. ux = gbfgetint32(fin); // Unknown 2
  357. ux = gbfgetint32(fin); // Unknown 3
  358. ux = gbfgetint32(fin); // Unknown 4
  359. (void) ux; // Silence warning.
  360. track = route_head_alloc();
  361. track_add_head(track);
  362. /* S1 .. S9: comments, hints, jokes, aso */
  363. for (i = 0; i < 9; i++) {
  364. char* s = read_str(fin);
  365. xfree(s);
  366. }
  367. tcount = gbfgetint32(fin);
  368. int datum = 118;
  369. if (tcount > 0) {
  370. datum = read_datum(fin);
  371. if (version == 8) {
  372. int len;
  373. gbfread(buf, 1, 4, fin);
  374. len = gbfgetint16(fin);
  375. gbfseek(fin, len, SEEK_CUR);
  376. }
  377. }
  378. while (tcount > 0) {
  379. Waypoint* wpt;
  380. tcount--;
  381. if (version == 8) {
  382. datum = read_datum(fin);
  383. }
  384. wpt = new Waypoint;
  385. wpt->latitude = gbfgetdbl(fin);
  386. wpt->longitude = gbfgetdbl(fin);
  387. wpt->altitude = gbfgetdbl(fin);
  388. if (datum < 0) {
  389. fatal(MYNAME ": Invalid datum %d found", datum);
  390. }
  391. convert_datum(wpt, datum);
  392. track_add_wpt(track, wpt);
  393. if (version == 8) {
  394. gbfseek(fin, 34, SEEK_CUR); /* skip unknown 34 bytes */
  395. }
  396. }
  397. if (version == 8) {
  398. i = gbfgetint16(fin);
  399. i = gbfgetc(fin);
  400. if (i == 0) {
  401. return;
  402. }
  403. gbfungetc(i, fin);
  404. datum = read_datum(fin);
  405. (void) gbfgetint16(fin);
  406. (void) gbfgetint32(fin);
  407. gbfread(buf, 1, 9, fin);
  408. if (strncmp(buf, "CWayPoint", 9) != 0) {
  409. warning(MYNAME ": Unsupported waypoint structure!\n");
  410. return;
  411. }
  412. while (! gbfeof(fin)) {
  413. Waypoint* wpt;
  414. i = gbfgetc(fin);
  415. if (i == 0) {
  416. break;
  417. }
  418. gbfungetc(i, fin);
  419. datum = read_datum(fin);
  420. wpt = new Waypoint;
  421. wpt->latitude = gbfgetdbl(fin);
  422. wpt->longitude = gbfgetdbl(fin);
  423. wpt->altitude = gbfgetdbl(fin);
  424. gbfseek(fin, 36, SEEK_CUR); /* skip unknown 36 bytes */
  425. wpt->notes = read_str(fin);
  426. wpt->description = read_str(fin);
  427. (void) gbfgetint16(fin);
  428. waypt_add(wpt);
  429. }
  430. return;
  431. }
  432. wcount = gbfgetint32(fin);
  433. if (wcount == 0) {
  434. return;
  435. }
  436. datum = read_datum(fin);
  437. while (wcount > 0) {
  438. Waypoint* wpt;
  439. int32_t namect, i;
  440. wcount--;
  441. wpt = new Waypoint;
  442. wpt->latitude = gbfgetdbl(fin);
  443. wpt->longitude = gbfgetdbl(fin);
  444. wpt->altitude = gbfgetdbl(fin);
  445. convert_datum(wpt, datum);
  446. namect = gbfgetint32(fin);
  447. // variants of shortname
  448. for (i = 0; i < namect; i++) {
  449. char* name;
  450. name = read_str(fin);
  451. if (name && *name) {
  452. switch (i) {
  453. case 0:
  454. wpt->description = name;
  455. break;
  456. case 1:
  457. wpt->shortname = name;
  458. break;
  459. }
  460. }
  461. xfree(name);
  462. }
  463. waypt_add(wpt);
  464. }
  465. }
  466. #if !ZLIB_INHIBITED
  467. static int
  468. inflate_buff(const char* buff, const size_t size, char** out_buff)
  469. {
  470. int res = Z_OK;
  471. z_stream strm;
  472. char out[DEFLATE_BUFF_SIZE];
  473. char* cout = NULL;
  474. uint32_t bytes = 0;
  475. uint32_t have;
  476. strm.zalloc = Z_NULL;
  477. strm.zfree = Z_NULL;
  478. strm.opaque = Z_NULL;
  479. strm.avail_in = 0;
  480. strm.next_in = Z_NULL;
  481. res = inflateInit(&strm);
  482. if (res != Z_OK) {
  483. return res;
  484. }
  485. strm.avail_in = size;
  486. strm.next_in = (Bytef*)buff;
  487. do {
  488. strm.avail_out = DEFLATE_BUFF_SIZE;
  489. strm.next_out = (Bytef*)out;
  490. res = inflate(&strm, Z_NO_FLUSH);
  491. switch (res) {
  492. case Z_NEED_DICT:
  493. res = Z_DATA_ERROR; /* and fall through */
  494. case Z_DATA_ERROR:
  495. case Z_MEM_ERROR:
  496. (void)inflateEnd(&strm);
  497. return res;
  498. }
  499. have = DEFLATE_BUFF_SIZE - strm.avail_out;
  500. if (have > 0) {
  501. cout = (char*) xrealloc(cout, bytes + have);
  502. memcpy(cout+bytes, out, have);
  503. bytes+=have;
  504. }
  505. } while (strm.avail_out == 0);
  506. *out_buff = cout;
  507. return res;
  508. }
  509. static void
  510. read_CXMLSafe(void)
  511. {
  512. char* xmlstr = NULL;
  513. xmlbin = NULL;
  514. xmlbinsize = 0;
  515. xml_init(fin->name, tlog3a_xgcb_map, NULL);
  516. xml_read();
  517. xml_deinit();
  518. if (xmlbin != NULL) {
  519. inflate_buff(xmlbin, xmlbinsize, &xmlstr);
  520. xfree(xmlbin);
  521. xml_init(NULL, tlog3b_xgcb_map, NULL);
  522. xml_readstring(xmlstr);
  523. xml_deinit();
  524. xfree(xmlstr);
  525. }
  526. }
  527. #endif
  528. static void
  529. read_XML(void)
  530. {
  531. xml_init(fin->name, tlog3b_xgcb_map, NULL);
  532. xml_read();
  533. xml_deinit();
  534. return;
  535. }
  536. /*******************************************************************************
  537. * %%% global callbacks called by gpsbabel main process %%% *
  538. *******************************************************************************/
  539. static void
  540. dmtlog_rd_init(const QString& fname)
  541. {
  542. fin = gbfopen_le(fname, "rb", MYNAME);
  543. xmlbin = NULL;
  544. xmltrk = NULL;
  545. xmlwpt = NULL;
  546. xmlgrid = QString();
  547. }
  548. static void
  549. dmtlog_rd_deinit(void)
  550. {
  551. gbfclose(fin);
  552. }
  553. static void
  554. dmtlog_read(void)
  555. {
  556. switch (gbfgetuint32(fin)) {
  557. case 0x4FFFF:
  558. read_CTrackFile(4);
  559. break;
  560. case 0x8FFFF:
  561. read_CTrackFile(8);
  562. break;
  563. case 0x4d58433c:
  564. #if !ZLIB_INHIBITED
  565. read_CXMLSafe();
  566. #else
  567. fatal(MYNAME ": Zlib was not included in this build.\n");
  568. #endif
  569. break;
  570. case 0x7254433c:
  571. read_XML();
  572. break;
  573. default:
  574. fatal(MYNAME ": Unknown or unsupported file type.\n");
  575. }
  576. }
  577. static void
  578. dmtlog_wr_init(const QString& fname)
  579. {
  580. fout = gbfopen_le(fname, "wb", MYNAME);
  581. }
  582. static void
  583. dmtlog_wr_deinit(void)
  584. {
  585. gbfclose(fout);
  586. }
  587. static void
  588. write_header(const route_head* trk)
  589. {
  590. int count, i;
  591. const char ZERO = '\0';
  592. header_written = 1;
  593. count = 0;
  594. if (trk != NULL) {
  595. queue* curr, *prev;
  596. QUEUE_FOR_EACH(&trk->waypoint_list, curr, prev) count++;
  597. }
  598. if (!trk || trk->rte_name.isEmpty()) {
  599. write_str("Name", fout);
  600. } else {
  601. write_str(trk->rte_name, fout);
  602. }
  603. // This fails for internationalization, but as this text is in the
  604. // file itself, it shouldn't be localized.
  605. QString cout = QString::number(count) + " trackpoints and " +
  606. QString::number(waypt_count()) + " waypoints";
  607. write_str(cout, fout);
  608. for (i = 3; i <= 8; i++) {
  609. gbfputc(ZERO, fout);
  610. }
  611. write_str("GPSBabel", fout);
  612. gbfputint32(count, fout);
  613. if (count > 0) {
  614. write_str("WGS84", fout);
  615. write_str("WGS84", fout);
  616. }
  617. }
  618. static void
  619. track_hdr_cb(const route_head* trk)
  620. {
  621. this_index++;
  622. if (this_index != track_index) {
  623. return;
  624. }
  625. write_header(trk);
  626. }
  627. static void
  628. track_tlr_cb(const route_head* trk)
  629. {
  630. }
  631. static void
  632. track_wpt_cb(const Waypoint* wpt)
  633. {
  634. if (this_index != track_index) {
  635. return;
  636. }
  637. gbfputdbl(wpt->latitude, fout);
  638. gbfputdbl(wpt->longitude, fout);
  639. gbfputdbl(wpt->altitude != unknown_alt ? wpt->altitude : 0, fout);
  640. }
  641. static void
  642. wpt_cb(const Waypoint* wpt)
  643. {
  644. int names;
  645. gbfputdbl(wpt->latitude, fout);
  646. gbfputdbl(wpt->longitude, fout);
  647. gbfputdbl(wpt->altitude != unknown_alt ? wpt->altitude : 0, fout);
  648. names = 1;
  649. if (!wpt->description.isEmpty()) {
  650. names = 2;
  651. }
  652. gbfputint32(names, fout);
  653. if (names > 1) {
  654. write_str(wpt->description, fout);
  655. }
  656. write_str(wpt->shortname.isEmpty() ? "Name" : wpt->shortname, fout);
  657. }
  658. static void
  659. dmtlog_write(void)
  660. {
  661. track_index = atoi(opt_index);
  662. /* ... validate index */
  663. gbfputint32(0x4FFFF, fout);
  664. gbfputuint16(0x0A, fout);
  665. gbfputs("CTrackFile", fout);
  666. gbfputint32(4, fout);
  667. gbfputint32(1, fout);
  668. gbfputint32(0x100001, fout);
  669. gbfputuint32((const uint32_t)gpsbabel_time, fout);
  670. header_written = 0;
  671. this_index = 0;
  672. track_disp_all(track_hdr_cb, track_tlr_cb, track_wpt_cb);
  673. if (!header_written) {
  674. write_header(NULL);
  675. }
  676. gbfputint32(waypt_count(), fout);
  677. if (waypt_count() > 0) {
  678. write_str("WGS84", fout);
  679. write_str("WGS84", fout);
  680. waypt_disp_all(wpt_cb);
  681. }
  682. }
  683. /**************************************************************************/
  684. ff_vecs_t dmtlog_vecs = {
  685. ff_type_file,
  686. {
  687. (ff_cap)(ff_cap_read | ff_cap_write) /* waypoints */,
  688. (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */,
  689. ff_cap_none /* routes */
  690. },
  691. dmtlog_rd_init,
  692. dmtlog_wr_init,
  693. dmtlog_rd_deinit,
  694. dmtlog_wr_deinit,
  695. dmtlog_read,
  696. dmtlog_write,
  697. NULL,
  698. dmtlog_args,
  699. CET_CHARSET_ASCII, 0
  700. };
  701. /**************************************************************************/