cb0768c74dc861cdc8c6bf7dfe85162ceeebee4f
[repair.git] / Repair / RepairCompiler / Daikon / Process.java
1 import java.io.*;
2 import java.util.*;
3
4 class Process {
5   static HashSet set=null;
6   static Hashtable currtable=new Hashtable();
7   static String declaration;
8
9   static void debug(String str) {
10     System.out.println(str);
11   }
12
13   static public void main(String[] args) {
14     debug("Opening file:"+args[0]);
15     BufferedReader br=null;
16     BufferedWriter bw=null;
17     try {
18       br=new BufferedReader(new FileReader(args[0]));
19       int count=0;
20       boolean start=false;
21       while(true) {
22         String line=br.readLine();
23         if (line==null)
24           break;
25         String replacewith=line+".elem[";
26         if (replacewith==null)
27           break;
28         currtable.put(line+"[",replacewith);
29       }
30
31       /* Built table */
32       br=new BufferedReader(new FileReader(args[1]));
33       bw=new BufferedWriter(new FileWriter("fixed-"+args[1]));
34       count=0;
35       start=false;
36       while(true) {
37         String line=br.readLine();
38         if (line==null)
39           break;
40         if (line.equals("")) {
41           bw.newLine();
42           continue;
43         }
44         if (line.equals("DECLARE")) {
45           bw.write(line);
46           bw.newLine();
47           start=true;
48           count=0;
49           declaration=br.readLine();
50           bw.write(declaration);
51           bw.newLine();
52           continue;
53         }
54         if (start) {
55           if ((count%4)==0) {
56             for(Iterator it=currtable.keySet().iterator();it.hasNext();) {
57               String str=(String)it.next();
58               String replace=(String)currtable.get(str);
59               line=replace(line,str,replace);
60             }
61           }
62           bw.write(line);
63           bw.newLine();
64           count++;
65         } else {
66           bw.write(line);
67           bw.newLine();
68         }
69       }
70       bw.close();
71
72
73       /* Built table */
74       br=new BufferedReader(new FileReader(args[2]));
75       bw=new BufferedWriter(new FileWriter("fixed-"+args[2]));
76       count=0;
77       start=false;
78       while(true) {
79         String line=br.readLine();
80         if (line==null)
81           break;
82         if (line.equals("")) {
83           bw.write(line);
84           bw.newLine();
85           start=true;
86           count=0;
87           declaration=br.readLine();
88           bw.write(declaration);
89           bw.newLine();
90           continue;
91         }
92         if (start) {
93           if ((count%3)==0) {
94             for(Iterator it=currtable.keySet().iterator();it.hasNext();) {
95               String str=(String)it.next();
96               String replace=(String)currtable.get(str);
97               line=replace(line,str,replace);
98             }
99           }
100           bw.write(line);
101           bw.newLine();
102           count++;
103         } else {
104           bw.write(line);
105           bw.newLine();
106         }
107       }
108       bw.close();
109
110     } catch (Exception e) {
111       e.printStackTrace();
112       System.exit(-1);
113     }
114   }
115
116   static String generatepostfix(String str, int numderefs) {
117     if (numderefs==0)
118       return "";
119     int start=str.length();
120     for(int i=0;i<numderefs;i++) {
121       start=str.lastIndexOf("[",start-1);
122     }
123     if (start==-1)
124       throw new Error();
125     return str.substring(start);
126   }
127
128
129   static String generateprefix(String str,int numdots) {
130     int offset=0;
131     for(int i=0;i<numdots;i++) {
132       offset=str.indexOf(".",offset)+1;
133     }
134     int nextdot=str.indexOf(".",offset);
135     if (nextdot==-1) {
136       nextdot=str.indexOf("[",offset);
137       if (nextdot==-1)
138         nextdot=str.length();
139     }
140     return str.substring(0,nextdot);
141   }
142
143   static String nextfield(String str,int numdots) {
144     int offset=0;
145     for(int i=0;i<=numdots;i++) {
146       offset=str.indexOf(".",offset)+1;
147       if (offset==0)
148         return "";
149     }
150     int nextdot=str.indexOf(".",offset);
151     if (nextdot==-1) {
152       nextdot=str.indexOf("[",offset);
153       if (nextdot==-1)
154         nextdot=str.length();
155     }
156     return "."+str.substring(offset,nextdot);
157   }
158
159   static int count(String str, String tofind) {
160     int offset=0;
161     int total=0;
162     while(true) {
163       int newoffset=str.indexOf(tofind,offset);
164       if (newoffset==-1)
165         break;
166       total++;
167       offset=newoffset+1;
168     }
169     return total;
170   }
171
172   static String replace(String str, String tofind, String toreplace) {
173     if (str.indexOf(tofind)!=-1) {
174       str=str.substring(0,str.indexOf(tofind))+str.substring(str.indexOf(tofind)+tofind.length());
175     }
176     return str;
177   }
178 }