01: /*
02: * ShellTokenizer.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: ShellTokenizer.java,v 1.1.1.1 2002/09/24 16:08:09 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.util.Vector;
25:
26: public final class ShellTokenizer {
27: private Vector v = new Vector();
28: private int index;
29:
30: public ShellTokenizer(String s) {
31: FastStringBuffer sb = new FastStringBuffer();
32: char quoteChar = 0;
33: int limit = s.length();
34: for (int i = 0; i < limit; i++) {
35: char c = s.charAt(i);
36: if (c == '\\') {
37: sb.append(c);
38: if (i < limit - 1) {
39: sb.append(s.charAt(++i));
40: continue;
41: }
42: } else if (quoteChar != 0) {
43: sb.append(c);
44: if (c == quoteChar)
45: quoteChar = 0;
46: } else if (c == '\'' || c == '"') {
47: sb.append(c);
48: quoteChar = c;
49: } else if (c == ' ' || c == '\t' || c == '=') {
50: if (sb.length() > 0) {
51: v.add(sb.toString());
52: sb.setLength(0);
53: }
54: } else
55: sb.append(c);
56: }
57: if (sb.length() > 0)
58: v.add(sb.toString());
59: }
60:
61: public boolean hasMoreTokens() {
62: return index < v.size();
63: }
64:
65: public String nextToken() {
66: String token = null;
67: if (index < v.size())
68: token = (String) v.get(index++);
69: return token;
70: }
71:
72: public String lastToken() {
73: String token = null;
74: if (v.size() > 0)
75: token = (String) v.get(v.size() - 1);
76: return token;
77: }
78: }
|