File: geotag.cpp

package info (click to toggle)
exiv2 0.24-4.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,904 kB
  • ctags: 7,389
  • sloc: cpp: 65,740; sh: 10,246; ansic: 1,622; makefile: 654; python: 210; awk: 92; sed: 16; perl: 5
file content (960 lines) | stat: -rw-r--r-- 29,112 bytes parent folder | download
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
// ***************************************************************** -*- C++ -*-

// geotag.cpp, $Rev: 2286 $
// Sample program to read gpx files and update the images with GPS tags

#include <exiv2/exiv2.hpp>

#include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "expat.h"

#include <vector>
#include <string>

#if defined(__MINGW32__) || defined(__MINGW64__)
# ifndef  __MINGW__
#  define __MINGW__
# endif
#endif

#ifdef __MINGW__
// rmills: dummy off for MinGW.  I don't have time to fix this for 0.24
int main(int,const char**)
{
	return 0;
}
#else

using namespace std;

#ifndef  lengthof
#define  lengthof(x) (sizeof(*x)/sizeof(x))
#endif
#ifndef nil
#define nil NULL
#endif
#ifndef _MAX_PATH
#define _MAX_PATH 1024
#endif

#ifdef   _MSC_VER
#include <windows.h>
char*    realpath(const char* file,char* path);
#define  lstat _stat
#define  stat  _stat
#if      _MSC_VER < 1400
#define strcpy_s(d,l,s) strcpy(d,s)
#define strcat_s(d,l,s) strcat(d,s)
#endif

#else
#include <dirent.h>
#include <unistd.h>
#include <sys/param.h>
#define  stricmp strcasecmp
#endif

// prototypes
class Options;
int getFileType(const char* path ,Options& options);
int getFileType(std::string& path,Options& options);

string getExifTime(const time_t t);
time_t parseTime(const char* ,bool bAdjust=false);
int    timeZoneAdjust();

// platform specific code
#ifdef   _MSC_VER
char* realpath(const char* file,char* path)
{
    char* result = (char*) malloc(_MAX_PATH);
    if   (result) GetFullPathName(file,_MAX_PATH,result,NULL);
    return result ;
}
#endif

// Command-line parser
class Options  {
public:
    bool        verbose;
    bool        help;
    bool        version;
    bool        dst;
    bool        dryrun;

    Options()
    {
        verbose     = false;
        help        = false;
        version     = false;
        dst         = false;
        dryrun      = false;
    }

    virtual ~Options() {} ;
} ;

enum
{   resultOK=0
,   resultSyntaxError
,   resultSelectFailed
};

enum                        // keyword indices
{   kwHELP = 0
,   kwVERSION
,   kwDST
,   kwDRYRUN
,   kwVERBOSE
,   kwADJUST
,   kwTZ
,   kwDELTA
,   kwMAX                   // manages keyword array
,   kwNEEDVALUE             // bogus keywords for error reporting
,   kwSYNTAX                // -- ditto --
,   kwNOVALUE = -kwVERBOSE  // keywords <= kwNOVALUE are flags (no value needed)
};

// file types supported
enum
{   typeUnknown   = 0
,   typeDirectory = 1
,   typeImage     = 2
,   typeXML       = 3
,   typeFile      = 4
,   typeDoc       = 5
,   typeCode      = 6
,   typeMax       = 7
};

