File failed to load: https://isis.astrogeology.usgs.gov/9.0.0/Object/assets/jax/output/NativeMML/config.js
Isis 3 Programmer Reference
TableRecord.cpp
1
5
6/* SPDX-License-Identifier: CC0-1.0 */
7
8#include "TableRecord.h"
9
10#include <iostream>
11#include <sstream>
12#include <string>
13#include <vector>
14
15#include "IException.h"
16#include "IString.h"
17#include "TableField.h"
18
19using namespace std;
20namespace Isis {
24
33 TableRecord::TableRecord(std::string tableRecordStr, char fieldDelimiter,
34 std::vector<QString> fieldNames, int numOfFieldValues) {
35 std::stringstream tableRecordStream;
36 tableRecordStream << tableRecordStr;
37
38 std::string fieldStr;
39 int i = 0;
40 while(std::getline(tableRecordStream, fieldStr, fieldDelimiter)) {
41 TableField tableField(fieldNames[i], TableField::Double);
42 tableField = std::stod(fieldStr); // convert string to double
43 this->operator+=(tableField);
44 i++;
45 }
46 }
47
51
52
59 p_fields.push_back(field);
60 }
61
70 return p_fields[field];
71 }
72
84 TableField &TableRecord::operator[](const QString &field) {
85 Isis::IString upTemp = field;
86 upTemp.UpCase();
87 for (int i = 0; i < (int)p_fields.size(); i++) {
88 Isis::IString upField = p_fields[i].name();
89 upField.UpCase();
90 if (upTemp == upField) return p_fields[i];
91 }
92
93 QString msg = "Field [" + field + "] does not exist in record";
94 throw IException(IException::Programmer, msg, _FILEINFO_);
95 }
96
103 return p_fields.size();
104 }
105
112 int bytes = 0;
113 for (int i = 0; i < (int)p_fields.size(); i++) bytes += p_fields[i].bytes();
114 return bytes;
115 }
116
124 void TableRecord::Pack(char *buf) const {
125 int sbyte = 0;
126 for (int f = 0; f < Fields(); f++) {
127 const Isis::TableField &field = p_fields[f];
128 if (field.isDouble()) {
129 vector<double> vals = field;
130 for (unsigned int i = 0; i < vals.size(); i++) {
131 //*((double *)(buf+sbyte)) = vals[i];
132 memmove(buf + sbyte, &vals[i], 8);
133 sbyte += sizeof(double);
134 }
135 }
136 else if (field.isInteger()) {
137 vector<int> vals = field;
138 for (unsigned int i = 0; i < vals.size(); i++) {
139 //*((int *)(buf+sbyte)) = vals[i];
140 memmove(buf + sbyte, &vals[i], 4);
141 sbyte += sizeof(int);
142 }
143 }
144 else if (field.isText()) {
145 QString val = (QString)field;
146 for (int i = 0; i < field.size(); i++) {
147 if (i < (int)val.length()) {
148 buf[sbyte] = val[i].toLatin1();
149 }
150 else {
151 buf[sbyte] = 0;
152 }
153 sbyte++;
154 }
155 }
156 else if (field.isReal()) {
157 vector<float> vals = field;
158 for (unsigned int i = 0; i < vals.size(); i++) {
159 //*((int *)(buf+sbyte)) = vals[i];
160 memmove(buf + sbyte, &vals[i], 4);
161 sbyte += sizeof(float);
162 }
163 }
164 else {
165 string msg = "Invalid field type";
166 throw IException(IException::Programmer, msg, _FILEINFO_);
167 }
168 }
169 }
170
176 void TableRecord::Unpack(const char *buf) {
177 int sbyte = 0;
178 for (int f = 0; f < Fields(); f++) {
179 Isis::TableField &field = p_fields[f];
180 field = (void *)&buf[sbyte];
181 sbyte += field.bytes();
182 }
183 }
184
192 void TableRecord::Swap(char *buf) const {
193 int sbyte = 0;
194 for (int f = 0; f < Fields(); f++) {
195 const Isis::TableField &field = p_fields[f];
196 if (field.isDouble()) {
197 for (int i = 0; i < field.size(); i++) {
198 char *swap = &buf[sbyte];
199 char temp;
200 temp = swap[0];
201 swap[0] = swap[7];
202 swap[7] = temp;
203 temp = swap[1];
204 swap[1] = swap[6];
205 swap[6] = temp;
206 temp = swap[2];
207 swap[2] = swap[5];
208 swap[5] = temp;
209 temp = swap[3];
210 swap[3] = swap[4];
211 swap[4] = temp;
212
213 sbyte += sizeof(double);
214 }
215 }
216 else if (field.isInteger()) {
217 for (int i = 0; i < field.size(); i++) {
218 char *swap = &buf[sbyte];
219 char temp;
220 temp = swap[0];
221 swap[0] = swap[3];
222 swap[3] = temp;
223 temp = swap[1];
224 swap[1] = swap[2];
225 swap[2] = temp;
226 sbyte += sizeof(int);
227 }
228 }
229 else if (field.isText()) {
230 sbyte += field.bytes();
231 }
232 else if (field.isReal()) {
233 for (int i = 0; i < field.size(); i++) {
234 char *swap = &buf[sbyte];
235 char temp;
236 temp = swap[0];
237 swap[0] = swap[3];
238 swap[3] = temp;
239 temp = swap[1];
240 swap[1] = swap[2];
241 swap[2] = temp;
242 sbyte += sizeof(float);
243 }
244 }
245 else {
246 string msg = "Unable to swap bytes. Invalid field type";
247 throw IException(IException::Programmer, msg, _FILEINFO_);
248 }
249 }
250 }
251
252
253
254 QString TableRecord::toString(TableRecord record, QString fieldDelimiter, bool fieldNames, bool endLine) {
255 QString recordValues;
256 if (fieldNames) {
257 for (int fieldIndex = 0;fieldIndex < record.Fields();fieldIndex++) {
258 // write out the name of each field
259 if (record[fieldIndex].size() == 1) {
260 recordValues += record[fieldIndex].name();
261 }
262 else {
263 for (int fieldValueIndex = 0;fieldValueIndex < record[fieldIndex].size();fieldValueIndex++) {
264 recordValues += record[fieldIndex].name();
265 if (record[fieldIndex].isText()) {
266 // if it's a text field, exit the loop by adding the appropriate number of bytes
267 fieldValueIndex += record[fieldIndex].bytes();
268 }
269 else {
270 // if the field is multivalued, write the index of the field
271 recordValues += "(" + Isis::toString(fieldValueIndex) + ")";
272 }
273 if (fieldValueIndex != record[fieldIndex].size() - 1) {
274 // add a delimiter to all but the last value in this field
275 recordValues += fieldDelimiter;
276 }
277 }
278 }
279 // add a delimiter to all but the last field in this record
280 if (fieldIndex != record.Fields() - 1) {
281 recordValues += fieldDelimiter;
282 }
283 }
284 // end field names line
285 recordValues += "\n";
286 }
287
288 for (int fieldIndex = 0;fieldIndex < record.Fields();fieldIndex++) {
289 // add value for each field in the record
290 recordValues += TableField::toString(record[fieldIndex], fieldDelimiter);
291 if (fieldIndex != record.Fields() - 1) {
292 // add delimiter to all but the last field in the record
293 recordValues += fieldDelimiter;
294 }
295 }
296 if (endLine) {
297 recordValues += "\n";
298 }
299 return recordValues;
300 }
301
302} // end namespace isis
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Adds specific functionality to C++ strings.
Definition IString.h:165
IString UpCase()
Converst any lower case characters in the object IString with uppercase characters.
Definition IString.cpp:617
Class for storing an Isis::Table's field information.
Definition TableField.h:47
bool isInteger() const
Determines whether the field type is Integer.
bool isDouble() const
Determines whether the field type is Double.
bool isReal() const
Determines whether the field type is Text.
@ Double
The values in the field are 8 byte doubles.
Definition TableField.h:54
int size() const
Returns the number of values stored for the field at each record.
bool isText() const
Determines whether the field type is Text.
int bytes() const
Returns the number of bytes in the field value.
std::vector< TableField > p_fields
Vector of TableFields in the record.
Definition TableRecord.h:60
TableRecord()
Constructs an empty TableRecord object. No member variables are set.
void Unpack(const char *buf)
Reads record information from the binary buffer.
int RecordSize() const
Returns the number of bytes per record.
int Fields() const
Returns the number of fields that are currently in the record.
void Pack(char *buf) const
Writes record information into the binary buffer.
~TableRecord()
Destroys the TableRecord object.
void Swap(char *buf) const
Swaps bytes of the buffer, depending on the TableField::Type.
void operator+=(Isis::TableField &field)
Adds a TableField to a TableRecord.
TableField & operator[](const int field)
Returns the TableField at the specified location in the TableRecord.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
Namespace for the standard library.