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.

holux.cc 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. Access to holux wpo files.
  3. Copyright (C) 2002 Jochen Becker, jb@bepo.com
  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. History:
  16. 2002-09-15 J. Becker start programming
  17. */
  18. /* This module is for the holux (gm-100) .wpo format */
  19. #include "defs.h"
  20. #include "holux.h"
  21. //#include <math.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. static gbfile* file_in, *file_out;
  25. static unsigned char* HxWFile;
  26. static short_handle mkshort_handle;
  27. #define MYNAME "Holux"
  28. static void rd_init(const QString& fname)
  29. {
  30. file_in = gbfopen_le(fname, "rb", MYNAME);
  31. }
  32. static void rd_deinit(void)
  33. {
  34. gbfclose(file_in);
  35. }
  36. static void
  37. wr_init(const QString& fname)
  38. {
  39. mkshort_handle = mkshort_new_handle();
  40. HxWFile = (unsigned char*) xcalloc(GM100_WPO_FILE_SIZE, 1);
  41. file_out = gbfopen_le(fname, "wb", MYNAME);
  42. }
  43. static void wr_deinit(void)
  44. {
  45. mkshort_del_handle(&mkshort_handle);
  46. gbfclose(file_out);
  47. }
  48. static void data_read(void)
  49. {
  50. char name[9], desc[90];
  51. double lat,lon;
  52. unsigned char* HxWpt;
  53. Waypoint* wpt_tmp;
  54. int iCount;
  55. int iDataRead;
  56. int iWptNum;
  57. int iWptIndex;
  58. WPT* pWptHxTmp;
  59. struct tm tm;
  60. struct tm* ptm;
  61. memset(&tm, 0, sizeof(tm));
  62. HxWpt = (unsigned char*) xcalloc(GM100_WPO_FILE_SIZE, 1);
  63. /* read the wpo file to the data-array */
  64. iDataRead = gbfread(HxWpt, 1, GM100_WPO_FILE_SIZE, file_in);
  65. if (iDataRead == 0) {
  66. fatal(MYNAME ": Error reading data from %s.\n", file_in->name);
  67. }
  68. iWptNum = le_read16(&((WPTHDR*)HxWpt)->num);
  69. /* Get the waypoints */
  70. for (iCount = 0; iCount < iWptNum ; iCount ++) {
  71. wpt_tmp = new Waypoint;
  72. iWptIndex = le_read16(&((WPTHDR*)HxWpt)->idx[iCount]);
  73. pWptHxTmp = (WPT*)&HxWpt[OFFS_WPT + (sizeof(WPT) * iWptIndex)];
  74. wpt_tmp->altitude = 0;
  75. strncpy(name,pWptHxTmp->name,sizeof(pWptHxTmp->name));
  76. name[sizeof(pWptHxTmp->name)]=0;
  77. strncpy(desc,pWptHxTmp->comment,sizeof(pWptHxTmp->comment));
  78. desc[sizeof(pWptHxTmp->comment)]=0;
  79. wpt_tmp->shortname = name;
  80. wpt_tmp->description = desc;
  81. wpt_tmp->SetCreationTime(0);
  82. if (pWptHxTmp->date.year) {
  83. #if 0
  84. /* Unless there's some endian swapping that I don't see,
  85. * this can't be right. Then again, the definition of the
  86. * the structure itself has a pretty serious disregard for
  87. * host word size issues... - rjl
  88. */
  89. ptm = gmtime((time_t*)&pWptHxTmp->time);
  90. #else
  91. time_t wt = le_read32(&pWptHxTmp->time);
  92. ptm = gmtime(&wt);
  93. #endif
  94. tm.tm_hour = ptm->tm_hour;
  95. tm.tm_min = ptm->tm_min;
  96. tm.tm_sec = ptm->tm_sec;
  97. tm.tm_mday = pWptHxTmp->date.day;
  98. tm.tm_mon = pWptHxTmp->date.month - 1;
  99. tm.tm_year = pWptHxTmp->date.year - 1900;
  100. wpt_tmp->SetCreationTime(mktime(&tm));
  101. }
  102. lon = le_read32(&pWptHxTmp->pt.iLongitude) / 36000.0;
  103. lat = (le_read32(&pWptHxTmp->pt.iLatitude) / 36000.0) * -1.0;
  104. wpt_tmp->longitude = lon;
  105. wpt_tmp->latitude = lat;
  106. waypt_add(wpt_tmp);
  107. }
  108. xfree(HxWpt);
  109. }
  110. const char* mknshort(const char* stIn,unsigned int sLen)
  111. {
  112. #define MAX_STRINGLEN 255
  113. static char strOut[MAX_STRINGLEN];
  114. char strTmp[MAX_STRINGLEN];
  115. char* shortstr = NULL;
  116. if (sLen > MAX_STRINGLEN) {
  117. return (stIn);
  118. }
  119. if (stIn == NULL) {
  120. return NULL;
  121. }
  122. setshort_length(mkshort_handle, sLen);
  123. setshort_mustuniq(mkshort_handle, 0);
  124. shortstr = mkshort(mkshort_handle, stIn);
  125. strcpy(strTmp,shortstr);
  126. xfree(shortstr);
  127. memset(strOut,' ', MAX_STRINGLEN);
  128. strncpy(strOut,strTmp,strlen(strTmp));
  129. return (strOut);
  130. }
  131. static void holux_disp(const Waypoint* wpt)
  132. {
  133. double lon,lat;
  134. short sIndex;
  135. WPT* pWptHxTmp;
  136. lon =(double)wpt->longitude * 36000;
  137. lat =(double)wpt->latitude * -36000;
  138. /* round it to increase the accuracy */
  139. if (lon != 0) {
  140. lon += (double)((int)lon/abs((int)lon)) * .5;
  141. }
  142. if (lat != 0) {
  143. lat += (double)((int)lat/abs((int)lat)) * .5;
  144. }
  145. sIndex = le_read16(&((WPTHDR*)HxWFile)->num);
  146. ((WPTHDR*)HxWFile)->idx[sIndex] = sIndex; /* set the waypoint index */
  147. le_write16(&((WPTHDR*)HxWFile)->idx[sIndex], sIndex); /* set the waypoint index */
  148. ((WPTHDR*)HxWFile)->used[sIndex] = 0xff; /* Waypoint used */
  149. /* set Waypoint */
  150. pWptHxTmp = (WPT*)&HxWFile[OFFS_WPT + (sizeof(WPT) * sIndex)];
  151. memset(pWptHxTmp->name,0x20,sizeof(pWptHxTmp->name));
  152. if (wpt->shortname != NULL) {
  153. strncpy(pWptHxTmp->name, mknshort(CSTRc(wpt->shortname),sizeof(pWptHxTmp->name)),sizeof(pWptHxTmp->name));
  154. } else {
  155. sprintf(pWptHxTmp->name,"W%d",sIndex);
  156. }
  157. memset(pWptHxTmp->comment,0x20,sizeof(pWptHxTmp->comment));
  158. if (wpt->description != NULL) {
  159. strncpy(pWptHxTmp->comment, mknshort(CSTRc(wpt->description),sizeof(pWptHxTmp->comment)),sizeof(pWptHxTmp->comment));
  160. }
  161. /*set the time */
  162. if (wpt->creation_time.isValid()) {
  163. /* tm = gmtime(&wpt->creation_time);*/ /* I get the wrong result with gmtime ??? */
  164. QDate date(wpt->GetCreationTime().date());
  165. QTime time(wpt->GetCreationTime().time());
  166. pWptHxTmp->time = (time.hour() * 3600) + (time.minute()* 60) + time.second();
  167. pWptHxTmp->date.day = date.day();
  168. pWptHxTmp->date.month = date.month();
  169. pWptHxTmp->date.year = date.year();
  170. } else {
  171. pWptHxTmp->time = 0;
  172. pWptHxTmp->date.day = 0;
  173. pWptHxTmp->date.month = 0;
  174. pWptHxTmp->date.year = 0;
  175. }
  176. // Note that conversions from double values to unsigned int
  177. // yield undefined results for negative values.
  178. // We intentionally convert to int, then do an implicit
  179. // conversion to unsigned in the call.
  180. le_write32(&pWptHxTmp->pt.iLatitude,(signed int) lat);
  181. le_write32(&pWptHxTmp->pt.iLongitude,(signed int) lon);
  182. pWptHxTmp->checked = 01;
  183. pWptHxTmp->vocidx = (short)0xffff;
  184. le_write16(&((WPTHDR*)HxWFile)->num, ++sIndex);
  185. le_write16(&((WPTHDR*)HxWFile)->next, ++sIndex);
  186. }
  187. static void data_write(void)
  188. {
  189. int iWritten;
  190. short sCount;
  191. /* init the waypoint area*/
  192. le_write32(&((WPTHDR*)HxWFile)->id, WPT_HDR_ID);
  193. ((WPTHDR*)HxWFile)->num = 0;
  194. ((WPTHDR*)HxWFile)->next = 0;
  195. /* clear index list */
  196. for (sCount = 0; sCount < MAXWPT; sCount++) {
  197. ((WPTHDR*)HxWFile)->idx[sCount] = (signed short)-1;
  198. }
  199. for (sCount = 0; sCount < MAXWPT; sCount++) {
  200. ((WPTHDR*)HxWFile)->used[sCount] = 0;
  201. }
  202. /* init the route area */
  203. le_write32(&((RTEHDR*)&HxWFile[ROUTESTART])->id, RTE_HDR_ID);
  204. ((RTEHDR*)&HxWFile[ROUTESTART])->num = 0;
  205. le_write16(&((RTEHDR*)&HxWFile[ROUTESTART])->next, 1);
  206. ((RTEHDR*)&HxWFile[ROUTESTART])->rteno = (signed short)-1;
  207. /* clear index list */
  208. for (sCount = 0; sCount < MAXRTE; sCount++) {
  209. ((RTEHDR*)&HxWFile[ROUTESTART])->idx[sCount] = (signed short)-1;
  210. }
  211. for (sCount = 0; sCount < MAXRTE; sCount++) {
  212. ((RTEHDR*)&HxWFile[ROUTESTART])->used[sCount] = 0;
  213. }
  214. waypt_disp_all(holux_disp);
  215. iWritten = gbfwrite(HxWFile, 1, GM100_WPO_FILE_SIZE,file_out);
  216. if (iWritten == 0) {
  217. fatal(MYNAME ": Error writing data to %s.\n", file_out->name);
  218. }
  219. xfree(HxWFile);
  220. }
  221. ff_vecs_t holux_vecs = {
  222. ff_type_file,
  223. FF_CAP_RW_WPT,
  224. rd_init,
  225. wr_init,
  226. rd_deinit,
  227. wr_deinit,
  228. data_read,
  229. data_write,
  230. NULL,
  231. NULL,
  232. CET_CHARSET_ASCII, 0 /* CET-REVIEW */
  233. };