NDEVR
API Documentation
DBFParser.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/BaseValues.h>
4#include <NDEVR/File.h>
5#include <NDEVR/Buffer.h>
6#include <fstream>
7#include <vector>
8namespace NDEVR
9{
15 class NDEVR_BASE_API DBFParser
16 {
17 public:
22 DBFParser(const File& file);
27 void DumpAll(const char* szDestFileName);
34 void DumpFields(const char* szDestFileName, const char** fields, size_t numFields);
39 uint04 recordCount() const { return stHeader.uNumRecords; }
45 template<class t_type>
47 {
48 uint04 index = Constant<uint04>::Invalid;
49 uint04 row_skip_size = 0;
50 uint04 final_offset = 0;
51 for (uint04 i = 0; i < columns.size(); ++i)
52 {
53 if (column == columns[i].archName)
54 {
55 index = i;
56 final_offset = szRowSize - (row_skip_size + columns[i].uLength);
57 break;
58 }
59 row_skip_size += columns[i].uLength;
60 }
61 Buffer<t_type> buff;
62 if (IsInvalid(index))
63 return buff;
64 clFile.seekg(m_data_start, std::ios_base::beg);
65 buff.ensureCapacity(stHeader.uNumRecords);
66 size_t uNumRecords = 0;
67 String s;
68 while (uNumRecords < stHeader.uNumRecords)
69 {
70 char deleted;
71 clFile.read(&deleted, 1);
72 if (deleted == 0x2A)
73 {
74 clFile.seekg(szRowSize, std::ios_base::cur);
75 continue;
76 }
77
78 if (row_skip_size > 0)
79 {
80 clFile.seekg(row_skip_size, std::ios_base::cur);
81 }
83 s.setSize(columns[index].uLength);
84 clFile.read(s.begin(), columns[index].uLength);
85 buff.add(s.trimWhiteSpace().getAs<t_type>());
86
88 if (final_offset > 0)
89 clFile.seekg(final_offset, std::ios_base::cur);
90
91 ++uNumRecords;
92 }
93 return buff;
94 }
95
96 private:
100 struct DbfHeader_s
101 {
102 uint8_t iType;
103 char arcLastUpdate[3];
104
105 uint04 uNumRecords;
106
107 uint02 uFirstRecordOffset;
108 uint02 uRecordSize;
109
110 char uReserved[15];
111 uint01 fFlags;
112 uint01 uCodePageMark;
113
114 char uReserved2[2];
115 };
116
117#pragma pack(push)
118#pragma pack(1)
122 struct DbfRecord_s
123 {
124 char archName[11];
125 char chFieldType;
126
127 uint04 uDisplacement;
128 uint01 uLength;
129 uint01 uDecimalPlaces;
130 uint01 fFlags;
131
132 uint04 uNextValue;
133 uint01 uStepValue;
134 char uReserved[8];
135 };
139 struct FieldInfo_s
140 {
141 const DbfRecord_s& rstRecord;
142 size_t szSkipSize;
143
148 FieldInfo_s(const DbfRecord_s& rec) :
149 rstRecord(rec),
150 szSkipSize(0)
151 {
152 }
153 };
154#pragma pack(pop)
155 std::ifstream clFile;
156 DbfHeader_s stHeader;
157 Buffer<DbfRecord_s> columns;
158 uint04 m_data_start;
159 uint04 szRowSize;
160 uint04 szLargestFieldSize;
161 };
162}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
void add(t_type &&object)
Adds object to the end of the buffer.
Definition Buffer.hpp:190
void DumpFields(const char *szDestFileName, const char **fields, size_t numFields)
Dumps only the specified fields from the .dbf file to a destination text file.
Buffer< t_type > readColumn(const StringView &column)
Reads all values from a named column and returns them converted to the requested type.
Definition DBFParser.h:46
void DumpAll(const char *szDestFileName)
Dumps all records from the .dbf file to a destination text file.
uint04 recordCount() const
Returns the number of records in the .dbf file.
Definition DBFParser.h:39
DBFParser(const File &file)
Constructs a DBFParser by opening and parsing the header of the given .dbf file.
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
decltype(auto) getAs() const
Converts a string into an object.
Definition String.h:188
String & trimWhiteSpace()
Trims any white space (tabs, spaces, etc) from the beginning and end of the string.
The primary namespace for the NDEVR SDK.
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388