// Position (from gpx file)
class Position
{
public:
             Position(time_t time,double lat,double lon,double ele) : time_(time),lon_(lon),lat_(lat),ele_(ele) {};
             Position() { time_=0 ; lon_=0.0 ; lat_=0.0 ; ele_=0.0 ; };
    virtual ~Position() {} ;
//  copy constructor
    Position(const Position& o) : time_(o.time_),lon_(o.lon_),lat_(o.lat_),ele_(o.ele_) {};

//  instance methods
    bool good()                 { return time_ || lon_ || lat_ || ele_ ; }
    std::string getTimeString() { if ( times_.empty() ) times_ = getExifTime(time_) ;  return times_; }
    time_t      getTime()       { return time_ ; }
    std::string toString();

//  getters/setters
    double lat()            {return lat_   ;}
    double lon()            {return lon_   ;}
    double ele()            {return ele_   ;}
    int    delta()          {return delta_ ;}
    void   delta(int delta) {delta_=delta  ;}

//  data
private:
    time_t      time_;
    double      lon_ ;
    double      lat_ ;
    double      ele_ ;
    std::string times_;
    int         delta_;

// public static data
public:
    static int    adjust_  ;
    static int    tz_      ;
    static int    dst_     ;
    static time_t deltaMax_;

// public static member functions
public:
    static int    Adjust() {return Position::adjust_ + Position::tz_ + Position::dst_ ;}
    static int    tz()     {return tz_    ;}
    static int    dst()    {return dst_   ;}
    static int    adjust() {return adjust_;}

    static std::string toExifString(double d,bool bRational,bool bLat);
    static std::string toExifString(double d);
    static std::string toExifTimeStamp(std::string& t);
};

std::string Position::toExifTimeStamp(std::string& t)
{
    char        result[200];
    const char* arg = t.c_str();
    int HH = 0 ;
    int mm = 0 ;
    int SS = 0 ;
    if ( strstr(arg,":") || strstr(arg,"-") ) {
        int  YY,MM,DD    ;
        char a,b,c,d,e   ;
        sscanf(arg,"%d%c%d%c%d%c%d%c%d%c%d",&YY,&a,&MM,&b,&DD,&c,&HH,&d,&mm,&e,&SS);
    }
    sprintf(result,"%d/1 %d/1 %d/1",HH,mm,SS);
    return std::string(result);
}

std::string Position::toExifString(double d)
{
    char result[200];
    d *= 100;
    sprintf(result,"%d/100",abs((int)d));
    return std::string(result);
}

std::string Position::toExifString(double d,bool bRational,bool bLat)
{
    const char* NS   = d>=0.0?"N":"S";
    const char* EW   = d>=0.0?"E":"W";
	const char* NSEW = bLat  ? NS: EW;
    if ( d < 0 ) d = -d;
    int deg = (int) d;
        d  -= deg;
        d  *= 60;
    int min = (int) d ;
        d  -= min;
        d  *= 60;
    int sec = (int)d;
    char result[200];
    sprintf(result,bRational ? "%d/1 %d/1 %d/1%s" : "%03d.%02d'%02d\"%s" ,deg,min,sec,bRational?"":NSEW);
    return std::string(result);
}

std::string Position::toString()
{
    char result[200];
    std::string sLat = Position::toExifString(lat_,false,true );
    std::string sLon = Position::toExifString(lon_,false,false);
    sprintf(result,"%s %s %-8.3f",sLon.c_str(),sLat.c_str(),ele_);
    return std::string(result);
}

int    Position::adjust_   = 0;
int    Position::tz_       = timeZoneAdjust();
int    Position::dst_      = 0;
time_t Position::deltaMax_ = 60 ;

// globals
typedef std::map<time_t,Position>           TimeDict_t;
typedef std::map<time_t,Position>::iterator TimeDict_i;
typedef std::vector<std::string>            strings_t;
TimeDict_t   gTimeDict ;
strings_t    gFiles;

///////////////////////////////////////////////////////////
// UserData - used by XML Parser
class UserData
{
public:
    UserData(Options& options) : indent(0),count(0),nTrkpt(0),bTime(false),bEle(false),options_(options) {};
    virtual ~UserData() {} ;

//  public data members
    int         indent;
    size_t      count ;
    Position    now ;
    Position    prev;
    int         nTrkpt;
    bool        bTime ;
    bool        bEle  ;
    double      ele;
    double      lat;
    double      lon;
    std::string xmlt;
    std::string exift;
    time_t      time;
    Options&    options_;
// static public data memembers
};

