Debian Codegraph @

current/template/A_SampleBasic2.java

raw symbols SCIP
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 * Reads a text file.
21 * Does something.
22 * @author Brian Bushnell
23 * @date April 9, 2020
24 *
25 */
26 public class A_SampleBasic2 {
27
28 /*--------------------------------------------------------------*/
29 /*---------------- Initialization ----------------*/
30 /*--------------------------------------------------------------*/
31
32 /**
33 * Code entrance from the command line.
34 * @param args Command line arguments
35 */
36 public static void main(String[] args){
37 //Start a timer immediately upon code entrance.
38 Timer t=new Timer();
39
40 //Create an instance of this class
41 A_SampleBasic2 x=new A_SampleBasic2(args);
42
43 //Run the object
44 x.process(t);
45
46 //Close the print stream if it was redirected
47 Shared.closeStream(x.outstream);
48 }
49
50 /**
51 * Constructor.
52 * @param args Command line arguments
53 */
54 public A_SampleBasic2(String[] args){
55
56 {//Preparse block for help, config files, and outstream
57 PreParser pp=new PreParser(args, /*getClass()*/null, false);
58 args=pp.args;
59 outstream=pp.outstream;
60 }
61
62 //Set shared static variables prior to parsing
63
64 {//Parse the arguments
65 final Parser parser=parse(args);
66 parser.out1="stdout.txt";
67 overwrite=parser.overwrite;
68 append=parser.append;
69
70 out=parser.out1;
71 }
72
73 checkFileExistence(); //Ensure files can be read and written
74 checkStatics(); //Adjust file-related static fields as needed for this program
75
76 ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false);
77 }
78
79 /*--------------------------------------------------------------*/
80 /*---------------- Initialization Helpers ----------------*/
81 /*--------------------------------------------------------------*/
82
83 /** Parse arguments from the command line */
84 private Parser parse(String[] args){
85
86 Parser parser=new Parser();
87 for(int i=0; i<args.length; i++){
88 String arg=args[i];
89 String[] split=arg.split("=");
90 String a=split[0].toLowerCase();
91 String b=split.length>1 ? split[1] : null;
92 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
93
94 if(a.equals("verbose")){
95 verbose=Parse.parseBoolean(b);
96 ByteFile1.verbose=verbose;
97 ByteFile2.verbose=verbose;
98 ReadWrite.verbose=verbose;
99 }else if(parser.parse(arg, a, b)){
100 //do nothing
101 }else{
102 outstream.println("Unknown parameter "+args[i]);
103 assert(false) : "Unknown parameter "+args[i];
104 // throw new RuntimeException("Unknown parameter "+args[i]);
105 }
106 }
107
108 return parser;
109 }
110
111 /** Ensure files can be read and written */
112 private void checkFileExistence(){
113 //Ensure output files can be written
114 if(!Tools.testOutputFiles(overwrite, append, false, out)){
115 outstream.println((out==null)+", "+out);
116 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n");
117 }
118 }
119
120 /** Adjust file-related static fields as needed for this program */
121 private static void checkStatics(){
122 //Adjust the number of threads for input file reading
123 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
124 ByteFile.FORCE_MODE_BF2=true;
125 }
126 }
127
128 /*--------------------------------------------------------------*/
129 /*---------------- Outer Methods ----------------*/
130 /*--------------------------------------------------------------*/
131
132 void process(Timer t){
133
134 ByteStreamWriter bsw=makeBSW(ffout);
135
136 // assert(false) : "Header goes here.";
137 if(bsw!=null){
138 // assert(false) : "Header goes here.";
139 }
140
141 processInner(bsw);
142
143 if(bsw!=null){errorState|=bsw.poisonAndWait();}
144
145 t.stop();
146
147 outstream.println(Tools.timeLinesBytesProcessed(t, linesOut, bytesOut, 8));
148 outstream.println();
149
150 if(errorState){
151 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
152 }
153 }
154
155 /*--------------------------------------------------------------*/
156 /*---------------- Inner Methods ----------------*/
157 /*--------------------------------------------------------------*/
158
159 private void processInner(ByteStreamWriter bsw){
160 ByteBuilder bb=new ByteBuilder();
161
162 for(long cycle=0; cycle<maxCycles; cycle++){
163 doSomething(bsw, bb, cycle);
164 }
165 }
166
167 private static ByteStreamWriter makeBSW(FileFormat ff){
168 if(ff==null){return null;}
169 ByteStreamWriter bsw=new ByteStreamWriter(ff);
170 bsw.start();
171 return bsw;
172 }
173
174 private boolean doSomething(ByteStreamWriter bsw, ByteBuilder bb, long cycle){
175
176 // if(line.length>0){
177 // linesProcessed++;
178 // bytesProcessed+=(line.length+1);
179 //
180 // if(true){
181 // linesOut++;
182 // bytesOut+=(line.length+1);
183 // for(int i=0; i<line.length && line[i]!='\t'; i++){
184 // bb.append(line[i]);
185 // }
186 // bb.nl();
187 // bsw.print(bb.toBytes());
188 // bb.clear();
189 // }
190 // }
191 // line=bf.nextLine();
192 return true;
193 }
194
195 /*--------------------------------------------------------------*/
196 /*---------------- Fields ----------------*/
197 /*--------------------------------------------------------------*/
198
199 private String out=null;
200 private long maxCycles=100;
201
202 /*--------------------------------------------------------------*/
203
204 private long linesOut=0;
205 private long bytesOut=0;
206
207 /*--------------------------------------------------------------*/
208 /*---------------- Final Fields ----------------*/
209 /*--------------------------------------------------------------*/
210
211 private final FileFormat ffout;
212
213 /*--------------------------------------------------------------*/
214 /*---------------- Common Fields ----------------*/
215 /*--------------------------------------------------------------*/
216
217 private PrintStream outstream=System.err;
218 public static boolean verbose=false;
219 public boolean errorState=false;
220 private boolean overwrite=true;
221 private boolean append=false;
222
223 }