File indexing completed on 2024-04-21 14:47:34

0001 /***************************************************************************
0002     File                 : OriginParser.cpp
0003     --------------------------------------------------------------------
0004     Copyright            : (C) 2008 Alex Kargovsky (kargovsky@yumr.phys.msu.su)
0005     Copyright            : (C) 2017 Stefan Gerlach (stefan.gerlach@uni.kn)
0006     Description          : Origin file parser base class
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "OriginParser.h"
0030 #include <cctype>
0031 #include <locale>
0032 
0033 using namespace Origin;
0034 
0035 bool OriginParser::iequals(const string& s1, const string& s2, const std::locale& loc) const {
0036     bool equal = s1.size() == s2.size();
0037     for (unsigned int n = 0; n < s1.size() && equal; ++n) {
0038         if (std::toupper(s1[n], loc) != std::toupper(s2[n], loc) )
0039             equal = false;
0040     }
0041     return equal;
0042 }
0043 
0044 vector<Origin::SpreadSheet>::difference_type OriginParser::findSpreadByName(const string& name) const
0045 {
0046     for (vector<SpreadSheet>::const_iterator it = spreadSheets.begin(); it != spreadSheets.end(); ++it)
0047     {
0048         if (iequals(it->name, name, locale())) return it - spreadSheets.begin();
0049     }
0050     return -1;
0051 }
0052 
0053 vector<Origin::Excel>::difference_type OriginParser::findExcelByName(const string& name) const
0054 {
0055     for (vector<Excel>::const_iterator it = excels.begin(); it != excels.end(); ++it)
0056     {
0057         if (iequals(it->name, name, locale())) return it - excels.begin();
0058     }
0059     return -1;
0060 }
0061 
0062 vector<Origin::SpreadColumn>::difference_type OriginParser::findSpreadColumnByName(vector<Origin::SpreadSheet>::size_type spread, const string& name) const
0063 {
0064     for (vector<SpreadColumn>::const_iterator it = spreadSheets[spread].columns.begin(); it != spreadSheets[spread].columns.end(); ++it)
0065     {
0066         if (it->name == name) return it - spreadSheets[spread].columns.begin();
0067     }
0068     return -1;
0069 }
0070 
0071 vector<Origin::SpreadColumn>::difference_type OriginParser::findExcelColumnByName(vector<Origin::Excel>::size_type excel, vector<Origin::SpreadSheet>::size_type sheet, const string& name) const
0072 {
0073     for (vector<SpreadColumn>::const_iterator it = excels[excel].sheets[sheet].columns.begin();     it != excels[excel].sheets[sheet].columns.end(); ++it)
0074     {
0075         if (it->name == name) return it - excels[excel].sheets[sheet].columns.begin();
0076     }
0077     return -1;
0078 }
0079 
0080 vector<Origin::Matrix>::difference_type OriginParser::findMatrixByName(const string& name) const
0081 {
0082     for (vector<Matrix>::const_iterator it = matrixes.begin(); it != matrixes.end(); ++it)
0083     {
0084         if (iequals(it->name, name, locale())) return it - matrixes.begin();
0085     }
0086     return -1;
0087 }
0088 
0089 vector<Origin::Function>::difference_type OriginParser::findFunctionByName(const string& name) const
0090 {
0091     for (vector<Function>::const_iterator it = functions.begin(); it != functions.end(); ++it)
0092     {
0093         if (iequals(it->name, name, locale())) return it - functions.begin();
0094     }
0095     return -1;
0096 }
0097 
0098 pair<string, string> OriginParser::findDataByIndex(unsigned int index) const
0099 {
0100     for(vector<SpreadSheet>::const_iterator it = spreadSheets.begin(); it != spreadSheets.end(); ++it)
0101     {
0102         for(vector<SpreadColumn>::const_iterator it1 = it->columns.begin(); it1 != it->columns.end(); ++it1)
0103         {
0104             if(it1->index == index)
0105                 return make_pair("T_" + it->name, it1->name);
0106         }
0107     }
0108 
0109     for(vector<Matrix>::const_iterator it = matrixes.begin(); it != matrixes.end(); ++it)
0110     {
0111         for(vector<MatrixSheet>::const_iterator it1 = it->sheets.begin(); it1 != it->sheets.end(); ++it1)
0112         {
0113             if(it1->index == index)
0114                 return make_pair("M_" + it->name, it1->name);
0115         }
0116 
0117     }
0118 
0119 
0120     for(vector<Excel>::const_iterator it = excels.begin(); it != excels.end(); ++it)
0121     {
0122         for(vector<SpreadSheet>::const_iterator it1 = it->sheets.begin(); it1 != it->sheets.end(); ++it1)
0123         {
0124             for(vector<SpreadColumn>::const_iterator it2 = it1->columns.begin(); it2 != it1->columns.end(); ++it2)
0125             {
0126                 if(it2->index == index) {
0127                     int sheetno = (int)(it1-it->sheets.begin())+1;
0128                     string sheetsuffix = string("@")+std::to_string(sheetno);
0129                     if (sheetno > 1)
0130                         return make_pair("E_" + it->name+sheetsuffix, it2->name);
0131                     else
0132                         return make_pair("E_" + it->name, it2->name);
0133                 }
0134             }
0135         }
0136     }
0137 
0138     for(vector<Function>::const_iterator it = functions.begin(); it != functions.end(); ++it)
0139     {
0140         if(it->index == index)
0141             return make_pair("F_" + it->name, it->name);
0142     }
0143 
0144     return pair<string, string>();
0145 }
0146 
0147 pair<ProjectNode::NodeType, string> OriginParser::findObjectByIndex(unsigned int index) const
0148 {
0149     for(vector<SpreadSheet>::const_iterator it = spreadSheets.begin(); it != spreadSheets.end(); ++it)
0150     {
0151         if(it->objectID == (int)index)
0152             return make_pair(ProjectNode::SpreadSheet, it->name);
0153     }
0154 
0155     for(vector<Matrix>::const_iterator it = matrixes.begin(); it != matrixes.end(); ++it)
0156     {
0157         if(it->objectID == (int)index)
0158             return make_pair(ProjectNode::Matrix, it->name);
0159     }
0160 
0161     for(vector<Excel>::const_iterator it = excels.begin(); it != excels.end(); ++it)
0162     {
0163         if(it->objectID == (int)index)
0164             return make_pair(ProjectNode::Excel, it->name);
0165     }
0166 
0167     for(vector<Graph>::const_iterator it = graphs.begin(); it != graphs.end(); ++it)
0168     {
0169         if(it->objectID == (int)index){
0170             if (it->is3D)
0171                 return make_pair(ProjectNode::Graph3D, it->name);
0172             else
0173                 return make_pair(ProjectNode::Graph, it->name);
0174         }
0175     }
0176 
0177     return pair<ProjectNode::NodeType, string>();
0178 }
0179 
0180 pair<ProjectNode::NodeType, Origin::Window> OriginParser::findWindowObjectByIndex(unsigned int index) const
0181 {
0182     for(vector<SpreadSheet>::const_iterator it = spreadSheets.begin(); it != spreadSheets.end(); ++it)
0183     {
0184         if(it->objectID == (int)index)
0185             return make_pair(ProjectNode::SpreadSheet, (Origin::Window)(*it));
0186     }
0187 
0188     for(vector<Matrix>::const_iterator it = matrixes.begin(); it != matrixes.end(); ++it)
0189     {
0190         if(it->objectID == (int)index)
0191             return make_pair(ProjectNode::Matrix, (Origin::Window)(*it));
0192     }
0193 
0194     for(vector<Excel>::const_iterator it = excels.begin(); it != excels.end(); ++it)
0195     {
0196         if(it->objectID == (int)index)
0197             return make_pair(ProjectNode::Excel, (Origin::Window)(*it));
0198     }
0199 
0200     for(vector<Graph>::const_iterator it = graphs.begin(); it != graphs.end(); ++it)
0201     {
0202         if(it->objectID == (int)index){
0203             if (it->is3D)
0204                 return make_pair(ProjectNode::Graph3D, (Origin::Window)(*it));
0205             else
0206                 return make_pair(ProjectNode::Graph, (Origin::Window)(*it));
0207         }
0208     }
0209 
0210     return pair<ProjectNode::NodeType, Origin::Window>();
0211 }
0212 
0213 void OriginParser::convertSpreadToExcel(vector<Origin::SpreadSheet>::size_type spread)
0214 {
0215     //add new Excel sheet
0216     excels.push_back(Excel(spreadSheets[spread].name, spreadSheets[spread].label, spreadSheets[spread].maxRows, spreadSheets[spread].hidden, spreadSheets[spread].loose));
0217 
0218     for(vector<SpreadColumn>::iterator it = spreadSheets[spread].columns.begin(); it != spreadSheets[spread].columns.end(); ++it)
0219     {
0220         unsigned int index = 0;
0221         int pos = (int)(it->name.find_last_of("@"));
0222         if(pos != -1)
0223         {
0224             index = strtol(it->name.substr(pos + 1).c_str(), nullptr, 10) - 1;
0225             it->name = it->name.substr(0, pos);
0226         }
0227 
0228         if(excels.back().sheets.size() <= index)
0229             excels.back().sheets.resize(index + 1);
0230 
0231         excels.back().sheets[index].columns.push_back(*it);
0232     }
0233 
0234     spreadSheets.erase(spreadSheets.begin() + spread);
0235 }
0236 
0237 int OriginParser::findColumnByName(int spread, const string& name)
0238 {
0239     size_t columns = spreadSheets[spread].columns.size();
0240     for (unsigned int i = 0; i < columns; i++){
0241         string colName = spreadSheets[spread].columns[i].name;
0242         if (colName.size() >= 11)
0243             colName.resize(11);
0244 
0245         if (name == colName)
0246             return i;
0247     }
0248     return -1;
0249 }