// XML Parser Callbacks
static void startElement(void* userData, const char* name, const char** atts )
{
    UserData* me = (UserData*) userData;
    //for ( int i = 0 ; i < me->indent ; i++ ) printf(" ");
    //printf("begin %s\n",name);
    me->bTime = strcmp(name,"time")==0;
    me->bEle  = strcmp(name,"ele")==0;

    if ( strcmp(name,"trkpt")==0 ) {
        me->nTrkpt++;
        while ( *atts ) {
            const char* a=atts[0];
            const char* v=atts[1];
            if ( !strcmp(a,"lat") ) me->lat = atof(v);
            if ( !strcmp(a,"lon") ) me->lon = atof(v);
            atts += 2 ;
        }
    }
    me->count++  ;
    me->indent++ ;
}

static void endElement(void* userData, const char* name)
{
    UserData* me = (UserData*) userData;
    me->indent-- ;
    if ( strcmp(name,"trkpt")==0 ) {

        me->nTrkpt--;
        me->now = Position(me->time,me->lat,me->lon,me->ele) ;

        if ( !me->prev.good() && me->options_.verbose ) {
            printf("trkseg %s begin ",me->now.getTimeString().c_str());
        }

        // printf("lat,lon = %f,%f ele = %f xml = %s exif = %s\n",me->lat,me->lon,me->ele,me->xmlt.c_str(),me->exift.c_str());

        // if we have a good previous position
        // add missed entries to timedict
        //if ( me->prev.good() && (me->now.getTime() - me->prev.getTime()) < Position::timeDiffMax ) {
        //  time_t missed = me->prev.getTime() ;
        //  while ( ++missed < me->now.getTime() )
        //      gTimeDict[missed] = me->prev ; // Position(missed,me->lat,me->lon,me->ele) ;
        //}

        // remember our location and put it in gTimeDict
        gTimeDict[me->time] = me->now ;
        me->prev = me->now ;
    }
    if ( strcmp(name,"trkseg")==0 && me->options_.verbose ) {
        printf("%s end\n",me->now.getTimeString().c_str());
    }
}

void charHandler(void* userData,const char* s,int len)
{
    UserData* me = (UserData*) userData;

    if ( me->nTrkpt == 1 ) {
        char buffer[100];
        int  l_max = 98 ; // lengthof(buffer) -2 ;

        if ( me->bTime && len > 5 ) {
            if ( len < l_max ) {
                memcpy(buffer,s,len);
                buffer[len]=0;
                char* b = buffer ;
                while ( *b == ' ' && b < buffer+len ) b++ ;
                me->xmlt  = b ;
                me->time  = parseTime(me->xmlt.c_str());
                me->exift = getExifTime(me->time);
            }
            me->bTime=false;
        }
        if ( me->bEle && len > 5 ) {
            if ( len < l_max ) {
                memcpy(buffer,s,len);
                buffer[len]=0;
                char* b = buffer ;
                while ( *b == ' ' && b < buffer+len ) b++ ;
                me->ele = atof(b);
            }
            me->bEle=false;
        }
    }
}

///////////////////////////////////////////////////////////
// Time Functions
time_t parseTime(const char* arg,bool bAdjust)
{
    time_t result = 0 ;
    try {
        //559 rmills@rmills-imac:~/bin $ exiv2 -pa ~/R.jpg | grep -i date
        //Exif.Image.DateTime                          Ascii      20  2009:08:03 08:58:57
        //Exif.Photo.DateTimeOriginal                  Ascii      20  2009:08:03 08:58:57
        //Exif.Photo.DateTimeDigitized                 Ascii      20  2009:08:03 08:58:57
        //Exif.GPSInfo.GPSDateStamp                    Ascii      21  2009-08-03T15:58:57Z

        // <time>2012-07-14T17:33:16Z</time>

        if ( strstr(arg,":") || strstr(arg,"-") ) {
            int  YY,MM,DD,HH,mm,SS ;
            char a,b,c,d,e   ;
            sscanf(arg,"%d%c%d%c%d%c%d%c%d%c%d",&YY,&a,&MM,&b,&DD,&c,&HH,&d,&mm,&e,&SS);

            struct tm T;
    #if 0
            int tm_sec;     /* seconds (0 - 60) */
            int tm_min;     /* minutes (0 - 59) */
            int tm_hour;    /* hours (0 - 23) */
            int tm_mday;    /* day of month (1 - 31) */
            int tm_mon;     /* month of year (0 - 11) */
            int tm_year;    /* year - 1900 */
            int tm_wday;    /* day of week (Sunday = 0) */
            int tm_yday;    /* day of year (0 - 365) */
            int tm_isdst;   /* is summer time in effect? */
            char *tm_zone;  /* abbreviation of timezone name */
            long tm_gmtoff; /* offset from UTC in seconds */
    #endif
            memset(&T,0,sizeof(T));
            T.tm_min  = mm  ;
            T.tm_hour = HH  ;
            T.tm_sec  = SS  ;
            if ( bAdjust ) T.tm_sec -= Position::Adjust();
            T.tm_year = YY -1900 ;
            T.tm_mon  = MM -1    ;
            T.tm_mday = DD  ;
            result = mktime(&T);
        }
    } catch ( ... ) {};
    return result ;
}

