USGS

Isis 3.0 Application Source Code Reference

Home

tabledump.cpp

Go to the documentation of this file.
00001 #define GUIHELPERS
00002 
00003 #include "Isis.h"
00004 
00005 #include <sstream>
00006 
00007 #include "FileName.h"
00008 #include "IString.h"
00009 #include "Table.h"
00010 
00011 using namespace Isis;
00012 using namespace std;
00013 
00014 int g_pos = 0;
00015 QString g_previousFile = "";
00016 
00017 void helperButtonGetTableList();
00018 
00019 map <QString, void *> GuiHelpers() {
00020   map <QString, void *> helper;
00021   helper ["helperButtonGetTableList"] = (void *) helperButtonGetTableList;
00022   return helper;
00023 }
00024 
00025 void IsisMain() {
00026   // Gather parameters from the UserInterface
00027   UserInterface &ui = Application::GetUserInterface();
00028   FileName file = ui.GetFileName("FROM");
00029   QString tableName = ui.GetString("NAME");
00030   Table table(tableName, file.expanded());
00031 
00032   // Set the character to separate the entries
00033   QString delimit;
00034   if (ui.GetString("DELIMIT") == "COMMA") {
00035     delimit = ",";
00036   }
00037   else if (ui.GetString("DELIMIT") == "SPACE") {
00038     delimit = " ";
00039   }
00040   else {
00041     delimit = ui.GetString("CUSTOM");
00042   }
00043 
00044   // Open the file and output the column headings
00045   stringstream ss(stringstream::in | stringstream::out);
00046 
00047   for (int i = 0; i < table[0].Fields(); i++) {
00048     for (int j = 0; j < table[0][i].size(); j++) {
00049       QString title = table[0][i].name();
00050       if (table[0][i].isText()) {
00051         j += table[0][i].bytes();
00052       }
00053       else if (table[0][i].size() > 1) {
00054         title += "(" + toString(j) + ")";
00055       }
00056       if (i == table[0].Fields() - 1 && j == table[0][i].size() - 1) {
00057         // We've reached the last field, omit the delimiter
00058         ss << title;
00059       }
00060       else {
00061         ss << title + delimit;
00062       }
00063     }
00064   }
00065 
00066   // Loop through for each record
00067   for (int i = 0; i < table.Records(); i++) {
00068     ss.put('\n');
00069 
00070     // Loop through each Field in the record
00071     for (int j = 0; j < table[i].Fields(); j++) {
00072       // if there is only one entry in this field,
00073       // cast and output accordingly
00074       if (table[i][j].size() == 1) {
00075         if (table[i][j].isInteger()) {
00076           ss << IString((int)table[i][j]);
00077         }
00078         else if (table[i][j].isDouble()) {
00079           ss << IString((double)table[i][j]);
00080         }
00081         else if (table[i][j].isText()) {
00082           ss << (QString)table[i][j];
00083         }
00084         if (j < table[i].Fields() - 1) {
00085           ss << delimit;
00086         }
00087       }
00088       // Otherwise, build a vector to contain the entries,
00089       // and output them with the delimiter character between
00090       else {
00091         if (table[i][j].isText()) {
00092           ss << (QString)table[i][j] << delimit;
00093         }
00094         else if (table[i][j].isInteger()) {
00095           vector<int> currField = table[i][j];
00096           for (int k = 0; k < (int)currField.size(); k++) {
00097             // check to see that we aren't on either the last field, or
00098             // (if we are), we aren't on the last element of the field
00099             if (j < table[i].Fields() - 1 ||
00100                 k < (int)currField.size() - 1) {
00101               ss << currField[k] << delimit;
00102             }
00103             else {
00104               ss << currField[k];
00105             }
00106           }
00107         }
00108         else if (table[i][j].isDouble()) {
00109           vector<double> currField = table[i][j];
00110           for (int k = 0; k < (int)currField.size(); k++) {
00111             // check to see that we aren't on either the last field, or
00112             // (if we are), we aren't on the last element of the field
00113             if (j < table[i].Fields() - 1 ||
00114                 k < (int)currField.size() - 1) {
00115               ss << currField[k] << delimit;
00116             }
00117             else {
00118               ss << currField[k];
00119             }
00120           }
00121         }
00122       }
00123     } // End Field loop
00124   } // End Record loop
00125   ss.put('\n');
00126 
00127 
00128   if (ui.WasEntered("TO")) {
00129     QString outfile(FileName(ui.GetFileName("TO")).expanded());
00130     ofstream outFile(outfile.toAscii().data());
00131     outFile << ss.str();
00132     outFile.close();
00133   }
00134   else if (ui.IsInteractive()) {
00135     QString log = ss.str().c_str();
00136     Application::GuiLog(log);
00137   }
00138   else {
00139     cout << ss.str();
00140   }
00141 }
00142 
00143 // Function to find the available table names and put them into the GUI
00144 void helperButtonGetTableList() {
00145   QString list;
00146   bool match = false;
00147 
00148   UserInterface &ui = Application::GetUserInterface();
00149   QString currentFile = ui.GetFileName("FROM");
00150   const Pvl label(FileName(currentFile).expanded());
00151 
00152   // Check to see if the "FILE" parameter has changed since last press
00153   if (currentFile != g_previousFile) {
00154     ui.Clear("NAME");
00155     g_pos = 0;
00156     g_previousFile = currentFile;
00157   }
00158 
00159   // Look for tables
00160   int cnt = 0;
00161   while (!match) {
00162     // If we've gone through all objects and found nothing, throw an exception
00163     if (cnt >= label.Objects()) {
00164       g_pos = 0;
00165       QString msg = "Parameter [FROM] has no tables.";
00166       throw IException(IException::User, msg, _FILEINFO_);
00167     }
00168     // When the end of the objects is hit, display "NAME" parameter as blank
00169     if (g_pos >= label.Objects()) {
00170       list = "";
00171       match = true;
00172       g_pos = 0;  // Prepare to start over again
00173     }
00174     // When we find a table, fetch its name to stick in the "NAME" parameter
00175     else if (label.Object(g_pos).Name() == "Table") {
00176       list = label.Object(g_pos)["Name"][0];
00177       match = true;
00178       g_pos++;
00179     }
00180     // If all else fails, keep looking for tables
00181     else {
00182       g_pos++;
00183       cnt++;
00184     }
00185   }
00186 
00187   ui.Clear("NAME");
00188   ui.PutString("NAME", list);
00189 }