1 // Copyright (C) 2010-2014 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "TableParser.h"
23 #include <QStringList>
32 int getLine(std::ifstream& streamIn, QString& str)
35 std::ostringstream streamOut;
37 while (streamIn.get(tmp)) {
44 str = streamOut.str().c_str();
46 return !streamIn.eof();
55 int iEnd = myRows[0].myValues.size();
61 if (myColumnTitles.size() != iEnd)
63 myColumnTitles.resize(iEnd);
66 if (myColumnUnits.size() != iEnd)
68 myColumnUnits.resize(iEnd);
71 int jEnd = myRows.size();
72 for (int j = 0; j < jEnd; j++)
74 if (myRows[j].myValues.size() != iEnd)
83 std::vector<std::string> GetTableNames(const char* fname, const char* separator,
84 const bool firstStringAsTitles)
87 std::vector<std::string> tableTitles;
90 table = GetTable(fname, separator, nb, firstStringAsTitles);
91 while (table.Check()) {
92 tableTitles.push_back(table.myTitle);
93 table = GetTable(fname, separator, ++nb, firstStringAsTitles);
99 Table2D GetTable(const char* fname, const char* separator, const int tableNb,
100 const bool firstStringAsTitles)
102 std::ifstream streamIn(fname);
106 throw std::runtime_error("Unable to open input Post-Pro table file.");
112 // Find beginning of table (tables are separated by empty lines)
113 while (getLine(streamIn, tmp) && tmp.trimmed() == "");
118 while (!streamIn.eof() && tmp.trimmed() != "")
120 QString data = tmp.trimmed();
122 QString keyword = "";
124 // Split string to data and comment (comment starts from '#' symbol)
125 int index = tmp.indexOf("#");
128 data = tmp.left(index).trimmed();
129 cmt = tmp.mid(index+1).trimmed();
132 // If comment is not empty, try to get keyword from it (separated by ':' symbol)
135 int index1 = cmt.indexOf(":");
139 QString tmpstr = cmt.left(index1).trimmed();
140 if (tmpstr == QString("TITLE") ||
141 tmpstr == QString("COLUMN_TITLES") ||
142 tmpstr == QString("COLUMN_UNITS") ||
143 tmpstr == QString("COMMENT"))
146 cmt = cmt.mid(index1+1).trimmed();
151 // If data is empty, process only comment
154 // If keyword is found, try to process it
155 // elsewise it is a simple comment, just ignore it
156 if (!keyword.isEmpty())
158 if (keyword == QString( "TITLE" ))
161 if (table2D.myTitle != "") {
162 title = QString(table2D.myTitle.c_str()) + QString(" ") + title;
164 table2D.myTitle = title.toLatin1().constData();
166 else if (keyword == QString("COLUMN_TITLES"))
168 // Comment may contain column headers
169 QStringList strList = cmt.split("|", QString::SkipEmptyParts);
171 for ( int i = 0; i < strList.count(); i++ )
173 QString tmpstr = strList[i].trimmed();
174 table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
177 else if (keyword == QString("COLUMN_UNITS"))
179 // Comment may contain column units
180 QStringList strList = cmt.split( " ", QString::SkipEmptyParts );
182 for (int i = 0; i < strList.count(); i++)
184 QString tmpstr = strList[i].trimmed();
185 table2D.myColumnUnits.push_back(tmpstr.toLatin1().constData());
188 else if (keyword == QString("COMMENT"))
190 // Keyword 'COMMENT' processing can be here,
191 // currently it is ignored
195 // Simple comment processing can be here,
196 // currently it is ignored
199 // If data is not empty, try to process it
203 QString datar1 = data.replace(QRegExp("\t"), " ");
204 QStringList valList = datar1.split(separator, QString::SkipEmptyParts);
205 if(table2D.myColumnTitles.size() == 0 && isFirst && firstStringAsTitles)
207 for ( int i = 0; i < valList.count(); i++ )
209 QString tmpstr = valList[i].trimmed();
210 table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
217 row.myTitle = cmt.toLatin1().constData();
220 for (int i = 0; i < valList.count(); i++)
222 if (valList[i].trimmed() != "")
224 Table2D::Value val = valList[i].trimmed().toLatin1().constData();
225 row.myValues.push_back(val);
229 if(row.myValues.size() > 0)
231 table2D.myRows.push_back(row);
237 getLine(streamIn, tmp);
243 if (count == tableNb)
245 if (QString::fromStdString(table2D.myTitle).isEmpty())
247 table2D.myTitle = QString("Table:%1").arg(tableNb).toStdString();
254 } while (!streamIn.eof());
258 // Return empty table