// West of GMT is negative (PDT = Pacific Daylight = -07:00 == -25200 seconds
int timeZoneAdjust()
{
    time_t    now   = time(NULL);
    struct tm local = *localtime(&now) ;
    int       offset;

#if   defined(_MSC_VER)
    TIME_ZONE_INFORMATION TimeZoneInfo;
    GetTimeZoneInformation( &TimeZoneInfo );
    offset = - (((int)TimeZoneInfo.Bias + (int)TimeZoneInfo.DaylightBias) * 60);
#elif defined(__CYGWIN__)
    struct tm lcopy  = *localtime(&now);
    time_t    gmt    =  timegm(&lcopy) ; // timegm modifies lcopy, so don't use local
    offset           = (int) ( ((long signed int) gmt) - ((long signed int) now) ) ;
#elif defined(OS_SOLARIS)
    time_t local_tt = (int) mktime(&local);
    time_t time_gmt = (int) mktime(gmtime(&now));
    offset = time_gmt - local_tt;
#else
    offset = local.tm_gmtoff ;
#endif

#if 0
    // debugging code
    struct tm utc = *gmtime(&now);
    printf("utc  :  offset = %6d dst = %d time = %s", 0     ,utc  .tm_isdst, asctime(&utc  ));
    printf("local:  offset = %6d dst = %d time = %s", offset,local.tm_isdst, asctime(&local));
    printf("timeZoneAdjust = %6d\n",offset);
#endif
    return offset ;
}

string getExifTime(const time_t t)
{
    static char result[100];
    strftime(result,sizeof(result),"%Y-%m-%d %H:%M:%S",localtime(&t));
    return result ;
}

std::string makePath(std::string dir,std::string file)
{
    return dir + std::string(EXV_SEPERATOR_STR) + file ;
}

const char* makePath(const char* dir,const char* file)
{
    static char result[_MAX_PATH] ;
    std::string r = makePath(std::string(dir),std::string(file));
    strcpy(result,r.c_str());
    return result;
}

// file utilities
bool readDir(const char* path,Options& options)
{
    bool bResult = false;

#ifdef _MSC_VER
    DWORD attrs    =  GetFileAttributes(path);
    bool  bOKAttrs =  attrs != INVALID_FILE_ATTRIBUTES;
    bool  bIsDir   = (attrs  & FILE_ATTRIBUTE_DIRECTORY) ? true : false ;

    if( bOKAttrs && bIsDir ) {
        bResult = true ;

        char     search[_MAX_PATH+10];
        strcpy_s(search,_MAX_PATH,path);
        strcat_s(search,_MAX_PATH,"\\*");

        WIN32_FIND_DATA ffd;
        HANDLE  hFind = FindFirstFile(search, &ffd);
        BOOL    bGo = hFind != INVALID_HANDLE_VALUE;

        if ( bGo ) {
            while ( bGo ) {
                if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    // _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
                }
                else
                {
                    std::string pathName = makePath(path,std::string(ffd.cFileName));
                    if ( getFileType(pathName,options) == typeImage ) {
                        gFiles.push_back( pathName );
                    }
                }
                bGo = FindNextFile(hFind, &ffd) != 0;
            }
            // CloseHandle(hFind);
        }
    }
