Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2010-2012 Kohei Yoshida
4 : *
5 : * Permission is hereby granted, free of charge, to any person
6 : * obtaining a copy of this software and associated documentation
7 : * files (the "Software"), to deal in the Software without
8 : * restriction, including without limitation the rights to use,
9 : * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : * copies of the Software, and to permit persons to whom the
11 : * Software is furnished to do so, subject to the following
12 : * conditions:
13 : *
14 : * The above copyright notice and this permission notice shall be
15 : * included in all copies or substantial portions of the Software.
16 : *
17 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 : * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 : * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 : * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 : * OTHER DEALINGS IN THE SOFTWARE.
25 : *
26 : ************************************************************************/
27 :
28 : #include "orcus/orcus_gnumeric.hpp"
29 : #include "orcus/xml_parser.hpp"
30 : #include "orcus/gnumeric/gnumeric_handler.hpp"
31 : #include "orcus/gnumeric/gnumeric_tokens.hpp"
32 :
33 : #include <zlib.h>
34 :
35 : #include <cstdlib>
36 : #include <iostream>
37 : #include <fstream>
38 : #include <vector>
39 : #include <sstream>
40 : #include <string>
41 :
42 : #include <boost/scoped_ptr.hpp>
43 :
44 : using namespace std;
45 :
46 : #define BUFFER_LENGTH 0x2000
47 :
48 : namespace orcus {
49 :
50 0 : orcus_gnumeric::orcus_gnumeric(spreadsheet::iface::import_factory* factory) :
51 0 : mp_factory(factory)
52 : {
53 0 : }
54 :
55 0 : orcus_gnumeric::~orcus_gnumeric()
56 : {
57 0 : }
58 :
59 0 : void orcus_gnumeric::read_content_xml(const char* p, size_t size)
60 : {
61 0 : xml_stream_parser parser(gnumeric_tokens, p, size, "content.xml");
62 : ::boost::scoped_ptr<gnumeric_content_xml_handler> handler(
63 0 : new gnumeric_content_xml_handler(gnumeric_tokens, mp_factory));
64 0 : parser.set_handler(handler.get());
65 0 : parser.parse();
66 0 : }
67 :
68 0 : void orcus_gnumeric::read_file(const char *fpath)
69 : {
70 0 : cout << "reading " << fpath << endl;
71 :
72 0 : gzFile file = gzopen(fpath, "rb");
73 :
74 0 : if (!file)
75 0 : return;
76 :
77 : std::string file_content;
78 :
79 0 : while (true)
80 : {
81 : char buffer[BUFFER_LENGTH];
82 0 : int read_characters = gzread(file, buffer, BUFFER_LENGTH);
83 0 : if (read_characters < 0)
84 : {
85 0 : std::cout << "Read error" << std::endl;
86 : break;
87 : }
88 :
89 0 : file_content.append(buffer, read_characters);
90 0 : if (read_characters < BUFFER_LENGTH)
91 : {
92 0 : if (gzeof(file))
93 : break;
94 : else
95 : {
96 : const char *error;
97 : int err;
98 0 : error = gzerror(file, &err);
99 0 : std::cout << "error: " << error << std::endl;
100 : }
101 : }
102 : }
103 :
104 0 : read_content_xml(file_content.c_str(), file_content.length());
105 : }
106 :
107 24 : }
|