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.

gpsutil.cc 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Access gpsutil files.
  3. Copyright (C) 2002, 2003, 2004 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. #include "cet_util.h"
  18. #include "magellan.h"
  19. #include <cmath>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. static gbfile* file_in, *file_out;
  23. static short_handle mkshort_handle;
  24. #define MYNAME "GPSUTIL"
  25. static void
  26. rd_init(const QString& fname)
  27. {
  28. file_in = gbfopen(fname, "rb", MYNAME);
  29. }
  30. static void
  31. rd_deinit(void)
  32. {
  33. gbfclose(file_in);
  34. }
  35. static void
  36. wr_init(const QString& fname)
  37. {
  38. file_out = gbfopen(fname, "w", MYNAME);
  39. mkshort_handle = mkshort_new_handle();
  40. }
  41. static void
  42. wr_deinit(void)
  43. {
  44. gbfclose(file_out);
  45. mkshort_del_handle(&mkshort_handle);
  46. }
  47. static void
  48. data_read(void)
  49. {
  50. char* ibuf;
  51. char desc[31];
  52. double lat,lon;
  53. char latdir, londir;
  54. int ilat, ilon;
  55. long alt;
  56. char alttype;
  57. char icon[3];
  58. Waypoint* wpt_tmp;
  59. int line = 0;
  60. /*
  61. * Make sure that all waypoints in single read have same
  62. * timestamp.
  63. */
  64. time_t now = current_time().toTime_t();
  65. icon[0] = 0;
  66. while ((ibuf = gbfgetstr(file_in))) {
  67. int n, len;
  68. char* sn;
  69. if ((line++ == 0) && file_in->unicode) {
  70. cet_convert_init(CET_CHARSET_UTF8, 1);
  71. }
  72. /* A sharp in column zero or an blank line is a comment */
  73. ibuf = lrtrim(ibuf);
  74. len = strlen(ibuf);
  75. if ((len == 0) || (*ibuf == '#')) {
  76. continue;
  77. }
  78. if (len > 71) {
  79. int offs = len - 71;
  80. sn = xstrndup(ibuf, offs + 8);
  81. ibuf += (offs + 9);
  82. } else {
  83. sn = xstrndup(ibuf, 8);
  84. ibuf += 9;
  85. }
  86. n = sscanf(ibuf, "%lf%c %lf%c %ld%c %30[^,] %2s",
  87. &lat, &latdir, &lon, &londir,
  88. &alt, &alttype, desc, icon);
  89. /* Require at least first threee fields, otherwise ignore */
  90. if (n < 2) {
  91. xfree(sn);
  92. continue;
  93. }
  94. rtrim(sn);
  95. rtrim(desc);
  96. rtrim(icon);
  97. wpt_tmp = new Waypoint;
  98. wpt_tmp->altitude = alt;
  99. wpt_tmp->shortname = sn;
  100. xfree(sn);
  101. wpt_tmp->description = desc;
  102. wpt_tmp->SetCreationTime(now);
  103. if (latdir == 'S') {
  104. lat = -lat;
  105. }
  106. if (londir == 'W') {
  107. lon = -lon;
  108. }
  109. lat /= 100.0;
  110. lon /= 100.0;
  111. ilon = (int)(lon);
  112. wpt_tmp->longitude = ilon + (lon - ilon)*(100.0/60.0);
  113. ilat = (int)(lat);
  114. wpt_tmp->latitude = ilat + (lat - ilat) * (100.0/60.0);
  115. wpt_tmp->icon_descr = mag_find_descr_from_token(icon);
  116. waypt_add(wpt_tmp);
  117. }
  118. }
  119. static void
  120. gpsutil_disp(const Waypoint* wpt)
  121. {
  122. double lon,lat;
  123. QString icon_token;
  124. char* tdesc = xstrdup(wpt->description);
  125. icon_token = mag_find_token_from_descr(wpt->icon_descr);
  126. lon = degrees2ddmm(wpt->longitude);
  127. lat = degrees2ddmm(wpt->latitude);
  128. gbfprintf(file_out, "%-8.8s %08.3f%c %09.3f%c %07.0f%c %-30.30s %s\n",
  129. global_opts.synthesize_shortnames ?
  130. CSTRc(mkshort_from_wpt(mkshort_handle, wpt)) :
  131. CSTRc(wpt->shortname),
  132. fabs(lat),
  133. lat < 0.0 ? 'S' : 'N',
  134. fabs(lon),
  135. lon < 0.0 ? 'W' : 'E',
  136. ((wpt->altitude == unknown_alt) ||
  137. (wpt->altitude < 0.0)) ? 0 : wpt->altitude,
  138. 'm',
  139. CSTRc(wpt->description) ? tdesc : "",
  140. CSTR(icon_token));
  141. xfree(tdesc);
  142. }
  143. static void
  144. data_write(void)
  145. {
  146. waypt_disp_all(gpsutil_disp);
  147. }
  148. ff_vecs_t gpsutil_vecs = {
  149. ff_type_file,
  150. FF_CAP_RW_WPT,
  151. rd_init,
  152. wr_init,
  153. rd_deinit,
  154. wr_deinit,
  155. data_read,
  156. data_write,
  157. NULL,
  158. NULL,
  159. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  160. };