Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2011 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/css_parser.hpp"
29 : #include "orcus/pstring.hpp"
30 : #include "orcus/global.hpp"
31 :
32 : #include <iostream>
33 : #include <string>
34 :
35 : using namespace orcus;
36 : using namespace std;
37 :
38 : namespace {
39 :
40 : class parser_handler
41 : {
42 : bool m_in_prop:1;
43 : public:
44 0 : parser_handler() : m_in_prop(false) {}
45 :
46 0 : void at_rule_name(const char* p, size_t n)
47 : {
48 0 : cout << "@" << string(p, n).c_str();
49 0 : }
50 :
51 0 : void selector_name(const char* p_elem, size_t n_elem, const char* p_class, size_t n_class)
52 : {
53 0 : cout << "(elem='" << string(p_elem, n_elem).c_str() << "'; class='" << string(p_class, n_class).c_str() << "') ";
54 0 : }
55 :
56 0 : void property_name(const char* p, size_t n)
57 : {
58 0 : cout << string(p, n).c_str() << ":";
59 0 : }
60 :
61 0 : void value(const char* p, size_t n)
62 : {
63 0 : cout << " '" << string(p, n).c_str() << "'";
64 0 : }
65 :
66 0 : void begin_parse()
67 : {
68 0 : cout << "========" << endl;
69 0 : }
70 :
71 0 : void end_parse()
72 : {
73 0 : cout << "========" << endl;
74 0 : }
75 :
76 0 : void begin_block()
77 : {
78 0 : cout << endl << "{" << endl;
79 0 : m_in_prop = true;
80 0 : }
81 :
82 0 : void end_block()
83 : {
84 0 : cout << "}" << endl;
85 0 : m_in_prop = false;
86 0 : }
87 :
88 0 : void begin_property()
89 : {
90 0 : if (m_in_prop)
91 0 : cout << " ";
92 0 : cout << "* ";
93 0 : }
94 :
95 0 : void end_property()
96 : {
97 : cout << endl;
98 0 : }
99 : };
100 :
101 : }
102 :
103 : class orcus_css
104 : {
105 : public:
106 : orcus_css();
107 : ~orcus_css();
108 :
109 : void read_file(const char* filepath);
110 : private:
111 : void parse(const string& strm);
112 : };
113 :
114 0 : orcus_css::orcus_css() {}
115 0 : orcus_css::~orcus_css() {}
116 :
117 0 : void orcus_css::read_file(const char* filepath)
118 : {
119 0 : cout << "reading " << filepath << endl;
120 : string strm;
121 0 : load_file_content(filepath, strm);
122 0 : parse(strm);
123 0 : }
124 :
125 0 : void orcus_css::parse(const string& strm)
126 : {
127 0 : if (strm.empty())
128 0 : return;
129 :
130 0 : cout << "original: '" << strm << "'" << endl << endl;
131 :
132 : parser_handler handler;
133 : css_parser<parser_handler> parser(&strm[0], strm.size(), handler);
134 0 : parser.parse();
135 : }
136 :
137 0 : int main(int argc, char** argv)
138 : {
139 0 : if (argc != 2)
140 : return EXIT_FAILURE;
141 :
142 : orcus_css app;
143 0 : app.read_file(argv[1]);
144 0 : pstring::intern::dispose();
145 :
146 : return EXIT_SUCCESS;
147 0 : }
|