File: gtm.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (841 lines) | stat: -rw-r--r-- 21,751 bytes parent folder | download | duplicates (2)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/******************************************************************************
 * $Id: gtm.cpp 20996 2010-10-28 18:38:15Z rouault $
 *
 * Project:  GTM Driver
 * Purpose:  Class for reading, parsing and handling a gtmfile.
 * Author:   Leonardo de Paula Rosa Piga; http://lampiao.lsc.ic.unicamp.br/~piga
 *
 ******************************************************************************
 * Copyright (c) 2009, Leonardo de Paula Rosa Piga
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/
#include "gtm.h"


/************************************************************************/
/*        Methods for dealing with write on files and buffers           */
/************************************************************************/
void appendDouble(void* pBuffer, double val)
{
    CPL_LSBPTR64(&val);
    memcpy(pBuffer, &val, 8);
}

void appendFloat(void* pBuffer, float val)
{
    CPL_LSBPTR32(&val)
    memcpy(pBuffer, &val, 4);
}

void appendInt(void* pBuffer, int val)
{
    CPL_LSBPTR32(&val)
    memcpy(pBuffer, &val, 4);
}

void appendUChar(void* pBuffer, unsigned char val)
{
    memcpy(pBuffer, &val, 1);
}

void appendUShort(void* pBuffer, unsigned short val)
{
    CPL_LSBPTR16(&val)
    memcpy(pBuffer, &val, 2);
}

void writeUChar(VSILFILE* fp, unsigned char val)
{
    VSIFWriteL(&val, 1, 1, fp);
}

void writeDouble(VSILFILE* fp, double val)
{
    CPL_LSBPTR64(&val)
    VSIFWriteL(&val, 1, 8, fp);
}

static double readDouble(VSILFILE* fp)
{
    double val;
    VSIFReadL( &val, 1, 8, fp );
    CPL_LSBPTR64(&val)
    return val;
}

static float readFloat(VSILFILE* fp)
{
    float val;
    VSIFReadL( &val, 1, 4, fp );
    CPL_LSBPTR32(&val)
    return val;
}

static int readInt(VSILFILE* fp)
{
    int val;
    VSIFReadL( &val, 1, 4, fp );
    CPL_LSBPTR32(&val)
    return val;
}

static unsigned char readUChar(VSILFILE* fp)
{
    unsigned char val;
    VSIFReadL( &val, 1, 1, fp );
    return val;
}

static unsigned short readUShort(VSILFILE* fp, int *pbSuccess = NULL)
{
    unsigned short val;
    if (VSIFReadL( &val, 1, 2, fp ) != 2)
    {
        if (pbSuccess) *pbSuccess = FALSE;
        return 0;
    }
    if (pbSuccess) *pbSuccess = TRUE;
    CPL_LSBPTR16(&val)
    return val;
}

void writeFloat(VSILFILE* fp, float val)
{
    CPL_LSBPTR32(&val)
    VSIFWriteL(&val, 1, 4, fp);
}

void writeInt(VSILFILE* fp, int val)
{
    CPL_LSBPTR32(&val)
    VSIFWriteL(&val, 1, 4, fp);
}

void writeUShort(VSILFILE* fp, unsigned short val)
{
    CPL_LSBPTR16(&val)
    VSIFWriteL(&val, 1, 2, fp);
}


/************************************************************************/
/*             Implementation of Waypoint Function Members              */
/************************************************************************/
Waypoint::Waypoint(double latitude,
                   double longitude,
                   double altitude,
                   const char* name,
                   const char* comment,
                   int icon,
                   GIntBig wptdate)
{
    this->latitude = latitude;
    this->longitude = longitude;
    this->altitude = altitude;
    this->name = CPLStrdup(name);
    this->comment = CPLStrdup(comment);
    this->icon = icon;
    this->wptdate = wptdate;
}

Waypoint::~Waypoint()
{
    CPLFree(name);
    CPLFree(comment);
}

double Waypoint::getLatitude() 
{
    return latitude;
}

double Waypoint::getLongitude()
{
    return longitude;
}

double Waypoint::getAltitude()
{
    return altitude;
}

const char* Waypoint::getName()
{
    return name;
}

const char* Waypoint::getComment()
{
    return comment;
}

int Waypoint::getIcon()
{
    return icon;
}

GIntBig Waypoint::getDate()
{
    return wptdate;
}

/************************************************************************/
/*               Implementation of Track Function Members               */
/************************************************************************/
Track::Track(const char* pszName,
             unsigned char type,
             int color)
{
    this->pszName = CPLStrdup(pszName);
    this->type = type;
    this->color = color;
    nPoints = 0;
    pasTrackPoints = NULL;
}

