diff options
Diffstat (limited to 'src/Client/XLSSheet.cpp')
-rw-r--r-- | src/Client/XLSSheet.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/Client/XLSSheet.cpp b/src/Client/XLSSheet.cpp new file mode 100644 index 0000000..915234f --- /dev/null +++ b/src/Client/XLSSheet.cpp @@ -0,0 +1,109 @@ +/* + * XLSSheet.cpp + * + * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "XLSSheet.h" +#include "XLSReader.h" + +#include <cstdlib> +#include <sstream> + +namespace Mad { +namespace Client { + +std::string XLSSheet::getSheetLevelField(const std::string &name) const { + xmlNodePtr entry = XLSReader::findNode(node, name); + if(entry) + return std::string((char*)xmlNodeGetContent(entry)); + else + return std::string(); +} + +void XLSSheet::readRows() { + rows.clear(); + + xmlNodePtr rowsNode = XLSReader::findNode(node, "rows"); + + xmlNodePtr rowEntry = rowsNode->children; + + if(firstRowColNames) { + for(; rowEntry != 0; rowEntry = rowEntry->next) { + if(rowEntry->type != XML_ELEMENT_NODE || xmlStrcmp(rowEntry->name, (xmlChar*)"row")) + continue; + + for(xmlNodePtr cellEntry = rowEntry->children; cellEntry != 0; cellEntry = cellEntry->next) { + if(cellEntry->type != XML_ELEMENT_NODE || xmlStrcmp(cellEntry->name, (xmlChar*)"cell")) + continue; + + xmlChar *colStr = xmlGetProp(cellEntry, (xmlChar*)"col"); + if(!colStr) + continue; + + unsigned long col = strtoul((char*)colStr, 0, 10); + if(col >= colCount) + continue; + + colNames[col] = (char*)xmlNodeGetContent(cellEntry); + } + + // Skip the first row + rowEntry = rowEntry->next; + break; + } + } + + for(; rowEntry != 0; rowEntry = rowEntry->next) { + if(rowEntry->type != XML_ELEMENT_NODE || xmlStrcmp(rowEntry->name, (xmlChar*)"row")) + continue; + + boost::shared_ptr<std::vector<std::string> > rowVector(new std::vector<std::string>(colCount)); + rows.push_back(rowVector); + + for(xmlNodePtr cellEntry = rowEntry->children; cellEntry != 0; cellEntry = cellEntry->next) { + if(cellEntry->type != XML_ELEMENT_NODE || xmlStrcmp(cellEntry->name, (xmlChar*)"cell")) + continue; + + xmlChar *colStr = xmlGetProp(cellEntry, (xmlChar*)"col"); + if(!colStr) + continue; + + unsigned long col = strtoul((char*)colStr, 0, 10); + if(col >= colCount) + continue; + + (*rowVector)[col] = (char*)xmlNodeGetContent(cellEntry); + } + } +} + +XLSSheet::XLSSheet(boost::shared_ptr<xmlDoc> doc0, xmlNodePtr node0) : doc(doc0), node(node0), firstRowColNames(false) { + title = getSheetLevelField("pagetitle"); + colCount = std::strtoul(getSheetLevelField("lastcol").c_str(), 0, 10)+1-std::strtoul(getSheetLevelField("firstcol").c_str(), 0, 10); + + for(unsigned col = 0; col < colCount; ++col) { + std::ostringstream stream; + stream << col; + + colNames.push_back(stream.str()); + } + + readRows(); +} + +} +} |