|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 |
|
19 namespace SymbianUtils |
|
20 { |
|
21 public static class PrefixParser |
|
22 { |
|
23 public static long SkipPrefixAndReadLong( string aPrefix, ref string aLine ) |
|
24 { |
|
25 long ret = 0; |
|
26 // |
|
27 if ( aLine.IndexOf( aPrefix ) >= 0 ) |
|
28 { |
|
29 SkipPrefix( aPrefix, ref aLine ); |
|
30 ret = ReadLong( ref aLine ); |
|
31 } |
|
32 // |
|
33 return ret; |
|
34 } |
|
35 |
|
36 public static long SkipPrefixAndReadLongHex( string aPrefix, ref string aLine ) |
|
37 { |
|
38 long ret = 0; |
|
39 // |
|
40 if ( aLine.IndexOf( aPrefix ) >= 0 ) |
|
41 { |
|
42 SkipPrefix( aPrefix, ref aLine ); |
|
43 ret = ReadLongHex( ref aLine ); |
|
44 } |
|
45 // |
|
46 return ret; |
|
47 } |
|
48 |
|
49 public static int SkipPrefixAndReadInt( string aPrefix, ref string aLine ) |
|
50 { |
|
51 int ret = 0; |
|
52 // |
|
53 if ( aLine.IndexOf( aPrefix ) >= 0 ) |
|
54 { |
|
55 SkipPrefix( aPrefix, ref aLine ); |
|
56 ret = (int) ReadLong( ref aLine ); |
|
57 } |
|
58 // |
|
59 return ret; |
|
60 } |
|
61 |
|
62 public static uint SkipPrefixAndReadUint( string aPrefix, ref string aLine ) |
|
63 { |
|
64 uint ret = 0; |
|
65 // |
|
66 if ( aLine.IndexOf( aPrefix ) >= 0 ) |
|
67 { |
|
68 SkipPrefix( aPrefix, ref aLine ); |
|
69 ret = (uint) ReadLong( ref aLine ); |
|
70 } |
|
71 // |
|
72 return ret; |
|
73 } |
|
74 |
|
75 public static bool SkipPrefixAndReadBool( string aPrefix, ref string aLine ) |
|
76 { |
|
77 bool ret = false; |
|
78 // |
|
79 if ( aLine.IndexOf( aPrefix ) >= 0 ) |
|
80 { |
|
81 SkipPrefix( aPrefix, ref aLine ); |
|
82 ret = ReadBool( ref aLine ); |
|
83 } |
|
84 // |
|
85 return ret; |
|
86 } |
|
87 |
|
88 public static void SkipPrefix( string aPrefix, ref string aLine ) |
|
89 { |
|
90 int index = aLine.IndexOf( aPrefix ); |
|
91 if ( index >= 0 ) |
|
92 aLine = aLine.Substring( index + aPrefix.Length ); |
|
93 aLine = aLine.Trim(); |
|
94 } |
|
95 |
|
96 public static uint ReadUint( ref string aLine ) |
|
97 { |
|
98 uint retUint = 0; |
|
99 long retLong = 0; |
|
100 SymbianUtils.NumberBaseUtils.TNumberBase numberBase; |
|
101 string discardedText; |
|
102 bool convertedOkay = SymbianUtils.NumberBaseUtils.TryTextToDecimalNumber( ref aLine, out discardedText, out retLong, out numberBase ); |
|
103 if ( convertedOkay ) |
|
104 { |
|
105 try |
|
106 { |
|
107 retUint = System.Convert.ToUInt32( retLong ); |
|
108 } |
|
109 catch ( OverflowException ) |
|
110 { |
|
111 } |
|
112 } |
|
113 // |
|
114 return retUint; |
|
115 } |
|
116 |
|
117 public static int ReadInt( ref string aLine ) |
|
118 { |
|
119 int retInt = 0; |
|
120 long retLong = 0; |
|
121 SymbianUtils.NumberBaseUtils.TNumberBase numberBase; |
|
122 string discardedText; |
|
123 bool convertedOkay = SymbianUtils.NumberBaseUtils.TryTextToDecimalNumber( ref aLine, out discardedText, out retLong, out numberBase ); |
|
124 if ( convertedOkay ) |
|
125 { |
|
126 try |
|
127 { |
|
128 retInt = System.Convert.ToInt32( retLong ); |
|
129 } |
|
130 catch ( OverflowException ) |
|
131 { |
|
132 } |
|
133 } |
|
134 // |
|
135 return retInt; |
|
136 } |
|
137 |
|
138 public static long ReadLong( ref string aLine ) |
|
139 { |
|
140 long ret = -1; |
|
141 SymbianUtils.NumberBaseUtils.TNumberBase numberBase; |
|
142 string discardedText; |
|
143 bool convertedOkay = SymbianUtils.NumberBaseUtils.TryTextToDecimalNumber( ref aLine, out discardedText, out ret, out numberBase ); |
|
144 return ret; |
|
145 } |
|
146 |
|
147 public static long ReadLongHex( ref string aLine ) |
|
148 { |
|
149 long ret = 0; |
|
150 string discardedText; |
|
151 bool convertedOkay = SymbianUtils.NumberBaseUtils.TryTextToDecimalNumber( ref aLine, out discardedText, out ret, SymbianUtils.NumberBaseUtils.TNumberBase.EHex ); |
|
152 return ret; |
|
153 } |
|
154 |
|
155 public static bool ReadBool( ref string aLine ) |
|
156 { |
|
157 bool ret = false; |
|
158 |
|
159 // Trim the start of the line |
|
160 aLine = aLine.TrimStart(); |
|
161 string upperLine = aLine.ToUpper(); |
|
162 // |
|
163 if ( upperLine == "YES" || upperLine == "TRUE" || upperLine == "1" ) |
|
164 { |
|
165 ret = true; |
|
166 } |
|
167 else if ( upperLine == "NO" || upperLine == "FALSE" || upperLine == "0" ) |
|
168 { |
|
169 ret = false; |
|
170 } |
|
171 else |
|
172 { |
|
173 int endIndex = 0; |
|
174 bool continueSearchingForEndIndex = true; |
|
175 for ( int i = 0; i < aLine.Length && continueSearchingForEndIndex; i++ ) |
|
176 { |
|
177 char character = aLine[ i ]; |
|
178 switch ( character ) |
|
179 { |
|
180 case '1': |
|
181 case '0': |
|
182 ++endIndex; |
|
183 break; |
|
184 default: |
|
185 continueSearchingForEndIndex = false; |
|
186 break; |
|
187 } |
|
188 } |
|
189 |
|
190 string boolAsString = aLine.Substring( 0, endIndex ); |
|
191 // |
|
192 try |
|
193 { |
|
194 int boolAsInt = System.Convert.ToInt32( boolAsString ); |
|
195 ret = ( boolAsInt > 0 ); |
|
196 aLine = aLine.Substring( endIndex ); |
|
197 } |
|
198 catch ( Exception ) |
|
199 { |
|
200 } |
|
201 } |
|
202 // |
|
203 return ret; |
|
204 } |
|
205 } |
|
206 } |