1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 // ===========================================================================
27 // CONTENT : CLASS StringScanner
28 // AUTHOR : Manfred Duchrow
29 // VERSION : 1.1 - 29/09/2002
30 // HISTORY :
31 // 11/07/2001 duma CREATED
32 // 29/09/2002 duma added -> endReached(), endNotReached()
33 //
34 // Copyright (c) 2001-2002, by Manfred Duchrow. All rights reserved.
35 // ===========================================================================
36 package com.sshtools.daemon.util;
37
38
39 // ===========================================================================
40 // IMPORTS
41 // ===========================================================================
42
43 /**
44 * Simple scanner that allows to navigate over the characters of a string.
45 *
46 * @author Manfred Duchrow
47 * @version 1.1
48 */
49 public class StringScanner {
50 // =========================================================================
51 // CONSTANTS
52 // =========================================================================
53
54 /** */
55 public static final char END_REACHED = (char) -1;
56
57 // =========================================================================
58 // INSTANCE VARIABLES
59 // =========================================================================
60
61 /** */
62 protected int length = 0;
63
64 /** */
65 protected int position = 0;
66
67 /** */
68 protected int pos_marker = 0;
69
70 /** */
71 protected char[] buffer = null;
72
73 // -------------------------------------------------------------------------
74 // =========================================================================
75 // CONSTRUCTORS
76 // =========================================================================
77
78 /**
79 * Initialize the new instance with the string that should be scanned.
80 *
81 * @param stringToScan
82 */
83 public StringScanner(String stringToScan) {
84 super();
85 length = stringToScan.length();
86 buffer = new char[length];
87 stringToScan.getChars(0, length, buffer, 0);
88 }
89
90 // StringScanner()
91 // =========================================================================
92 // PUBLIC CLASS METHODS
93 // =========================================================================
94
95 /**
96 * Returns true, if the given character indicates that the end of the
97 * scanned string is reached.
98 *
99 * @param character
100 *
101 * @return
102 */
103 public boolean endReached(char character) {
104 return (character == END_REACHED);
105 }
106
107 // endReached()
108 // -------------------------------------------------------------------------
109
110 /**
111 * Returns true, if the given character does <b>not</b> indicate that the
112 * end of the scanned string si reached.
113 *
114 * @param character
115 *
116 * @return
117 */
118 public boolean endNotReached(char character) {
119 return (!endReached(character));
120 }
121
122 // endNotReached()
123 // =========================================================================
124 // PUBLIC INSTANCE METHODS
125 // =========================================================================
126
127 /**
128 * Returns the string the scanner was initialized with
129 *
130 * @return
131 */
132 public String toString() {
133 return new String(buffer);
134 }
135
136 // toString()
137 // -------------------------------------------------------------------------
138
139 /**
140 * Moves the position pointer count characters. positive values move
141 * forwards, negative backwards. The position never becomes negative !
142 *
143 * @param count
144 */
145 public void skip(int count) {
146 position += count;
147
148 if (position < 0) {
149 position = 0;
150 }
151 }
152
153 // skip()
154 // -------------------------------------------------------------------------
155
156 /**
157 * Returns the character at the current position without changing the
158 * position, that is subsequent calls to this method return always the
159 * same character.
160 *
161 * @return
162 */
163 public char peek() {
164 return ((position < length()) ? buffer[position] : END_REACHED);
165 }
166
167 // skip()
168 // -------------------------------------------------------------------------
169
170 /**
171 * Returns the character at the current position and increments the
172 * position afterwards by 1.
173 *
174 * @return
175 */
176 public char nextChar() {
177 char next = this.peek();
178
179 if (endNotReached(next)) {
180 this.skip(1);
181 }
182
183 return next;
184 }
185
186 // nextChar()
187 // -------------------------------------------------------------------------
188
189 /**
190 * Returns true, if the scanner has reached the end and a further
191 * invocation of nextChar() would return the END_REACHED character.
192 *
193 * @return
194 */
195 public boolean atEnd() {
196 return (endReached(this.peek()));
197 }
198
199 // atEnd()
200 // -------------------------------------------------------------------------
201
202 /**
203 * Returns true, if the scanner has not yet reached the end.
204 *
205 * @return
206 */
207 public boolean hasNext() {
208 return !this.atEnd();
209 }
210
211 // hasNext()
212 // -------------------------------------------------------------------------
213
214 /**
215 * Returns the next character that is no whitespace and leaves the position
216 * pointer one character after the returned one.
217 *
218 * @return
219 */
220 public char nextNoneWhitespaceChar() {
221 char next = this.nextChar();
222
223 while ((endNotReached(next)) && (Character.isWhitespace(next))) {
224 next = this.nextChar();
225 }
226
227 return next;
228 }
229
230 // nextNoneWhitespaceChar()
231 // -------------------------------------------------------------------------
232
233 /**
234 * Returns the current position in the string
235 *
236 * @return
237 */
238 public int getPosition() {
239 return position;
240 }
241
242 // getPosition()
243 // -------------------------------------------------------------------------
244
245 /**
246 * Remembers the current position for later use with restorePosition()
247 */
248 public void markPosition() {
249 pos_marker = position;
250 }
251
252 // markPosition()
253 // -------------------------------------------------------------------------
254
255 /**
256 * Restores the position to the value of the latest markPosition() call
257 */
258 public void restorePosition() {
259 this.setPosition(pos_marker);
260 }
261
262 // restorePosition()
263
264 /**
265 *
266 *
267 * @return
268 */
269 protected int length() {
270 return length;
271 }
272
273 // length()
274 // -------------------------------------------------------------------------
275 protected void setPosition(int pos) {
276 if ((pos >= 0) && (pos <= this.length())) {
277 position = pos;
278 }
279 }
280
281 // setPosition()
282 // -------------------------------------------------------------------------
283 }
284
285
286 // class StringScanner