
package parse;
import java.io.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author Sungwook Moon
 */
public class ipaq_parser {

    private static class parse_trace
    {
        String process_mac(String input)
        {        
            String result = "", temp = "";
            StringTokenizer word2 = new StringTokenizer(input);

            for (int bb=0; bb<5; bb++)
            {
                temp = word2.nextToken(": ");
                if (temp.length() < 2) result += "0" + temp+":";
                else result += temp+":";
            }
            temp = word2.nextToken(": ");
            if (temp.length() < 2) result += "0" + temp;
            else result += temp;
            
            return result;
        }
        
        boolean isMAC(String input)
        {
            StringTokenizer word = new StringTokenizer(input, ":");
            if (word.countTokens() < 3) {System.out.println(word.countTokens()+" "+input);return false;}
            return true;            
        }
        public parse_trace()
        {
            int date_drift = 0, min_drift = 0;
            int hour_drift = 3;
            
            // ipaq 8 time drift
            
            date_drift = 1284;
            hour_drift = 2;
            min_drift = 55;
             
            //////////////////////
            
            // ipaq 3 time drift
            /*
            date_drift = 1285;
            hour_drift = 3;
            min_drift = 15;
            */
            //////////////////////
            
            String input_directory = "c:/bt_trace/";
            String output_directory = "c:/bt_trace_output/";
            String str, word1, word2, word3;
            String name, mac;
            String [] fileName;
            //String year, month, date, hour,min,sec;
            int year, month, date, hour, min, sec; 
            int total_sec = 0;

            try {
                File directory = new File(input_directory);                    
                fileName = directory.list();        
                for (int aa=0; aa<fileName.length; aa++)
                {

                    FileInputStream readfile = new FileInputStream(input_directory+""+fileName[aa]);  
                    BufferedReader in = new BufferedReader(new InputStreamReader(readfile));
                    FileOutputStream output = new FileOutputStream(output_directory+""+fileName[aa]);

                    str = in.readLine();
                    while (str != null)            
                    {
                        StringTokenizer word = new StringTokenizer(str);
                        total_sec = 0;
                        
                        // Time string
                        word1 = word.nextToken("/");
                        word2 = word.nextToken("/");
                        word3 = word.nextToken("/ ");
                        word3 = "20"+word3;
                        month = Integer.parseInt(word1)-1;
                        date = Integer.parseInt(word2);
                        year = Integer.parseInt(word3);

                        word1 = word.nextToken(" :");
                        word2 = word.nextToken(":");
                        word3 = word.nextToken(": ");
                        hour = Integer.parseInt(word1);
                        min = Integer.parseInt(word2);
                        sec = Integer.parseInt(word3);
                        
                        // name and mac string
                        name = word.nextToken("[");
                        name = name.substring(1);
                        
                        mac = word.nextToken("[]");
                        while (isMAC(mac)== false) 
                        {
                            mac = word.nextToken("[]");
                        }
                        
                        // take care of MAC issue e.g. (0:d:45:32:32:55) => (00:0d:45:32:32:55)
                        mac = process_mac(mac);
                        
                        // time string conversion
                        Calendar ca = new java.util.GregorianCalendar();
                        ca.clear();                        
                        ca.set(year,month,date,hour,min,sec);

                        total_sec = (int)(ca.getTimeInMillis()/1000);
                        
                        // taking care of time drift or time gap
                        total_sec += (date_drift*3600*24);
                        total_sec += (hour_drift*3600);
                        total_sec += (min_drift*60);
                        
                        ////////////////////////////////////////

                        output.write((total_sec+",").getBytes());
                        output.write((mac+",").getBytes());
                        output.write((name).getBytes());
                        output.write(("\r\n").getBytes());
                        str = in.readLine();
                    }
                    in.close();
                    output.close();
                    readfile.close();
                }
            }catch(IOException e) {System.err.print(e);}

        }
    }
    
    public static void main(String[] args) {
        
        parse_trace pt = new parse_trace();
    
    }
}


