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