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.

161 lines
3.6KB

  1. /*
  2. Support for Google Navigator tracklines (.trl).
  3. Copyright (C) 2008 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 "defs.h"
  17. #define MYNAME "gnav_trl"
  18. static
  19. arglist_t gnav_trl_args[] = {
  20. ARG_TERMINATOR
  21. };
  22. typedef struct gnav_trl_s {
  23. uint32_t time;
  24. float lat;
  25. float lon;
  26. uint32_t alt;
  27. } gnav_trl_t;
  28. static gbfile* fin, *fout;
  29. /*******************************************************************************
  30. * %%% global callbacks called by gpsbabel main process %%% *
  31. *******************************************************************************/
  32. static void
  33. gnav_trl_rd_init(const QString& fname)
  34. {
  35. fin = gbfopen_le(fname, "rb", MYNAME);
  36. }
  37. static void
  38. gnav_trl_rw_init(const QString& fname)
  39. {
  40. fout = gbfopen_le(fname, "wb", MYNAME);
  41. }
  42. static void
  43. gnav_trl_rd_deinit(void)
  44. {
  45. gbfclose(fin);
  46. }
  47. static void
  48. gnav_trl_rw_deinit(void)
  49. {
  50. gbfclose(fout);
  51. }
  52. static double
  53. read_altitude(void* ptr)
  54. {
  55. unsigned char* i = (unsigned char*) ptr;
  56. char buf[sizeof(float)];
  57. le_write32(&buf, i[2] << 24 | i[1] << 16 | i[0] <<8 | i[3]);
  58. return le_read_float(&buf);
  59. }
  60. static void
  61. write_altitude(void* ptr, const float alt)
  62. {
  63. char buf[sizeof(float)];
  64. unsigned char* i = (unsigned char*) &buf;
  65. le_write_float(&buf, alt);
  66. le_write32(ptr, i[0] << 24 | i[3] << 16 | i[2] << 8 | i[1]);
  67. }
  68. static void
  69. gnav_trl_read(void)
  70. {
  71. route_head* trk = NULL;
  72. while (! gbfeof(fin)) {
  73. gnav_trl_t rec;
  74. Waypoint* wpt;
  75. if (gbfread(&rec, sizeof(rec), 1, fin) != 1) {
  76. fatal(MYNAME ": Unexpected EOF (end of file)!\n");
  77. }
  78. wpt = new Waypoint;
  79. wpt->SetCreationTime(le_read32(&rec.time));
  80. wpt->latitude = le_read_float(&rec.lat);
  81. wpt->longitude = le_read_float(&rec.lon);
  82. wpt->altitude = read_altitude(&rec.alt);
  83. if (trk == NULL) {
  84. trk = route_head_alloc();
  85. track_add_head(trk);
  86. }
  87. track_add_wpt(trk, wpt);
  88. }
  89. }
  90. static void
  91. gnav_trl_write_trkpt(const Waypoint* wpt)
  92. {
  93. gnav_trl_t rec;
  94. le_write32(&rec.time, wpt->GetCreationTime().toTime_t());
  95. le_write_float(&rec.lat, wpt->latitude);
  96. le_write_float(&rec.lon, wpt->longitude);
  97. if (wpt->altitude != unknown_alt) {
  98. write_altitude(&rec.alt, wpt->altitude);
  99. } else {
  100. write_altitude(&rec.alt, 0);
  101. }
  102. gbfwrite(&rec, sizeof(rec), 1, fout);
  103. }
  104. static void
  105. gnav_trl_write(void)
  106. {
  107. track_disp_all(NULL, NULL, gnav_trl_write_trkpt);
  108. }
  109. /**************************************************************************/
  110. ff_vecs_t gnav_trl_vecs = {
  111. ff_type_file,
  112. {
  113. ff_cap_none /* waypoints */,
  114. (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */,
  115. ff_cap_none /* routes */
  116. },
  117. gnav_trl_rd_init,
  118. gnav_trl_rw_init,
  119. gnav_trl_rd_deinit,
  120. gnav_trl_rw_deinit,
  121. gnav_trl_read,
  122. gnav_trl_write,
  123. NULL,
  124. gnav_trl_args,
  125. CET_CHARSET_UTF8, 1 /* CET - do nothing ! */
  126. };
  127. /**************************************************************************/