-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
80 lines (67 loc) · 2.61 KB
/
Utils.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 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.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
/** Miscellaneous utilties.
* @author P. N. Hilfinger */
class Utils {
/** Return the integer value denoted by NUMERAL. Raises an exception for
* numbers in the wrong format. */
static int toInt(String numeral) {
return Integer.parseInt(numeral);
}
/** Return the long value denoted by NUMERAL. Raises an exception for
* numbers in the wrong format. */
static long toLong(String numeral) {
return Long.parseLong(numeral);
}
/** Set the message level for this package to LEVEL. The debug() routine
* (below) will print any message with a positive level that is <= LEVEL.
* Initially, the level is 0. */
public static void setMessageLevel(int level) {
_messageLevel = level;
}
/** Returns the current message level, as set by setMessageLevel. */
public static int getMessageLevel() {
return _messageLevel;
}
/** Print a message on the standard error if LEVEL is positive and <= the
* current message level. FORMAT and ARGS are as for the .printf
* methods. */
public static void debug(int level, String format, Object... args) {
if (level > 0 && level <= _messageLevel) {
System.err.printf(format, args);
System.err.println();
}
}
/** Print the contents of the resource named NAME on OUT.
* NAME will typically be a file name based in one of the directories
* in the class path. */
static void printHelpResource(String name, PrintStream out) {
try {
InputStream resource =
Utils.class.getClassLoader().getResourceAsStream(name);
BufferedReader str =
new BufferedReader(new InputStreamReader(resource));
for (String s = str.readLine(); s != null; s = str.readLine()) {
out.println(s);
}
str.close();
out.flush();
} catch (IOException excp) {
out.printf("No help found.");
out.flush();
}
}
/** Shorthand: return String.format(FORMAT, ARGS). */
static String fmt(String format, Object... args) {
return String.format(format, args);
}
/** The current package-wide message level. */
private static int _messageLevel = 0;
}