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.

ozi.cc 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. OziExplorer Waypoints/Tracks/Routes
  3. Comma Delimited
  4. As described in OziExplorer Help File
  5. Copyright (C) 2002-2005 Robert Lipe, robertlipe+source@gpsbabel.org
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  17. */
  18. #include "defs.h"
  19. #include "cet_util.h"
  20. #include "csv_util.h"
  21. #include "jeeps/gpsmath.h"
  22. #include <ctype.h>
  23. #include <math.h> /* for floor */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <QtCore/QFileInfo>
  27. #define MYNAME "OZI"
  28. #define BADCHARS ",\r\n"
  29. #define DAYS_SINCE_1990 25569
  30. typedef struct {
  31. format_specific_data fs;
  32. int fgcolor;
  33. int bgcolor;
  34. } ozi_fsdata;
  35. static gbfile* file_in, *file_out;
  36. static short_handle mkshort_handle;
  37. static route_head* trk_head;
  38. static route_head* rte_head;
  39. static int track_out_count;
  40. static int route_out_count;
  41. static int route_wpt_count;
  42. static int new_track;
  43. static char* snlenopt = NULL;
  44. static char* snwhiteopt = NULL;
  45. static char* snupperopt = NULL;
  46. static char* snuniqueopt = NULL;
  47. static char* wptfgcolor = NULL;
  48. static char* wptbgcolor = NULL;
  49. static char* pack_opt = NULL;
  50. static int datum;
  51. static char* proximityarg = NULL;
  52. static double proximity;
  53. static char* altunit_opt;
  54. static char* proxunit_opt;
  55. static char altunit;
  56. static char proxunit;
  57. static double alt_scale;
  58. static double prox_scale;
  59. static
  60. arglist_t ozi_args[] = {
  61. {
  62. "pack", &pack_opt, "Write all tracks into one file",
  63. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  64. },
  65. {
  66. "snlen", &snlenopt, "Max synthesized shortname length",
  67. "32", ARGTYPE_INT, "1", NULL
  68. },
  69. {
  70. "snwhite", &snwhiteopt, "Allow whitespace synth. shortnames",
  71. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  72. },
  73. {
  74. "snupper", &snupperopt, "UPPERCASE synth. shortnames",
  75. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  76. },
  77. {
  78. "snunique", &snuniqueopt, "Make synth. shortnames unique",
  79. NULL, ARGTYPE_BOOL, ARG_NOMINMAX
  80. },
  81. {
  82. "wptfgcolor", &wptfgcolor, "Waypoint foreground color",
  83. "black", ARGTYPE_STRING, ARG_NOMINMAX
  84. },
  85. {
  86. "wptbgcolor", &wptbgcolor, "Waypoint background color",
  87. "yellow", ARGTYPE_STRING, ARG_NOMINMAX
  88. },
  89. {
  90. "proximity", &proximityarg, "Proximity distance",
  91. "0", ARGTYPE_STRING, ARG_NOMINMAX
  92. },
  93. {
  94. "altunit", &altunit_opt, "Unit used in altitude values",
  95. "feet", ARGTYPE_STRING, ARG_NOMINMAX
  96. },
  97. {
  98. "proxunit", &proxunit_opt, "Unit used in proximity values",
  99. "miles", ARGTYPE_STRING, ARG_NOMINMAX
  100. },
  101. ARG_TERMINATOR
  102. };
  103. static gpsdata_type ozi_objective;
  104. static QString ozi_ofname;
  105. static void
  106. ozi_copy_fsdata(ozi_fsdata** dest, ozi_fsdata* src)
  107. {
  108. /* No strings to mess with. Straight forward copy. */
  109. *dest = (ozi_fsdata*)xmalloc(sizeof(*src));
  110. ** dest = *src;
  111. (*dest)->fs.next = NULL;
  112. }
  113. static void
  114. ozi_free_fsdata(void* fsdata)
  115. {
  116. xfree(fsdata);
  117. }
  118. static
  119. ozi_fsdata*
  120. ozi_alloc_fsdata(void)
  121. {
  122. ozi_fsdata* fsdata = (ozi_fsdata*) xcalloc(sizeof(*fsdata), 1);
  123. fsdata->fs.type = FS_OZI;
  124. fsdata->fs.copy = (fs_copy) ozi_copy_fsdata;
  125. fsdata->fs.destroy = ozi_free_fsdata;
  126. fsdata->fs.convert = NULL;
  127. /* Provide defaults via command line defaults */
  128. fsdata->fgcolor = color_to_bbggrr(wptfgcolor);
  129. fsdata->bgcolor = color_to_bbggrr(wptbgcolor);
  130. return fsdata;
  131. }
  132. void
  133. ozi_get_time_str(const Waypoint* waypointp, char* buff, gbsize_t buffsz)
  134. {
  135. if (waypointp->creation_time.isValid()) {
  136. double time = (waypt_time(waypointp) / SECONDS_PER_DAY) + DAYS_SINCE_1990;
  137. snprintf(buff, buffsz, "%.7f", time);
  138. } else {
  139. *buff = '\0';
  140. }
  141. }
  142. void
  143. ozi_set_time_str(const QString& str, Waypoint* waypointp)
  144. {
  145. double ozi_time = str.toDouble();
  146. if (ozi_time > DAYS_SINCE_1990) {
  147. waypointp->SetCreationTime((ozi_time - DAYS_SINCE_1990) * SECONDS_PER_DAY,
  148. lround(1000.0 * (ozi_time - (int) ozi_time)));
  149. }
  150. }
  151. static void
  152. ozi_convert_datum(Waypoint* wpt)
  153. {
  154. if (datum != DATUM_WGS84) {
  155. double lat, lon, alt;
  156. GPS_Math_Known_Datum_To_WGS84_M(wpt->latitude, wpt->longitude, 0.0,
  157. &lat, &lon, &alt, datum);
  158. wpt->latitude = lat;
  159. wpt->longitude = lon;
  160. }
  161. }
  162. static void
  163. ozi_openfile(const QString& fname)
  164. {
  165. const char* ozi_extensions[] = {0, "plt", "wpt", "rte"};
  166. /* if we're doing multi-track output, sequence the filenames like:
  167. * mytrack.plt, mytrack-1.plt...unless we're writing to stdout.
  168. */
  169. if (fname == "-") {
  170. if (! file_out) {
  171. file_out = gbfopen(fname, "wb", MYNAME);
  172. }
  173. return;
  174. }
  175. QString buff;
  176. if ((track_out_count) && (ozi_objective == trkdata)) {
  177. buff = QString("-%d").arg(track_out_count);
  178. } else {
  179. buff = QString("");
  180. }
  181. /* remove extension and add buff + ozi's extension */
  182. QString sname(fname);
  183. int suffix_len = QFileInfo(fname).suffix().length();
  184. if (suffix_len > 0) {
  185. /* drop the suffix and the period */
  186. sname.chop(suffix_len + 1);
  187. }
  188. QString tmpname = QString("%1%2.%3").arg(sname).arg(buff).arg(ozi_extensions[ozi_objective]);
  189. /* re-open file_out with the new filename */
  190. if (file_out) {
  191. gbfclose(file_out);
  192. file_out = NULL;
  193. }
  194. file_out = gbfopen(tmpname, "wb", MYNAME);
  195. return;
  196. }
  197. static void
  198. ozi_track_hdr(const route_head* rte)
  199. {
  200. static const char* ozi_trk_header =
  201. "OziExplorer Track Point File Version 2.1\r\n"
  202. "WGS 84\r\n"
  203. "Altitude is in %s\r\n"
  204. "Reserved 3\r\n"
  205. "0,2,255,%s,0,0,2,8421376\r\n"
  206. "0\r\n";
  207. if ((! pack_opt) || (track_out_count == 0)) {
  208. ozi_openfile(ozi_ofname);
  209. gbfprintf(file_out, ozi_trk_header,
  210. altunit == 'f' ? "Feet" : "Meters",
  211. rte->rte_name.isEmpty() ? "ComplimentsOfGPSBabel" : CSTRc(rte->rte_name));
  212. }
  213. track_out_count++;
  214. new_track = 1;
  215. }
  216. static void
  217. ozi_track_disp(const Waypoint* waypointp)
  218. {
  219. double alt;
  220. char ozi_time[16];
  221. ozi_get_time_str(waypointp, ozi_time, sizeof(ozi_time));
  222. if (waypointp->altitude == unknown_alt) {
  223. alt = -777;
  224. } else {
  225. alt = waypointp->altitude * alt_scale;
  226. }
  227. gbfprintf(file_out, "%.6f,%.6f,%d,%.0f,%s,,\r\n",
  228. waypointp->latitude, waypointp->longitude, new_track,
  229. alt, ozi_time);
  230. new_track = 0;
  231. }
  232. static void
  233. ozi_track_tlr(const route_head*)
  234. {
  235. }
  236. static void
  237. ozi_track_pr()
  238. {
  239. track_disp_all(ozi_track_hdr, ozi_track_tlr, ozi_track_disp);
  240. }
  241. static void
  242. ozi_route_hdr(const route_head* rte)
  243. {
  244. static const char* ozi_route_header =
  245. "OziExplorer Route File Version 1.0\r\n"
  246. "WGS 84\r\n"
  247. "Reserved 1\r\n"
  248. "Reserved 2\r\n";
  249. /* prologue on 1st pass only */
  250. if (route_out_count == 0) {
  251. gbfprintf(file_out, ozi_route_header);
  252. }
  253. route_out_count++;
  254. route_wpt_count = 0;
  255. /*
  256. * Route Record
  257. * Field 1 : R - indicating route details
  258. * Field 2 : Number - this is the location in the array, must be unique, usually start at 0 for Garmins 1 for other and increment.
  259. * Field 3 : Name - the waypoint name, use the correct length name to suit the GPS type.
  260. * Field 4 : Description.
  261. * Field 5 : Route Color as displayed on map (RGB).
  262. *
  263. * R, 0,R0 ,,255
  264. * R, 1, ICP GALHETA,, 16711680
  265. */
  266. gbfprintf(file_out, "R,%d,%s,%s,\r\n",
  267. route_out_count,
  268. CSTRc(rte->rte_name),
  269. CSTRc(rte->rte_desc));
  270. }
  271. static void
  272. ozi_route_disp(const Waypoint* waypointp)
  273. {
  274. char ozi_time[16];
  275. route_wpt_count++;
  276. ozi_get_time_str(waypointp, ozi_time, sizeof(ozi_time));
  277. /*
  278. double alt;
  279. if (waypointp->altitude == unknown_alt) {
  280. alt = -777;
  281. } else {
  282. alt = waypointp->altitude * alt_scale;
  283. }
  284. */
  285. /*
  286. * Field 1 : W - indicating route waypoint details.
  287. * Field 2 : Route Number - location in array of routes
  288. * Field 3 : Number - this is the location in the array of route waypoints, this field is now ignored.
  289. * Field 4 : Wp Number - this is the number of the waypoint (the Wp number within the GPS for lowrances)
  290. * Field 5 : Name - the waypoint name, use the correct length name to suit the GPS type.
  291. * Field 6 : Latitude - decimal degrees.
  292. * Field 7 : Longitude - decimal degrees.
  293. * Field 8 : Date - see Date Format below, if blank a preset date will be used
  294. * Field 9 : Symbol - 0 to number of symbols in GPS
  295. * Field 10 : Status - always set to 1
  296. * Field 11 : Map Display Format
  297. * Field 12 : Foreground Color (RGB value)
  298. * Field 13 : Background Color (RGB value)
  299. * Field 14 : Description (max 40), no commas
  300. * Field 15 : Pointer Direction
  301. * Field 16 : Garmin Display Format
  302. *
  303. * W,1,7,7,007,-25.581670,-48.316660,36564.54196,10,1,4,0,65535,TR ILHA GALHETA,0,0
  304. */
  305. gbfprintf(file_out, "W,%d,,%d,%s,%.6f,%.6f,%s,0,1,3,0,65535,%s,0,0\r\n",
  306. route_out_count,
  307. route_wpt_count,
  308. CSTR(waypointp->shortname),
  309. waypointp->latitude,
  310. waypointp->longitude,
  311. ozi_time,
  312. CSTR(waypointp->description));
  313. }
  314. static void
  315. ozi_route_tlr(const route_head*)
  316. {
  317. }
  318. static void
  319. ozi_route_pr()
  320. {
  321. route_disp_all(ozi_route_hdr, ozi_route_tlr, ozi_route_disp);
  322. }
  323. static void
  324. ozi_init_units(const int direction) /* 0 = in; 1 = out */
  325. {
  326. altunit = tolower(*altunit_opt);
  327. switch (altunit) {
  328. case 'm': /* meters, okay */
  329. alt_scale = 1.0;
  330. break;
  331. case 'f': /* feet, okay */
  332. alt_scale = FEET_TO_METERS(1.0);
  333. break;
  334. default:
  335. fatal(MYNAME ": Unknown value (%s) for option 'altunit'!\n", altunit_opt);
  336. }
  337. if (direction != 0) {
  338. alt_scale = 1 / alt_scale;
  339. }
  340. proxunit = tolower(*proxunit_opt);
  341. switch (proxunit) {
  342. case 'm': /* miles, okay */
  343. prox_scale = MILES_TO_METERS(1.0);
  344. break;
  345. case 'n': /* nautical miles, okay */
  346. prox_scale = NMILES_TO_METERS(1.0);
  347. break;
  348. case 'k': /* kilometers, okay */
  349. prox_scale = 1000.0;
  350. break;
  351. default:
  352. fatal(MYNAME ": Unknown value (%s) for option 'proxunit'!\n", proxunit_opt);
  353. }
  354. if (direction != 0) {
  355. prox_scale = 1 / prox_scale;
  356. }
  357. }
  358. static void
  359. rd_init(const QString& fname)
  360. {
  361. file_in = gbfopen(fname, "rb", MYNAME);
  362. mkshort_handle = mkshort_new_handle();
  363. ozi_init_units(0);
  364. }
  365. static void
  366. rd_deinit(void)
  367. {
  368. gbfclose(file_in);
  369. file_in = NULL;
  370. mkshort_del_handle(&mkshort_handle);
  371. }
  372. static void
  373. wr_init(const QString& fname)
  374. {
  375. /* At this point, we have no idea whether we'll be writing waypoint,
  376. * route, or tracks. So we'll hold off opening any files until
  377. * we're actually ready to write.
  378. */
  379. ozi_ofname = fname;
  380. mkshort_handle = mkshort_new_handle();
  381. /* set mkshort options from the command line if applicable */
  382. if (global_opts.synthesize_shortnames) {
  383. setshort_length(mkshort_handle, atoi(snlenopt));
  384. if (snwhiteopt) {
  385. setshort_whitespace_ok(mkshort_handle, atoi(snwhiteopt));
  386. }
  387. if (snupperopt) {
  388. setshort_mustupper(mkshort_handle, atoi(snupperopt));
  389. }
  390. if (snuniqueopt) {
  391. setshort_mustuniq(mkshort_handle, atoi(snuniqueopt));
  392. }
  393. setshort_badchars(mkshort_handle, "\",");
  394. }
  395. ozi_init_units(1);
  396. parse_distance(proximityarg, &proximity, 1 / prox_scale, MYNAME);
  397. file_out = NULL;
  398. }
  399. static void
  400. wr_deinit(void)
  401. {
  402. if (file_out != NULL) {
  403. gbfclose(file_out);
  404. file_out = NULL;
  405. }
  406. ozi_ofname.clear();
  407. mkshort_del_handle(&mkshort_handle);
  408. }
  409. static void
  410. ozi_parse_waypt(int field, const QString& str, Waypoint* wpt_tmp, ozi_fsdata* fsdata)
  411. {
  412. double alt;
  413. if (str.isEmpty()) {
  414. return;
  415. }
  416. switch (field) {
  417. case 0:
  418. /* sequence # */
  419. break;
  420. case 1:
  421. /* waypoint name */
  422. wpt_tmp->shortname = str.trimmed();
  423. break;
  424. case 2:
  425. /* degrees latitude */
  426. wpt_tmp->latitude = str.toDouble();
  427. break;
  428. case 3:
  429. /* degrees longitude */
  430. wpt_tmp->longitude = str.toDouble();
  431. break;
  432. case 4:
  433. /* DAYS since 1900 00:00:00 in days.days (5.5) */
  434. ozi_set_time_str(str, wpt_tmp);
  435. break;
  436. case 5:
  437. /* icons 0-xx. Ozi seems to use some kind of internal tables to
  438. pick numbers for icons based on GPS type. We don't know what those
  439. tables are, so we read just the numbers. This converts badly to
  440. other types, but it at least maintains fidelity for an ozi->ozi
  441. operation. */
  442. if (str.toInt() > 0) {
  443. wpt_tmp->icon_descr = str;
  444. }
  445. break;
  446. case 6:
  447. /* unknown - always 1 */
  448. break;
  449. case 7:
  450. /* display format options 0-8 */
  451. break;
  452. case 8:
  453. /* foreground color (0=black) */
  454. fsdata->fgcolor = str.toInt();
  455. break;
  456. case 9:
  457. /* background color (65535=yellow) */
  458. fsdata->bgcolor = str.toInt();
  459. break;
  460. case 10:
  461. /* Description */
  462. wpt_tmp->description = str.trimmed();
  463. break;
  464. case 11:
  465. /* pointer direction 0,1,2,3 bottom,top,left,right */
  466. break;
  467. case 12:
  468. /* garmin gps display flags (0-name w/sym, 1-sym only, 2-comment w/symbol */
  469. break;
  470. case 13:
  471. /* proximity distance - meters */
  472. WAYPT_SET(wpt_tmp, proximity, str.toDouble() * prox_scale);
  473. break;
  474. case 14:
  475. /* altitude */
  476. alt = str.toDouble();
  477. if (alt == -777) {
  478. wpt_tmp->altitude = unknown_alt;
  479. } else {
  480. wpt_tmp->altitude = alt * alt_scale;
  481. }
  482. break;
  483. case 15:
  484. /* waypoint text name size */
  485. break;
  486. case 16:
  487. /* bold checkbox (1=bold, default 0) */
  488. break;
  489. case 17:
  490. /* symbol size - 17 default */
  491. break;
  492. /*
  493. * Fields 18-23 were added around version 3.90.4g of
  494. * Ozi, but aren't documented. We silently ignore
  495. * these or any additional fields we don't need.
  496. */
  497. default:
  498. break;
  499. }
  500. }
  501. static void
  502. ozi_parse_track(int field, char* str, Waypoint* wpt_tmp, char* trk_name)
  503. {
  504. double alt;
  505. if (*str == '\0') {
  506. return;
  507. }
  508. switch (field) {
  509. case 0:
  510. /* latitude */
  511. wpt_tmp->latitude = atof(str);
  512. break;
  513. case 1:
  514. /* longitude */
  515. wpt_tmp->longitude = atof(str);
  516. break;
  517. case 2:
  518. /* new track flag */
  519. if ((atoi(str) == 1) && (trk_head->rte_waypt_ct > 0)) {
  520. trk_head = route_head_alloc();
  521. track_add_head(trk_head);
  522. if (trk_name) {
  523. trk_head->rte_name = trk_name;
  524. }
  525. }
  526. break;
  527. case 3:
  528. /* altitude */
  529. alt = atof(str);
  530. if (alt == -777) {
  531. wpt_tmp->altitude = unknown_alt;
  532. } else {
  533. wpt_tmp->altitude = alt * alt_scale;
  534. }
  535. break;
  536. case 4:
  537. /* DAYS since 1900 00:00:00 in days.days (5.5) */
  538. ozi_set_time_str(str, wpt_tmp);
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. static void
  545. ozi_parse_routepoint(int field, char* str, Waypoint* wpt_tmp)
  546. {
  547. if (*str == '\0') {
  548. return;
  549. }
  550. switch (field) {
  551. case 0:
  552. /* W */
  553. break;
  554. case 1:
  555. /* route # */
  556. break;
  557. case 2:
  558. /* waypoint # -- ignored by ozi */
  559. break;
  560. case 3:
  561. /* waypoint # */
  562. break;
  563. case 4:
  564. /* waypoint name */
  565. wpt_tmp->shortname = csv_stringclean(str, QString(","));
  566. break;
  567. case 5:
  568. /* latitude */
  569. wpt_tmp->latitude = atof(str);
  570. break;
  571. case 6:
  572. /* longitude */
  573. wpt_tmp->longitude = atof(str);
  574. break;
  575. case 7:
  576. /* DAYS since 1900 00:00:00 in days.days (5.5) */
  577. ozi_set_time_str(str, wpt_tmp);
  578. break;
  579. case 8:
  580. /* symbol */
  581. break;
  582. case 9:
  583. /* status */
  584. break;
  585. case 10:
  586. /* map display format */
  587. break;
  588. case 11:
  589. /* foreground color (RGB) */
  590. break;
  591. case 12:
  592. /* background color (RGB) */
  593. break;
  594. case 13:
  595. /* description */
  596. wpt_tmp->description = csv_stringclean(str, QString(","));
  597. break;
  598. default:
  599. break;
  600. }
  601. }
  602. static void
  603. ozi_parse_routeheader(int field, const QString& str, Waypoint*)
  604. {
  605. switch (field) {
  606. case 0:
  607. /* R */
  608. rte_head = route_head_alloc();
  609. route_add_head(rte_head);
  610. break;
  611. case 1:
  612. /* route # */
  613. rte_head->rte_num = str.toInt();
  614. break;
  615. case 2:
  616. /* route name */
  617. rte_head->rte_name = csv_stringclean(str, ",");
  618. break;
  619. case 3:
  620. /* route description */
  621. rte_head->rte_desc = csv_stringclean(str, ",");
  622. break;
  623. case 4:
  624. /* route color */
  625. break;
  626. default:
  627. break;
  628. }
  629. }
  630. static void
  631. data_read(void)
  632. {
  633. QString buff;
  634. char* trk_name = NULL;
  635. Waypoint* wpt_tmp;
  636. int i;
  637. int linecount = 0;
  638. while ((buff = gbfgetstr(file_in)), !buff.isNull()) {
  639. if ((linecount++ == 0) && file_in->unicode) {
  640. cet_convert_init(CET_CHARSET_UTF8, 1);
  641. }
  642. /*
  643. * this is particularly nasty. use the first line of the file
  644. * to attempt to divine the data type we are parsing
  645. */
  646. if (linecount == 1) {
  647. if (buff.contains("Track Point")) {
  648. trk_head = route_head_alloc();
  649. track_add_head(trk_head);
  650. ozi_objective = trkdata;
  651. } else if (buff.contains("Route File")) {
  652. ozi_objective = rtedata;
  653. } else {
  654. ozi_objective = wptdata;
  655. }
  656. } else if (linecount == 2) {
  657. datum = GPS_Lookup_Datum_Index(buff);
  658. if (datum < 0) {
  659. fatal(MYNAME ": Unsupported datum '%s'.\n", qPrintable(buff));
  660. }
  661. } else if (linecount == 3) {
  662. if (buff.startsWith( "Altitude is in ", Qt::CaseInsensitive)) {
  663. QString unit = buff.mid(15);
  664. if (unit.startsWith("Feet", Qt::CaseInsensitive)) {
  665. altunit = 'f';
  666. alt_scale = FEET_TO_METERS(1.0);
  667. } else if (unit.startsWith("Meter", Qt::CaseInsensitive)) {
  668. altunit = 'm';
  669. alt_scale = 1.0;
  670. } else {
  671. fatal(MYNAME ": Unknown unit (%s) used by altitude values!\n", qPrintable(unit));
  672. }
  673. }
  674. } else if ((linecount == 5) && (ozi_objective == trkdata)) {
  675. int field = 0;
  676. char* s = csv_lineparse(CSTR(buff), ",", "", linecount);
  677. while (s) {
  678. field ++;
  679. if (field == 4) {
  680. trk_head->rte_name = QString(s).trimmed();
  681. }
  682. s = csv_lineparse(NULL, ",", "", linecount);
  683. }
  684. }
  685. if (buff.contains(',')) {
  686. bool ozi_fsdata_used = false;
  687. ozi_fsdata* fsdata = ozi_alloc_fsdata();
  688. wpt_tmp = new Waypoint;
  689. /* data delimited by commas, possibly enclosed in quotes. */
  690. char* orig_s = xstrdup(CSTR(buff));
  691. char* s = csv_lineparse(orig_s, ",", "", linecount);
  692. i = 0;
  693. bool header = false;
  694. while (s) {
  695. switch (ozi_objective) {
  696. case trkdata:
  697. ozi_parse_track(i, s, wpt_tmp, trk_name);
  698. break;
  699. case rtedata:
  700. if (buff[0] == 'R') {
  701. ozi_parse_routeheader(i, QString(s), wpt_tmp);
  702. header = true;
  703. } else {
  704. ozi_parse_routepoint(i, s, wpt_tmp);
  705. }
  706. break;
  707. case wptdata:
  708. case unknown_gpsdata:
  709. ozi_parse_waypt(i, s, wpt_tmp, fsdata);
  710. break;
  711. case posndata:
  712. fatal(MYNAME ": realtime positioning not supported.\n");
  713. break;
  714. }
  715. i++;
  716. s = csv_lineparse(NULL, ",", "", linecount);
  717. }
  718. xfree(orig_s);
  719. switch (ozi_objective) {
  720. case trkdata:
  721. if (linecount > 6) {/* skipping over file header */
  722. ozi_convert_datum(wpt_tmp);
  723. track_add_wpt(trk_head, wpt_tmp);
  724. } else {
  725. delete wpt_tmp;
  726. }
  727. break;
  728. case rtedata:
  729. if (linecount > 5 && wpt_tmp) {/* skipping over file header */
  730. ozi_convert_datum(wpt_tmp);
  731. if (!header) {
  732. route_add_wpt(rte_head, wpt_tmp);
  733. } else {
  734. delete wpt_tmp;
  735. }
  736. } else {
  737. delete wpt_tmp;
  738. }
  739. break;
  740. case wptdata:
  741. case unknown_gpsdata:
  742. if (linecount > 4) { /* skipping over file header */
  743. ozi_fsdata_used = true;
  744. fs_chain_add(&(wpt_tmp->fs),
  745. (format_specific_data*) fsdata);
  746. ozi_convert_datum(wpt_tmp);
  747. waypt_add(wpt_tmp);
  748. } else {
  749. delete wpt_tmp;
  750. }
  751. break;
  752. case posndata:
  753. fatal(MYNAME ": realtime positioning not supported.\n");
  754. break;
  755. }
  756. if (!ozi_fsdata_used) {
  757. fs_chain_destroy((format_specific_data*) fsdata);
  758. }
  759. } else {
  760. /* empty line */
  761. }
  762. }
  763. }
  764. static void
  765. ozi_waypt_pr(const Waypoint* wpt)
  766. {
  767. static int index = 0;
  768. double alt;
  769. char ozi_time[16];
  770. QString description;
  771. QString shortname;
  772. int faked_fsdata = 0;
  773. ozi_fsdata* fs = NULL;
  774. int icon = 0;
  775. fs = (ozi_fsdata*) fs_chain_find(wpt->fs, FS_OZI);
  776. if (!fs) {
  777. fs = ozi_alloc_fsdata();
  778. faked_fsdata = 1;
  779. }
  780. ozi_get_time_str(wpt, ozi_time, sizeof(ozi_time));
  781. if (wpt->altitude == unknown_alt) {
  782. alt = -777;
  783. } else {
  784. alt = wpt->altitude * alt_scale;
  785. }
  786. if ((wpt->shortname.isEmpty()) || (global_opts.synthesize_shortnames)) {
  787. if (!wpt->description.isEmpty()) {
  788. if (global_opts.synthesize_shortnames) {
  789. shortname = mkshort_from_wpt(mkshort_handle, wpt);
  790. } else {
  791. shortname = csv_stringclean(wpt->description, BADCHARS);
  792. }
  793. } else {
  794. /* no description available */
  795. shortname = xstrdup("");
  796. }
  797. } else {
  798. shortname = csv_stringclean(wpt->shortname, BADCHARS);
  799. }
  800. if (wpt->description.isEmpty()) {
  801. if (!shortname.isEmpty()) {
  802. description = csv_stringclean(shortname, BADCHARS);
  803. } else {
  804. description = xstrdup("");
  805. }
  806. } else {
  807. description = csv_stringclean(wpt->description, BADCHARS);
  808. }
  809. index++;
  810. if (wpt->icon_descr.toInt()) {
  811. icon = wpt->icon_descr.toInt();
  812. }
  813. gbfprintf(file_out,
  814. "%d,%s,%.6f,%.6f,%s,%d,%d,%d,%d,%d,%s,%d,%d,",
  815. index, CSTRc(shortname), wpt->latitude, wpt->longitude, ozi_time, icon,
  816. 1, 3, fs->fgcolor, fs->bgcolor, CSTRc(description), 0, 0);
  817. if (WAYPT_HAS(wpt, proximity) && (wpt->proximity > 0)) {
  818. gbfprintf(file_out, "%.1f,", wpt->proximity * prox_scale);
  819. } else if (proximity > 0) {
  820. gbfprintf(file_out,"%.1f,", proximity * prox_scale);
  821. } else {
  822. gbfprintf(file_out,"%d,", 0);
  823. }
  824. gbfprintf(file_out, "%.0f,%d,%d,%d\r\n", alt, 6, 0, 17);
  825. if (faked_fsdata) {
  826. xfree(fs);
  827. }
  828. }
  829. static void
  830. data_write(void)
  831. {
  832. static const char* ozi_wpt_header =
  833. "OziExplorer Waypoint File Version 1.1\r\n"
  834. "WGS 84\r\n"
  835. "Reserved 2\r\n"
  836. "Reserved 3\r\n";
  837. track_out_count = route_out_count = 0;
  838. if (waypt_count()) {
  839. ozi_objective = wptdata;
  840. ozi_openfile(ozi_ofname);
  841. gbfprintf(file_out, ozi_wpt_header);
  842. waypt_disp_all(ozi_waypt_pr);
  843. }
  844. if (track_count()) {
  845. ozi_objective = trkdata;
  846. ozi_track_pr(); /* ozi_track_hdr handles filenames / file_out */
  847. }
  848. if (route_count()) {
  849. ozi_objective = rtedata;
  850. ozi_openfile(ozi_ofname); /* ozi routes go in one big file */
  851. ozi_route_pr();
  852. }
  853. }
  854. ff_vecs_t ozi_vecs = {
  855. ff_type_file,
  856. FF_CAP_RW_ALL,
  857. rd_init,
  858. wr_init,
  859. rd_deinit,
  860. wr_deinit,
  861. data_read,
  862. data_write,
  863. NULL,
  864. ozi_args,
  865. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  866. };