Track::~Track()
{
    CPLFree(pszName);
    pszName = NULL;
    CPLFree(pasTrackPoints);
}

const char* Track::getName() {
    return pszName;
}

unsigned char Track::getType()
{
    return type;
}


int Track::getColor()
{
    return color;
}

void Track::addPoint(double x, double y, GIntBig datetime, double altitude)
{
    pasTrackPoints = (TrackPoint*)
        CPLRealloc(pasTrackPoints, (nPoints + 1) * sizeof(TrackPoint));
    pasTrackPoints[nPoints].x = x;
    pasTrackPoints[nPoints].y = y;
    pasTrackPoints[nPoints].datetime = datetime;
    pasTrackPoints[nPoints].altitude = altitude;
    nPoints ++;
}

int Track::getNumPoints()
{
    return nPoints;
}

const TrackPoint* Track::getPoint(int pointNum)
{
    if (pointNum >=0 && pointNum < nPoints)
        return &pasTrackPoints[pointNum];
    else
        return NULL;
}


/************************************************************************/
/*                Implementation of GTM Function Members                */
/************************************************************************/
GTM::GTM()
{
    pGTMFile = NULL;
    pszFilename = NULL;

    nwptstyles = 0;
    nwpts = 0;
    ntcks = 0;
    n_tk = 0;
    n_maps = 0;
    headerSize = 0;

    firstWaypointOffset = 0;
    actualWaypointOffset = 0;
    waypointFetched = 0;
  
    firstTrackpointOffset = 0;
    actualTrackpointOffset = 0;
    trackpointFetched = 0;

    firstTrackOffset = 0;
    actualTrackOffset = 0;
    trackFetched = 0;
}

GTM::~GTM()
{
    CPLFree(pszFilename);
    if (pGTMFile != NULL)
    {
        VSIFCloseL(pGTMFile);
        pGTMFile = NULL;
    }
}

bool GTM::Open(const char* pszFilename)
{

    if (pGTMFile != NULL)
        VSIFCloseL(pGTMFile);
        
    CPLFree(this->pszFilename);
    this->pszFilename = CPLStrdup(pszFilename);

    pGTMFile = VSIFOpenL( pszFilename, "r" );
    if (pGTMFile == NULL)
    {
        return FALSE;
    }
    return TRUE;
}


bool GTM::isValid()
{
    //  2 bytes - version number
    // 10 bytes - "TrackMaker" string
    char buffer[13];

    char* szHeader;
    short version;

/* -------------------------------------------------------------------- */
/*      If we aren't sure it is GTM, load a header chunk and check      */
/*      for signs it is GTM                                             */
/* -------------------------------------------------------------------- */
    size_t nRead = VSIFReadL( buffer, 1, sizeof(buffer)-1, pGTMFile );
    if (nRead <= 0)
    {
        VSIFCloseL( pGTMFile );
        pGTMFile = NULL;
        return FALSE;
    }
    buffer[12] = '\0';
    
/* -------------------------------------------------------------------- */
/*      If it looks like a GZip header, this may be a .gtz file, so     */
/*      try opening with the /vsigzip/ prefix                           */
/* -------------------------------------------------------------------- */
    if (buffer[0] == 0x1f && ((unsigned char*)buffer)[1] == 0x8b &&
        strncmp(pszFilename, "/vsigzip/", strlen("/vsigzip/")) != 0)
    {
        char* pszGZIPFileName = (char*)CPLMalloc(
                           strlen("/vsigzip/") + strlen(pszFilename) + 1);
        sprintf(pszGZIPFileName, "/vsigzip/%s", pszFilename);
        VSILFILE* fp = VSIFOpenL(pszGZIPFileName, "rb");
        if (fp)
        {
            VSILFILE* pGTMFileOri = pGTMFile;
            pGTMFile = fp;
            if (isValid())
            {
                VSIFCloseL(pGTMFileOri);
                CPLFree(pszGZIPFileName);
                return TRUE;
            }
            else
            {
                if (pGTMFile)
                    VSIFCloseL(pGTMFile);
                pGTMFile = pGTMFileOri;
            }
        }
        CPLFree(pszGZIPFileName);
    }
    
    version = CPL_LSBINT16PTR(buffer);
    /*Skip string length */
    szHeader = buffer + 2;
    if (version == 211 && strcmp(szHeader, "TrackMaker") == 0 )
    {
        return TRUE;
    }
    return FALSE;
}

