-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfieldOutput.cpp
More file actions
182 lines (163 loc) · 5.43 KB
/
fieldOutput.cpp
File metadata and controls
182 lines (163 loc) · 5.43 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**********
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**********/
/*
A C++ program to parse DJI's ".txt" log files (recorded by the "DJI Go 4" app).
Version 2019-02-08
Copyright (c) 2019 Live Networks, Inc. All rights reserved.
For the latest version of this program (and more information), visit http://djilogs.live555.com
Outputting a single field of data from the table.
Implementation.
*/
#include "FieldDatabase.hh"
#include <stdio.h>
#include <time.h>
void FieldDatabase::outputField(char const* label, unsigned numFractionalDigits) {
FieldValue const* fieldValue = lookupFieldValue(label);
if (fieldValue == NULL) return; // output nothing for a nonexistent field
int timeIsInMilliseconds = 0; // by default
switch (fieldValue->fType) {
case IntegerByteUnsigned: {
printf("%u", fieldValue->fByte);
break;
}
case IntegerByteSigned: {
printf("%d", (int8_t)(fieldValue->fByte));
break;
}
case Integer2ByteUnsigned: {
printf("%u", fieldValue->fBytes2);
break;
}
case Integer2ByteSigned: {
printf("%d", (int16_t)(fieldValue->fBytes2));
break;
}
case Date2Byte: {
// Interpret the two bytes as: 7 bits (years since 1980) + 4 bits (month) + 5 bits (day of month):
printf("%u/%02u/%02u",
((fieldValue->fBytes2&0xFE00)>>9) + 1980,
(fieldValue->fBytes2&0x01E0)>>5,
(fieldValue->fBytes2&0x001F));
break;
}
case Integer4ByteUnsigned: {
printf("%u", fieldValue->fBytes4);
break;
}
case Integer4ByteSigned: {
printf("%d", (int32_t)(fieldValue->fBytes4));
break;
}
case Version4Byte: {
// Use the first 3 bytes (big-endian) as version numbers:
u_int32_t v = fieldValue->fBytes4;
printf("%u.%u.%u", (v>>24)&0xFF, (v>>16)&0xFF, (v>>8)&0xFF);
break;
}
case Float: {
printf("%.*f", numFractionalDigits, fieldValue->fFloat);
break;
}
case Double: {
printf("%.*f", numFractionalDigits, fieldValue->fDouble);
break;
}
case Timestamp8ByteInMilliseconds: {
timeIsInMilliseconds = 1;
// fall through to:
}
case Timestamp8ByteInSeconds: {
u_int64_t time = fieldValue->fBytes8;
u_int64_t timeInSeconds;
unsigned milliseconds;
if (timeIsInMilliseconds) {
timeInSeconds = time/1000;
milliseconds = time%1000;
} else {
timeInSeconds = time;
milliseconds = 0;
}
struct tm* convertedTime = gmtime((time_t*)&timeInSeconds);
if (convertedTime == NULL) {
fprintf(stderr, "outputField(8-byte timestamp): gmtime(%llu) failed!\n", timeInSeconds);
return;
}
printf("%u/%02u/%02u %02u:%02u:%02u",
convertedTime->tm_year + 1900, convertedTime->tm_mon + 1, convertedTime->tm_mday,
convertedTime->tm_hour, convertedTime->tm_min, convertedTime->tm_sec);
if (timeIsInMilliseconds) {
printf(".%03u", milliseconds);
}
break;
}
case String: {
printf("%s", fieldValue->fStr);
break;
}
}
}
void FieldDatabase::outputFieldAsBoolean(char const* label) {
FieldValue const* fieldValue = lookupFieldValue(label);
if (fieldValue == NULL) return; // output nothing for a nonexistent field
int booleanValue = 0;
switch (fieldValue->fType) {
case IntegerByteUnsigned:
case IntegerByteSigned: {
booleanValue = fieldValue->fByte != 0;
break;
}
case Integer2ByteUnsigned:
case Integer2ByteSigned: {
booleanValue = fieldValue->fBytes2 != 0;
break;
}
case Integer4ByteUnsigned:
case Integer4ByteSigned: {
booleanValue = fieldValue->fBytes4 != 0;
break;
}
default: {
return; // print nothing if the field cannot be interpreted as Boolean
}
}
printf(booleanValue ? "True" : "False");
}
void FieldDatabase::outputFieldInterpreted(char const* label, char const* interpretedLabel) {
// First, check whether we currently have a value for the field "label":
FieldValue const* fieldValue = lookupFieldValue(label);
if (fieldValue == NULL) return; // output nothing for a nonexistent field
// Next, check its type. It needs to be an unsigned integer type <= 4 bytes long:
u_int32_t intValue;
switch (fieldValue->fType) {
case IntegerByteUnsigned: {
intValue = (u_int32_t)(fieldValue->fByte);
break;
}
case Integer2ByteUnsigned: {
intValue = (u_int32_t)(fieldValue->fBytes2);
break;
}
case Integer4ByteUnsigned: {
intValue = fieldValue->fBytes4;;
break;
}
default: {
return; // bad type
}
}
// Now, look up an InterpretationTable for "interpretedLabel":
InterpretationTable* interpretationTable = fInterpretationTableMap[interpretedLabel];
if (interpretationTable == NULL) return;
// And use this to look up (and print) a string 'interpretation' of our integer value:
printf("%s", interpretationTable->lookup(intValue));
}