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.

bushnell_trl.cc 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Read and write Bushnell trail *.trl files.
  3. Copyright (C) 2009 Robert Lipe (robertlipe+source@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 "Bushnell Trail"
  18. static gbfile* file_in;
  19. static gbfile* file_out;
  20. static int trkpt_count;
  21. static route_head* trk_head;
  22. static
  23. arglist_t bushnell_args[] = {
  24. ARG_TERMINATOR
  25. };
  26. static void
  27. rd_init(const QString& fname)
  28. {
  29. char h[0x14]; // Believed to be zero terminated.
  30. file_in = gbfopen_le(fname, "rb", MYNAME);
  31. gbfread(h, 1, sizeof(h), file_in);
  32. trk_head = route_head_alloc();
  33. track_add_head(trk_head);
  34. trk_head->rte_name = lrtrim(h);
  35. }
  36. static void
  37. rd_deinit(void)
  38. {
  39. gbfclose(file_in);
  40. }
  41. static void
  42. wr_init(const QString& fname)
  43. {
  44. int i;
  45. int l = fname.length();
  46. char obuf[20] = { 0 } ;
  47. char* p = obuf;
  48. file_out = gbfopen_le(fname, "w", MYNAME);
  49. trkpt_count = 0;
  50. QString fnameu = fname.toUpper();
  51. for (i = 0; (i < l) && (i < 20); i++) {
  52. char c = fnameu[i].toLatin1();
  53. if (isalnum(c)) {
  54. *p++ = c;
  55. }
  56. if (c == '.') {
  57. break;
  58. }
  59. }
  60. gbfwrite(&obuf, 1, 20, file_out);
  61. }
  62. static void
  63. wr_deinit(void)
  64. {
  65. int i = trkpt_count;
  66. while (i < 4502) {
  67. gbfputint32(0, file_out);
  68. gbfputint32(0, file_out);
  69. i++;
  70. }
  71. gbfputint32(0, file_out);
  72. gbfputint16(trkpt_count, file_out);
  73. gbfputc(1, file_out);
  74. gbfclose(file_out);
  75. }
  76. /*
  77. * Each file contains a single waypoint.
  78. */
  79. static void
  80. bushnell_read(void)
  81. {
  82. int lat_tmp,lon_tmp;
  83. while (1) {
  84. Waypoint* wpt_tmp;
  85. lat_tmp = gbfgetint32(file_in);
  86. lon_tmp = gbfgetint32(file_in);
  87. if (!lat_tmp && !lon_tmp) {
  88. break;
  89. }
  90. wpt_tmp = new Waypoint;
  91. wpt_tmp->latitude = lat_tmp / 10000000.0;
  92. wpt_tmp->longitude = lon_tmp / 10000000.0;
  93. track_add_wpt(trk_head, wpt_tmp);
  94. }
  95. }
  96. static void
  97. bushnell_write_one(const Waypoint* wpt)
  98. {
  99. int32_t lat = wpt->latitude * 10000000.0;
  100. int32_t lon = wpt->longitude * 10000000.0;
  101. trkpt_count++;
  102. if (trkpt_count > 4502) {
  103. fatal(MYNAME " too many trackpoints. Max is 4502.");
  104. }
  105. gbfputint32(lat, file_out);
  106. gbfputint32(lon, file_out);
  107. }
  108. static void
  109. bushnell_write(void)
  110. {
  111. track_disp_all(NULL, NULL, bushnell_write_one);
  112. }
  113. ff_vecs_t bushnell_trl_vecs = {
  114. ff_type_file,
  115. { ff_cap_none, (ff_cap)(ff_cap_read | ff_cap_write), ff_cap_none },
  116. rd_init,
  117. wr_init,
  118. rd_deinit,
  119. wr_deinit,
  120. bushnell_read,
  121. bushnell_write,
  122. NULL,
  123. bushnell_args,
  124. CET_CHARSET_MS_ANSI, 0 /* Not really sure... */
  125. };