#else
    DIR*    dir = opendir (path);
    if (dir != NULL)
    {
        bResult = true;
        struct dirent*  ent;

        // print all the files and directories within directory
        while ((ent = readdir (dir)) != NULL)
        {
            std::string pathName = makePath(path,ent->d_name);
            struct stat  buf     ;
            lstat(path, &buf );
            if ( ent->d_name[0] != '.' ) {

                // printf("reading %s => %s\n",ent->d_name,pathName.c_str());
                if ( getFileType(pathName,options) == typeImage ) {
                    gFiles.push_back( pathName );
                }
            }
        }
        closedir (dir);
    }
#endif
    return bResult ;
}

inline size_t sip(FILE* f,char* buffer,size_t max_len,size_t len)
{
    while ( !feof(f) && len < max_len && buffer[len-1] != '>')
        buffer[len++] = fgetc(f);
    return len;
}

bool readXML(const char* path,Options& options)
{
    FILE*       f       = fopen(path,"r");
    XML_Parser  parser  = XML_ParserCreate(NULL);
    bool bResult        = f && parser ;
    if ( bResult ) {
        char   buffer[8*1024];
        UserData me(options) ;

        XML_SetUserData            (parser, &me);
        XML_SetElementHandler      (parser, startElement, endElement);
        XML_SetCharacterDataHandler(parser,charHandler);

        // a little sip at the data
        size_t len = fread(buffer,1,sizeof(buffer)-100,f);
        const char* lead   = "<?xml" ;
        bResult = strncmp(lead,buffer,strlen(lead))==0;

        // swallow it
        if ( bResult ) {
            len = sip(f,buffer,sizeof buffer,len);
            bResult = XML_Parse(parser, buffer,(int)len, len == 0 ) == XML_STATUS_OK;
        }

        // drink the rest of the file
        while ( bResult && len != 0 ) {
            len = fread(buffer,1,sizeof(buffer)-100,f);
            len = sip(f,buffer,sizeof buffer,len);
            bResult = XML_Parse(parser, buffer,(int)len, len == 0 ) == XML_STATUS_OK;
        };
    }

    if ( f      ) fclose(f);
    if ( parser ) XML_ParserFree(parser);

    return bResult ;
}

bool readImage(const char* path,Options& /* options */)
{
    using namespace Exiv2;
    bool bResult = false ;

    try {
        Image::AutoPtr image = ImageFactory::open(path);
        if ( image.get() ) {
            image->readMetadata();
            ExifData &exifData = image->exifData();
            bResult = !exifData.empty();
        }
    } catch ( ... ) {};
    return bResult ;
}

time_t readImageTime(std::string path,std::string* pS=NULL)
{
    using namespace Exiv2;

    time_t       result       = 0 ;
	static std::map<std::string,time_t> cache;
	if ( cache.count(path) == 1 ) return cache[path];

    const char* dateStrings[] =
    { "Exif.Photo.DateTimeOriginal"
    , "Exif.Photo.DateTimeDigitized"
    , "Exif.Image.DateTime"
    , NULL
    };
    const char* ds            = dateStrings[0] ;

    while ( !result && ds++  ) {
        try {
            Image::AutoPtr image = ImageFactory::open(path);
            if ( image.get() ) {
                image->readMetadata();
                ExifData &exifData = image->exifData();
            //  printf("%s => %s\n",(ds-1), exifData[ds-1].toString().c_str());
                result = parseTime(exifData[ds-1].toString().c_str(),true);
                if ( result && pS ) *pS = exifData[ds-1].toString();
            }
        } catch ( ... ) {};
    }
	if ( result ) cache[path] = result;
    return result ;
}

bool sina(const char* s,const char** a)
{
    bool bResult = false ;
    int i = 0 ;
    while ( *s == '-' ) s++;
    while ( !bResult && a[i]) {
        const char* A = a[i] ;
        while ( *A == '-' ) A++ ;
        bResult = stricmp(s,A)==0;
        i++;
    }
    return bResult;
}

