📄 util.java
字号:
/*
* Created on 2004-7-14
*
*/
package edu.berkeley.cs164.util;
/**
* @author Lexin Shan, CS164, University of California - Berkeley
*
* Utilities for printing debugging output, compiler errors and warning. Use
* these instead of System.out and System.err, which your code should never call
* directly, to enable test coverage measurements.
*/
public class Util {
private static String[] args;
/** Debug flags specified on the command line. */
private static boolean debugFlags[];
/** Has processArgs been called from the compiler code? */
private static boolean processArgsCalled;
/**
* The first non-debug-flag command line argument
*/
private static String inputFileName = "";
/**
* The second non-debug-flag command line argument
*/
private static String outputFileName = "";
/**
* Note: You must call this method from your main method. Process the
* arguments. Identify the debug flags if they present.
*
* @param args
*/
public static void processArgs(String[] args) {
processArgsCalled = true;
Util.args = args;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.equals("-d")) {
// process debug flags
i++;
if (i >= args.length) {
errorMsg("Malformed command line arguments (-d must be followed by a string)");
Util.exit();
}
Util.enableDebugFlags(args[i]);
} else {
// process input and output file names
inputFileName = args[i++];
if (i < args.length) {
outputFileName = args[i++];
}
}
}
}
/**
* Enable all the debug flags in <i>flagsString </i>.
*
* @param flagsString
* the flags to enable.
*/
private static void enableDebugFlags(String flagsString) {
if (debugFlags == null)
debugFlags = new boolean[0x80];
char[] newFlags = flagsString.toCharArray();
for (int i = 0; i < newFlags.length; i++) {
char c = newFlags[i];
if (c >= 0 && c < 0x80)
debugFlags[(int) c] = true;
}
}
/**
* Print <i>message </i> if <i>flag </i> was enabled on the command line. To
* specify which flags to enable, use the -d command line option. For
* example, to enable flags a, c, and e, do the following:
*
* <p>
*
* <pre>
*
* java PA1 -d ace
*
* </pre>
*
* <p>
* Our compiler uses several debugging flags already, but you are encouraged
* to add your own.
*
* @param flag
* the debug flag that must be set to print this message.
* @param message
* the debug message.
*/
public static void debug(char flag, String message) {
if (test(flag))
System.out.println(message);
}
/**
* Tests if <i>flag </i> was enabled on the command line.
*
* @param flag
* the debug flag to test.
*
* @return <tt>true</tt> if this flag was enabled on the command line.
*/
private static boolean test(char flag) {
if (!processArgsCalled) {
Util.warningMsg("method Util.processArgs() must be called from main().");
}
if (debugFlags == null)
return false;
else if (debugFlags[(int) '+'])
return true;
else if (flag >= 0 && flag < 0x80 && debugFlags[(int) flag])
return true;
else
return false;
}
/**
* A univeral exit that everybody has to use to end the project's run. The
* reason it's required to call this method is it lets instrumenter
* recognize the exit point of the program and therefore it can insert code
* to output the code coverage result
*
* @param status -
* Termination status. By convention, a nonzero status code
* indicates abnormal termination.
*/
public static void exit() {
/*
* TODO: should we define several several status constants? do we need
* status at all?
*
* i actually tried this before. pythong doesn't seem to pick up the
* exit status number of the process.
*
* TODO: should a failure exit of the compiler print a standard string
* similar to DONE printed in done()?
*
* i expect the students to call done() in any successful exit. thus,
* anything that doesn't print DONE is an abnormal exit. The reason i
* don't want to requir the students to call a standard error exit
* routine is there are too many error exit points whereas there is
* usually only one succesful exit point.
*/
System.exit(1);
}
/**
* Write "DONE" to the console and exit the program.
*
*/
public static void done() {
System.out.println("DONE");
Util.exit();
}
/**
* Print out error message <i>msg </i> in an appropriate format for testing
* purpose
*
* @param msg -
* the message to print out
*
*/
public static void errorMsg(String msg) {
System.out.print("ERROR ");
System.out.println(msg);
}
/**
* Print out a warning message <i>msg </i> in an appropriate format for
* testing purpose
*
* @param msg -
* the message to print out
*
*/
public static void warningMsg(String msg) {
System.out.print("WARNING ");
System.out.println(msg);
}
/**
* @return Returns the inputFileName.
*/
public static String getInputFileName() {
if (!processArgsCalled) {
Util.warningMsg("method Util.processArgs() must be called from main().");
}
if (inputFileName == null) {
errorMsg("Input file name not provided. Set it in Eclipse/Run/Run.../Arguments");
exit();
}
return inputFileName;
}
/**
* @return Returns the outputFileName.
*/
public static String getOutputFileName() {
if (!processArgsCalled) {
Util.warningMsg("method Util.processArgs() must be called from main().");
}
if (inputFileName == null) {
errorMsg("Output file name not provided. Set it in Eclipse/Run/Run.../Arguments");
exit();
}
return outputFileName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -