File failed to load: https://isis.astrogeology.usgs.gov/6.0.0/Object/assets/jax/output/NativeMML/config.js
Isis 3 Programmer Reference
TableRecord.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "TableRecord.h"
9 
10 #include <iostream>
11 #include <string>
12 #include <vector>
13 
14 #include "IException.h"
15 #include "IString.h"
16 #include "TableField.h"
17 
18 using namespace std;
19 namespace Isis {
21  TableRecord::TableRecord(){
22  }
23 
25  TableRecord::~TableRecord() {
26  }
27 
28 
34  void TableRecord::operator+=(Isis::TableField &field) {
35  p_fields.push_back(field);
36  }
37 
45  Isis::TableField &TableRecord::operator[](const int field) {
46  return p_fields[field];
47  }
48 
60  TableField &TableRecord::operator[](const QString &field) {
61  Isis::IString upTemp = field;
62  upTemp.UpCase();
63  for (int i = 0; i < (int)p_fields.size(); i++) {
64  Isis::IString upField = p_fields[i].name();
65  upField.UpCase();
66  if (upTemp == upField) return p_fields[i];
67  }
68 
69  QString msg = "Field [" + field + "] does not exist in record";
70  throw IException(IException::Programmer, msg, _FILEINFO_);
71  }
72 
78  int TableRecord::Fields() const {
79  return p_fields.size();
80  }
81 
87  int TableRecord::RecordSize() const {
88  int bytes = 0;
89  for (int i = 0; i < (int)p_fields.size(); i++) bytes += p_fields[i].bytes();
90  return bytes;
91  }
92 
100  void TableRecord::Pack(char *buf) const {
101  int sbyte = 0;
102  for (int f = 0; f < Fields(); f++) {
103  const Isis::TableField &field = p_fields[f];
104  if (field.isDouble()) {
105  vector<double> vals = field;
106  for (unsigned int i = 0; i < vals.size(); i++) {
107  //*((double *)(buf+sbyte)) = vals[i];
108  memmove(buf + sbyte, &vals[i], 8);
109  sbyte += sizeof(double);
110  }
111  }
112  else if (field.isInteger()) {
113  vector<int> vals = field;
114  for (unsigned int i = 0; i < vals.size(); i++) {
115  //*((int *)(buf+sbyte)) = vals[i];
116  memmove(buf + sbyte, &vals[i], 4);
117  sbyte += sizeof(int);
118  }
119  }
120  else if (field.isText()) {
121  QString val = (QString)field;
122  for (int i = 0; i < field.size(); i++) {
123  if (i < (int)val.length()) {
124  buf[sbyte] = val[i].toLatin1();
125  }
126  else {
127  buf[sbyte] = 0;
128  }
129  sbyte++;
130  }
131  }
132  else if (field.isReal()) {
133  vector<float> vals = field;
134  for (unsigned int i = 0; i < vals.size(); i++) {
135  //*((int *)(buf+sbyte)) = vals[i];
136  memmove(buf + sbyte, &vals[i], 4);
137  sbyte += sizeof(float);
138  }
139  }
140  else {
141  string msg = "Invalid field type";
142  throw IException(IException::Programmer, msg, _FILEINFO_);
143  }
144  }
145  }
146 
152  void TableRecord::Unpack(const char *buf) {
153  int sbyte = 0;
154  for (int f = 0; f < Fields(); f++) {
155  Isis::TableField &field = p_fields[f];
156  field = (void *)&buf[sbyte];
157  sbyte += field.bytes();
158  }
159  }
160 
168  void TableRecord::Swap(char *buf) const {
169  int sbyte = 0;
170  for (int f = 0; f < Fields(); f++) {
171  const Isis::TableField &field = p_fields[f];
172  if (field.isDouble()) {
173  for (int i = 0; i < field.size(); i++) {
174  char *swap = &buf[sbyte];
175  char temp;
176  temp = swap[0];
177  swap[0] = swap[7];
178  swap[7] = temp;
179  temp = swap[1];
180  swap[1] = swap[6];
181  swap[6] = temp;
182  temp = swap[2];
183  swap[2] = swap[5];
184  swap[5] = temp;
185  temp = swap[3];
186  swap[3] = swap[4];
187  swap[4] = temp;
188 
189  sbyte += sizeof(double);
190  }
191  }
192  else if (field.isInteger()) {
193  for (int i = 0; i < field.size(); i++) {
194  char *swap = &buf[sbyte];
195  char temp;
196  temp = swap[0];
197  swap[0] = swap[3];
198  swap[3] = temp;
199  temp = swap[1];
200  swap[1] = swap[2];
201  swap[2] = temp;
202  sbyte += sizeof(int);
203  }
204  }
205  else if (field.isText()) {
206  sbyte += field.bytes();
207  }
208  else if (field.isReal()) {
209  for (int i = 0; i < field.size(); i++) {
210  char *swap = &buf[sbyte];
211  char temp;
212  temp = swap[0];
213  swap[0] = swap[3];
214  swap[3] = temp;
215  temp = swap[1];
216  swap[1] = swap[2];
217  swap[2] = temp;
218  sbyte += sizeof(float);
219  }
220  }
221  else {
222  string msg = "Unable to swap bytes. Invalid field type";
223  throw IException(IException::Programmer, msg, _FILEINFO_);
224  }
225  }
226  }
227 
228 
229 
230  QString TableRecord::toString(TableRecord record, QString fieldDelimiter, bool fieldNames, bool endLine) {
231  QString recordValues;
232  if (fieldNames) {
233  for (int fieldIndex = 0;fieldIndex < record.Fields();fieldIndex++) {
234  // write out the name of each field
235  if (record[fieldIndex].size() == 1) {
236  recordValues += record[fieldIndex].name();
237  }
238  else {
239  for (int fieldValueIndex = 0;fieldValueIndex < record[fieldIndex].size();fieldValueIndex++) {
240  recordValues += record[fieldIndex].name();
241  if (record[fieldIndex].isText()) {
242  // if it's a text field, exit the loop by adding the appropriate number of bytes
243  fieldValueIndex += record[fieldIndex].bytes();
244  }
245  else {
246  // if the field is multivalued, write the index of the field
247  recordValues += "(" + Isis::toString(fieldValueIndex) + ")";
248  }
249  if (fieldValueIndex != record[fieldIndex].size() - 1) {
250  // add a delimiter to all but the last value in this field
251  recordValues += fieldDelimiter;
252  }
253  }
254  }
255  // add a delimiter to all but the last field in this record
256  if (fieldIndex != record.Fields() - 1) {
257  recordValues += fieldDelimiter;
258  }
259  }
260  // end field names line
261  recordValues += "\n";
262  }
263 
264  for (int fieldIndex = 0;fieldIndex < record.Fields();fieldIndex++) {
265  // add value for each field in the record
266  recordValues += TableField::toString(record[fieldIndex], fieldDelimiter);
267  if (fieldIndex != record.Fields() - 1) {
268  // add delimiter to all but the last field in the record
269  recordValues += fieldDelimiter;
270  }
271  }
272  if (endLine) {
273  recordValues += "\n";
274  }
275  return recordValues;
276  }
277 
278 } // end namespace isis
Isis::TableRecord::Fields
int Fields() const
Returns the number of fields that are currently in the record.
Definition: TableRecord.cpp:78
Isis::TableRecord
Definition: TableRecord.h:38
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::TableField::isInteger
bool isInteger() const
Determines whether the field type is Integer.
Definition: TableField.cpp:122
Isis::IString::UpCase
IString UpCase()
Converst any lower case characters in the object IString with uppercase characters.
Definition: IString.cpp:617
Isis::TableField::isReal
bool isReal() const
Determines whether the field type is Text.
Definition: TableField.cpp:150
Isis::TableField::bytes
int bytes() const
Returns the number of bytes in the field value.
Definition: TableField.cpp:159
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::TableField::isDouble
bool isDouble() const
Determines whether the field type is Double.
Definition: TableField.cpp:132
Isis::TableField::size
int size() const
Returns the number of values stored for the field at each record.
Definition: TableField.cpp:168
std
Namespace for the standard library.
Isis::TableField::isText
bool isText() const
Determines whether the field type is Text.
Definition: TableField.cpp:141
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::TableField
Class for storing an Isis::Table's field information.
Definition: TableField.h:47

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the USGS Astrogeology Discussion Board
To report a bug, or suggest a feature go to: ISIS Github
File Modified: 07/13/2023 15:17:21