package sweeney;

  /**
     * A Class to :
	 	 1) remove hl7 escapce characters from a string 
		 2) uudecode it to a target file name
     * Ron Sweeney, ClubPACS Western Michigan 2007
	 * Thanks for all the fish.
	 * Released as "works for me"
	 * Assumes no warranty, either written or implied.		 
  */
	
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import sun.misc.UUDecoder;


public class StripDecode {

  
	 public void decode(String inFile, String outFile) {
         try {
            UUDecoder uudc = new UUDecoder();
            InputStream in = new FileInputStream(inFile);
            OutputStream out = new FileOutputStream(outFile);
            uudc.decodeBuffer(in, out);
            in.close();
            out.close();
         
         } catch (IOException e) {
            System.out.println(e.toString());
         }
      }
	
	 public void strip(String segment, String uufile) {
		 
		
	        int recCount = 0;
		    int fromIndex;
		    int len;
		    boolean stop;
		
		    String[][] CODES = new String[][]
		    {
		        {"\\F\\","|"},
		        {"\\S\\","^"},
		        {"\\T\\","&"},
		        {"\\R\\","~"},
		        {"\\E\\","\\"},
		        {"\\X0D\\","\r"},
		        {"\\X0A\\","\n"}
		    };
		

	        try {

	      	       StringReader fr = new StringReader(segment);
			BufferedReader br = new BufferedReader(fr);

			String gar = new String();
	 while ((gar = br.readLine()) != null) {
		
		StringBuffer content = new StringBuffer(gar);
	           stop = true;
			            char[] ca;
			            for(int i=0;i<content.length();i++) {
			                ca = new char[3];
			                char c = content.charAt(i); 
			                if(c=='\\') {
			                    content.getChars(i,i+3,ca,0);
			                    boolean skip = false;
			                    for(int j=0;j<5;j++) {
			                        if(new String(ca).equals(CODES[j][0])) {
			                            content.replace(i,i+CODES[j][0].length(),CODES[j][1]);
			                            skip = true;
			                            break;
			                        }
			                    }
			                    if(!skip) {
			                        ca = new char[5];
			                        content.getChars(i,i+5,ca,0);
			                        for(int j=5;j<7;j++) {
			                            if(new String(ca).equals(CODES[j][0])) {
			                                content.replace(i,i+CODES[j][0].length(),CODES[j][1]);
			                                break;
			                            }
			                        }
			                    }
			                }
			 
	           }

			          
			        FileWriter fstream = new FileWriter(uufile);
			        BufferedWriter out = new BufferedWriter(fstream);
			        out.write(content.toString());
			        //Close the output stream
			        out.close();
			          
			           
			        
			            
	}

	        } catch (IOException e) {
	        	
	         
	           System.out.println("Uh oh, got an IOException error!");
	           e.printStackTrace();
	        }

		 
	 }
	
   public static void main(String args[]) {
       
       
    	
		
   }


}


