Codebase list gpsbabel / debian/1.7.0+ds-6 naviguide.cc
debian/1.7.0+ds-6

Tree @debian/1.7.0+ds-6 (Download .tar.gz)

naviguide.cc @debian/1.7.0+ds-6raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
    Naviguide Routes


    Copyright (C) 2009 Erez Zuler

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 */

#include "defs.h"
#include "csv_util.h"
#include "jeeps/gpsmath.h"
#include <QtCore/QDebug>
#include <QtCore/QTextCodec>
#include <cmath>

#define MYNAME        "Naviguide"


/************* Specific Naviguide data formats ****************/

/* Naviguide file header */
struct ng_file_header_t {
  uint16_t nof_wp;    /* Little endean format */
  unsigned char pad1[6];      /* 0xff, 0xff, 0x01, 0x00, 0x06, 0x00 */
  char signature[9]; /* cWaypoint */
  unsigned char pad2[4];      /* 0x01, 0x00, 0x00, 0x00 */
};

/* Naviguide waypoint/rout data  */
struct ng_wp_data_t {
  unsigned char pad1[8];   /*  0xfe, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00 */
  /* coordination are in old israeli grid */
  int32_t East;
  int32_t North;
  unsigned char pad2[2];  /* 0x01, 0x01 */
  uint32_t Alt;
  char CommentLength;
};

struct ng_next_wp_t {
  unsigned char pad1[2]; /* 0x01, 0x80 */
  uint16_t next_wp;
  unsigned char pad2[2]; /* 0x00, 0x00 */
};

struct ng_wp_no_comment_t {
  unsigned char chHeaderLen;
  char strName[255];
  ng_wp_data_t wp_data;
};


/* Global variables */

static gbfile* file_in, *file_out;
static uint16_t nof_wp;
static route_head* rte_head;
static ng_file_header_t ng_file_header;
static ng_wp_no_comment_t WPNC;
static ng_next_wp_t ng_next_wp;
static char strComment[101];

/* Process options */
/* wp - process only waypoints */
/* rte - process as route */
/* wprte - Process waypoints and route */
static char* process = nullptr;
static char* reorder = nullptr;
static int process_rte = 1;
static int reorder_wp = 0;

static char temp_short_name[5];




/* Forward declarations */
static void ng_read_file_header();

static
QVector<arglist_t> ng_args = {
  {
    "output", &process, "'wp' - Create waypoint file , 'rte' - Create route file",
    "rte", ARGTYPE_STRING, ARG_NOMINMAX, nullptr
  },
  {
    "reorder", &reorder, "'n' - Keep the existing wp name, 'y' - rename waypoints",
    "n", ARGTYPE_STRING, ARG_NOMINMAX, nullptr
  },

};

/*===================Utilities ==========================================*/

static void
ng_convert_datum(Waypoint* wpt)
{
  double lat, lon;

  auto east = (double) WPNC.wp_data.East;
  auto north = (double) WPNC.wp_data.North;
  auto alt = (double) WPNC.wp_data.Alt;

  GPS_Math_ICS_EN_To_WGS84(east, north, &lat, &lon);
  wpt->latitude = lat;
  wpt->longitude = lon;
  wpt->altitude = alt;
}



/*=================== File read/write utilities ==========================================*/

static void
ng_fwrite_wp_data(const QString& s, const QString& d, ng_wp_data_t* wp_data, gbfile* f)
{
  char z[50];

  memset(z, 0, 50);
  int i = strlen(STRFROMUNICODE(s));
  gbfputc(i, f);
  gbfwrite(STRFROMUNICODE(s), 1, i, f);

  gbfwrite(&wp_data->pad1[0], 8, 1, f);
  gbfputint32(wp_data->East, f);
  gbfputint32(wp_data->North, f);
  gbfwrite(&wp_data->pad2[0], 2, 1, f);
  gbfputint32(wp_data->Alt, f);

  i = strlen(STRFROMUNICODE(d));
  gbfputc(i, f);
  gbfwrite(STRFROMUNICODE(d), 1, i, f);
  gbfwrite(z, 44, 1, f);
}

static void
ng_fwrite_next_wp(ng_next_wp_t* nwp, gbfile* f)
{
  gbfwrite(nwp->pad1, 2, 1, f);
  gbfputint16(nwp->next_wp, f);
  gbfwrite(nwp->pad2, 2, 1, f);
}

static void
ng_fread_wp_data(char* d, ng_wp_no_comment_t* wpnc, gbfile* f)
{
  gbfread(&wpnc->chHeaderLen ,sizeof(wpnc->chHeaderLen), 1, f);
  gbfread(&wpnc->strName, wpnc->chHeaderLen, 1, f);
  wpnc->strName[wpnc->chHeaderLen] = 0;


  gbfread(&wpnc->wp_data, 8, 1, f);
  wpnc->wp_data.East = gbfgetint32(f);
  wpnc->wp_data.North = gbfgetint32(f);
  gbfread(&wpnc->wp_data.pad2,2, 1, f);
  wpnc->wp_data.Alt = gbfgetint32(f);
  gbfread(&wpnc->wp_data.CommentLength, 1, 1, f);
  int i = (int)wpnc->wp_data.CommentLength;


  /* Read the comment field */
  gbfread(d, i + 44, 1, f);

}

static void
ng_fread_next_wp(ng_next_wp_t* nwp, gbfile* f)
{
  gbfread(&nwp->pad1, 2, 1, f);
  nwp->next_wp = gbfgetint16(f);
  gbfread(&nwp->pad2, 2, 1, f);
}

