Isis 3 Programmer Reference
TableField.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "TableField.h"
8
9#include "IException.h"
10#include "PvlGroup.h"
11#include "PvlKeyword.h"
12
13using namespace std;
14namespace Isis {
15
25 TableField::TableField(const QString &name, TableField::Type type,
26 int size) {
27 m_name = name;
28 m_type = type;
29 m_size = size;
31 m_bytes = 4 * m_size;
32 m_ivalues.resize(m_size);
33 }
34 else if (m_type == TableField::Double) {
35 m_bytes = 8 * m_size;
36 m_dvalues.resize(m_size);
37 }
38 else if (m_type == TableField::Text) {
39 m_bytes = 1 * m_size;
40 m_svalue.resize(m_size);
41 }
42 else if (m_type == TableField::Real) {
43 m_bytes = 4 * m_size;
44 m_rvalues.resize(m_size);
45 }
46 }
47
60 m_name = (QString) field["Name"];
61 m_size = (int) field["Size"];
62 if ((QString) field["Type"] == "Integer") {
64 m_bytes = 4 * m_size;
65 m_ivalues.resize(m_size);
66 }
67 else if ((QString) field["Type"] == "Double") {
69 m_bytes = 8 * m_size;
70 m_dvalues.resize(m_size);
71 }
72 else if ((QString) field["Type"] == "Text") {
74 m_bytes = 1 * m_size;
75 m_svalue.resize(m_size);
76 }
77 else if ((QString) field["Type"] == "Real") {
79 m_bytes = 4 * m_size;
80 m_rvalues.resize(m_size);
81 }
82 else {
83 QString msg = "Field [" + m_name + "] has invalid type.";
84 throw IException(IException::Programmer, msg, _FILEINFO_);
85 }
86 }
87
91
97 QString TableField::name() const {
98 return m_name;
99 }
100
113 return m_type;
114 }
115
123 return (m_type == TableField::Integer);
124 }
125
132 bool TableField::isDouble() const {
133 return (m_type == TableField::Double);
134 }
135
141 bool TableField::isText() const {
142 return (m_type == TableField::Text);
143 }
144
150 bool TableField::isReal() const {
151 return (m_type == TableField::Real);
152 }
153
159 int TableField::bytes() const {
160 return m_bytes;
161 }
162
168 int TableField::size() const {
169 return m_size;
170 }
171
188 TableField::operator int() const {
189 if (m_type != TableField::Integer) {
190 QString msg = "Field [" + m_name + "] is not Integer.";
191 throw IException(IException::Programmer, msg, _FILEINFO_);
192 }
193 if (m_ivalues.size() > 1) {
194 QString msg = "Field [" + m_name + "] has multiple Integer values. "
195 "Use std::vector<int>().";
196 throw IException(IException::Programmer, msg, _FILEINFO_);
197 }
198 return m_ivalues[0];
199 }
200
217 TableField::operator double() const {
218 if (m_type != TableField::Double) {
219 QString msg = "Field [" + m_name + "] is not a Double.";
220 throw IException(IException::Programmer, msg, _FILEINFO_);
221 }
222 if (m_dvalues.size() > 1) {
223 QString msg = "Field [" + m_name + "] has multiple Double values. "
224 "Use std::vector<double>().";
225 throw IException(IException::Programmer, msg, _FILEINFO_);
226 }
227 return m_dvalues[0];
228 }
229
246 TableField::operator float() const {
247 if (m_type != TableField::Real) {
248 QString msg = "Field [" + m_name + "] is not Real.";
249 throw IException(IException::Programmer, msg, _FILEINFO_);
250 }
251 if (m_rvalues.size() > 1) {
252 QString msg = "Field [" + m_name + "] has multiple Real values. "
253 "Use std::vector<float>().";
254 throw IException(IException::Programmer, msg, _FILEINFO_);
255 }
256 return m_rvalues[0];
257 }
258
273 TableField::operator QString() const {
274 if (m_type != TableField::Text) {
275 QString msg = "Field [" + m_name + "] is not Text.";
276 throw IException(IException::Programmer, msg, _FILEINFO_);
277 }
278 return QString(m_svalue.toLatin1().data());
279 }
280
293 TableField::operator std::vector<int>() const {
294 if (m_type != TableField::Integer) {
295 QString msg = "Field [" + m_name + "] is not an Integer array.";
296 throw IException(IException::Programmer, msg, _FILEINFO_);
297 }
298 return m_ivalues;
299 }
300
313 TableField::operator std::vector<double>() const {
314 if (m_type != TableField::Double) {
315 QString msg = "Field [" + m_name + "] is not a Double array.";
316 throw IException(IException::Programmer, msg, _FILEINFO_);
317 }
318 return m_dvalues;
319 }
320
333 TableField::operator std::vector<float>() const {
334 if (m_type != TableField::Real) {
335 QString msg = "Field [" + m_name + "] is not a Real array.";
336 throw IException(IException::Programmer, msg, _FILEINFO_);
337 }
338 return m_rvalues;
339 }
340
350 void TableField::operator=(const int value) {
352 QString msg = "Unable to set field to the given int value. Field ["
353 + m_name + "] Type is not Integer.";
354 throw IException(IException::Programmer, msg, _FILEINFO_);
355 }
356 if (m_size > 1) {
357 QString msg = "Unable to set field to the given int value. "
358 "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
359 "Integer values. Use operator=(vector<int>).";
360 throw IException(IException::Programmer, msg, _FILEINFO_);
361 }
362 m_ivalues[0] = value;
363 }
364
374 void TableField::operator=(const double value) {
375 if (m_type != TableField::Double) {
376 QString msg = "Unable to set field to the given double value. Field ["
377 + m_name + "] Type is not Double.";
378 throw IException(IException::Programmer, msg, _FILEINFO_);
379 }
380 if (m_size > 1) {
381 QString msg = "Unable to set field to the given double value. "
382 "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
383 "Double values. Use operator=(vector<double>).";
384 throw IException(IException::Programmer, msg, _FILEINFO_);
385 }
386 m_dvalues[0] = value;
387 }
388
398 void TableField::operator=(const float value) {
399 if (m_type != TableField::Real) {
400 QString msg = "Unable to set field to the given float value. Field ["
401 + m_name + "] Type is not Real.";
402 throw IException(IException::Programmer, msg, _FILEINFO_);
403 }
404 if (m_size > 1) {
405 QString msg = "Unable to set field to the given float value. "
406 "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
407 "Real values. Use operator=(vector<float>).";
408 throw IException(IException::Programmer, msg, _FILEINFO_);
409 }
410 m_rvalues[0] = value;
411 }
412
420 void TableField::operator=(const QString &value) {
421 QString val = value;
422 if (m_type != TableField::Text) {
423 QString msg = "Unable to set field to the given string value. Field ["
424 + m_name + "] Type is not Text.";
425 throw IException(IException::Programmer, msg, _FILEINFO_);
426 }
427 if (m_size < (int) val.size()) {// automos with tracking ???
428 for (int i = m_size; i < val.size(); i++) {
429 // if the extra characters are not spaces or nulls, throw an erro
430 if (val[i] != ' ' && val[i] != '\0') {
431 QString msg = "Unable to set the Text TableField to the given string. "
432 "The number of bytes allowed for this field value ["
433 + Isis::toString(m_size) + "] is less than the length of the "
434 "given string [" + value + "].";
435 throw IException(IException::Unknown, msg, _FILEINFO_);
436 }
437 }
438 // if the extra characters are spaces and nulls, concatenate the string
439 val.resize(m_size);
440 }
441 m_svalue = val;
442 }
443
453 void TableField::operator=(const std::vector<int> &values) {
455 QString msg = "Unable to set field to the given vector of int values. "
456 "Field [" + m_name + "] Type is not Integer.";
457 throw IException(IException::Programmer, msg, _FILEINFO_);
458 }
459 else if ((int) values.size() != m_size) {
460 QString msg = "Unable to set field to the given vector of int values. "
461 "Field [" + m_name + "] values has size ["
462 + Isis::toString(m_size) + "].";
463 throw IException(IException::Programmer, msg, _FILEINFO_);
464 }
465 m_ivalues = values;
466 }
467
477 void TableField::operator=(const std::vector<double> &values) {
478 if (m_type != TableField::Double) {
479 QString msg = "Unable to set field to the given vector of double values. "
480 "Field [" + m_name + "] Type is not Double.";
481 throw IException(IException::Programmer, msg, _FILEINFO_);
482 }
483 else if ((int) values.size() != m_size) {
484 QString msg = "Unable to set field to the given vector of double values. "
485 "Field [" + m_name + "] values has size ["
486 + Isis::toString(m_size) + "].";
487 throw IException(IException::Programmer, msg, _FILEINFO_);
488 }
489 m_dvalues = values;
490 }
491
502 void TableField::operator=(const std::vector<float> &values) {
503 if (m_type != TableField::Real) {
504 QString msg = "Unable to set field to the given vector of float values. "
505 "Field [" + m_name + "] Type is not Real.";
506 throw IException(IException::Programmer, msg, _FILEINFO_);
507 }
508 else if ((int) values.size() != m_size) {
509 QString msg = "Unable to set field to the given vector of float values. "
510 "Field [" + m_name + "] values has size ["
511 + Isis::toString(m_size) + "].";
512 throw IException(IException::Programmer, msg, _FILEINFO_);
513 }
514 m_rvalues = values;
515 }
516
528 void TableField::operator=(const void *ibuf) {
529 char *buf = (char *) ibuf;
530 if (m_type == TableField::Double) {
531 for (unsigned int i = 0; i < m_dvalues.size(); i++) {
532 // m_dvalues[i] = ((double *) buf)[i];
533 double bufDouble;
534 memmove(&bufDouble, buf + i * 8, 8);
535 m_dvalues[i] = bufDouble;
536 }
537 }
538 else if (m_type == TableField::Integer) {
539 for (unsigned int i = 0; i < m_ivalues.size(); i++) {
540 // m_ivalues[i] = ((int *) buf)[i];
541 int bufInt;
542 memmove(&bufInt, buf + i * 4, 4);
543 m_ivalues[i] = bufInt;
544 }
545 }
546 else if (m_type == TableField::Text) {
547 m_svalue.resize(bytes());
548 for (int i = 0; i < bytes(); i++) {
549 m_svalue[i] = buf[i];
550 }
551 }
552 else if (m_type == TableField::Real) {
553 for (unsigned int i = 0; i < m_rvalues.size(); i++) {
554 // m_ivalues[i] = ((int *) buf)[i];
555 float bufFloat;
556 memmove(&bufFloat, buf + i * 4, 4);
557 m_rvalues[i] = bufFloat;
558 }
559 }
560 else {
561 string msg = "Undefined field type [" + IString(m_type) + "].";
562 throw IException(IException::Programmer, msg, _FILEINFO_);
563 }
564 }
565
575 void TableField::operator=(const char *buf) {
576 if (m_type != TableField::Text) {
577 QString msg = "Unable to set field to the given string value. Field [" + m_name + "] Type is not Text.";
578 throw IException(IException::Programmer, msg, _FILEINFO_);
579 }
580 m_svalue = buf;
581 }
582
590 PvlGroup group("Field");
591 group += PvlKeyword("Name", m_name);
592 if (m_type == TableField::Double) {
593 group += PvlKeyword("Type", "Double");
594 }
595 else if (m_type == TableField::Integer) {
596 group += PvlKeyword("Type", "Integer");
597 }
598 else if (m_type == TableField::Text) {
599 group += PvlKeyword("Type", "Text");
600 }
601 else if (m_type == TableField::Real) {
602 group += PvlKeyword("Type", "Real");
603 }
604 group += PvlKeyword("Size", Isis::toString(m_size));
605
606 return group;
607 }
608
609
610 QString TableField::toString(const TableField &field, QString delimiter){
611 QString fieldValues = "";
612 if (field.size()== 1){
613 if (field.isText()){
614 fieldValues = (QString)field;
615 }
616 else if (field.isInteger()){
617 fieldValues = Isis::toString((int)field);
618 }
619 else if (field.isDouble()){
620 fieldValues = Isis::toString((double)field);
621 }
622 else { //real
623 fieldValues = Isis::toString((float)field);
624 }
625 }
626 // Otherwise, build a vector to contain the entries
627 else {
628 if (field.isText()){
629 fieldValues +=(QString)field;
630 }
631 else if (field.isInteger()){
632 vector< int > currField = field;
633 for (int i = 0;i <(int)currField.size();i++){
634 fieldValues += Isis::toString(currField[i]);
635 if (i <(int)currField.size()- 1){
636 // add delimiter for all but the last element of the field
637 fieldValues += delimiter;
638 }
639 }
640 }
641 else if (field.isDouble()){
642 vector< double > currField = field;
643 for (int i = 0;i <(int)currField.size();i++){
644 fieldValues += Isis::toString(currField[i]);
645 if (i <(int)currField.size()- 1){
646 fieldValues += delimiter;
647 }
648 }
649 }
650 else { //if (field.isReal()) {
651 vector< float > currField = field;
652 for (int i = 0;i <(int)currField.size();i++){
653 fieldValues += Isis::toString(currField[i]);
654 if (i <(int)currField.size()- 1){
655 fieldValues += delimiter;
656 }
657 }
658 }
659 }
660 return fieldValues;
661 }
662} // end namespace isis
663
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
@ 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
Contains multiple PvlContainers.
Definition PvlGroup.h:41
A single keyword-value pair.
Definition PvlKeyword.h:87
Class for storing an Isis::Table's field information.
Definition TableField.h:47
std::vector< float > m_rvalues
Vector containing Real field values.
Definition TableField.h:109
int m_bytes
Number of bytes in field.
Definition TableField.h:102
bool isInteger() const
Determines whether the field type is Integer.
std::vector< double > m_dvalues
Vector containing double field values.
Definition TableField.h:106
bool isDouble() const
Determines whether the field type is Double.
bool isReal() const
Determines whether the field type is Text.
QString name() const
Returns the name of the TableField.
Type
This enum describes the value type for the TableField.
Definition TableField.h:52
@ Integer
The values in the field are 4 byte integers.
Definition TableField.h:53
@ Real
The values in the field are 4 byte reals or floats.
Definition TableField.h:57
@ Text
The values in the field are text strings with 1 byte per character.
Definition TableField.h:55
@ 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.
PvlGroup pvlGroup()
Creates and returns a PvlGroup named "Field" containing the following keywords and their respective v...
int m_size
Field size.
Definition TableField.h:100
bool isText() const
Determines whether the field type is Text.
QString m_name
Field name.
Definition TableField.h:98
TableField(const QString &name, Type type, int size=1)
Constructs a TableField object with the given field name, field value type, and field size.
Type type() const
Returns the enumerated value of the TableField value's type.
int bytes() const
Returns the number of bytes in the field value.
std::vector< int > m_ivalues
Vector containing integer field values.
Definition TableField.h:103
~TableField()
Destroys the TableField object.
QString m_svalue
String containing text value of field.
Definition TableField.h:112
Type m_type
Field value type.
Definition TableField.h:99
void operator=(const int value)
Sets field value equal to input if TableField::Type is Integer and size is 1.
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.