bool GTM::readHeaderNumbers()
{
    if (pGTMFile == NULL)
        return FALSE;

   
    /* I'm supposing that the user has already checked if the file is
       valid.  */
    /* Also, I'm ignoring some header parameters that are unnecessary
       for my purpose. If you want more features, implement it. :-P */

    /* Read Number of Waypoint Styles*/
    /* Seek file */
    if (VSIFSeekL(pGTMFile, NWPTSTYLES_OFFSET, SEEK_SET) != 0)
        return FALSE;
    /* Read nwptstyles */
    nwptstyles = readInt(pGTMFile);
    if (nwptstyles < 0)
        return FALSE;

    /* Read Number of Waypoints */
    /* Seek file */
    if ( VSIFSeekL(pGTMFile, NWPTS_OFFSET, SEEK_SET) != 0)
        return FALSE;
    /* Read nwpts */
    nwpts = readInt(pGTMFile);
    if (nwpts < 0)
        return FALSE;

    /* Read Number of Trackpoints */
    ntcks = readInt(pGTMFile);
    if (ntcks < 0)
        return FALSE;

    /* Read Number of images */
    /* Seek file */
    if ( VSIFSeekL(pGTMFile, NMAPS_OFFSET, SEEK_SET) != 0)
        return FALSE;
    /* read n_maps */
    n_maps = readInt(pGTMFile);
    if (n_maps < 0)
        return FALSE;

    /* Read Number of Tracks */
    n_tk = readInt(pGTMFile);
    if (n_tk < 0)
        return FALSE;

    /* Figure out the header size */
    headerSize = 99; // Constant size plus size of strings
    unsigned short stringSize;

    /* Read gradfont string size */
    if ( VSIFSeekL(pGTMFile, 99, SEEK_SET) != 0)
        return FALSE;
    stringSize = readUShort(pGTMFile);
    headerSize += stringSize + 2; // String + size field

    /* Read labelfont string size */
    if ( VSIFSeekL(pGTMFile, stringSize, SEEK_CUR) != 0)
        return FALSE;
    stringSize = readUShort(pGTMFile);
    headerSize += stringSize + 2; // String + size field


    /* Read userfont string size */
    if ( VSIFSeekL(pGTMFile, stringSize, SEEK_CUR) != 0)
        return FALSE;
    stringSize = readUShort(pGTMFile);
    headerSize += stringSize + 2; // String + size field

    /* Read newdatum string size */
    if ( VSIFSeekL(pGTMFile, stringSize, SEEK_CUR) != 0)
        return FALSE;
    stringSize = readUShort(pGTMFile);
    headerSize += stringSize + 2; // String + size field



/* -------------------------------------------------------------------- */
/*                 Checks if it is using WGS84 datum                    */
/* -------------------------------------------------------------------- */
    /* Read newdatum string size */
    if ( VSIFSeekL(pGTMFile, headerSize + 34, SEEK_SET) != 0)
        return FALSE;
    if (readInt(pGTMFile) != 217)
    {
        CPLError( CE_Warning, CPLE_AppDefined,
                  "You are attempting to open a file that is not using WGS84 datum.\n"
                  "Coordinates will be returned as if they were WGS84, but no reprojection will be done.");
    }

    /* Look for the offsets */
    /* Waypoints */
    firstWaypointOffset = findFirstWaypointOffset();
    if (firstWaypointOffset == 0)
        return FALSE;
    actualWaypointOffset = firstWaypointOffset;
    /* Trackpoints */
    firstTrackpointOffset = findFirstTrackpointOffset();
    if (firstTrackpointOffset == 0)
        return FALSE;
    actualTrackpointOffset = firstTrackpointOffset;

    /* Tracks */
    firstTrackOffset = findFirstTrackOffset();
    if (firstTrackOffset == 0)
        return FALSE;
    actualTrackOffset = firstTrackOffset;

    return TRUE;
}

/************************************************************************/
/*                        Waypoint control functions                    */
/************************************************************************/
int GTM::getNWpts()
{
    return nwpts;
}

bool GTM::hasNextWaypoint()
{
    return waypointFetched < nwpts;
}

void GTM::rewindWaypoint() 
{
    actualWaypointOffset = firstWaypointOffset;
    waypointFetched = 0;
}

