djiparsetxt
FieldDatabase.cpp
Go to the documentation of this file.
1 /**********
2 This program is free software: you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation, either version 3 of the License, or
5 (at your option) any later version.
6 
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11 
12 You should have received a copy of the GNU General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
14 **********/
15 /*
16  A C++ program to parse DJI's ".txt" log files (recorded by the "DJI Go 4" app).
17  Version 2021-05-20
18 
19  Copyright (c) 2021 Live Networks, Inc. All rights reserved.
20  For the latest version of this program (and more information), visit http://djilogs.live555.com
21 
22  A database that stores values of fields. (Each field is indexed by a 'label' (column name).)
23  Implementation.
24 */
25 
26 #include "FieldDatabase.hh"
27 #include <string.h>
28 
30 
31 FieldValue::FieldValue(FieldType type, u_int8_t val) : fType(type), fByte(val) {}
32 FieldValue::FieldValue(FieldType type, u_int16_t val) : fType(type), fBytes2(val) {}
33 FieldValue::FieldValue(FieldType type, u_int32_t val) : fType(type), fBytes4(val) {}
34 FieldValue::FieldValue(FieldType type, u_int64_t val) : fType(type), fBytes8(val) {}
35 FieldValue::FieldValue(FieldType type, float val) : fType(type), fFloat(val) {}
36 FieldValue::FieldValue(FieldType type, double val) : fType(type), fDouble(val) {}
37 FieldValue::FieldValue(FieldType type, char const* str) : fType(type) {
38  fStr = strdup(str);
39 }
40 
42  if (fType == String) delete[] fStr;
43 }
44 
45 
47 
50 }
51 
53  // Iterate through each of our 'unordered map's, deleting its contents:
54  // "fUMap":
55  {
56  std::unordered_map<char const*, FieldValue*>::iterator itr;
57  for (itr = fUMap.begin(); itr != fUMap.end(); itr++) {
58  delete itr->second;
59  }
60  }
61  // "fInterpretationTableMap":
62  {
63  std::unordered_map<char const*, InterpretationTable*>::iterator itr;
64  for (itr = fInterpretationTableMap.begin(); itr != fInterpretationTableMap.end(); itr++) {
65  delete itr->second;
66  }
67  }
68 }
69 
70 void FieldDatabase::addByteField(char const* label, u_int8_t value, int isSigned) {
71  addFieldValue(label, isSigned
72  ? new FieldValue(IntegerByteSigned, value)
73  : new FieldValue(IntegerByteUnsigned, value));
74 }
75 
76 void FieldDatabase::add2ByteField(char const* label, u_int16_t value, int isSigned) {
77  addFieldValue(label, isSigned
78  ? new FieldValue(Integer2ByteSigned, value)
79  : new FieldValue(Integer2ByteUnsigned, value));
80 }
81 
82 void FieldDatabase::add2ByteDateField(char const* label, u_int16_t value) {
83  addFieldValue(label, new FieldValue(Date2Byte, value));
84 }
85 
86 void FieldDatabase::add4ByteField(char const* label, u_int32_t value, int isSigned) {
87  addFieldValue(label, isSigned
88  ? new FieldValue(Integer4ByteSigned, value)
89  : new FieldValue(Integer4ByteUnsigned, value));
90 }
91 
92 void FieldDatabase::add4ByteVersionField(char const* label, u_int32_t value) {
93  addFieldValue(label, new FieldValue(Version4Byte, value));
94 }
95 
96 void FieldDatabase::addFloatField(char const* label, float value) {
97  addFieldValue(label, new FieldValue(Float, value));
98 }
99 
100 void FieldDatabase::addDoubleField(char const* label, double value) {
101  addFieldValue(label, new FieldValue(Double, value));
102 }
103 
104 void FieldDatabase::add8ByteTimestampField(char const* label, u_int64_t value, int isInMilliseconds) {
105  addFieldValue(label, isInMilliseconds
107  : new FieldValue(Timestamp8ByteInSeconds, value));
108 }
109 
110 void FieldDatabase::addStringField(char const* label, char const* str) {
111  addFieldValue(label, new FieldValue(String, str));
112 }
113 
114 void FieldDatabase::addFieldValue(char const* label, FieldValue* fieldValue) {
115  // First, delete any existing "FieldValue" for this label (the assignment below doesn't do that):
116  delete fUMap[label];
117 
118  // Then, store the new "FieldValue":
119  fUMap[label] = fieldValue;
120 }
121 
122 FieldValue const* FieldDatabase::lookupFieldValue(char const* label) {
123  // return fUMap[label]; // claimed not to work for "char const*" keys
124  for (std::unordered_map<char const*, FieldValue*>::iterator itr = fUMap.begin(); itr != fUMap.end(); ++itr) {
125  if (strcmp(itr->first, label) == 0) {
126  return itr->second;
127  }
128  }
129 
130  return NULL;
131 }
132 
134 ::newInterpretationTable(char const* interpretedLabel, char const* defaultResultString) {
135  InterpretationTable* it = new InterpretationTable(defaultResultString);
136  fInterpretationTableMap[interpretedLabel] = it;
137 
138  return it;
139 }
std::unordered_map< char const *, FieldValue * > fUMap
FieldType fType
void initializeInterpretationTables()
char const * fStr
FieldType
InterpretationTable * newInterpretationTable(char const *interpretedLabel, char const *defaultResultString)
void add2ByteDateField(char const *label, u_int16_t value)
void add8ByteTimestampField(char const *label, u_int64_t value, int isInMilliseconds)
void addStringField(char const *label, char const *str)
void addFieldValue(char const *label, FieldValue *fieldValue)
std::unordered_map< char const *, InterpretationTable * > fInterpretationTableMap
void addByteField(char const *label, u_int8_t value, int isSigned)
void add4ByteVersionField(char const *label, u_int32_t value)
void add2ByteField(char const *label, u_int16_t value, int isSigned)
FieldValue(FieldType type, u_int8_t val)
void addFloatField(char const *label, float value)
void addDoubleField(char const *label, double value)
virtual ~FieldValue()
void add4ByteField(char const *label, u_int32_t value, int isSigned)
FieldValue const * lookupFieldValue(char const *label)
virtual ~FieldDatabase()