Back to index

salome-paravis  6.5.0
Classes | Functions
TableParser.h File Reference
#include <string>
#include <vector>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Table2D
struct  Table2D::Row

Functions

std::vector< std::string > GetTableNames (const char *fname, const char *separator, const bool firstStringAsTitles)
Table2D GetTable (const char *fname, const char *separator, const int tableNb, const bool firstStringAsTitles)

Class Documentation

struct Table2D::Row

Definition at line 31 of file TableParser.h.

Collaboration diagram for Table2D::Row:
Class Members
string myTitle
string myUnit
Values myValues

Function Documentation

Table2D GetTable ( const char *  fname,
const char *  separator,
const int  tableNb,
const bool  firstStringAsTitles 
)

Definition at line 99 of file TableParser.cxx.

{
  std::ifstream streamIn(fname);
  
  if(!streamIn.good())
    {
      throw std::runtime_error("Unable to open input Post-Pro table file.");
    }
  
  QString tmp;
  int count = 0;
  do {
    // Find beginning of table (tables are separated by empty lines)
    while (getLine(streamIn, tmp) && tmp.trimmed() == "");

    Table2D table2D;

    bool isFirst = true;
    while (!streamIn.eof() && tmp.trimmed() != "") 
      {
       QString data = tmp.trimmed();
       QString cmt = "";
       QString keyword = "";

       // Split string to data and comment (comment starts from '#' symbol)
       int index = tmp.indexOf("#");
       if (index >= 0) 
         {
           data = tmp.left(index).trimmed();
           cmt = tmp.mid(index+1).trimmed();
         }

       // If comment is not empty, try to get keyword from it (separated by ':' symbol)
       if (!cmt.isEmpty()) 
         {
           int index1 = cmt.indexOf(":");

           if (index1 >= 0) 
             {
              QString tmpstr = cmt.left(index1).trimmed();
              if (tmpstr == QString("TITLE") ||
                  tmpstr == QString("COLUMN_TITLES") ||
                  tmpstr == QString("COLUMN_UNITS") ||
                  tmpstr == QString("COMMENT")) 
                {
                  keyword = tmpstr;
                  cmt = cmt.mid(index1+1).trimmed();
                }
             }
         }
       
       // If data is empty, process only comment
       if (data.isEmpty()) 
         {
           // If keyword is found, try to process it
           // elsewise it is a simple comment, just ignore it
           if (!keyword.isEmpty()) 
             {
              if (keyword == QString( "TITLE" )) 
                {
                  QString title = cmt;
                  if (table2D.myTitle != "") {
                    title = QString(table2D.myTitle.c_str()) + QString(" ") + title;
                  }
                  table2D.myTitle = title.toLatin1().constData();
                }
              else if (keyword == QString("COLUMN_TITLES")) 
                {
                  // Comment may contain column headers
                  QStringList strList = cmt.split("|", QString::SkipEmptyParts);

                  for ( int i = 0; i < strList.count(); i++ ) 
                    {
                     QString tmpstr = strList[i].trimmed();
                     table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
                    }
                }
              else if (keyword == QString("COLUMN_UNITS")) 
                {
                  // Comment may contain column units
                  QStringList strList = cmt.split( " ", QString::SkipEmptyParts );
           
                  for (int i = 0; i < strList.count(); i++) 
                    {
                     QString tmpstr = strList[i].trimmed();
                     table2D.myColumnUnits.push_back(tmpstr.toLatin1().constData());
                    }
                }
              else if (keyword == QString("COMMENT")) 
                {
                  // Keyword 'COMMENT' processing can be here,
                  // currently it is ignored
                }
             }
           else {
             // Simple comment processing can be here,
             // currently it is ignored
           }
         }
       // If data is not empty, try to process it
       else {
         Table2D::Row row;
       
         QString datar1 = data.replace(QRegExp("\t"), " ");
         QStringList valList = datar1.split(separator, QString::SkipEmptyParts);
         if(table2D.myColumnTitles.size() == 0 && isFirst && firstStringAsTitles) 
           {
             for ( int i = 0; i < valList.count(); i++ ) 
              {
                QString tmpstr = valList[i].trimmed();
                table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
              }
           }
         else 
           {
             if (!cmt.isEmpty()) 
              {
                row.myTitle = cmt.toLatin1().constData();
              }
          
             for (int i = 0; i < valList.count(); i++) 
              {
                if (valList[i].trimmed() != "") 
                  {
                    Table2D::Value val = valList[i].trimmed().toLatin1().constData();
                    row.myValues.push_back(val);
                  }
              }
         
             if(row.myValues.size() > 0)
              {
                table2D.myRows.push_back(row);
              }
           }

         isFirst = false;
       }
       getLine(streamIn, tmp);
      }
    
    
    if(table2D.Check()) 
      {
       if (count == tableNb) 
         {
           if (QString::fromStdString(table2D.myTitle).isEmpty()) 
             {
              table2D.myTitle = QString("Table:%1").arg(tableNb).toStdString();
             }
           return table2D;
         }
       count++;
      }
    
  } while (!streamIn.eof());
  
  streamIn.close();
  
  // Return empty table
  Table2D emptyTable;
  return emptyTable;
}

Here is the call graph for this function:

Here is the caller graph for this function:

std::vector<std::string> GetTableNames ( const char *  fname,
const char *  separator,
const bool  firstStringAsTitles 
)

Definition at line 83 of file TableParser.cxx.

{
  Table2D table;
  std::vector<std::string> tableTitles;

  int nb = 0;
  table = GetTable(fname, separator, nb, firstStringAsTitles);
  while (table.Check()) {
    tableTitles.push_back(table.myTitle);
    table = GetTable(fname, separator, ++nb, firstStringAsTitles);
  }
  
  return tableTitles;
}

Here is the call graph for this function: