/* * XLSSheet.cpp * * Copyright (C) 2009 Matthias Schiffer * * 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 . */ #include "XLSSheet.h" #include "XLSReader.h" #include #include namespace Mad { namespace Client { Core::String XLSSheet::getSheetLevelField(const Core::String &name) const { xmlNodePtr entry = XLSReader::findNode(node, name); if(entry) return Core::String::fromUTF8((char*)xmlNodeGetContent(entry)); else return Core::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] = Core::String::fromUTF8((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 > rowVector(new std::vector(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] = Core::String::fromUTF8((char*)xmlNodeGetContent(cellEntry)); } } } XLSSheet::XLSSheet(boost::shared_ptr doc0, xmlNodePtr node0) : doc(doc0), node(node0), firstRowColNames(false) { title = getSheetLevelField("pagetitle"); colCount = std::strtoul(getSheetLevelField("lastcol").toString().c_str(), 0, 10)+1-std::strtoul(getSheetLevelField("firstcol").toString().c_str(), 0, 10); for(unsigned col = 0; col < colCount; ++col) { std::ostringstream stream; stream << col; colNames.push_back(stream.str().c_str()); } readRows(); } } }