|
1 // tregexarg.cpp |
|
2 // |
|
3 // Copyright (c) 2005 - 2006, Google Inc. |
|
4 // All rights reserved. |
|
5 // |
|
6 // Redistribution and use in source and binary forms, with or without |
|
7 // modification, are permitted provided that the following conditions are |
|
8 // met: |
|
9 // |
|
10 // * Redistributions of source code must retain the above copyright |
|
11 // notice, this list of conditions and the following disclaimer. |
|
12 // * Redistributions in binary form must reproduce the above |
|
13 // copyright notice, this list of conditions and the following disclaimer |
|
14 // in the documentation and/or other materials provided with the |
|
15 // distribution. |
|
16 // * Neither the name of Google Inc. nor the names of its |
|
17 // contributors may be used to endorse or promote products derived from |
|
18 // this software without specific prior written permission. |
|
19 // |
|
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 // |
|
32 // Author: Sanjay Ghemawat |
|
33 // |
|
34 // Heavily refactored for Symbian OS by Accenture. |
|
35 |
|
36 /***** Parsers for various types *****/ |
|
37 #include "tregexarg.h" |
|
38 |
|
39 EXPORT_C TBool TRegExArg::ParseNull(const TDesC8& /*aString*/, |
|
40 TAny* aDestination) |
|
41 { |
|
42 // We fail if somebody asked us to store into a non-NULL TAny* pointer |
|
43 return (aDestination == NULL); |
|
44 } |
|
45 |
|
46 EXPORT_C TBool TRegExArg::ParseTDes8(const TDesC8& aString, TAny* aDestination) |
|
47 { |
|
48 if (aDestination == NULL) return ETrue; |
|
49 TDes8* dest = reinterpret_cast<TDes8*>(aDestination); |
|
50 if(dest->MaxLength() < aString.Length()) |
|
51 { |
|
52 return EFalse; |
|
53 } |
|
54 else |
|
55 { |
|
56 dest->Copy(aString); |
|
57 return ETrue; |
|
58 } |
|
59 } |
|
60 |
|
61 EXPORT_C TBool TRegExArg::ParseTPtrC8(const TDesC8& aString, TAny* aDestination) |
|
62 { |
|
63 if (aDestination == NULL) return ETrue; |
|
64 TPtrC8* dest = reinterpret_cast<TPtrC8*>(aDestination); |
|
65 |
|
66 dest->Set(aString); |
|
67 return ETrue; |
|
68 } |
|
69 |
|
70 EXPORT_C TBool TRegExArg::ParseTChar(const TDesC8& aString, TAny* aDestination) |
|
71 { |
|
72 if (aString.Length() != 1) return EFalse; |
|
73 if (aDestination == NULL) return ETrue; |
|
74 *(reinterpret_cast<TChar*>(aDestination)) = aString[0]; |
|
75 return ETrue; |
|
76 } |
|
77 |
|
78 EXPORT_C TBool TRegExArg::ParseTInt8(const TDesC8& aString, TAny* aDestination) |
|
79 { |
|
80 if (aString.Length() == 0) return EFalse; |
|
81 |
|
82 TInt8 val = 0; |
|
83 TLex8 lex(aString); |
|
84 TInt err = lex.Val(val); |
|
85 if(err) return EFalse; |
|
86 if (aDestination == NULL) return ETrue; |
|
87 |
|
88 *(reinterpret_cast<TInt8*>(aDestination)) = val; |
|
89 return ETrue; |
|
90 } |
|
91 |
|
92 EXPORT_C TBool TRegExArg::ParseTInt16(const TDesC8& aString, TAny* aDestination) |
|
93 { |
|
94 if (aString.Length() == 0) return EFalse; |
|
95 |
|
96 TInt16 val = 0; |
|
97 TLex8 lex(aString); |
|
98 TInt err = lex.Val(val); |
|
99 if(err) return EFalse; |
|
100 if (aDestination == NULL) return ETrue; |
|
101 |
|
102 *(reinterpret_cast<TInt16*>(aDestination)) = val; |
|
103 return ETrue; |
|
104 } |
|
105 |
|
106 EXPORT_C TBool TRegExArg::ParseTInt32(const TDesC8& aString, TAny* aDestination) |
|
107 { |
|
108 if (aString.Length() == 0) return EFalse; |
|
109 |
|
110 TInt32 val = 0; |
|
111 TLex8 lex(aString); |
|
112 TInt err = lex.Val(val); |
|
113 if(err) return EFalse; |
|
114 if (aDestination == NULL) return ETrue; |
|
115 |
|
116 *(reinterpret_cast<TInt32*>(aDestination)) = val; |
|
117 return ETrue; |
|
118 } |
|
119 |
|
120 EXPORT_C TBool TRegExArg::ParseTInt(const TDesC8& aString, TAny* aDestination) |
|
121 { |
|
122 if (aString.Length() == 0) return EFalse; |
|
123 |
|
124 TInt val = 0; |
|
125 TLex8 lex(aString); |
|
126 TInt err = lex.Val(val); |
|
127 if(err) return EFalse; |
|
128 if (aDestination == NULL) return ETrue; |
|
129 |
|
130 *(reinterpret_cast<TInt*>(aDestination)) = val; |
|
131 return ETrue; |
|
132 } |
|
133 |
|
134 TBool TRegExArg::ParseTInt64Radix(const TDesC8& aString, TAny* aDestination, |
|
135 TRadix aRadix) |
|
136 { |
|
137 if (aString.Length() == 0) |
|
138 return EFalse; |
|
139 |
|
140 TInt64 val = 0; |
|
141 TLex8 lex(aString); |
|
142 // Note TInt64 is treated as unsigned by TLex |
|
143 TInt err = lex.Val(val, aRadix); |
|
144 |
|
145 // If val is returned as negative then it means we have overflowed. |
|
146 if (err || val < 0) |
|
147 return EFalse; |
|
148 |
|
149 if (aDestination == NULL) return ETrue; |
|
150 |
|
151 *(reinterpret_cast<TInt64*> (aDestination)) = val; |
|
152 return ETrue; |
|
153 } |
|
154 |
|
155 TBool TRegExArg::ParseTUint8Radix(const TDesC8& aString, TAny* aDestination, |
|
156 TRadix aRadix) |
|
157 { |
|
158 if (aString.Length() == 0) |
|
159 return EFalse; |
|
160 |
|
161 TUint8 val = 0; |
|
162 TLex8 lex(aString); |
|
163 TInt err = lex.Val(val, aRadix); |
|
164 if (err) |
|
165 return EFalse; |
|
166 if (aDestination == NULL) return ETrue; |
|
167 |
|
168 *(reinterpret_cast<TUint8*> (aDestination)) = val; |
|
169 return ETrue; |
|
170 } |
|
171 |
|
172 TBool TRegExArg::ParseTUint16Radix(const TDesC8& aString, TAny* aDestination, |
|
173 TRadix aRadix) |
|
174 { |
|
175 if (aString.Length() == 0) |
|
176 return EFalse; |
|
177 |
|
178 TUint16 val = 0; |
|
179 TLex8 lex(aString); |
|
180 TInt err = lex.Val(val, aRadix); |
|
181 if (err) |
|
182 return EFalse; |
|
183 if (aDestination == NULL) return ETrue; |
|
184 |
|
185 *(reinterpret_cast<TUint16*> (aDestination)) = val; |
|
186 return ETrue; |
|
187 } |
|
188 |
|
189 TBool TRegExArg::ParseTUint32Radix(const TDesC8& aString, TAny* aDestination, |
|
190 TRadix aRadix) |
|
191 { |
|
192 if (aString.Length() == 0) |
|
193 return EFalse; |
|
194 |
|
195 TUint32 val = 0; |
|
196 TLex8 lex(aString); |
|
197 TInt err = lex.Val(val, aRadix); |
|
198 if (err) |
|
199 return EFalse; |
|
200 if (aDestination == NULL) return ETrue; |
|
201 |
|
202 *(reinterpret_cast<TUint32*> (aDestination)) = val; |
|
203 return ETrue; |
|
204 } |
|
205 |
|
206 TBool TRegExArg::ParseTUintRadix(const TDesC8& aString, TAny* aDestination, |
|
207 TRadix aRadix) |
|
208 { |
|
209 if (aString.Length() == 0) |
|
210 return EFalse; |
|
211 |
|
212 TUint val = 0; |
|
213 TLex8 lex(aString); |
|
214 TInt err = lex.Val(val, aRadix); |
|
215 if (err) |
|
216 return EFalse; |
|
217 if (aDestination == NULL) return ETrue; |
|
218 |
|
219 *(reinterpret_cast<TUint*> (aDestination)) = val; |
|
220 return ETrue; |
|
221 } |
|
222 EXPORT_C TBool TRegExArg::ParseTReal32(const TDesC8& aString, |
|
223 TAny* aDestination) |
|
224 { |
|
225 if (aString.Length() == 0) return EFalse; |
|
226 |
|
227 TReal32 val = 0; |
|
228 TLex8 lex(aString); |
|
229 TInt err = lex.Val(val); |
|
230 if(err) return EFalse; |
|
231 if (aDestination == NULL) return ETrue; |
|
232 |
|
233 *(reinterpret_cast<TReal32*>(aDestination)) = val; |
|
234 return ETrue; |
|
235 } |
|
236 |
|
237 EXPORT_C TBool TRegExArg::ParseTReal64(const TDesC8& aString, |
|
238 TAny* aDestination) |
|
239 { |
|
240 if (aString.Length() == 0) return EFalse; |
|
241 |
|
242 TReal64 val = 0; |
|
243 TLex8 lex(aString); |
|
244 TInt err = lex.Val(val); |
|
245 if(err) return EFalse; |
|
246 if (aDestination == NULL) return ETrue; |
|
247 |
|
248 *(reinterpret_cast<TReal64*>(aDestination)) = val; |
|
249 return ETrue; |
|
250 } |
|
251 |
|
252 #define DEFINE_INTEGER_PARSERS(name) \ |
|
253 EXPORT_C TBool TRegExArg::Parse##name(const TDesC8& aString, \ |
|
254 TAny* aDestination) \ |
|
255 { \ |
|
256 return Parse##name##Radix(aString, aDestination, EDecimal); \ |
|
257 } \ |
|
258 EXPORT_C TBool TRegExArg::Parse##name##Hex(const TDesC8& aString, \ |
|
259 TAny* aDestination) \ |
|
260 { \ |
|
261 return Parse##name##Radix(aString, aDestination, EHex); \ |
|
262 } \ |
|
263 EXPORT_C TBool TRegExArg::Parse##name##Octal(const TDesC8& aString, \ |
|
264 TAny* aDestination) \ |
|
265 { \ |
|
266 return Parse##name##Radix(aString, aDestination, EOctal); \ |
|
267 } |
|
268 |
|
269 DEFINE_INTEGER_PARSERS(TInt64) |
|
270 DEFINE_INTEGER_PARSERS(TUint8) |
|
271 DEFINE_INTEGER_PARSERS(TUint16) |
|
272 DEFINE_INTEGER_PARSERS(TUint32) |
|
273 DEFINE_INTEGER_PARSERS(TUint) |
|
274 |
|
275 #undef DEFINE_INTEGER_PARSERS |