summaryrefslogtreecommitdiffstats
path: root/src/Client/XLSReader.cpp
blob: 27ff2f55313ef1e8eba6ca4e82d685bec207a413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 Core::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.extractUTF8().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)));
  }
}

}
}