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.

pocketfms_wp.cc 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. PocketFMS waypoint text files (wpt).
  3. Copyright (C) 2009 Tobias Kretschmar, tobias.kretschmar@gmx.de
  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 "csv_util.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #define MYNAME "PocketFMS waypoint text file format"
  21. static gbfile* file_in, *file_out;
  22. static void
  23. rd_init(const QString& fname)
  24. {
  25. file_in = gbfopen_le(fname, "r", MYNAME);
  26. }
  27. double wppos_to_dec(char* value)
  28. {
  29. if (strstr(value, "\xB0") == NULL) {
  30. return atof(value);
  31. } else {
  32. int degrees, minutes;
  33. float seconds;
  34. int sign = 1;
  35. if (toupper(value[0]) == 'N' || toupper(value[0]) == 'E' || value[0] == '+') {
  36. value = &value[1];
  37. } else if (toupper(value[0]) == 'S' || toupper(value[0]) == 'W' || value[0] == '-') {
  38. value = &value[1];
  39. sign = -1;
  40. }
  41. sscanf(value, "%d\xB0%d'%f\"", &degrees, &minutes, &seconds);
  42. return sign * (degrees + ((float)minutes / 60) + (seconds / 3600));
  43. }
  44. }
  45. static void
  46. data_read(void)
  47. {
  48. char* buff;
  49. int linecount = 0;
  50. while ((buff = gbfgetstr(file_in))) {
  51. char* s;
  52. Waypoint* wpt;
  53. rtrim(buff);
  54. if (strlen(buff) == 0) {
  55. break;
  56. }
  57. linecount++;
  58. wpt = new Waypoint;
  59. s = buff;
  60. s = csv_lineparse(s, "\\w", "", linecount);
  61. if (!s) {
  62. fatal(MYNAME "Invalid name");
  63. }
  64. wpt->shortname = s;
  65. s = csv_lineparse(NULL, "\\w", "", linecount);
  66. if (!s) {
  67. fatal(MYNAME "Invalid latitude %s", qPrintable(wpt->shortname));
  68. }
  69. wpt->latitude = wppos_to_dec(s);
  70. s = csv_lineparse(NULL, "\\w", "", linecount);
  71. if (!s) {
  72. fatal(MYNAME "Invalid longitude %s", qPrintable(wpt->shortname));
  73. }
  74. wpt->longitude = wppos_to_dec(s);
  75. waypt_add(wpt);
  76. }
  77. }
  78. static void
  79. rd_deinit(void)
  80. {
  81. gbfclose(file_in);
  82. }
  83. static void
  84. wr_init(const QString& fname)
  85. {
  86. file_out = gbfopen_le(fname, "w", MYNAME);
  87. }
  88. static void
  89. enigma_waypt_disp(const Waypoint* wpt)
  90. {
  91. if (!wpt->shortname.isEmpty()) {
  92. // The output might have a space or control character.
  93. int i, l = wpt->shortname.length();
  94. char *t = (char*) xmalloc(l + 1);
  95. char* d = t;
  96. for (i = 0; i < l; i++) {
  97. char s = wpt->shortname[i].cell();
  98. if (isgraph(s)) {
  99. *d++ = s;
  100. }
  101. }
  102. *d = 0;
  103. gbfprintf(file_out, "%s %f %f\n", t, wpt->latitude, wpt->longitude);
  104. xfree(t);
  105. } else {
  106. gbfprintf(file_out, "%s %f %f\n", "NONAME", wpt->latitude, wpt->longitude);
  107. }
  108. }
  109. static void
  110. data_write(void)
  111. {
  112. waypt_disp_all(enigma_waypt_disp);
  113. }
  114. static void
  115. wr_deinit(void)
  116. {
  117. gbfclose(file_out);
  118. }
  119. ff_vecs_t pocketfms_wp_vecs = {
  120. ff_type_file,
  121. {
  122. (ff_cap)(ff_cap_read | ff_cap_write), /* waypoints */
  123. ff_cap_none, /* tracks */
  124. ff_cap_none, /* routes */
  125. },
  126. rd_init,
  127. wr_init,
  128. rd_deinit,
  129. wr_deinit,
  130. data_read,
  131. data_write,
  132. NULL,
  133. NULL,
  134. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  135. };