Waypoint* GTM::fetchNextWaypoint()
{
    unsigned short stringSize;

    double latitude, longitude;
    char name[11];
    char* comment;
    unsigned short icon;
    int i;
    float altitude;
    GIntBig wptdate;

    /* Point to the actual waypoint offset */
    if ( VSIFSeekL(pGTMFile, actualWaypointOffset, SEEK_SET) != 0)
        return NULL;

    latitude = readDouble(pGTMFile);
    longitude = readDouble(pGTMFile);

    if ( !readFile( name, 1, 10 ) )
        return NULL;
    /* Trim string name */
    for (i = 9; i >= 0; --i)
    {
        if (name[i] != ' ')
        {
            name[i+1] = '\0';
            break;
        }
    }
    if (i < 0)
        name[0] = '\0';

    /* Read String Length */
    stringSize = readUShort(pGTMFile);
    /* Read Comment String */
    comment = (char*) VSIMalloc2(sizeof(char), stringSize+1);
    if ( stringSize != 0 && !readFile( comment, 1, sizeof(char)*stringSize ) )
    {
        CPLFree(comment);
        return NULL;
    }
    comment[stringSize] = '\0';

    /* Read Icon */
    icon = readUShort(pGTMFile);
    
    /* Display number */
    readUChar(pGTMFile);
    
    /* Waypoint date */
    
    wptdate = readInt(pGTMFile);
    if (wptdate != 0)
        wptdate += GTM_EPOCH;
    
    /* Rotation text angle */
    readUShort(pGTMFile);
    
    /* Altitude */
    altitude = readFloat(pGTMFile);
  
    Waypoint* poWaypoint = new Waypoint(latitude, longitude, altitude,
                                        name, comment, (int) icon, wptdate);


    /* Set actual waypoint offset to the next it there is one */
    ++waypointFetched;
    if (waypointFetched < nwpts)
    {
        actualWaypointOffset += 8 + 8 + 10 + 2 + stringSize + 2 + 1 + 4 + 2 + 4 + 2;
    }

    CPLFree(comment);
    return poWaypoint;
}


/************************************************************************/
/*                        Track control functions                    */
/************************************************************************/
int GTM::getNTracks()
{
    return n_tk;
}

bool GTM::hasNextTrack()
{
    return trackFetched < n_tk;
}

void GTM::rewindTrack() 
{
    actualTrackpointOffset = firstTrackpointOffset;
    actualTrackOffset = firstTrackOffset;
    trackFetched = 0;
    trackpointFetched = 0;
}

Track* GTM::fetchNextTrack()
{
    unsigned short stringSize;

    char* pszName;
    unsigned char type;
    int color;
  

    /* Point to the actual track offset */
    if ( VSIFSeekL(pGTMFile, actualTrackOffset, SEEK_SET) != 0)
        return NULL;


    /* Read string length */
    stringSize = readUShort(pGTMFile);
    /* Read name string */
    pszName = (char*) VSIMalloc2(sizeof(char), stringSize+1);
    if ( stringSize != 0 && !readFile( pszName, 1, sizeof(char) * stringSize ) )
    {
        CPLFree(pszName);
        return NULL;
    }
    pszName[stringSize] = '\0';

    /* Read type */
    type = readUChar(pGTMFile);
  
    /* Read color */
    color = readInt(pGTMFile);
    
    Track* poTrack = new Track(pszName, type, color);

    CPLFree(pszName);
    /* Adjust actual Track offset */
    actualTrackOffset = VSIFTellL(pGTMFile) + 7;
    ++trackFetched;

    /* Now, We read all trackpoints for this track */
    double latitude, longitude;
    GIntBig datetime;
    unsigned char start;
    float altitude;
    /* NOTE: Parameters are passed by reference */
    if ( !readTrackPoints(latitude, longitude, datetime, start, altitude) )
    {
        delete poTrack;
        return NULL;
    }

    /* Check if it is the start, if not we have done something wrong */
    if (start != 1)
    {
        delete poTrack;
        return NULL;
    }
    poTrack->addPoint(longitude, latitude, datetime, altitude);
  
    do
    {
        /* NOTE: Parameters are passed by reference */
        if ( !readTrackPoints(latitude, longitude, datetime, start, altitude) )
        {
            delete poTrack;
            return NULL;
        }
        if (start == 0)
            poTrack->addPoint(longitude, latitude, datetime, altitude);
    } while(start == 0 && trackpointFetched < ntcks);

    /* We have read one more than necessary, so we adjust the offset */
    if (trackpointFetched < ntcks)
    {
        actualTrackpointOffset -= 25;
        --trackpointFetched;
    }

    return poTrack;
}



