Line |
Branch |
Decision |
Exec |
Source |
1 |
|
|
|
#ifndef RECORD_H |
2 |
|
|
|
#define RECORD_H |
3 |
|
|
|
|
4 |
|
|
|
#include <string> |
5 |
|
|
|
#include <fstream> // For std::ifstream |
6 |
|
|
|
#include <stdexcept> // For std::runtime_error |
7 |
|
|
|
#include <iostream> // For std::cerr |
8 |
|
|
|
#include "MlgDataType.h" // Include the MlgDataType enum |
9 |
|
|
|
|
10 |
|
|
|
extern int32_t readSwappedInt(std::ifstream* ifs); |
11 |
|
|
|
extern float readSwappedFloat(std::ifstream* ifs); |
12 |
|
|
|
extern int16_t readSwappedShort(std::ifstream* ifs); |
13 |
|
|
|
extern int8_t readByte(std::ifstream* ifs); |
14 |
|
|
|
|
15 |
|
|
|
class Record { |
16 |
|
|
|
private: |
17 |
|
|
|
std::string fieldName; |
18 |
|
|
|
MlgDataType type; |
19 |
|
|
|
float scale; |
20 |
|
|
|
|
21 |
|
|
|
public: |
22 |
|
|
✗ |
Record(const std::string& fieldName, MlgDataType type, float scale) |
23 |
|
|
✗ |
: fieldName(fieldName), type(type), scale(scale) {} |
24 |
|
|
|
|
25 |
|
|
✗ |
const std::string& getFieldName() const { return fieldName; } |
26 |
|
|
|
|
27 |
|
|
✗ |
float read(std::ifstream& ifs) { |
28 |
|
|
✗ |
float value = 0.0f; |
29 |
|
|
|
int32_t temp_s32; |
30 |
|
|
|
uint32_t temp_u32; |
31 |
|
|
|
int16_t temp_s16; |
32 |
|
|
|
uint16_t temp_u16; |
33 |
|
|
|
int8_t temp_s8; |
34 |
|
|
|
uint8_t temp_u8; |
35 |
|
|
|
float temp_float; |
36 |
|
|
|
|
37 |
|
|
✗ |
switch (type) { |
38 |
|
|
✗ |
case MlgDataType::MLG_DATA_S8: |
39 |
|
|
✗ |
temp_s8 = readByte(&ifs); |
40 |
|
|
✗ |
value = static_cast<float>(temp_s8) * scale; |
41 |
|
|
✗ |
break; |
42 |
|
|
✗ |
case MlgDataType::MLG_DATA_U8: |
43 |
|
|
✗ |
temp_u8 = static_cast<uint8_t>(readByte(&ifs)); // Read as signed, cast to unsigned |
44 |
|
|
✗ |
value = static_cast<float>(temp_u8) * scale; |
45 |
|
|
✗ |
break; |
46 |
|
|
✗ |
case MlgDataType::MLG_DATA_S16: |
47 |
|
|
✗ |
temp_s16 = readSwappedShort(&ifs); |
48 |
|
|
✗ |
value = static_cast<float>(temp_s16) * scale; |
49 |
|
|
✗ |
break; |
50 |
|
|
✗ |
case MlgDataType::MLG_DATA_U16: |
51 |
|
|
✗ |
temp_u16 = static_cast<uint16_t>(readSwappedShort(&ifs)); // Read as signed, cast to unsigned |
52 |
|
|
✗ |
value = static_cast<float>(temp_u16) * scale; |
53 |
|
|
✗ |
break; |
54 |
|
|
✗ |
case MlgDataType::MLG_DATA_S32: |
55 |
|
|
✗ |
temp_s32 = readSwappedInt(&ifs); |
56 |
|
|
✗ |
value = static_cast<float>(temp_s32) * scale; |
57 |
|
|
✗ |
break; |
58 |
|
|
✗ |
case MlgDataType::MLG_DATA_U32: |
59 |
|
|
✗ |
temp_u32 = static_cast<uint32_t>(readSwappedInt(&ifs)); // Read as signed, cast to unsigned |
60 |
|
|
✗ |
value = static_cast<float>(temp_u32) * scale; |
61 |
|
|
✗ |
break; |
62 |
|
|
✗ |
case MlgDataType::MLG_DATA_BIT: |
63 |
|
|
✗ |
temp_u8 = static_cast<uint8_t>(readByte(&ifs)); // Assuming bit is stored as a byte (0 or 1) |
64 |
|
|
✗ |
value = static_cast<float>(temp_u8); |
65 |
|
|
✗ |
break; |
66 |
|
|
✗ |
case MlgDataType::MLG_DATA_FLOAT: |
67 |
|
|
✗ |
temp_float = readSwappedFloat(&ifs); |
68 |
|
|
✗ |
value = temp_float * scale; |
69 |
|
|
✗ |
break; |
70 |
|
|
✗ |
default: |
71 |
|
|
✗ |
std::cerr << "Unsupported MLG data type: " << static_cast<int>(type) << std::endl; |
72 |
|
|
✗ |
throw std::runtime_error("Unsupported MLG data type"); |
73 |
|
|
|
} |
74 |
|
|
✗ |
return value; |
75 |
|
|
|
} |
76 |
|
|
|
}; |
77 |
|
|
|
|
78 |
|
|
|
#endif // RECORD_H |
79 |
|
|
|
|