1 // Copyright (C) 2010-2023 CEA, EDF
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 streamOut << std::ends;
45 str = streamOut.str().c_str();
47 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(
84 const char* fname, const char* separator, const bool firstStringAsTitles)
87 std::vector<std::string> tableTitles;
90 table = GetTable(fname, separator, nb, firstStringAsTitles);
93 tableTitles.push_back(table.myTitle);
94 table = GetTable(fname, separator, ++nb, firstStringAsTitles);
101 const char* fname, const char* separator, const int tableNb, const bool firstStringAsTitles)
103 std::ifstream streamIn(fname);
105 if (!streamIn.good())
107 throw std::runtime_error("Unable to open input Post-Pro table file.");
114 // Find beginning of table (tables are separated by empty lines)
115 while (getLine(streamIn, tmp) && tmp.trimmed() == "")
121 while (!streamIn.eof() && tmp.trimmed() != "")
123 QString data = tmp.trimmed();
125 QString keyword = "";
127 // Split string to data and comment (comment starts from '#' symbol)
128 int index = tmp.indexOf("#");
131 data = tmp.left(index).trimmed();
132 cmt = tmp.mid(index + 1).trimmed();
135 // If comment is not empty, try to get keyword from it (separated by ':' symbol)
138 int index1 = cmt.indexOf(":");
142 QString tmpstr = cmt.left(index1).trimmed();
143 if (tmpstr == QString("TITLE") || tmpstr == QString("COLUMN_TITLES") ||
144 tmpstr == QString("COLUMN_UNITS") || tmpstr == QString("COMMENT"))
147 cmt = cmt.mid(index1 + 1).trimmed();
152 // If data is empty, process only comment
155 // If keyword is found, try to process it
156 // elsewise it is a simple comment, just ignore it
157 if (!keyword.isEmpty())
159 if (keyword == QString("TITLE"))
162 if (table2D.myTitle != "")
164 title = QString(table2D.myTitle.c_str()) + QString(" ") + title;
166 table2D.myTitle = title.toLatin1().constData();
168 else if (keyword == QString("COLUMN_TITLES"))
170 // Comment may contain column headers
171 QStringList strList = cmt.split("|", QString::SkipEmptyParts);
173 for (int i = 0; i < strList.count(); i++)
175 QString tmpstr = strList[i].trimmed();
176 table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
179 else if (keyword == QString("COLUMN_UNITS"))
181 // Comment may contain column units
182 QStringList strList = cmt.split(" ", QString::SkipEmptyParts);
184 for (int i = 0; i < strList.count(); i++)
186 QString tmpstr = strList[i].trimmed();
187 table2D.myColumnUnits.push_back(tmpstr.toLatin1().constData());
190 else if (keyword == QString("COMMENT"))
192 // Keyword 'COMMENT' processing can be here,
193 // currently it is ignored
198 // Simple comment processing can be here,
199 // currently it is ignored
202 // If data is not empty, try to process it
207 QString datar1 = data.replace(QRegExp("\t"), " ");
208 QStringList valList = datar1.split(separator, QString::SkipEmptyParts);
209 if (table2D.myColumnTitles.size() == 0 && isFirst && firstStringAsTitles)
211 for (int i = 0; i < valList.count(); i++)
213 QString tmpstr = valList[i].trimmed();
214 table2D.myColumnTitles.push_back(tmpstr.toLatin1().constData());
221 row.myTitle = cmt.toLatin1().constData();
224 for (int i = 0; i < valList.count(); i++)
226 if (valList[i].trimmed() != "")
228 Table2D::Value val = valList[i].trimmed().toLatin1().constData();
229 row.myValues.push_back(val);
233 if (row.myValues.size() > 0)
235 table2D.myRows.push_back(row);
241 getLine(streamIn, tmp);
246 if (count == tableNb)
248 if (QString::fromStdString(table2D.myTitle).isEmpty())
250 table2D.myTitle = QString("Table:%1").arg(tableNb).toStdString();
257 } while (!streamIn.eof());
261 // Return empty table