/************************************************************************/
/*                        Private Methods Implementation                */
/************************************************************************/
vsi_l_offset GTM::findFirstWaypointOffset()
{
    /* Skip header and datum */
    if ( VSIFSeekL(pGTMFile, headerSize + DATUM_SIZE, SEEK_SET) != 0)
        return 0;
  
    /* Skip images */
    unsigned short stringSize;
    for (int i = 0; i < n_maps; ++i)
    {
        /* Read image name string size */
        stringSize = readUShort(pGTMFile);

        /* skip image name string */
        if ( VSIFSeekL(pGTMFile, stringSize, SEEK_CUR) != 0)
            return 0;

        /* read image comment string size */
        stringSize = readUShort(pGTMFile);

        /* skip image comment string */
        if ( VSIFSeekL(pGTMFile, stringSize, SEEK_CUR) != 0)
            return 0;
    
        /* skip the others image parameters */
        if ( VSIFSeekL(pGTMFile, 30, SEEK_CUR) != 0)
            return 0;
    }
    return VSIFTellL(pGTMFile);
}


vsi_l_offset GTM::findFirstTrackpointOffset()
{
    if (firstWaypointOffset == 0)
    {
        firstWaypointOffset = findFirstWaypointOffset();
        if (firstWaypointOffset == 0)
            return 0;
    }

    /*---------------------------------------------*/
    /*       We are going to skip the waypoints    */
    /*---------------------------------------------*/
    /* Seek file to the first Waypoint */
    if (VSIFSeekL(pGTMFile, firstWaypointOffset, SEEK_SET) != 0)
        return 0;
  
    unsigned short stringSize;
    int bSuccess;
    /* Skip waypoints */
    for (int i = 0; i < nwpts; ++i)
    {
        /* Seek file to the string size comment field */
        if (VSIFSeekL(pGTMFile, 26, SEEK_CUR) != 0)
            return 0;
        /* Read string comment size */
        stringSize = readUShort(pGTMFile, &bSuccess);
    
        /* Skip to the next Waypoint */
        if (bSuccess == FALSE || VSIFSeekL(pGTMFile, stringSize + 15, SEEK_CUR) != 0)
            return 0;
    }
  
    /* Skip waypoint styles */
    /* If we don't have waypoints, we don't have waypoint styles, even
       though the nwptstyles is telling the contrary. */
    if (nwpts != 0)
    {
        for (int i = 0; i < nwptstyles; ++i)
        {
            /* Seek file to the string size facename field */
            if (VSIFSeekL(pGTMFile, 4, SEEK_CUR) != 0)
                return 0;

            /* Read string facename size */
            stringSize = readUShort(pGTMFile, &bSuccess);

            /* Skip to the next Waypoint Style*/
            if (bSuccess == FALSE || VSIFSeekL(pGTMFile, stringSize + 24, SEEK_CUR) != 0)
                return 0;
        }
    }
    /* We've found the first track. Return the offset*/
    return VSIFTellL(pGTMFile);
}

vsi_l_offset GTM::findFirstTrackOffset()
{
    if (firstTrackpointOffset == 0)
    {
        firstTrackpointOffset = findFirstTrackpointOffset();
        if (firstTrackpointOffset == 0)
            return 0;
    }
    /* First track offset is the first trackpoint offset plus number of
       trackpoints time size of a trackpoint*/
    return (vsi_l_offset) (firstTrackpointOffset + ntcks * 25);
}

bool GTM::readTrackPoints(double& latitude, double& longitude, GIntBig& datetime,
                          unsigned char& start, float& altitude) {
    /* Point to the actual trackpoint offset */
    if ( VSIFSeekL(pGTMFile, actualTrackpointOffset, SEEK_SET) != 0)
        return FALSE;

    /* Read latitude */
    latitude = readDouble(pGTMFile);
  
    /* Read longitude */
    longitude = readDouble(pGTMFile);

    /* Read trackpoint date */
    datetime = readInt(pGTMFile);
    if (datetime != 0)
        datetime += GTM_EPOCH;
    
    /* Read start flag */
    if ( !readFile( &start, 1, 1 ) )
        return FALSE;
        
    /* Read altitude */
    altitude = readFloat(pGTMFile);

    ++trackpointFetched;
    if (trackpointFetched < ntcks)
    {
        actualTrackpointOffset += 8 + 8 + 4 + 1 + 4;
    }
    return TRUE;
}

bool GTM::readFile(void* pBuffer, size_t nSize, size_t nCount)
{
    size_t nRead;
    nRead = VSIFReadL( pBuffer, nSize, nCount, pGTMFile );
    if (nRead <= 0)
    {
        VSIFCloseL( pGTMFile );
        pGTMFile = NULL;
        return FALSE;
    }
    return TRUE;
}