Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4973 | right-hear | 1 | /* |
2 | Generic parser class for HTML viewer. |
||
3 | Copyright (C) 2003 Jarek Pelczar (jarekp3@wp.pl) |
||
4 | |||
5 | This program is free software; you can redistribute it and/or modify |
||
6 | it under the terms of the GNU General Public License as published by |
||
7 | the Free Software Foundation; either version 2 of the License, or |
||
8 | (at your option) any later version. |
||
9 | |||
10 | This program is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | GNU General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU General Public License |
||
16 | along with this program; if not, write to the Free Software |
||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
18 | */ |
||
19 | |||
20 | #include |
||
21 | #include"parser.h" |
||
22 | #include |
||
23 | |||
24 | CParser::CParser() |
||
25 | { |
||
26 | IsEOF=false; |
||
27 | Look=0; |
||
28 | } |
||
29 | |||
30 | CParser::~CParser() |
||
31 | { |
||
32 | } |
||
33 | |||
34 | char CParser::GetChar() |
||
35 | { |
||
36 | return 0; |
||
37 | } |
||
38 | |||
39 | void CParser::Init() |
||
40 | { |
||
41 | GetChar(); |
||
42 | } |
||
43 | |||
44 | void CParser::SkipWhite() |
||
45 | { |
||
46 | if(IsEOF) return; |
||
47 | while(Look==' ' || Look=='\t' || Look=='\n' || Look=='\r') |
||
48 | GetChar(); |
||
49 | } |
||
50 | |||
51 | int CParser::Match(char c) |
||
52 | { |
||
53 | if(IsEOF) return 0; |
||
54 | SkipWhite(); |
||
55 | if(Look==c) return 1; |
||
56 | return 0; |
||
57 | } |
||
58 | |||
59 | static inline int hex2dec(char c) |
||
60 | { |
||
61 | if(c>='0'&&c<='9') return c-'0'; |
||
62 | if(c>='a'&&c<='f') return c-'a'+10; |
||
63 | if(c>='A'&&c<='F') return c-'A'+10; |
||
64 | return 0; |
||
65 | } |
||
66 | |||
67 | unsigned long CParser::__GetNum() |
||
68 | { |
||
69 | unsigned long __ret=0; |
||
70 | SkipWhite(); |
||
71 | while(isdigit(Look)) |
||
72 | { |
||
73 | __ret=(__ret*10)+(Look-'0'); |
||
74 | GetChar(); |
||
75 | } |
||
76 | return __ret; |
||
77 | } |
||
78 | |||
79 | unsigned long CParser::__GetHexNum() |
||
80 | { |
||
81 | unsigned long __ret=0; |
||
82 | SkipWhite(); |
||
83 | while(isxdigit(Look)) |
||
84 | { |
||
85 | __ret=(__ret<<4)+hex2dec(Look); |
||
86 | GetChar(); |
||
87 | } |
||
88 | return __ret; |
||
89 | } |
||
90 | |||
91 | unsigned long CParser::__GetOctNum() |
||
92 | { |
||
93 | unsigned long __ret=0; |
||
94 | SkipWhite(); |
||
95 | while(Look>='0' && Look<='7') |
||
96 | { |
||
97 | __ret=(__ret<<3)+(Look-'0'); |
||
98 | GetChar(); |
||
99 | } |
||
100 | return __ret; |
||
101 | } |
||
102 | |||
103 | /* This routine simply calls previously defined functions. |
||
104 | So the number may be (of course in HTML) : |
||
105 | number example class |
||
106 | #1BADB002 hex |
||
107 | #0x1BADB002 hex |
||
108 | $0x1BADB002 hex |
||
109 | 0xF002 hex |
||
110 | 03312 octal |
||
111 | 32768 decimal |
||
112 | Note: |
||
113 | We don't have to remember leading zeroes because they always give 0 |
||
114 | so there isn't anything to complain about. |
||
115 | */ |
||
116 | unsigned long CParser::GetNum() |
||
117 | { |
||
118 | if(Look=='#') |
||
119 | { |
||
120 | Match('#'); |
||
121 | if(Look=='0') |
||
122 | { |
||
123 | GetChar(); |
||
124 | if(Look=='x') |
||
125 | { |
||
126 | GetChar(); |
||
127 | return __GetHexNum(); |
||
128 | } |
||
129 | } |
||
130 | return __GetHexNum(); |
||
131 | } |
||
132 | if(Look=='$') |
||
133 | { |
||
134 | Match('$'); |
||
135 | return __GetHexNum(); |
||
136 | } |
||
137 | if(Look=='0') |
||
138 | { |
||
139 | GetChar(); |
||
140 | if(Look=='x') |
||
141 | { |
||
142 | GetChar(); |
||
143 | return __GetHexNum(); |
||
144 | } |
||
145 | return __GetOctNum(); |
||
146 | } |
||
147 | return __GetNum(); |
||
148 | } |
||
149 | |||
150 | int CParser::GetToken(char * tokbuf,int maxlen) |
||
151 | { |
||
152 | int p; |
||
153 | if(!tokbuf || !maxlen) return 0; |
||
154 | if(IsEOF) return 0; |
||
155 | SkipWhite(); |
||
156 | if(!isalpha(Look) && Look!='_') return 0; |
||
157 | p=0; |
||
158 | while(maxlen-- && (isalpha(Look) || isdigit(Look) || Look=='_')) |
||
159 | { |
||
160 | *tokbuf++=Look; |
||
161 | GetChar(); |
||
162 | p++; |
||
163 | } |
||
164 | return p; |
||
165 | }3)+(Look-'0'); |