-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (142 loc) · 7.45 KB
/
main.cpp
File metadata and controls
167 lines (142 loc) · 7.45 KB
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
/*
MIT License
Copyright (c) 2025 pavel.sokolov@gmail.com / CEZEO software Ltd. All rights reserved.
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 <fcntl.h>
#include <spdlog/spdlog.h>
#ifdef _WIN32
#include <io.h>
#endif
#include <cxxopts.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include "datasource.h"
#include "parser.h"
constexpr int kToolError{-1};
constexpr const char kBanner[] = R"%(
.:+oooooooooooooooooooooooooooooooooooooo: `/ooooooooooo/` :ooooo+/-`
`+dCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOEZshCEZEOCEZEOEZ#doCEZEOEZEZNs.
:CEZEON#ddddddddddddddddddddddddddddddNCEZEO#h.:hdddddddddddh/.yddddCEZEO#N+
:CEZEO+. .-----------.` `+CEZEOd/ .-----------. `:CEZEO/
CEZEO/ :CEZEOCEZEOEZNd. `/dCEZEO+` sNCEZEOCEZEO#Ny -CEZEO
CEZEO/ :#NCEZEOCEZEONd. :hCEZEOo` oNCEZEOCEZEO#Ny -CEZEO
:CEZEOo.` `-----------.` -yNEZ#Ns. `.-----------.` `/CEZEO/
:CEZEONCEZEOd/.ydCEZEOCEZEOdo.sNCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOEZNEZEZN+
`+dCEZEOEZEZdoCEZEOCEZEOEZ#N+CEZEOCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOCEZEOEZ#s.
.:+ooooo/` :+oooooooooo+. .+ooooooooooooooooooooooooooooooooooooo+/.
C E Z E O S O F T W A R E (c) 2025 FIT telemetry converter to VTT or JSON
)%";
constexpr const char kHelp[] = R"%(
usage: fitconvert -i input_file -o output_file -t output_type -f offset -s N
-i - path to .fit file to read data from
-o - path to .vtt or .json file to write to
-t - export type: vtt or json
-f - offset in milliseconds to sync video and .fit data (optional)
* if the offset is positive - 'offset' second of the data from .fit file will be displayed at the first second of the video.
it is for situations when you started video after starting recording your activity(that generated .fit file)
* if the offset is negative - the first second of .fit data will be displayed at abs('offset') second of the video
it is for situations when you started your activity (that generated .fit file) after starting the video
-s - smooth values by inserting N (0-5) smoothed values between timestamps (optional)
-v - values format: metric or imperial (optional, default metric)
-d - data to process, enumerate delimited by comma (default all): speed,distance,heartrate,altitude,power,cadence,temperature
)%";
int main(int argc, char* argv[]) {
spdlog::set_pattern("[%H:%M:%S.%e] %^[%l]%$ %v");
try {
cxxopts::Options cmd_options("FIT converter", "FIT telemetry converter to .VTT or .JSON");
cmd_options.add_options() //
("i,input", "", cxxopts::value<std::string>()) //
("o,output", "", cxxopts::value<std::string>()) //
("h,help", "") //
("d,data", "", cxxopts::value<std::string>()->default_value("")) //
("t,type", "", cxxopts::value<std::string>()->default_value(kOutputVttTag.data())) //
("f,offset", "", cxxopts::value<int64_t>()->default_value("0")) //
("v,values", "", cxxopts::value<std::string>()->default_value(kValuesMetric.data())) //
("s,smooth", "", cxxopts::value<uint8_t>()->default_value("0")); //
const auto cmd_result = cmd_options.parse(argc, argv);
if (argc < 2 || cmd_result.count("help") > 0 || cmd_result.count("input") == 0 || cmd_result.count("output") == 0) {
std::cout << kBanner << std::endl;
std::cout << kHelp << std::endl;
}
const std::string input_fit_file(cmd_result["input"].as<std::string>());
const std::string output_file(cmd_result["output"].as<std::string>());
const std::string output_type(cmd_result["type"].as<std::string>());
const int64_t offset(cmd_result["offset"].as<int64_t>());
const uint8_t smoothness(cmd_result["smooth"].as<uint8_t>());
const std::string datatypes(cmd_result["data"].as<std::string>());
const std::string values(cmd_result["values"].as<std::string>());
if (output_file == kStdoutTag) {
// disable informative output for cou output
spdlog::set_level(spdlog::level::err);
#ifdef _WIN32
(void)_setmode(_fileno(stdout), _O_BINARY);
#endif
} else {
spdlog::set_level(spdlog::level::info);
}
#ifdef _WIN32
if (input_fit_file == kStdinTag) {
(void)_setmode(_fileno(stdin), _O_BINARY);
}
#endif
if (output_type != kOutputJsonTag && output_type != kOutputVttTag) {
SPDLOG_ERROR("unknown type format specified: '{}, only 'vtt' or 'json' is supported", output_type);
return kToolError;
}
if (values != kValuesMetric && values != kValuesImperial) {
SPDLOG_ERROR("unknown values format specified: '{}, only 'metric' or 'imperial' is supported", values);
return kToolError;
}
uint32_t datatypes_mask = DataTypeNamesToMask(datatypes);
if (0 == datatypes_mask) {
datatypes_mask = std::numeric_limits<uint32_t>::max();
}
if (smoothness > 5) {
SPDLOG_ERROR("smoothness can not be more than 5");
return kToolError;
}
std::unique_ptr<DataSource> data_source;
size_t data_source_size{0};
if (kStdinTag == input_fit_file) {
data_source = std::make_unique<DataSourceStdin>();
} else {
data_source = std::make_unique<DataSourceFile>(input_fit_file);
}
const std::unique_ptr<FitResult> result =
Convert(std::move(data_source), output_type, offset, smoothness, datatypes_mask, values == kValuesImperial);
if (result->first == ParseResult::kSuccess) {
if (kStdoutTag == output_file) {
std::cout.write(result->second.GetString(), result->second.GetSize());
if (result->second.GetSize()) {
SPDLOG_ERROR("result is empty");
}
} else {
std::filesystem::remove(output_file);
std::ofstream output_stream(output_file, std::ios::out | std::ios::app | std::ios::binary);
output_stream.exceptions(std::ios_base::badbit);
output_stream.write(result->second.GetString(), result->second.GetSize());
}
} else {
SPDLOG_ERROR(".fit file problem during processing");
return kToolError;
}
} catch (const std::ios_base::failure& fail) {
SPDLOG_ERROR("file problem during processing: {}", fail.what());
return kToolError;
} catch (const std::exception& e) {
SPDLOG_ERROR("exception during processing: {}", e.what());
return kToolError;
}
return 0;
}