summaryrefslogtreecommitdiffstats
path: root/src/Client/XLSReader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Client/XLSReader.cpp')
-rw-r--r--src/Client/XLSReader.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/Client/XLSReader.cpp b/src/Client/XLSReader.cpp
new file mode 100644
index 0000000..9bd9dda
--- /dev/null
+++ b/src/Client/XLSReader.cpp
@@ -0,0 +1,89 @@
+/*
+ * XLSReader.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 "XLSReader.h"
+#include "XLSSheet.h"
+
+#include <cstdio>
+#include <boost/filesystem.hpp>
+#include <boost/regex.hpp>
+
+namespace Mad {
+namespace Client {
+
+xmlNodePtr XLSReader::findNode(xmlNodePtr parent, const std::string &name) {
+ if(!parent)
+ return 0;
+
+ for(xmlNodePtr entry = parent->children; entry != 0; entry = entry->next) {
+ if(entry->type == XML_ELEMENT_NODE && !xmlStrcmp(entry->name, (xmlChar*)name.c_str())) {
+ return entry;
+ }
+ }
+
+ return 0;
+}
+
+XLSReader::XLSReader(const std::string &filename) throw (Core::Exception) {
+ static const std::string XLHTML_EXEC = "xlhtml -xml";
+ static const boost::regex r("'");
+
+ if(!boost::filesystem::exists(filename))
+ throw Core::Exception(Core::Exception::NOT_FOUND);
+
+ std::string escapedFilename = boost::regex_replace(filename, r, "\\\\'", boost::match_default);
+
+ std::FILE *stream = popen((XLHTML_EXEC + " '" + escapedFilename + "' 2>&1").c_str(), "r");
+
+ std::string data;
+
+ while(!std::feof(stream)) {
+ char buffer[1024];
+
+ size_t bytes = std::fread(buffer, 1, sizeof(buffer), stream);
+ if(!bytes)
+ break;
+
+ data += std::string(buffer, bytes);
+ }
+
+ int ret = pclose(stream);
+ if(!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
+ throw Core::Exception(Core::Exception::INVALID_INPUT);
+ }
+
+ doc.reset(xmlParseMemory(data.c_str(), data.length()), xmlFreeDoc);
+ if(!doc) {
+ throw Core::Exception(Core::Exception::NOT_AVAILABLE);
+ }
+
+ xmlNodePtr sheetsNode = findNode(xmlDocGetRootElement(doc.get()), "sheets");
+
+ if(!sheetsNode) {
+ throw Core::Exception(Core::Exception::NOT_AVAILABLE);
+ }
+
+ for(xmlNodePtr entry = sheetsNode->children; entry != 0; entry = entry->next) {
+ if(entry->type == XML_ELEMENT_NODE && !xmlStrcmp(entry->name, (xmlChar*)"sheet"))
+ sheets.push_back(boost::shared_ptr<XLSSheet>(new XLSSheet(doc, entry)));
+ }
+}
+
+}
+}