/* *This is a basic program which, as of now, only switches two strings in text files. *I HOPE that this will develop into a program which can create .wf files as well, *allowing users to specify "waypoints" to travel through and the speed at which to travel through them, *as well as the type of path (circular, linear) on which to travel. * *PLAN: write waypoints and speed/direction information in a .wfeasy file, read it in the input, make the *transitions, and output to a .wf file. * *11/7/09 John Hoffman * */ import java.io.*; import java.util.Scanner; import java.util.ArrayList; import java.util.Iterator; public class speckedit { private static Scanner br = new Scanner(System.in); private static String appendFileName = null; private static boolean appendyn = false; public static void main(String[] args) { System.out.println("What would you like to do?"); System.out.println("1) Create a .wf file in this primitive wizard interface"); System.out.println("2) Modify a word in a massive speck file"); while(true) { System.out.print("So what will it be? 1 or 2? "); String choice = br.nextLine(); if(choice.equals("1")) wfcreator(); else if(choice.equals("2")) speckedit(); else if(choice.equals("exit")) System.exit(0); else System.out.println("Invalid input!!!"); } } public static void speckedit() { System.out.print("Enter Filename: "); String filename = ""; filename = br.nextLine(); if(filename.equals("p")) filename = "proton100G310d.speck"; System.out.println(filename); Scanner speckFile = null; try { speckFile = new Scanner(new File("/CosmicRaySim/ManyShowers/data/"+filename)); } catch (FileNotFoundException e) { System.out.println("No such file"); System.exit(1); } System.out.print("Enter the old string: "); String oldStr = br.nextLine(); System.out.print("Enter the new string: "); String newStr = br.nextLine(); System.out.println("Switching "+oldStr+" to "+newStr+" in "+filename+"..."); try { BufferedWriter out = new BufferedWriter(new FileWriter("/CosmicRaySim/ManyShowers/data/protonedited.speck")); while(speckFile.hasNext()) { out.write(converted(speckFile.nextLine(), oldStr, newStr)); out.newLine(); } out.close(); System.out.println("OK, I think we're finished."); } catch (IOException e) { System.out.println("Something bad happened during the filewrite."); } } public static String converted(String line, String old, String news) { return line.replaceAll(old, news); } public static boolean isinteger(String x) { try { int s = Integer.parseInt(x); } catch (NumberFormatException nfe) { return false; } return true; } public static void wfcreator() { ArrayList waypoints = new ArrayList(); ArrayList numframes = new ArrayList(); System.out.println("++++++++++WF CREATOR++++++++++"); System.out.print("Open .wf file?(Y/N): "); String openyn = br.nextLine(); if (openyn.equals("Y") || openyn.equals("y")) { appendyn = true; System.out.print("Which file? (Include file extension): "); appendFileName = br.nextLine(); } System.out.println("Enter waypoint/location data as: fx fy fz rx ry rz fov"); System.out.println("To stop, enter 'exit'."); while(!appendyn) { System.out.print("Enter your starting location: "); String inp = br.nextLine(); if(!invalid(inp)) { waypoints.add(inp); break; } else { System.out.println("Incorrect format."); } } if(appendyn) { try { File wfapp = new File("/Volumes/UDISK/ManyShowers/data/"+appendFileName); Scanner wfread = new Scanner(wfapp); String string1 = null; while(wfread.hasNextLine()) { string1 = wfread.nextLine(); } if(!invalid(string1)) { waypoints.add(string1); } } catch(Exception e) { System.out.println(e.toString()+"<--Can't get last line of .wf file..."); System.exit(0); } } System.out.println("Begin at "+waypoints.get(0)); while(true) { System.out.print("Next waypoint: "); String inp = br.nextLine(); if(inp.equals("exit")) break; else if(invalid(inp)) System.out.println("Incorrect format."); else waypoints.add(inp); while(true) { System.out.print("In how many steps?: "); String inp2 = br.nextLine(); if(isinteger(inp2)) { System.out.println("Adding "+Integer.parseInt(inp2)); numframes.add(Integer.parseInt(inp2)); break; } else System.out.println("not an integer"); } } System.out.println(waypoints); System.out.println("Translating waypoints..."); if(!translate(waypoints, numframes)) { System.out.print("Translation failed."); System.exit(1); } /*try { BufferedWriter out = new BufferedWriter(new FileWriter("/CosmicRaySim/ManyShowers/data/protonedited.wf")); System.out.println("OK, I think we're finished."); } catch (IOException e) { System.out.println("Something bad happened during the filewrite."); }*/ } public static boolean translate(ArrayList untran, ArrayList num) { ArrayList tx = new ArrayList(); ArrayList ty = new ArrayList(); ArrayList tz = new ArrayList(); ArrayList rx = new ArrayList(); ArrayList ry = new ArrayList(); ArrayList rz = new ArrayList(); ArrayList fov = new ArrayList(); Iterator untranit = untran.iterator(); while(untranit.hasNext()) { int count = 0; Scanner linescan = new Scanner(untranit.next()); while(linescan.hasNext()) { double addVal = Double.parseDouble(linescan.next()); switch (count) { case 0: tx.add(addVal); break; case 1: ty.add(addVal); break; case 2: tz.add(addVal); break; case 3: rx.add(addVal); break; case 4: ry.add(addVal); break; case 5: rz.add(addVal); break; case 6: fov.add(addVal); break; default: System.out.println("Switch statement failure in translate function, c = "+count); return false; } count++; } } System.out.println(tx+", "+ty+", "+tz+", "+rx+", "+ry+", "+rz+", "+fov); System.out.println("Adding transition frames..."); if(!transition(tx, ty, tz, rx, ry, rz, fov, num)) { System.out.println("TRANSITION FAILED"); } return true; } public static boolean transition(ArrayList otx, ArrayList oty, ArrayList otz, ArrayList orx, ArrayList ory, ArrayList orz, ArrayList ofov, ArrayList numsteps) { ArrayList finaltx = new ArrayList(); ArrayList finalty = new ArrayList(); ArrayList finaltz = new ArrayList(); ArrayList finalrx = new ArrayList(); ArrayList finalry = new ArrayList(); ArrayList finalrz = new ArrayList(); ArrayList finalfov = new ArrayList(); for(int c = 0; c writetx, ArrayList writety, ArrayList writetz, ArrayList writerx, ArrayList writery, ArrayList writerz, ArrayList writefov) { String givenName = null; if(!appendyn) { System.out.print("Name the .wf file (include extension): "); givenName = br.nextLine(); } String autostartfile = "nothing"; System.out.print("Automatically start in a file? Y/N: "); String autostart = br.nextLine(); boolean autostartBOOL = false; if(autostart.equals("y") || autostart.equals("Y")) { autostartBOOL = true; System.out.print("Which file?(include extension): "); autostartfile = br.nextLine(); } try { if(!appendyn) { BufferedWriter out = new BufferedWriter(new FileWriter("/Volumes/UDISK/ManyShowers/data/"+givenName)); for(int y = 0; y < writetx.size(); y++) { System.out.println("Printing: "+writetx.get(y)+" "+writety.get(y)+" "+writetz.get(y)+" "+writerx.get(y)+" "+writery.get(y)+" "+writerz.get(y)+", "+ writefov.get(y)); out.write(writetx.get(y)+" "+writety.get(y)+" "+writetz.get(y)+" "+writerx.get(y)+" "+writery.get(y)+" "+writerz.get(y)+" "+writefov.get(y)); out.newLine(); } out.close(); System.out.println("No problems during the file-write!"); } else { PrintStream out3 = new PrintStream(new AppendFileStream("/Volumes/UDISK/ManyShowers/data/"+appendFileName)); for(int y = 0; y < writetx.size(); y++) { System.out.println("Appending "+appendFileName+": "+writetx.get(y)+" "+writety.get(y)+" "+writetz.get(y)+" "+writerx.get(y)+" "+writery.get(y)+" "+writerz.get(y)+", "+ writefov.get(y)); out3.println(writetx.get(y)+" "+writety.get(y)+" "+writetz.get(y)+" "+writerx.get(y)+" "+writery.get(y)+" "+writerz.get(y)+" "+writefov.get(y)); } out3.close(); System.out.println("No problems during the file-write!"); } } catch (IOException e) { System.out.println("Something bad happened during the filewrite."); return false; } if(autostartBOOL) { try { PrintStream out2 = new PrintStream(new AppendFileStream("/Volumes/UDISK/ManyShowers/data/"+autostartfile)); if(!appendyn) out2.println("eval readpath "+givenName); else out2.println("eval readpath"+appendFileName); out2.println("eval play"); out2.close(); } catch(Exception e) { System.out.print(e.toString()+" <--I can't add the .wf file to "+autostartfile); } } return true; } } class AppendFileStream extends OutputStream { RandomAccessFile fd; public AppendFileStream(String file) throws IOException { fd = new RandomAccessFile(file,"rw"); fd.seek(fd.length()); } public void close() throws IOException { fd.close(); } public void write(byte[] b) throws IOException { fd.write(b); } public void write(byte[] b,int off,int len) throws IOException { fd.write(b,off,len); } public void write(int b) throws IOException { fd.write(b); } }