Isis 3 Programmer Reference
TableField.cpp
Go to the documentation of this file.
1 
23 #include "TableField.h"
24 
25 #include "IException.h"
26 #include "PvlGroup.h"
27 #include "PvlKeyword.h"
28 
29 using namespace std;
30 namespace Isis {
31 
41  TableField::TableField(const QString &name, TableField::Type type,
42  int size) {
43  m_name = name;
44  m_type = type;
45  m_size = size;
46  if (m_type == TableField::Integer) {
47  m_bytes = 4 * m_size;
48  m_ivalues.resize(m_size);
49  }
50  else if (m_type == TableField::Double) {
51  m_bytes = 8 * m_size;
52  m_dvalues.resize(m_size);
53  }
54  else if (m_type == TableField::Text) {
55  m_bytes = 1 * m_size;
56  m_svalue.resize(m_size);
57  }
58  else if (m_type == TableField::Real) {
59  m_bytes = 4 * m_size;
60  m_rvalues.resize(m_size);
61  }
62  }
63 
75  TableField::TableField(PvlGroup &field) {
76  m_name = (QString) field["Name"];
77  m_size = (int) field["Size"];
78  if ((QString) field["Type"] == "Integer") {
79  m_type = TableField::Integer;
80  m_bytes = 4 * m_size;
81  m_ivalues.resize(m_size);
82  }
83  else if ((QString) field["Type"] == "Double") {
84  m_type = TableField::Double;
85  m_bytes = 8 * m_size;
86  m_dvalues.resize(m_size);
87  }
88  else if ((QString) field["Type"] == "Text") {
89  m_type = TableField::Text;
90  m_bytes = 1 * m_size;
91  m_svalue.resize(m_size);
92  }
93  else if ((QString) field["Type"] == "Real") {
94  m_type = TableField::Real;
95  m_bytes = 4 * m_size;
96  m_rvalues.resize(m_size);
97  }
98  else {
99  QString msg = "Field [" + m_name + "] has invalid type.";
100  throw IException(IException::Programmer, msg, _FILEINFO_);
101  }
102  }
103 
105  TableField::~TableField() {
106  }
107 
113  QString TableField::name() const {
114  return m_name;
115  }
116 
128  TableField::Type TableField::type() const {
129  return m_type;
130  }
131 
138  bool TableField::isInteger() const {
139  return (m_type == TableField::Integer);
140  }
141 
148  bool TableField::isDouble() const {
149  return (m_type == TableField::Double);
150  }
151 
157  bool TableField::isText() const {
158  return (m_type == TableField::Text);
159  }
160 
166  bool TableField::isReal() const {
167  return (m_type == TableField::Real);
168  }
169 
175  int TableField::bytes() const {
176  return m_bytes;
177  }
178 
184  int TableField::size() const {
185  return m_size;
186  }
187 
204  TableField::operator int() const {
205  if (m_type != TableField::Integer) {
206  QString msg = "Field [" + m_name + "] is not Integer.";
207  throw IException(IException::Programmer, msg, _FILEINFO_);
208  }
209  if (m_ivalues.size() > 1) {
210  QString msg = "Field [" + m_name + "] has multiple Integer values. "
211  "Use std::vector<int>().";
212  throw IException(IException::Programmer, msg, _FILEINFO_);
213  }
214  return m_ivalues[0];
215  }
216 
233  TableField::operator double() const {
234  if (m_type != TableField::Double) {
235  QString msg = "Field [" + m_name + "] is not a Double.";
236  throw IException(IException::Programmer, msg, _FILEINFO_);
237  }
238  if (m_dvalues.size() > 1) {
239  QString msg = "Field [" + m_name + "] has multiple Double values. "
240  "Use std::vector<double>().";
241  throw IException(IException::Programmer, msg, _FILEINFO_);
242  }
243  return m_dvalues[0];
244  }
245 
262  TableField::operator float() const {
263  if (m_type != TableField::Real) {
264  QString msg = "Field [" + m_name + "] is not Real.";
265  throw IException(IException::Programmer, msg, _FILEINFO_);
266  }
267  if (m_rvalues.size() > 1) {
268  QString msg = "Field [" + m_name + "] has multiple Real values. "
269  "Use std::vector<float>().";
270  throw IException(IException::Programmer, msg, _FILEINFO_);
271  }
272  return m_rvalues[0];
273  }
274 
289  TableField::operator QString() const {
290  if (m_type != TableField::Text) {
291  QString msg = "Field [" + m_name + "] is not Text.";
292  throw IException(IException::Programmer, msg, _FILEINFO_);
293  }
294  return QString(m_svalue.toLatin1().data());
295  }
296 
309  TableField::operator std::vector<int>() const {
310  if (m_type != TableField::Integer) {
311  QString msg = "Field [" + m_name + "] is not an Integer array.";
312  throw IException(IException::Programmer, msg, _FILEINFO_);
313  }
314  return m_ivalues;
315  }
316 
329  TableField::operator std::vector<double>() const {
330  if (m_type != TableField::Double) {
331  QString msg = "Field [" + m_name + "] is not a Double array.";
332  throw IException(IException::Programmer, msg, _FILEINFO_);
333  }
334  return m_dvalues;
335  }
336 
349  TableField::operator std::vector<float>() const {
350  if (m_type != TableField::Real) {
351  QString msg = "Field [" + m_name + "] is not a Real array.";
352  throw IException(IException::Programmer, msg, _FILEINFO_);
353  }
354  return m_rvalues;
355  }
356 
366  void TableField::operator=(const int value) {
367  if (m_type != TableField::Integer) {
368  QString msg = "Unable to set field to the given int value. Field ["
369  + m_name + "] Type is not Integer.";
370  throw IException(IException::Programmer, msg, _FILEINFO_);
371  }
372  if (m_size > 1) {
373  QString msg = "Unable to set field to the given int value. "
374  "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
375  "Integer values. Use operator=(vector<int>).";
376  throw IException(IException::Programmer, msg, _FILEINFO_);
377  }
378  m_ivalues[0] = value;
379  }
380 
390  void TableField::operator=(const double value) {
391  if (m_type != TableField::Double) {
392  QString msg = "Unable to set field to the given double value. Field ["
393  + m_name + "] Type is not Double.";
394  throw IException(IException::Programmer, msg, _FILEINFO_);
395  }
396  if (m_size > 1) {
397  QString msg = "Unable to set field to the given double value. "
398  "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
399  "Double values. Use operator=(vector<double>).";
400  throw IException(IException::Programmer, msg, _FILEINFO_);
401  }
402  m_dvalues[0] = value;
403  }
404 
414  void TableField::operator=(const float value) {
415  if (m_type != TableField::Real) {
416  QString msg = "Unable to set field to the given float value. Field ["
417  + m_name + "] Type is not Real.";
418  throw IException(IException::Programmer, msg, _FILEINFO_);
419  }
420  if (m_size > 1) {
421  QString msg = "Unable to set field to the given float value. "
422  "Field [" + m_name + "] has [" + Isis::toString(m_size) + "] "
423  "Real values. Use operator=(vector<float>).";
424  throw IException(IException::Programmer, msg, _FILEINFO_);
425  }
426  m_rvalues[0] = value;
427  }
428 
436  void TableField::operator=(const QString &value) {
437  QString val = value;
438  if (m_type != TableField::Text) {
439  QString msg = "Unable to set field to the given string value. Field ["
440  + m_name + "] Type is not Text.";
441  throw IException(IException::Programmer, msg, _FILEINFO_);
442  }
443  if (m_size < (int) val.size()) {// automos with tracking ???
444  for (int i = m_size; i < val.size(); i++) {
445  // if the extra characters are not spaces or nulls, throw an erro
446  if (val[i] != ' ' && val[i] != '\0') {
447  QString msg = "Unable to set the Text TableField to the given string. "
448  "The number of bytes allowed for this field value ["
449  + Isis::toString(m_size) + "] is less than the length of the "
450  "given string [" + value + "].";
451  throw IException(IException::Unknown, msg, _FILEINFO_);
452  }
453  }
454  // if the extra characters are spaces and nulls, concatenate the string
455  val.resize(m_size);
456  }
457  m_svalue = val;
458  }
459 
469  void TableField::operator=(const std::vector<int> &values) {
470  if (m_type != TableField::Integer) {
471  QString msg = "Unable to set field to the given vector of int values. "
472  "Field [" + m_name + "] Type is not Integer.";
473  throw IException(IException::Programmer, msg, _FILEINFO_);
474  }
475  else if ((int) values.size() != m_size) {
476  QString msg = "Unable to set field to the given vector of int values. "
477  "Field [" + m_name + "] values has size ["
478  + Isis::toString(m_size) + "].";
479  throw IException(IException::Programmer, msg, _FILEINFO_);
480  }
481  m_ivalues = values;
482  }
483 
493  void TableField::operator=(const std::vector<double> &values) {
494  if (m_type != TableField::Double) {
495  QString msg = "Unable to set field to the given vector of double values. "
496  "Field [" + m_name + "] Type is not Double.";
497  throw IException(IException::Programmer, msg, _FILEINFO_);
498  }
499  else if ((int) values.size() != m_size) {
500  QString msg = "Unable to set field to the given vector of double values. "
501  "Field [" + m_name + "] values has size ["
502  + Isis::toString(m_size) + "].";
503  throw IException(IException::Programmer, msg, _FILEINFO_);
504  }
505  m_dvalues = values;
506  }
507 
518  void TableField::operator=(const std::vector<float> &values) {
519  if (m_type != TableField::Real) {
520  QString msg = "Unable to set field to the given vector of float values. "
521  "Field [" + m_name + "] Type is not Real.";
522  throw IException(IException::Programmer, msg, _FILEINFO_);
523  }
524  else if ((int) values.size() != m_size) {
525  QString msg = "Unable to set field to the given vector of float values. "
526  "Field [" + m_name + "] values has size ["
527  + Isis::toString(m_size) + "].";
528  throw IException(IException::Programmer, msg, _FILEINFO_);
529  }
530  m_rvalues = values;
531  }
532 
544  void TableField::operator=(const void *ibuf) {
545  char *buf = (char *) ibuf;
546  if (m_type == TableField::Double) {
547  for (unsigned int i = 0; i < m_dvalues.size(); i++) {
548  // m_dvalues[i] = ((double *) buf)[i];
549  double bufDouble;
550  memmove(&bufDouble, buf + i * 8, 8);
551  m_dvalues[i] = bufDouble;
552  }
553  }
554  else if (m_type == TableField::Integer) {
555  for (unsigned int i = 0; i < m_ivalues.size(); i++) {
556  // m_ivalues[i] = ((int *) buf)[i];
557  int bufInt;
558  memmove(&bufInt, buf + i * 4, 4);
559  m_ivalues[i] = bufInt;
560  }
561  }
562  else if (m_type == TableField::Text) {
563  m_svalue.resize(bytes());
564  for (int i = 0; i < bytes(); i++) {
565  m_svalue[i] = buf[i];
566  }
567  }
568  else if (m_type == TableField::Real) {
569  for (unsigned int i = 0; i < m_rvalues.size(); i++) {
570  // m_ivalues[i] = ((int *) buf)[i];
571  float bufFloat;
572  memmove(&bufFloat, buf + i * 4, 4);
573  m_rvalues[i] = bufFloat;
574  }
575  }
576  else {
577  string msg = "Undefined field type [" + IString(m_type) + "].";
578  throw IException(IException::Programmer, msg, _FILEINFO_);
579  }
580  }
581 
591  void TableField::operator=(const char *buf) {
592  if (m_type != TableField::Text) {
593  QString msg = "Unable to set field to the given string value. Field [" + m_name + "] Type is not Text.";
594  throw IException(IException::Programmer, msg, _FILEINFO_);
595  }
596  m_svalue = buf;
597  }
598 
605  PvlGroup TableField::pvlGroup() {
606  PvlGroup group("Field");
607  group += PvlKeyword("Name", m_name);
608  if (m_type == TableField::Double) {
609  group += PvlKeyword("Type", "Double");
610  }
611  else if (m_type == TableField::Integer) {
612  group += PvlKeyword("Type", "Integer");
613  }
614  else if (m_type == TableField::Text) {
615  group += PvlKeyword("Type", "Text");
616  }
617  else if (m_type == TableField::Real) {
618  group += PvlKeyword("Type", "Real");
619  }
620  group += PvlKeyword("Size", Isis::toString(m_size));
621 
622  return group;
623  }
624 
625 
626  QString TableField::toString(const TableField &field, QString delimiter){
627  QString fieldValues = "";
628  if (field.size()== 1){
629  if (field.isText()){
630  fieldValues = (QString)field;
631  }
632  else if (field.isInteger()){
633  fieldValues = Isis::toString((int)field);
634  }
635  else if (field.isDouble()){
636  fieldValues = Isis::toString((double)field);
637  }
638  else { //real
639  fieldValues = Isis::toString((float)field);
640  }
641  }
642  // Otherwise, build a vector to contain the entries
643  else {
644  if (field.isText()){
645  fieldValues +=(QString)field;
646  }
647  else if (field.isInteger()){
648  vector< int > currField = field;
649  for (int i = 0;i <(int)currField.size();i++){
650  fieldValues += Isis::toString(currField[i]);
651  if (i <(int)currField.size()- 1){
652  // add delimiter for all but the last element of the field
653  fieldValues += delimiter;
654  }
655  }
656  }
657  else if (field.isDouble()){
658  vector< double > currField = field;
659  for (int i = 0;i <(int)currField.size();i++){
660  fieldValues += Isis::toString(currField[i]);
661  if (i <(int)currField.size()- 1){
662  fieldValues += delimiter;
663  }
664  }
665  }
666  else { //if (field.isReal()) {
667  vector< float > currField = field;
668  for (int i = 0;i <(int)currField.size();i++){
669  fieldValues += Isis::toString(currField[i]);
670  if (i <(int)currField.size()- 1){
671  fieldValues += delimiter;
672  }
673  }
674  }
675  }
676  return fieldValues;
677  }
678 } // end namespace isis
679 
Namespace for the standard library.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
bool isInteger() const
Determines whether the field type is Integer.
Definition: TableField.cpp:138
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A single keyword-value pair.
Definition: PvlKeyword.h:98
int size() const
Returns the number of values stored for the field at each record.
Definition: TableField.cpp:184
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool isText() const
Determines whether the field type is Text.
Definition: TableField.cpp:157
Class for storing an Isis::Table&#39;s field information.
Definition: TableField.h:63
bool isDouble() const
Determines whether the field type is Double.
Definition: TableField.cpp:148
Type
This enum describes the value type for the TableField.
Definition: TableField.h:68