summaryrefslogtreecommitdiff
path: root/src/antpm-fit2gpx.cpp
blob: 0c2f30268a81d815a046a2875cbc637b2a080fc2 (plain)
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
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
// ***** BEGIN LICENSE BLOCK *****
//////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2014 RALOVICH, Kristóf                            //
//                                                                      //
// 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 3 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.                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
// ***** END LICENSE BLOCK *****

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

#include "AntMessage.hpp"
#include "FIT.hpp"
#include "common.hpp"
#include "Log.hpp"

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>

namespace po = boost::program_options;
namespace fs = boost::filesystem;
using namespace std;
using namespace antpm;


namespace antpm
{

template<>
Log*
ClassInstantiator<Log>::instantiate()
{
  return new Log(NULL);
}

}

const
std::vector<fs::path>
find_files(const fs::path & dir_path,         // in this directory,
           const std::string & ext // search for this extension,
           )
{
  std::vector<fs::path> paths;
  if ( !fs::exists( dir_path ) ) return paths;
  fs::directory_iterator end_itr; // default construction yields past-the-end
  for ( fs::directory_iterator itr( dir_path );
    itr != end_itr;
    ++itr )
  {
    if ( fs::is_directory(itr->status()) )
    {
      std::vector<fs::path> paths2(find_files( itr->path(), ext) );
      paths.insert(paths.end(), paths2.begin(), paths2.end());
    }
    else if ( fs::extension(*itr) == ext ) // see below
    {
      paths.push_back(*itr);
    }
  }
  return paths;
}


int
main(int argc, char** argv)
{
  antpm::Log::instance()->addSink(std::cout);
#ifndef NDEBUG
  antpm::Log::instance()->setLogReportingLevel(antpm::LOG_DBG);
#else
  antpm::Log::instance()->setLogReportingLevel(antpm::LOG_INF);
#endif

  // Declare the supported options.
  std::string fitFolder;
  std::string fitRootFile;
  bool        fixLastWrt = false;
  int         verbosityLevel = antpm::Log::instance()->getLogReportingLevel();
  po::options_description desc("Allowed options");
  desc.add_options()
    ("help,h",                                                  "produce help message")
    ("fitFolder,F",      po::value<std::string>(&fitFolder),    "Folder with FIT files")
    ("decode-fit-root,D", po::value<std::string>(&fitRootFile), "FIT file, encoding the root directory contents on a device, e.g. /tmp/0000.fit")
    ("fix-last-write,L", po::value<bool>(&fixLastWrt)->zero_tokens()->implicit_value(true), "Correct last-written time of .fit files on disk. No conversion to GPX takes place.")
    ("v",                po::value<int>(&verbosityLevel),       "Adjust verbosity level, varies in [1, 6]")
    ("version,V",                                               "Print version information")
    ;

  std::vector<const char*> args(argv, argv+argc);
  po::variables_map vm;
  try
  {
    //po::parsed_options parsed = po::parse_command_line(argc, argv, desc);
    po::parsed_options parsed = po::command_line_parser(argc, argv).options(desc).run();
    po::store(parsed, vm);
    po::notify(vm);
  }
  catch(po::error& error)
  {
    cerr << error.what() << "\n";
    cerr << desc << "\n";
    return EXIT_FAILURE;
  }

  if(vm.count("version") || vm.count("V"))
  {
    cout << argv[0] << " " << antpm::getVersionString() << "\n";
    return EXIT_SUCCESS;
  }

  if(vm.count("v"))
  {
    if(verbosityLevel >= antpm::LOG_ERR && verbosityLevel <= antpm::LOG_DBG3)
    {
      antpm::Log::instance()->setLogReportingLevel(static_cast<antpm::LogLevel>(verbosityLevel));
    }
  }

  if(vm.count("help") || vm.count("h")
     || (fitFolder.empty() && fitRootFile.empty()))
  {
    cout << desc << "\n";
    return EXIT_SUCCESS;
  }

  if(!fitRootFile.empty())
  {
    ZeroFileContent zfc;
    FIT fit;
    vector<uchar> v(readFile(fitRootFile.c_str()));
    if(fit.parseZeroFile(v, zfc))
       return EXIT_SUCCESS;
    else
      return EXIT_FAILURE;
  }

  if(!fs::is_directory(fs::path(fitFolder)))
  {
    LOG_VAR(fitFolder);
    CHECK_RETURN_RV_LOG_OK(fs::is_directory(fs::path(fitFolder)), EXIT_FAILURE);
  }

  std::vector<fs::path> fitFiles(find_files(fs::path(fitFolder), ".fit"));
  std::sort(fitFiles.begin(), fitFiles.end());

  for (size_t i = 0; i < fitFiles.size(); i++)
  {
    const std::string in(fitFiles[i].string());
    if(fixLastWrt) // for fixing up FIT file dates created before fixing time conversion bug
    {
      vector<uchar> fitBytes(readFile(in.c_str()));
      std::time_t fileCreationTime; // GMT/UTC
      bool has_ct = FIT::getCreationDate(fitBytes, fileCreationTime);
      time_t lastWrite             = fs::last_write_time(fs::path(in)); // GMT/UTC

      char tbuf[256];
      strftime(tbuf, sizeof(tbuf), "%d-%m-%Y %H:%M:%S", localtime(&lastWrite));

      char tbuf2[256];
      strftime(tbuf2, sizeof(tbuf2), "%d-%m-%Y %H:%M:%S", localtime(&fileCreationTime));

      if(has_ct)
      {
        time_t diff = (lastWrite-fileCreationTime);
        if(diff>1 || diff<-1)
        {
          LOG(LOG_INF) << in << ", fixing "
               << " new=" << tbuf2
               << "  ---->  old=" << tbuf
               << "\n";
          fs::last_write_time(fs::path(in), fileCreationTime);
        }
        else
        {
          LOG(LOG_DBG) << "Last write time already correct: " << in << "\n";
        }
      }
      else
      {
        LOG(LOG_INF) << "No FIT creation time: " << in << "\n";
      }
    }
    else
    {
#if BOOST_VERSION<=104300
      const std::string out(fitFiles[i].parent_path().string()+"//"+fitFiles[i].stem()+".gpx");
#else
      const std::string out(fitFiles[i].parent_path().string()+"//"+fitFiles[i].stem().string()+".gpx");
#endif
      LOG_VAR2(in, out);
      GPX gpx;
      FIT fit;
      vector<uchar> v(readFile(in.c_str()));
      if(fit.parse(v, gpx))
        gpx.writeToFile(out);
    }
  }


  return EXIT_SUCCESS;
}