"Fossies" - the Fresh Open Source Software Archive

Member "labplot-2.8.2/src/backend/cantorWorksheet/VariableParser.cpp" (24 Feb 2021, 5674 Bytes) of package /linux/privat/labplot-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "VariableParser.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8.1_vs_2.8.2.

    1 /***************************************************************************
    2     File                 : VariableParser.h
    3     Project              : LabPlot
    4     Description          : Variable parser for different CAS backends
    5     --------------------------------------------------------------------
    6     Copyright            : (C) 2015 Garvit Khatri (garvitdelhi@gmail.com)
    7     Copyright            : (C) 2016 by Alexander Semke (alexander.semke@web.de)
    8 
    9  ***************************************************************************/
   10 
   11 /***************************************************************************
   12  *                                                                         *
   13  *  This program is free software; you can redistribute it and/or modify   *
   14  *  it under the terms of the GNU General Public License as published by   *
   15  *  the Free Software Foundation; either version 2 of the License, or      *
   16  *  (at your option) any later version.                                    *
   17  *                                                                         *
   18  *  This program is distributed in the hope that it will be useful,        *
   19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
   20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
   21  *  GNU General Public License for more details.                           *
   22  *                                                                         *
   23  *   You should have received a copy of the GNU General Public License     *
   24  *   along with this program; if not, write to the Free Software           *
   25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
   26  *   Boston, MA  02110-1301  USA                                           *
   27  *                                                                         *
   28  ***************************************************************************/
   29 
   30 #include "VariableParser.h"
   31 #include <QStringList>
   32 #include "backend/lib/trace.h"
   33 #include <cmath>    // NAN
   34 
   35 VariableParser::VariableParser(QString name, QString value)
   36     : m_backendName(std::move(name)), m_string(std::move(value)) {
   37 
   38     PERFTRACE("parsing variable");
   39     if (m_backendName.compare(QStringLiteral("Maxima"), Qt::CaseInsensitive) == 0)
   40         parseMaximaValues();
   41     else if ( (m_backendName.compare(QStringLiteral("Python"), Qt::CaseInsensitive) == 0)
   42             || (m_backendName.compare(QStringLiteral("Python 3"), Qt::CaseInsensitive) == 0)
   43             || (m_backendName.compare(QStringLiteral("Python 2"), Qt::CaseInsensitive) == 0) )
   44         parsePythonValues();
   45     else if (m_backendName.compare(QStringLiteral("Sage"), Qt::CaseInsensitive) == 0)
   46         parsePythonValues();
   47     else if (m_backendName.compare(QStringLiteral("R"), Qt::CaseInsensitive) == 0)
   48         parseRValues();
   49     else if (m_backendName.compare(QStringLiteral("Julia"), Qt::CaseInsensitive) == 0)
   50         parsePythonValues();
   51     else if (m_backendName.compare(QStringLiteral("Octave"), Qt::CaseInsensitive) == 0)
   52         parseOctaveValues();
   53 }
   54 
   55 void VariableParser::parseMaximaValues() {
   56     if (m_string.count(QStringLiteral("[")) < 2) {
   57         m_string = m_string.replace(QStringLiteral("["), QString());
   58         m_string = m_string.replace(QStringLiteral("]"), QString());
   59         m_string = m_string.trimmed();
   60 
   61         const QStringList valueStringList = m_string.split(QStringLiteral(","));
   62         parseValues(valueStringList);
   63     }
   64 }
   65 
   66 void VariableParser::parsePythonValues() {
   67     QStringList valueStringList;
   68     m_string = m_string.trimmed();
   69     if (m_string.startsWith(QStringLiteral("array"))) {
   70         //parse numpy arrays, string representation like array([1,2,3,4,5])
   71         m_string = m_string.replace(QStringLiteral("array(["), QString());
   72         m_string = m_string.replace(QStringLiteral("])"), QString());
   73     } else if (m_string.startsWith(QStringLiteral("["))) {
   74         //parse python's lists
   75         m_string = m_string.replace(QStringLiteral("["), QString());
   76         m_string = m_string.replace(QStringLiteral("]"), QString());
   77     } else if (m_string.startsWith(QStringLiteral("("))) {
   78         //parse python's tuples
   79         m_string = m_string.replace(QStringLiteral("("), QString());
   80         m_string = m_string.replace(QStringLiteral(")"), QString());
   81     } else {
   82         return;
   83     }
   84 
   85     if (m_string.count(QStringLiteral(","))>1)
   86         valueStringList = m_string.split(QStringLiteral(","));
   87     else
   88         valueStringList = m_string.split(QStringLiteral(" "));
   89 
   90     parseValues(valueStringList);
   91 }
   92 
   93 void VariableParser::parseRValues() {
   94     m_string = m_string.trimmed();
   95     const QStringList valueStringList = m_string.split(QStringLiteral(", "));
   96     parseValues(valueStringList);
   97 }
   98 
   99 void VariableParser::parseOctaveValues() {
  100     m_string = m_string.trimmed();
  101 
  102     QStringList valueStringList;
  103     const QStringList tempStringList = m_string.split(QLatin1Char('\n'));
  104     for (const QString& values : tempStringList) {
  105         //TODO: in newer version of Cantor the rows with "Columns..." were removed already.
  106         //we can stop looking for this substring in some point in time later.
  107         if (!values.isEmpty() && !values.trimmed().startsWith(QStringLiteral("Columns")))
  108             valueStringList << values.split(QLatin1Char(' '));
  109     }
  110 
  111     valueStringList.removeAll(QString());
  112     parseValues(valueStringList);
  113 }
  114 
  115 bool VariableParser::isParsed() {
  116     return m_parsed;
  117 }
  118 
  119 QVector< double > VariableParser::values() {
  120     return m_values;
  121 }
  122 
  123 void VariableParser::parseValues(const QStringList& values) {
  124     PERFTRACE("parsing variable values string list");
  125     for (const QString& v : values) {
  126         bool isNumber = false;
  127         double value = v.trimmed().toDouble(&isNumber);
  128 
  129         //accept the variable only if there is at least one numerical value in the array.
  130         if (isNumber) {
  131             if (!m_parsed)
  132                 m_parsed = true;
  133         } else {
  134             value = NAN;
  135         }
  136 
  137         m_values << value;
  138     }
  139 }