-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandSources.java
47 lines (40 loc) · 1.49 KB
/
CommandSources.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Skeleton code copyright (C) 2008, 2022 Paul N. Hilfinger and the
* Regents of the University of California. Do not distribute this or any
* derivative work without permission. */
package ataxx;
import java.util.Stack;
/** Provides command input from a stack of CommandSource objects.
* @author P. N. Hilfinger
*/
class CommandSources implements CommandSource {
/** Read and return a line of input from the input stream,
* removing comments and leading and trailing whitespace,
* and skipping blank lines. Returns null when input exhausted.
* PROMPT suggests a prompt string that might be used, if
* appropriate to the input method. */
public String getCommand(String prompt) {
while (!_inputs.isEmpty()) {
String line;
line = _inputs.peek().getCommand(prompt);
if (line != null) {
if (line.indexOf('#') != -1) {
line = line.substring(0, line.indexOf('#'));
}
line = line.trim();
if (line.length() > 0) {
return line;
}
} else {
_inputs.pop();
}
}
return null;
}
/** Make SOURCE the latest input source from which subsequent input
* will be read. */
void addSource(CommandSource source) {
_inputs.add(source);
}
/** Stack of input sources, most recent on top. */
private Stack<CommandSource> _inputs = new Stack<>();
}