| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.io.UnsupportedEncodingException; |
| 6 |
|
| 7 |
import fileIO.ReadWrite; |
| 8 |
import fileIO.TextFile; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import tracker.ReadStats; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class UnicodeToAscii { |
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
|
| 27 |
|
| 28 |
Timer t=new Timer(); |
| 29 |
UnicodeToAscii x=new UnicodeToAscii(args); |
| 30 |
x.process(t); |
| 31 |
|
| 32 |
|
| 33 |
Shared.closeStream(x.outstream); |
| 34 |
} |
| 35 |
|
| 36 |
public UnicodeToAscii(String[] args){ |
| 37 |
|
| 38 |
{ |
| 39 |
PreParser pp=new PreParser(args, getClass(), false); |
| 40 |
args=pp.args; |
| 41 |
outstream=pp.outstream; |
| 42 |
} |
| 43 |
|
| 44 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 45 |
ReadWrite.setZipThreads(Shared.threads()); |
| 46 |
|
| 47 |
Parser parser=new Parser(); |
| 48 |
for(int i=0; i<args.length; i++){ |
| 49 |
String arg=args[i]; |
| 50 |
String[] split=arg.split("="); |
| 51 |
String a=split[0].toLowerCase(); |
| 52 |
String b=split.length>1 ? split[1] : null; |
| 53 |
|
| 54 |
if(parser.parse(arg, a, b)){ |
| 55 |
|
| 56 |
}else if(a.equals("null")){ |
| 57 |
|
| 58 |
}else if(a.equals("verbose")){ |
| 59 |
verbose=Parse.parseBoolean(b); |
| 60 |
}else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 61 |
parser.in1=arg; |
| 62 |
}else if(parser.out1==null && i==1 && !arg.contains("=")){ |
| 63 |
parser.out1=arg; |
| 64 |
}else{ |
| 65 |
outstream.println("Unknown parameter "+args[i]); |
| 66 |
assert(false) : "Unknown parameter "+args[i]; |
| 67 |
|
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
in1=parser.in1; |
| 73 |
in2=parser.in2; |
| 74 |
|
| 75 |
out1=parser.out1; |
| 76 |
out2=parser.out2; |
| 77 |
|
| 78 |
overwrite=parser.overwrite; |
| 79 |
append=parser.append; |
| 80 |
} |
| 81 |
|
| 82 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 83 |
in2=in1.replace("#", "2"); |
| 84 |
in1=in1.replace("#", "1"); |
| 85 |
} |
| 86 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 87 |
out2=out1.replace("#", "2"); |
| 88 |
out1=out1.replace("#", "1"); |
| 89 |
} |
| 90 |
|
| 91 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 92 |
|
| 93 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 94 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 95 |
|
| 96 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 97 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 98 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 99 |
} |
| 100 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2) || !ReadStats.testFiles(false)){ |
| 101 |
throw new RuntimeException("Duplicate filenames are not allowed."); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private void process(Timer t){ |
| 106 |
|
| 107 |
if(in1!=null && out1!=null){process(in1, out1);} |
| 108 |
if(in2!=null && out2!=null){process(in2, out2);} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
private void process(String infile, String outfile){ |
| 113 |
TextFile tf=new TextFile(infile, true); |
| 114 |
TextStreamWriter tsw=new TextStreamWriter(outfile, overwrite, append, true); |
| 115 |
tsw.start(); |
| 116 |
for(String line=tf.readLine(false); line!=null; line=tf.readLine(false)){ |
| 117 |
String line2=line; |
| 118 |
try { |
| 119 |
line2=new String(line.getBytes(), "UTF-8"); |
| 120 |
} catch (UnsupportedEncodingException e) { |
| 121 |
try { |
| 122 |
line2=new String(line.getBytes(), "UTF-16"); |
| 123 |
} catch (UnsupportedEncodingException e1) {} |
| 124 |
} |
| 125 |
tsw.println(Tools.fixHeader(line2, false, true)); |
| 126 |
|
| 127 |
} |
| 128 |
tf.close(); |
| 129 |
tsw.poisonAndWait(); |
| 130 |
} |
| 131 |
|
| 132 |
private PrintStream outstream=System.err; |
| 133 |
|
| 134 |
private String in1, in2; |
| 135 |
private String out1, out2; |
| 136 |
@SuppressWarnings("unused") |
| 137 |
private boolean verbose=false; |
| 138 |
private boolean overwrite=true; |
| 139 |
private boolean append=false; |
| 140 |
|
| 141 |
} |