int readFile(const char* path,Options /* options */)
{
    FILE* f     = fopen(path,"r");
    int nResult = f ? typeFile : typeUnknown;
    if (  f ) {
        const char* docs[] = { ".doc",".txt", nil };
        const char* code[] = { ".cpp",".h"  ,".pl" ,".py" ,".pyc", nil };
        const char*  ext   = strstr(path,".");
        if  ( ext ) {
            if ( sina(ext,docs) ) nResult = typeDoc;
            if ( sina(ext,code) ) nResult = typeCode;
        }
    }
    if ( f ) fclose(f) ;

    return nResult ;
}

Position* searchTimeDict(TimeDict_t& td, const time_t& time,long long delta)
{
    Position* result = NULL;
    for ( int t = 0 ; !result && t < delta ; t++ ) {
        for ( int x = 0 ; !result && x < 2 ; x++ ) {
            int T = t * ((x==0)?-1:1);
            if ( td.count(time+T) ) {
                result = &td[time+T];
                result->delta(T);
            }
        }
    }
    return result;
}

int getFileType(std::string& path,Options& options) { return getFileType(path.c_str(),options); }
int getFileType(const char* path,Options& options)
{
    return readXML  (path,options) ? typeXML
        :  readDir  (path,options) ? typeDirectory
        :  readImage(path,options) ? typeImage
        :  readFile (path,options)
        ;
}

int version(const char* program)
{
    printf("%s: %s %s\n",program,__DATE__,__TIME__);
    return 0;
}

int help(const char* program,char const* words[],int nWords,bool /*bVerbose*/)
{
    printf("usage: %s ",program);
    for ( int i = 0 ; i < nWords ; i++ ) {
        if ( words[i] )
            printf("%c-%s%s",i?'|':'{',words[i],i>(-kwNOVALUE)?" value":"");
    }
    printf("} path+\n");
    return 0;
}

int compare(const char* a,const char* b)
{
    int result=*a && *b;
    while ( result && *a && *b) {
        char A=*a++;
        char B=*b++;
        result=tolower(A)==tolower(B);
    }
    return result;
}

int find(const char* arg,char const* words[],int nWords)
{
    if ( arg[0] != '-' ) return kwSYNTAX;

    int result=0;
    int count =0;

    for ( int i = 0 ; i < nWords ; i++) {
        int j = 0 ;
        while ( arg[j] == '-' ) j++;
        if ( ::compare(arg+j,words[i]) ) {
            result = i ;
            count++;
        }
    }

    return count==1?result:kwSYNTAX;
}

int parseTZ(const char* adjust)
{
    int   h=0;
    int   m=0;
    char  c  ;
    try {
        sscanf(adjust,"%d%c%d",&h,&c,&m);
    } catch ( ... ) {} ;

    return (3600*h)+(60*m);
}

bool mySort(std::string a,std::string b)
{
    time_t A = readImageTime(a);
    time_t B = readImageTime(b);
    return (A<B);
}

