Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2011-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_csv.hpp"
29 :
30 : #include "orcus/csv_parser.hpp"
31 : #include "orcus/pstring.hpp"
32 : #include "orcus/global.hpp"
33 : #include "orcus/spreadsheet/import_interface.hpp"
34 :
35 : #include <cstring>
36 : #include <iostream>
37 :
38 : using namespace std;
39 :
40 : namespace orcus {
41 :
42 : namespace {
43 :
44 : class csv_handler
45 : {
46 : public:
47 : csv_handler(spreadsheet::iface::import_factory& factory) :
48 0 : m_factory(factory), mp_sheet(NULL), m_row(0), m_col(0) {}
49 :
50 0 : void begin_parse()
51 : {
52 : const char* sheet_name = "data";
53 0 : mp_sheet = m_factory.append_sheet(sheet_name, strlen(sheet_name));
54 0 : }
55 :
56 0 : void end_parse() {}
57 0 : void begin_row() {}
58 :
59 0 : void end_row()
60 : {
61 0 : ++m_row;
62 0 : m_col = 0;
63 0 : }
64 :
65 : void cell(const char* p, size_t n)
66 : {
67 0 : mp_sheet->set_auto(m_row, m_col, p, n);
68 0 : ++m_col;
69 : }
70 :
71 : private:
72 : spreadsheet::iface::import_factory& m_factory;
73 : spreadsheet::iface::import_sheet* mp_sheet;
74 : spreadsheet::row_t m_row;
75 : spreadsheet::col_t m_col;
76 : };
77 :
78 : }
79 :
80 0 : orcus_csv::orcus_csv(spreadsheet::iface::import_factory* factory) : mp_factory(factory) {}
81 :
82 0 : void orcus_csv::read_file(const char* filepath)
83 : {
84 0 : cout << "reading " << filepath << endl;
85 : string strm;
86 0 : load_file_content(filepath, strm);
87 0 : parse(strm);
88 0 : }
89 :
90 0 : void orcus_csv::parse(const string& strm)
91 : {
92 0 : if (strm.empty())
93 0 : return;
94 :
95 0 : csv_handler handler(*mp_factory);
96 : csv_parser_config config;
97 0 : config.delimiters.push_back(',');
98 0 : config.text_qualifier = '"';
99 : csv_parser<csv_handler> parser(&strm[0], strm.size(), handler, config);
100 : try
101 : {
102 0 : parser.parse();
103 : }
104 0 : catch (const csv_parse_error& e)
105 : {
106 0 : cout << "parse failed: " << e.what() << endl;
107 0 : }
108 : }
109 :
110 24 : }
111 :
|