Back to index

salome-paravis  6.5.0
TableParser.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "TableParser.h"
00021 
00022 #include <QString>
00023 #include <QStringList>
00024 
00025 // STL includes
00026 #include <fstream>
00027 #include <sstream>
00028 #include <stdexcept>
00029 
00030 using namespace std;
00031 
00032 int getLine(std::ifstream& streamIn, QString& str)
00033 {
00034   char tmp;
00035   std::ostringstream streamOut;
00036   
00037   while (streamIn.get(tmp)) {
00038     streamOut<<tmp;
00039     if (tmp == '\n') 
00040       break;
00041   }
00042   
00043   streamOut<<std::ends;
00044   str = streamOut.str().c_str();
00045   
00046   return !streamIn.eof();
00047 }
00048 
00049 
00050 bool Table2D::Check()
00051 {
00052   if (myRows.empty()) 
00053     return false;
00054 
00055   int iEnd = myRows[0].myValues.size();
00056   if (iEnd == 0)
00057     {
00058       return false;
00059     }
00060 
00061   if (myColumnTitles.size() != iEnd) 
00062     {
00063       myColumnTitles.resize(iEnd);
00064     }
00065 
00066   if (myColumnUnits.size() != iEnd)
00067     {
00068       myColumnUnits.resize(iEnd);
00069     }
00070 
00071   int jEnd = myRows.size();
00072   for (int j = 0; j < jEnd; j++)
00073     {
00074       if (myRows[j].myValues.size() != iEnd)
00075        {
00076          return false;
00077        }
00078     }
00079   
00080   return true;
00081 }
00082 
00083 std::vector<std::string> GetTableNames(const char* fname, const char* separator,
00084                                    const bool firstStringAsTitles)
00085 {
00086   Table2D table;
00087   std::vector<std::string> tableTitles;
00088 
00089   int nb = 0;
00090   table = GetTable(fname, separator, nb, firstStringAsTitles);
00091   while (table.Check()) {
00092     tableTitles.push_back(table.myTitle);
00093     table = GetTable(fname, separator, ++nb, firstStringAsTitles);
00094   }
00095   
00096   return tableTitles;
00097 }
00098 
00099 Table2D GetTable(const char* fname, const char* separator, const int tableNb,
00100                const bool firstStringAsTitles)
00101 {
00102   std::ifstream streamIn(fname);
00103   
00104   if(!streamIn.good())
00105     {
00106       throw std::runtime_error("Unable to open input Post-Pro table file.");
00107     }
00108   
00109   QString tmp;
00110   int count = 0;
00111   do {
00112     // Find beginning of table (tables are separated by empty lines)
00113     while (getLine(streamIn, tmp) && tmp.trimmed() == "");
00114 
00115     Table2D table2D;
00116 
00117     bool isFirst = true;
00118     while (!streamIn.eof() && tmp.trimmed() != "") 
00119       {
00120        QString data = tmp.trimmed();
00121        QString cmt = "";
00122        QString keyword = "";
00123 
00124        // Split string to data and comment (comment starts from '#' symbol)
00125        int index = tmp.indexOf("#");
00126        if (index >= 0) 
00127          {
00128            data = tmp.left(index).trimmed();
00129            cmt = tmp.mid(index+1).trimmed();
00130          }
00131 
00132        // If comment is not empty, try to get keyword from it (separated by ':' symbol)
00133        if (!cmt.isEmpty()) 
00134          {
00135            int index1 = cmt.indexOf(":");
00136 
00137            if (index1 >= 0) 
00138              {
00139               QString tmpstr = cmt.left(index1).trimmed();
00140               if (tmpstr == QString("TITLE") ||
00141                   tmpstr == QString("COLUMN_TITLES") ||
00142                   tmpstr == QString("COLUMN_UNITS") ||
00143                   tmpstr == QString("COMMENT")) 
00144                 {
00145                   keyword = tmpstr;
00146                   cmt = cmt.mid(index1+1).trimmed();
00147                 }
00148              }
00149          }
00150        
00151        // If data is empty, process only comment
00152        if (data.isEmpty()) 
00153          {
00154            // If keyword is found, try to process it
00155            // elsewise it is a simple comment, just ignore it
00156            if (!keyword.isEmpty()) 
00157              {
00158               if (keyword == QString( "TITLE" )) 
00159                 {
00160                   QString title = cmt;
00161                   if (table2D.myTitle != "") {
00162                     title = QString(table2D.myTitle.c_str()) + QString(" ") + title;
00163                   }
00164                   table2D.myTitle = title.toLatin1().constData();
00165                 }
00166               else if (keyword == QString("COLUMN_TITLES")) 
00167                 {
00168                   // Comment may contain column headers
00169                   QStringList strList = cmt.split("|", QString::SkipEmptyParts);
00170 
00171                   for ( int i = 0; i < strList.count(); i++ ) 
00172                     {
00173                      QString tmpstr = strList[i].trimmed();
00174                      table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
00175                     }
00176                 }
00177               else if (keyword == QString("COLUMN_UNITS")) 
00178                 {
00179                   // Comment may contain column units
00180                   QStringList strList = cmt.split( " ", QString::SkipEmptyParts );
00181            
00182                   for (int i = 0; i < strList.count(); i++) 
00183                     {
00184                      QString tmpstr = strList[i].trimmed();
00185                      table2D.myColumnUnits.push_back(tmpstr.toLatin1().constData());
00186                     }
00187                 }
00188               else if (keyword == QString("COMMENT")) 
00189                 {
00190                   // Keyword 'COMMENT' processing can be here,
00191                   // currently it is ignored
00192                 }
00193              }
00194            else {
00195              // Simple comment processing can be here,
00196              // currently it is ignored
00197            }
00198          }
00199        // If data is not empty, try to process it
00200        else {
00201          Table2D::Row row;
00202        
00203          QString datar1 = data.replace(QRegExp("\t"), " ");
00204          QStringList valList = datar1.split(separator, QString::SkipEmptyParts);
00205          if(table2D.myColumnTitles.size() == 0 && isFirst && firstStringAsTitles) 
00206            {
00207              for ( int i = 0; i < valList.count(); i++ ) 
00208               {
00209                 QString tmpstr = valList[i].trimmed();
00210                 table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
00211               }
00212            }
00213          else 
00214            {
00215              if (!cmt.isEmpty()) 
00216               {
00217                 row.myTitle = cmt.toLatin1().constData();
00218               }
00219           
00220              for (int i = 0; i < valList.count(); i++) 
00221               {
00222                 if (valList[i].trimmed() != "") 
00223                   {
00224                     Table2D::Value val = valList[i].trimmed().toLatin1().constData();
00225                     row.myValues.push_back(val);
00226                   }
00227               }
00228          
00229              if(row.myValues.size() > 0)
00230               {
00231                 table2D.myRows.push_back(row);
00232               }
00233            }
00234 
00235          isFirst = false;
00236        }
00237        getLine(streamIn, tmp);
00238       }
00239     
00240     
00241     if(table2D.Check()) 
00242       {
00243        if (count == tableNb) 
00244          {
00245            if (QString::fromStdString(table2D.myTitle).isEmpty()) 
00246              {
00247               table2D.myTitle = QString("Table:%1").arg(tableNb).toStdString();
00248              }
00249            return table2D;
00250          }
00251        count++;
00252       }
00253     
00254   } while (!streamIn.eof());
00255   
00256   streamIn.close();
00257   
00258   // Return empty table
00259   Table2D emptyTable;
00260   return emptyTable;
00261 }