The following Java code reads a problem from an LP or MPS file and
writes it in LP or MPS format to another file.
import qs.*;
import java.io.*;
/**
* use as java pp [params] infile outfile
* for paremeters see usage routine
* needs qsopt.jar on classpath
* pp stands for pretty printer
*
* @author Monika Mevenkamp, All Rights Reserved
*/
class pp {
// for defaults see pp() constructor
private String inFile;
private String outFile;
private boolean inMpsFile;
private boolean outMpsFile;
public pp() {
inFile = null;
outFile = null;
inMpsFile = false;
outMpsFile = false;
}
public pp(String av[]) throws QSException {
this();
getargs(av);
}
public static void main(String av[]) {
pp printer = null;
try {
printer = new pp(av);
System.out.println("Pretty Print: " + printer);
Problem prob = Problem.read(printer.inFile, printer.inMpsFile);
if (prob == null) {
throw new QSException("Could not parse problem.");
}
prob.write(printer.outFile, printer.outMpsFile);
} catch (QSException e) {
System.err.println(e.toString());
} catch (IOException e) {
System.err.println("Could not access file\n");
System.err.println(e);
}
}
public static void usage() throws QSException {
String msg;
msg = "Usage: java pp [- below -] in out\n";
msg += " -m input file is in MPS format " +
"(default: LP format)\n";
msg += " -M output file is written in MPS format " +
"(default: LP format)\n";
msg += "\n";
msg += " in input file name\n";
msg += " out output file name\n";
throw new QSException(msg);
}
public void getargs(String av[]) throws QSException {
try {
int i = 0;
while (av[i].charAt(0) == '-') {
switch (av[i].charAt(1)) {
case 'm' :
inMpsFile = true;
break;
case 'M' :
outMpsFile = true;
break;
default :
usage();
}
i++;
}
inFile = av[i];
outFile = av[i + 1];
if ((inFile == null) || (outFile == null)) {
usage();
}
} catch (ArrayIndexOutOfBoundsException e) {
usage();
}
}
public String toString() {
String s = "File \"" + inFile + "\" -> \"" + outFile + "\"";
s += ((inMpsFile) ? "MPS" : "LP") + " -> " +
((outMpsFile) ? "MPS" : "LP");
return s;
}
}
|