14 #include "IException.h" 
   15 #include "TableField.h" 
   20   Table::Table(Blob &blob) {
 
   45     p_assoc = Table::None;
 
   48     for (
int f = 0; f < rec.
Fields(); f++) {
 
   49       p_label.addGroup(rec[f].pvlGroup());
 
   67   Table::Table(
const QString &tableName) {
 
   69     p_assoc = Table::None;
 
   87   Table::Table(
const QString &tableName, 
const QString &file) {
 
   88     Blob blob(tableName, 
"Table", file);
 
  108   Table::Table(
const QString &tableName, 
const QString &file, 
const Pvl &fileHeader) {
 
  109     Blob blob(tableName, 
"Table");
 
  110     blob.
Read(file, fileHeader);
 
  129     for (
unsigned int i = 0; i < other.
p_recbufs.size(); i++) {
 
  130       char *data = 
new char[RecordSize()];
 
  132       for (
int j = 0; j < RecordSize(); j++) {
 
  136       p_recbufs.push_back(data);
 
  146   void Table::initFromBlob(
Blob &blob) {
 
  149     p_label = blob.
Label();
 
  151     p_name = p_label[
"Name"][0];
 
  152     p_records = p_label[
"Records"];
 
  155     for (
int g = 0; g < p_label.groups(); g++) {
 
  156       if (p_label.group(g).isNamed(
"Field")) {
 
  164     p_assoc = Table::None;
 
  165     if (p_label.hasKeyword(
"Association")) {
 
  166       QString temp = (QString) p_label[
"Association"];
 
  167       temp = temp.toUpper();
 
  168       if (temp == 
"SAMPLES") p_assoc = Table::Samples;
 
  169       if (temp == 
"LINES") p_assoc = Table::Lines;
 
  170       if (temp == 
"BANDS") p_assoc = Table::Bands;
 
  176     if (
Isis::IsLsb() && (bo == Isis::Msb)) p_swap = 
true;
 
  177     if (
Isis::IsMsb() && (bo == Isis::Lsb)) p_swap = 
true;
 
  179     for (
int rec = 0; rec < p_records; rec++) {
 
  180       size_t bufferPos = rec * RecordSize();
 
  182       char *buf = 
new char[RecordSize()];
 
  183       memcpy(buf, &blob.
getBuffer()[bufferPos], RecordSize());
 
  185       if (p_swap) p_record.Swap(buf);
 
  186       p_recbufs.push_back(buf);
 
  209     for (
unsigned int i = 0; i < other.
p_recbufs.size(); i++) {
 
  210       char *data = 
new char[RecordSize()];
 
  212       for (
int j = 0; j < RecordSize(); j++) {
 
  216       p_recbufs.push_back(data);
 
  236   void Table::Write(
const QString &file) {
 
  237     Blob blob = toBlob();
 
  247   QString Table::Name()
 const {
 
  270   void Table::SetAssociation(
const Table::Association assoc) {
 
  281   bool Table::IsSampleAssociated() {
 
  282     return (p_assoc == Table::Samples);
 
  292   bool Table::IsLineAssociated() {
 
  293     return (p_assoc == Table::Lines);
 
  303   bool Table::IsBandAssociated() {
 
  304     return (p_assoc == Table::Bands);
 
  313   int Table::Records()
 const {
 
  314     return p_recbufs.size();
 
  323   int Table::RecordFields()
 const {
 
  324     return p_record.Fields();
 
  333   int Table::RecordSize()
 const {
 
  334     return p_record.RecordSize();
 
  346     p_record.
Unpack(p_recbufs[index]);
 
  357     if (RecordSize() == 0) {
 
  358       IString msg = 
"Unable to add records to Isis Table [" 
  359                     + p_name + 
"]. Bytes per record = [0 bytes].";
 
  360       throw IException(IException::Unknown, msg, _FILEINFO_);
 
  364        QString msg = 
"Unable to add the given record with size = [" 
  366                      + p_name + 
"] with record size = [" 
  367                      + 
Isis::toString(RecordSize()) + 
" bytes]. Record sizes must match.";
 
  368        throw IException(IException::Unknown, msg, _FILEINFO_);
 
  370     char *newbuf = 
new char[RecordSize()];
 
  372     p_recbufs.push_back(newbuf);
 
  383     rec.
Pack(p_recbufs[index]);
 
  392   void Table::Delete(
const int index) {
 
  393     vector<char *>::iterator it = p_recbufs.begin();
 
  394     for (
int i = 0; i < index; i++, it++);
 
  395     delete [] p_recbufs[index];
 
  403   void Table::Clear() {
 
  404     for (
int i = 0; i < (int)p_recbufs.size(); i++) 
delete [] p_recbufs[i];
 
  415     Blob tableBlob(Name(), 
"Table");
 
  420     int nbytes = Records() * RecordSize();
 
  423       blobLabel+= 
PvlKeyword(
"ByteOrder", Isis::ByteOrderName(Isis::Lsb));
 
  426       blobLabel+= 
PvlKeyword(
"ByteOrder", Isis::ByteOrderName(Isis::Msb));
 
  429     if (p_assoc == Samples) {
 
  432     else if (p_assoc == Lines) {
 
  435     else if (p_assoc == Bands) {
 
  439     for (
int i = 0; i < p_label.keywords(); i++) {
 
  440       if (!blobLabel.
hasKeyword(p_label[i].name())) {
 
  441         blobLabel += p_label[i];
 
  445     for (
int i = 0; i < p_label.comments(); i++){
 
  446       blobLabel.addComment(p_label.comment(i));
 
  449     for (
int g = 0; g < p_label.groups(); g++) {
 
  450       blobLabel += p_label.
group(g);
 
  454     char *buf = 
new char[nbytes];
 
  456     for (
int rec = 0; rec < Records(); rec++) {
 
  457       size_t bufferPos = rec * RecordSize();
 
  459       memcpy(&buf[bufferPos], p_recbufs[rec], RecordSize());
 
  480   QString Table::toString(
Table table, QString fieldDelimiter) {
 
  483     tableValues += TableRecord::toString(table[0], fieldDelimiter, 
true, 
true);
 
  485     for (
int recordIndex = 1; recordIndex < table.
Records(); recordIndex++) {
 
  486       tableValues += TableRecord::toString(table[recordIndex], fieldDelimiter);