| 1 |
package template; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteFile1; |
| 7 |
import fileIO.ByteFile2; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import structures.ByteBuilder; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class A_SampleBasic { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
|
| 38 |
Timer t=new Timer(); |
| 39 |
|
| 40 |
|
| 41 |
A_SampleBasic x=new A_SampleBasic(args); |
| 42 |
|
| 43 |
|
| 44 |
x.process(t); |
| 45 |
|
| 46 |
|
| 47 |
Shared.closeStream(x.outstream); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public A_SampleBasic(String[] args){ |
| 57 |
|
| 58 |
{ |
| 59 |
PreParser pp=new PreParser(args, null, false); |
| 60 |
args=pp.args; |
| 61 |
outstream=pp.outstream; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
{ |
| 67 |
final Parser parser=parse(args); |
| 68 |
parser.out1="stdout.txt"; |
| 69 |
overwrite=parser.overwrite; |
| 70 |
append=parser.append; |
| 71 |
|
| 72 |
in=parser.in1; |
| 73 |
|
| 74 |
out=parser.out1; |
| 75 |
} |
| 76 |
|
| 77 |
fixExtensions(); |
| 78 |
checkFileExistence(); |
| 79 |
checkStatics(); |
| 80 |
|
| 81 |
ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false); |
| 82 |
ffin=FileFormat.testInput(in, FileFormat.TXT, null, true, true); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private Parser parse(String[] args){ |
| 91 |
|
| 92 |
Parser parser=new Parser(); |
| 93 |
for(int i=0; i<args.length; i++){ |
| 94 |
String arg=args[i]; |
| 95 |
String[] split=arg.split("="); |
| 96 |
String a=split[0].toLowerCase(); |
| 97 |
String b=split.length>1 ? split[1] : null; |
| 98 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 99 |
|
| 100 |
if(a.equals("verbose")){ |
| 101 |
verbose=Parse.parseBoolean(b); |
| 102 |
ByteFile1.verbose=verbose; |
| 103 |
ByteFile2.verbose=verbose; |
| 104 |
ReadWrite.verbose=verbose; |
| 105 |
}else if(parser.parse(arg, a, b)){ |
| 106 |
|
| 107 |
}else{ |
| 108 |
outstream.println("Unknown parameter "+args[i]); |
| 109 |
assert(false) : "Unknown parameter "+args[i]; |
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return parser; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
private void fixExtensions(){ |
| 119 |
in=Tools.fixExtension(in); |
| 120 |
if(in==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
private void checkFileExistence(){ |
| 125 |
|
| 126 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 127 |
outstream.println((out==null)+", "+out); |
| 128 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
if(!Tools.testInputFiles(false, true, in)){ |
| 133 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if(!Tools.testForDuplicateFiles(true, in, out)){ |
| 138 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
private static void checkStatics(){ |
| 144 |
|
| 145 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 146 |
ByteFile.FORCE_MODE_BF2=true; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
void process(Timer t){ |
| 155 |
|
| 156 |
ByteFile bf=ByteFile.makeByteFile(ffin); |
| 157 |
ByteStreamWriter bsw=makeBSW(ffout); |
| 158 |
|
| 159 |
|
| 160 |
if(bsw!=null){ |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
processInner(bf, bsw); |
| 165 |
|
| 166 |
errorState|=bf.close(); |
| 167 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 168 |
|
| 169 |
t.stop(); |
| 170 |
|
| 171 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 172 |
|
| 173 |
outstream.println(); |
| 174 |
outstream.println("Valid Lines: \t"+linesOut); |
| 175 |
outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut)); |
| 176 |
|
| 177 |
if(errorState){ |
| 178 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private void processInner(ByteFile bf, ByteStreamWriter bsw){ |
| 187 |
byte[] line=bf.nextLine(); |
| 188 |
ByteBuilder bb=new ByteBuilder(); |
| 189 |
|
| 190 |
while(line!=null){ |
| 191 |
if(line.length>0){ |
| 192 |
linesProcessed++; |
| 193 |
bytesProcessed+=(line.length+1); |
| 194 |
|
| 195 |
if(true){ |
| 196 |
for(int i=0; i<line.length && line[i]!='\t'; i++){ |
| 197 |
bb.append(line[i]); |
| 198 |
} |
| 199 |
bb.nl(); |
| 200 |
linesOut++; |
| 201 |
bytesOut+=bb.length(); |
| 202 |
bsw.print(bb.toBytes()); |
| 203 |
bb.clear(); |
| 204 |
} |
| 205 |
} |
| 206 |
line=bf.nextLine(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 211 |
if(ff==null){return null;} |
| 212 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 213 |
bsw.start(); |
| 214 |
return bsw; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private String in=null; |
| 222 |
private String out=null; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private long linesProcessed=0; |
| 227 |
private long linesOut=0; |
| 228 |
private long bytesProcessed=0; |
| 229 |
private long bytesOut=0; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private final FileFormat ffin; |
| 236 |
private final FileFormat ffout; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private PrintStream outstream=System.err; |
| 243 |
public static boolean verbose=false; |
| 244 |
public boolean errorState=false; |
| 245 |
private boolean overwrite=true; |
| 246 |
private boolean append=false; |
| 247 |
|
| 248 |
} |