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.

g7towin.cc 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. Support for G7ToWin data files (.g7t),
  3. Copyright (C) 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. /*
  17. History:
  18. 04/07/2007: start programming
  19. 04/15/2007: added to gpsbabel
  20. */
  21. #include "defs.h"
  22. #include "csv_util.h"
  23. #include "cet_util.h"
  24. #include "garmin_fs.h"
  25. #include "garmin_tables.h"
  26. #include "jeeps/gpsmath.h"
  27. #include "strptime.h"
  28. #include <time.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #if CSVFMTS_ENABLED
  32. #define MYNAME "g7towin"
  33. #define G7T_HEADER "Version 2:G7T"
  34. static gbfile* fin;
  35. static grid_type grid;
  36. static int datum;
  37. static gpsdata_type mode;
  38. static double altf;
  39. static int gardown;
  40. static int event_ct;
  41. static
  42. arglist_t g7towin_args[] = {
  43. ARG_TERMINATOR
  44. };
  45. #define WAYPT__OFS 0x00000
  46. #define TRKPT__OFS 0x01000
  47. #define WPT_c0_OFS 0x0c000
  48. #define WPT_c1_OFS 0x0c100
  49. #define WPT_c2_OFS 0x0c200
  50. #define WPT_c3_OFS 0x0c300
  51. #define WPT_c4_OFS 0x0c400
  52. #define WPT_c5_OFS 0x0c500
  53. #define WPT_c6_OFS 0x0c600
  54. #define WPT_c7_OFS 0x0c700
  55. #define WPT_c8_OFS 0x0c800
  56. #define WPT_cA_OFS 0x0cA00
  57. #define WPT_cB_OFS 0x0cB00
  58. #define WPT_cC_OFS 0x0cC00
  59. #define WPT_cD_OFS 0x0cD00
  60. static void
  61. parse_line(char* buff, int index, const char* delimiter, Waypoint* wpt)
  62. {
  63. char* cin;
  64. garmin_fs_p gmsd = GMSD_FIND(wpt);
  65. while ((cin = csv_lineparse(buff, delimiter, "", index++))) {
  66. buff = NULL;
  67. cin = lrtrim(cin);
  68. if ((*cin == '\0') ||
  69. (strcmp(cin, "INF") == 0) ||
  70. (strcmp(cin, "1e25") == 0) ||
  71. (strcmp(cin, "1.0e25") == 0)) {
  72. continue;
  73. }
  74. switch (index) {
  75. int categories;
  76. struct tm tm;
  77. char* cerr;
  78. case TRKPT__OFS + 1:
  79. cin += parse_coordinates(cin, datum, grid,
  80. &wpt->latitude, &wpt->longitude, MYNAME);
  81. while (isspace(*cin)) {
  82. cin++;
  83. }
  84. memset(&tm, 0, sizeof(tm));
  85. cerr = strptime(cin, "%a %b %d %H:%M:%S %Y", &tm);
  86. if (cerr == NULL) {
  87. fatal(MYNAME ": Unable to convert date (%s)!\n", cin);
  88. }
  89. wpt->SetCreationTime(mkgmtime(&tm));
  90. break;
  91. case WAYPT__OFS + 1:
  92. wpt->description = (cin);
  93. break;
  94. case WAYPT__OFS + 2:
  95. wpt->icon_descr = gt_find_desc_from_icon_number(
  96. atoi(cin), PCX);
  97. break;
  98. case WAYPT__OFS + 4:
  99. if (strcmp(cin, "S+C") == 0) {
  100. GMSD_SET(display, gt_display_mode_symbol_and_comment);
  101. } else if (strcmp(cin, "S") == 0) {
  102. GMSD_SET(display, gt_display_mode_symbol);
  103. } else if (strcmp(cin, "S+N") == 0) {
  104. GMSD_SET(display, gt_display_mode_symbol_and_name);
  105. }
  106. break;
  107. case WPT_cA_OFS + 1:
  108. case WPT_c1_OFS + 1:
  109. wpt->shortname = cin;
  110. break;
  111. case WPT_cA_OFS + 4:
  112. case WPT_c4_OFS + 2:
  113. GMSD_SETSTR(city, cin);
  114. break;
  115. case WPT_cA_OFS + 5:
  116. case WPT_c4_OFS + 3:
  117. GMSD_SETSTR(state, cin);
  118. break;
  119. case WPT_cA_OFS + 6:
  120. case WPT_c4_OFS + 4:
  121. GMSD_SETSTR(cc, cin);
  122. break;
  123. case WPT_cB_OFS + 1:
  124. case WPT_c6_OFS + 2:
  125. GMSD_SETSTR(facility, cin);
  126. break;
  127. case WPT_cB_OFS + 2:
  128. case WPT_c6_OFS + 3:
  129. GMSD_SETSTR(addr, cin);
  130. break;
  131. case WPT_cB_OFS + 3: /*cross road */
  132. case WPT_c6_OFS + 4:
  133. GMSD_SETSTR(cross_road, cin);
  134. break;
  135. case TRKPT__OFS + 2: /* altitude */
  136. case WPT_cC_OFS + 1:
  137. case WPT_c5_OFS + 1:
  138. case WPT_c8_OFS + 1:
  139. wpt->altitude = altf * atof(cin);
  140. break;
  141. case TRKPT__OFS + 3: /* depth */
  142. case WPT_cC_OFS + 2:
  143. case WPT_c5_OFS + 2:
  144. case WPT_c8_OFS + 2:
  145. WAYPT_SET(wpt, depth, altf * atof(cin));
  146. break;
  147. case TRKPT__OFS + 10: /* temperature */
  148. if (*cin == '|') {
  149. cin++; /* in track points */
  150. }
  151. if (strcmp(cin, "1e25") == 0) {
  152. break;
  153. }
  154. if (strcmp(cin, "1.0e25") == 0) {
  155. break;
  156. }
  157. /* !!! NO BREAK !!! */
  158. case WPT_cD_OFS + 1:
  159. case WPT_cB_OFS + 6:
  160. WAYPT_SET(wpt, temperature, atof(cin));
  161. break;
  162. case WAYPT__OFS + 6: /* proximity */
  163. case WPT_cD_OFS + 2:
  164. WAYPT_SET(wpt, proximity, atof(cin));
  165. break;
  166. case WPT_cB_OFS + 5:
  167. case WPT_cD_OFS + 3:
  168. categories = atoi(cin);
  169. if (categories != 0) {
  170. GMSD_SET(category, atoi(cin));
  171. }
  172. break;
  173. #if 0
  174. /* currently unused */
  175. case TRKPT__OFS + 5: /* distance from previous point */
  176. case TRKPT__OFS + 6: /* distance from segment start */
  177. case TRKPT__OFS + 7: /* distance from start */
  178. case TRKPT__OFS + 8: /* velocity from previous point */
  179. case TRKPT__OFS + 9: /* time (in seconds) from previous point */
  180. break;
  181. case WAYPT__OFS + 3: /* ignore color */
  182. break;
  183. case WAYPT__OFS + 5: /* always '0' */
  184. break;
  185. case TRKPT__OFS + 4:
  186. if (case_ignore_strcmp(cin, "FT") == 0) ;
  187. else if (case_ignore_strcmp(cin, "M") == 0) ;
  188. else if (case_ignore_strcmp(cin, "SM") == 0) ;
  189. else if (case_ignore_strcmp(cin, "NM") == 0) ;
  190. else if (case_ignore_strcmp(cin, "KM") == 0) ;
  191. break;
  192. case WPT_cB_OFS + 4: /* unknown (datatype) */
  193. break;
  194. case WPT_cC_OFS + 3: /* waypt_class (always FF) */
  195. break;
  196. case WPT_cC_OFS + 4: /* class & subclass */
  197. case WPT_cC_OFS + 5:
  198. case WPT_cC_OFS + 6:
  199. case WPT_cC_OFS + 7:
  200. case WPT_cC_OFS + 8:
  201. case WPT_cC_OFS + 9:
  202. case WPT_cC_OFS + 10:
  203. case WPT_cC_OFS + 11:
  204. case WPT_cC_OFS + 12:
  205. case WPT_cC_OFS + 13:
  206. case WPT_cC_OFS + 14:
  207. case WPT_cC_OFS + 15:
  208. case WPT_cC_OFS + 16:
  209. case WPT_cC_OFS + 17:
  210. case WPT_cC_OFS + 18:
  211. case WPT_cC_OFS + 19:
  212. case WPT_cC_OFS + 20:
  213. case WPT_cC_OFS + 21:
  214. break;
  215. case WPT_cC_OFS + 22:
  216. /* distance */
  217. break;
  218. #endif
  219. }
  220. }
  221. }
  222. static Waypoint*
  223. parse_waypt(char* buff)
  224. {
  225. char* cin, *cerr;
  226. int i;
  227. struct tm tm;
  228. Waypoint* wpt;
  229. garmin_fs_p gmsd;
  230. wpt = new Waypoint;
  231. gmsd = garmin_fs_alloc(-1);
  232. fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
  233. if (gardown) {
  234. cin = buff + 6;
  235. } else {
  236. /* We've seen waypoints with length of 14 and 15 !!! */
  237. cin = buff + 15;
  238. while ((cin > buff) && (! isspace(*cin))) {
  239. cin--;
  240. }
  241. }
  242. while (isspace(*cin)) {
  243. cin--;
  244. }
  245. if (cin >= buff) {
  246. char*s = xstrndup(buff, cin - buff + 1);
  247. wpt->shortname = s;
  248. xfree(s);
  249. }
  250. if (gardown) {
  251. buff += 6;
  252. } else {
  253. buff += 15;
  254. }
  255. while (isspace(*buff)) {
  256. buff++;
  257. }
  258. buff += parse_coordinates(buff, datum, grid,
  259. &wpt->latitude, &wpt->longitude, MYNAME);
  260. while (isspace(*buff)) {
  261. buff++;
  262. }
  263. memset(&tm, 0, sizeof(tm));
  264. cerr = strptime(buff, "%a %b %d %H:%M:%S %Y", &tm);
  265. if (cerr == NULL) {
  266. fatal(MYNAME ": Unable to convert date (%s)!\n", buff);
  267. }
  268. wpt->SetCreationTime(mkgmtime(&tm));
  269. /* go over time stamp */
  270. i = 5;
  271. while (buff && i) {
  272. i--;
  273. buff = strchr(buff, ' ');
  274. if (buff) {
  275. buff++;
  276. }
  277. }
  278. if (gardown && (buff == NULL)) {
  279. return wpt;
  280. }
  281. is_fatal((buff == NULL), MYNAME ": Incomplete waypoint line!");
  282. while (isspace(*buff)) {
  283. buff++;
  284. }
  285. parse_line(buff, WAYPT__OFS, "^", wpt);
  286. return wpt;
  287. }
  288. static Waypoint*
  289. parse_trkpt(char* buff)
  290. {
  291. garmin_fs_p gmsd;
  292. Waypoint* wpt;
  293. wpt = new Waypoint;
  294. gmsd = garmin_fs_alloc(-1);
  295. fs_chain_add(&wpt->fs, (format_specific_data*) gmsd);
  296. parse_line(buff, TRKPT__OFS, ";", wpt);
  297. return wpt;
  298. }
  299. /*
  300. * parse_categories is currently only a dummy procedure.
  301. * w'll need a central storage with binding to the module
  302. * which has established a list of category names.
  303. */
  304. static void
  305. parse_categories(char* buff)
  306. {
  307. char* cin;
  308. int cat = 0;
  309. while ((cin = csv_lineparse(buff, ",", "", cat++))) {
  310. uint16_t cx;
  311. buff = NULL;
  312. cin = lrtrim(cin);
  313. if (*cin == 0) {
  314. continue;
  315. }
  316. garmin_fs_convert_category(cin, &cx);
  317. }
  318. }
  319. /* main functions */
  320. static void
  321. rd_init(const QString& fname)
  322. {
  323. fin = gbfopen(fname, "rb", MYNAME);
  324. gardown = 1;
  325. mode = wptdata;
  326. grid = grid_lat_lon_dmm;
  327. datum = DATUM_WGS84;
  328. altf = 1;
  329. event_ct = 0;
  330. }
  331. static void
  332. rd_deinit(void)
  333. {
  334. gbfclose(fin);
  335. }
  336. static void
  337. data_read(void)
  338. {
  339. char* buff;
  340. int line = 0;
  341. Waypoint* wpt = NULL;
  342. route_head* head = NULL;
  343. while ((buff = gbfgetstr(fin))) {
  344. char* cin = buff;
  345. char* cdata;
  346. if ((line++ == 0) && fin->unicode) {
  347. cet_convert_init(CET_CHARSET_UTF8, 1);
  348. }
  349. cin = lrtrim(buff);
  350. if (!*cin) {
  351. continue;
  352. }
  353. cdata = cin+1;
  354. while (! isspace(*cdata)) {
  355. cdata++;
  356. }
  357. while (isspace(*cdata)) {
  358. cdata++;
  359. }
  360. if (! *cdata) {
  361. continue;
  362. }
  363. switch (*cin) {
  364. case '#': /* comment */
  365. break;
  366. case 'A':
  367. if (case_ignore_strncmp(cdata, "Meter", 5) == 0) {
  368. altf = 1.0;
  369. } else if (case_ignore_strncmp(cdata, "Feet", 4) == 0) {
  370. altf = FEET_TO_METERS(1.0);
  371. }
  372. break;
  373. case 'C': /* categories */
  374. parse_categories(cdata);
  375. break;
  376. case 'D':
  377. datum = gt_lookup_datum_index(cdata, MYNAME);
  378. break;
  379. case 'I': /* event point */
  380. wpt = new Waypoint;
  381. cdata += parse_coordinates(cdata, datum, grid,
  382. &wpt->latitude, &wpt->longitude, MYNAME);
  383. wpt->shortname = QString("Event%1").arg(++event_ct);
  384. while (isspace(*cdata)) {
  385. cdata++;
  386. }
  387. if (*cdata == ';') {
  388. cdata++;
  389. wpt->icon_descr = gt_find_desc_from_icon_number(
  390. atoi(cdata), PCX);
  391. }
  392. waypt_add(wpt);
  393. break;
  394. case 'M':
  395. grid = gt_lookup_grid_type(cdata, MYNAME);
  396. break;
  397. case 'P': /* proximity waypoint */
  398. case 'W': /* normal waypoint */
  399. wpt = parse_waypt(cin + 3);
  400. if (wpt) {
  401. if (mode == rtedata) {
  402. route_add_wpt(head, wpt);
  403. } else {
  404. waypt_add(wpt);
  405. }
  406. }
  407. break;
  408. case 'c': /* additional lines */
  409. switch (*(cin+1)) {
  410. int index;
  411. case 'A':
  412. case 'B':
  413. case 'C':
  414. case 'D':
  415. index = WPT_cA_OFS + ((*(cin+1) - 'A') * 256);
  416. parse_line(cdata, index, "|", wpt);
  417. break;
  418. case '1':
  419. case '2':
  420. case '3':
  421. case '4':
  422. case '5':
  423. case '6':
  424. case '7':
  425. case '8':
  426. index = WPT_c0_OFS + ((*(cin+1) - '0') * 256);
  427. parse_line(cdata, index, ";", wpt);
  428. break;
  429. case 'L':
  430. waypt_add_url(wpt, xstrdup(cdata), NULL);
  431. break;
  432. default:
  433. break;
  434. }
  435. break;
  436. case 'N': /* track log header */
  437. mode = trkdata;
  438. head = route_head_alloc();
  439. cdata = strchr(cdata, '-');
  440. if (cdata) {
  441. while (isspace(*cdata)) {
  442. cdata++;
  443. }
  444. if (*cdata) {
  445. char* s;
  446. s = strrchr(cdata, ',');
  447. if (s) {
  448. *s = '\0';
  449. s = strrchr(cdata, ',');
  450. if (s) {
  451. *s = '\0';
  452. head->rte_name = cdata;
  453. }
  454. }
  455. }
  456. }
  457. track_add_head(head);
  458. break;
  459. case 'R': /* route header */
  460. mode = rtedata;
  461. head = route_head_alloc();
  462. cdata += 3; /*skip route number */
  463. if (*cdata) {
  464. head->rte_name = cdata;
  465. }
  466. route_add_head(head);
  467. break;
  468. case 'T':
  469. wpt = parse_trkpt(cdata);
  470. if (wpt) {
  471. track_add_wpt(head, wpt);
  472. }
  473. break;
  474. case 'V':
  475. if (strcmp(cin, G7T_HEADER) != 0) {
  476. fatal(MYNAME ": Invalid version or invalid file!\n");
  477. }
  478. gardown = 0;
  479. break;
  480. default:
  481. break;
  482. }
  483. }
  484. }
  485. /* --------------------------------------------------------------------------- */
  486. ff_vecs_t g7towin_vecs = {
  487. ff_type_file,
  488. { ff_cap_read, ff_cap_read, ff_cap_read },
  489. rd_init,
  490. NULL,
  491. rd_deinit,
  492. NULL,
  493. data_read,
  494. NULL,
  495. NULL,
  496. g7towin_args,
  497. CET_CHARSET_MS_ANSI, 0
  498. };
  499. #endif /* CSVFMTS_ENABLED */