/* =================== Write data functions ====================================*/

static void
ng_fill_header_default()
{
  ng_file_header_t default_header = {
    0x00,
    {0xff, 0xff, 0x01, 0x00, 0x09, 0x00},
    {'C', 'W', 'a', 'y', 'P', 'o', 'i', 'n', 't'},
    {0x01, 0x00, 0x00, 0x00},
  };

  ng_file_header =default_header;

}


static void
ng_fill_waypoint_default()
{
  ng_wp_data_t default_wp  = {
    {0xfe, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00},
    0,
    0,
    {0x01, 0x01},
    0,
    0x00,

  };

  ng_next_wp_t default_ng_next_wp = {
    {0x01, 0x80},
    0,
    {0x00, 0x00},
  };

  WPNC.wp_data = default_wp;
  ng_next_wp = default_ng_next_wp;
}


static void
ng_waypt_rd(const Waypoint* wpt)
{
  char z[50];
  double lat, lon;
  static int current_wp_ix=0;

  memset(z, 0, 50);
  current_wp_ix++;
  ng_fill_waypoint_default();

  if (!GPS_Math_WGS84_To_ICS_EN(wpt->latitude, wpt->longitude, &lon, &lat)) {
    fatal(MYNAME ": Waypoint %d is out of the israeli grid area", current_wp_ix);
  }

  WPNC.wp_data.North = (int32_t)lat;
  WPNC.wp_data.East = (int32_t)lon;

  QString s;
  if (reorder_wp) {
    sprintf(temp_short_name, "A%03d", current_wp_ix);
    s = temp_short_name;
  }

  else {
    s = wpt->shortname;
  }

  ng_fwrite_wp_data(s, wpt->description, &WPNC.wp_data, file_out);


  /* if not Last WP, write the next one index */

  if (nof_wp > current_wp_ix) {
    ng_next_wp.next_wp = current_wp_ix + 1;

    ng_fwrite_next_wp(&ng_next_wp, file_out);

  }
}

static void
header_write()
{
  ng_file_header.nof_wp = nof_wp;
  gbfputint16(nof_wp, file_out);
  gbfwrite(&ng_file_header.pad1[0], 19, 1, file_out);

}


static void
data_write()
{
  nof_wp = waypt_count();
  if (nof_wp) {
    header_write();
    waypt_disp_all(ng_waypt_rd);
  } else {
    nof_wp = route_waypt_count();
    if (nof_wp) {
      header_write();
      route_disp_all(nullptr, nullptr, ng_waypt_rd);
    }
  }
}


static void
wr_init(const QString& fname)
{
  file_out = gbfopen_le(fname, "wb", MYNAME);
  ng_fill_header_default();
  if (nullptr != reorder)
    if (!case_ignore_strcmp(reorder, "y")) {
      reorder_wp = 1;
    }

}

static void
wr_deinit()
{
  gbfclose(file_out);
}

/*=========================== Read data functions ==================================*/

static void
rd_init(const QString& fname)
{
  file_in = gbfopen_le(fname, "rb", MYNAME);

  ng_read_file_header();

  if (nullptr != process) {
    if (!case_ignore_strcmp(process, "wp")) {
      process_rte = 0;
    }
    if (!case_ignore_strcmp(process, "rte")) {
      process_rte = 1;
    }
  }


}

static void
rd_deinit()
{
  gbfclose(file_in);
  file_in = nullptr;
}



static void
ng_read_file_header()
{

  nof_wp = gbfgetint16(file_in);
  gbfread(&ng_file_header.pad1[0], 19, 1, file_in);
  ng_file_header.nof_wp = nof_wp;


  if (strncmp("CWayPoint", ng_file_header.signature, 9)) {
    fatal("\nInvalid Naviguide file format\n");
  }


}

static void
data_read()
{
  if (process_rte) {
    rte_head = new route_head;
    route_add_head(rte_head);
  }

  for (int n = 0; n < nof_wp; ++n) {

    auto* wpt_tmp = new Waypoint;

    /* Read waypoint data */

    ng_fread_wp_data(strComment, &WPNC, file_in);


    if (n < nof_wp - 1) {
      /*
      	gbfread (&ng_next_wp.pad1[0], 2, 1, file_in);
      	ng_next_wp.next_wp = gbfgetint16 (file_in);
      	gbfread (&ng_next_wp.pad2[0], 2, 1, file_in);
      	*/
      ng_fread_next_wp(&ng_next_wp, file_in);

    }
    /* Clear commas form the comment for CSV file commonality */
    for (unsigned i = 0; i <strlen(strComment); ++i) {
      if (strComment[i] == ',') {
        strComment[i] = ' ';
      }
    }

    /* put the data in the waypoint structure */
    ng_convert_datum(wpt_tmp);
    wpt_tmp->shortname = STRTOUNICODE(WPNC.strName);
    wpt_tmp->description = STRTOUNICODE(strComment);

    if (process_rte) {
      route_add_wpt(rte_head, wpt_tmp);
    } else {
      waypt_add(wpt_tmp);
    }
  }
} /* data_read */



ff_vecs_t ng_vecs = {
  ff_type_file,
  FF_CAP_RW_WPT,
  rd_init,
  wr_init,
  rd_deinit,
  wr_deinit,
  data_read,
  data_write,
  nullptr,
  &ng_args,
  CET_CHARSET_HEBREW, 0
  , NULL_POS_OPS,
  nullptr
};