int main(int argc,const char* argv[])
{
    int result=0;
    const char* program = argv[0];

    const char* types[typeMax];
    types[typeUnknown  ] = "unknown";
    types[typeDirectory] = "directory";
    types[typeImage    ] = "image";
    types[typeXML      ] = "xml";
    types[typeDoc      ] = "doc";
    types[typeCode     ] = "code";
    types[typeFile     ] = "file";

    char const* keywords[kwMAX];
    memset(keywords,0,sizeof(keywords));
    keywords[kwHELP    ] = "help";
    keywords[kwVERSION ] = "version";
    keywords[kwVERBOSE ] = "verbose";
    keywords[kwDRYRUN  ] = "dryrun";
    keywords[kwDST     ] = "dst";
    keywords[kwADJUST  ] = "adjust";
    keywords[kwTZ      ] = "tz";
    keywords[kwDELTA   ] = "delta";

    map<std::string,string> shorts;
    shorts["-?"] = "-help";
    shorts["-h"] = "-help";
    shorts["-v"] = "-verbose";
    shorts["-V"] = "-version";
    shorts["-d"] = "-dst";
    shorts["-a"] = "-adjust";
    shorts["-t"] = "-tz";
    shorts["-D"] = "-delta";
    shorts["-s"] = "-delta";
    shorts["-X"] = "-dryrun";

    Options options ;
    options.help    = sina(keywords[kwHELP   ],argv) || argc < 2;
    options.verbose = sina(keywords[kwVERBOSE],argv);
    options.dryrun  = sina(keywords[kwDRYRUN ],argv);
    options.version = sina(keywords[kwVERSION],argv);
    options.dst     = sina(keywords[kwDST    ],argv);
    options.dryrun  = sina(keywords[kwDRYRUN ],argv);

    for ( int i = 1 ; !result && i < argc ; i++ ) {
        const char* arg   = argv[i++];
        if ( shorts.count(arg) ) arg = shorts[arg].c_str();

        const char* value = argv[i  ];
        int        ivalue = ::atoi(value?value:"0");
        int         key   = ::find(arg,keywords,kwMAX);
        int         needv = key < kwMAX && key > (-kwNOVALUE);

        if (!needv ) i--;
        if ( needv && !value) key = kwNEEDVALUE;

        switch ( key ) {
            case kwDST      : options.dst     = true ; break;
            case kwHELP     : options.help    = true ; break;
            case kwVERSION  : options.version = true ; break;
            case kwDRYRUN   : options.dryrun  = true ; break;
            case kwVERBOSE  : options.verbose = true ; break;
            case kwTZ       : Position::tz_      = parseTZ(value);break;
            case kwADJUST   : Position::adjust_  = ivalue;break;
            case kwDELTA    : Position::deltaMax_= ivalue;break;
            case kwNEEDVALUE: fprintf(stderr,"error: %s requires a value\n",arg); result = resultSyntaxError ; break ;
            case kwSYNTAX   : default:
            {
                int  type   = getFileType(arg,options) ;
                if ( options.verbose ) printf("%s %s ",arg,types[type]) ;
                if ( type == typeImage ) {
                    time_t t    = readImageTime(std::string(arg)) ;
                    char*  path = realpath(arg,NULL);
                    if  ( t && path ) {
                        if ( options.verbose) printf("%s %ld %s",path,(long int)t,asctime(localtime(&t)));
                        gFiles.push_back(path);
                    }
                    if ( path ) :: free((void*) path);
                }
                if ( type == typeUnknown ) {
                    fprintf(stderr,"error: illegal syntax %s\n",arg);
                    result = resultSyntaxError ;
                }
                if ( options.verbose ) printf("\n") ;
            }break;
        }
    }

    if ( options.help    ) ::help(program,keywords,kwMAX,options.verbose);
    if ( options.version ) ::version(program);

    if ( !result ) {
        sort(gFiles.begin(),gFiles.end(),mySort);
        if ( options.dst ) Position::dst_ = 3600;
        if ( options.verbose ) {
            int t = Position::tz();
            int d = Position::dst();
            int a = Position::adjust();
            int A = Position::Adjust();
            int s = A     ;
            int h = s/3600;
                s-= h*3600;
                s = abs(s);
            int m = s/60  ;
                s-= m*60  ;
            printf("tz,dsl,adjust = %d,%d,%d total = %dsecs (= %d:%d:%d)\n",t,d,a,A,h,m,s);
        }
        for ( size_t p = 0 ; !options.dryrun && p < gFiles.size() ; p++ ) {
            std::string arg = gFiles[p] ;
            std::string stamp ;
            try {
                time_t t       = readImageTime(arg,&stamp) ;
                Position* pPos = searchTimeDict(gTimeDict,t,Position::deltaMax_);
                Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(gFiles[p]);
                if ( image.get() ) {
                    image->readMetadata();
                    Exiv2::ExifData& exifData = image->exifData();
#if 0
                    /*
					char* keys[]={ "Exif.Image.GPSTag"
						         , "Exif.GPSInfo.GPSProcessingMethod"
					             , "Exif.GPSInfo.GPSAltitudeRef"
							     , "Exif.GPSInfo.GPSVersionID"
                                 , "Exif.GPSInfo.GPSProcessingMethod"
                                 , "Exif.GPSInfo.GPSVersionID"
                                 , "Exif.GPSInfo.GPSMapDatum"
                                 , "Exif.GPSInfo.GPSLatitude"
                                 , "Exif.GPSInfo.GPSLongitude"
                                 , "Exif.GPSInfo.GPSAltitude"
                                 , "Exif.GPSInfo.GPSAltitudeRef"
                                 , "Exif.GPSInfo.GPSLatitudeRef"
                                 , "Exif.GPSInfo.GPSLongitudeRef"
                                 , "Exif.GPSInfo.GPSDateStamp"
                                 , "Exif.GPSInfo.GPSTimeStamp"
					};
					static int bPrint = true ;
					for ( int k = 0 ; k < 15 ;   k++ ) {
						try {
							if ( bPrint ) printf("erasing %s\n",keys[k]);
							Exiv2::ExifKey  key = Exiv2::ExifKey(keys[k]);
							Exiv2::ExifData::iterator kk = exifData.findKey(key);
							if ( kk != exifData.end() ) exifData.erase(kk);
						} catch (...) {};
					}
					bPrint = false;
                    */
#endif
#if 0
					Exiv2::ExifData::const_iterator end = exifData.end();
					for (Exiv2::ExifData::iterator i = exifData.begin(); i != end; ++i) {
						char name[100];
						strcpy(name,i->key().c_str());
						// std::cout << "sniff " << i->key() << std::endl;
						if ( strstr(name,"GPS") )  {
							Exiv2::ExifData::iterator pos;
							Exiv2::ExifKey exifKey = Exiv2::ExifKey(name);
							pos = exifData.findKey(exifKey);
							while( pos != exifData.end()) {
								exifData.erase(pos);
							}
						}
					}
#endif
					if ( pPos ) {
						/*
						   struct _stat buf;
   int result;
   char timebuf[26];
   char* filename = "crt_stat.c";
   errno_t err;

   // Get data associated with "crt_stat.c": 
   result = _stat( filename, &buf );

   int _utime(
   const char *filename,
   struct _utimbuf *times 
);
   */

                        exifData["Exif.GPSInfo.GPSProcessingMethod" ] = "65 83 67 73 73 0 0 0 72 89 66 82 73 68 45 70 73 88"; // ASCII HYBRID-FIX
                        exifData["Exif.GPSInfo.GPSVersionID"        ] = "2 2 0 0";
                        exifData["Exif.GPSInfo.GPSMapDatum"         ] = "WGS-84";

                        exifData["Exif.GPSInfo.GPSLatitude"         ] = Position::toExifString(pPos->lat(),true,true);
                        exifData["Exif.GPSInfo.GPSLongitude"        ] = Position::toExifString(pPos->lon(),true,false);
                        exifData["Exif.GPSInfo.GPSAltitude"         ] = Position::toExifString(pPos->ele());

                        exifData["Exif.GPSInfo.GPSAltitudeRef"      ] = pPos->ele()<0.0?"1":"0";
						exifData["Exif.GPSInfo.GPSLatitudeRef"      ] = pPos->lat()>0?"N":"S";
						exifData["Exif.GPSInfo.GPSLongitudeRef"     ] = pPos->lon()>0?"E":"W";

                        exifData["Exif.GPSInfo.GPSDateStamp"        ] = stamp;
                        exifData["Exif.GPSInfo.GPSTimeStamp"        ] = Position::toExifTimeStamp(stamp);
						exifData["Exif.Image.GPSTag"                ] = 4908;

						printf("%s %s % 2d\n",arg.c_str(),pPos->toString().c_str(),pPos->delta());
                    } else {
                        printf("%s *** not in time dict ***\n",arg.c_str());
                    }
                    image->writeMetadata();
				}
            } catch ( ... ) {};
        }
    }

    return result ;
}
#endif