Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: rcsdesign/Merger.java


1   package rcsdesign;
2   
3   import java.util.*;
4   import java.io.*;
5   import rcs.utils.*;
6   
7   
8   class BackupLineData
9   {
10    public String orig_line;
11    public String line_for_compare;
12    public String subsection;
13    public int subsection_number = 0;
14    public int brace_count;
15    public int paren_count;
16  }
17  
18  /*
19   *
20   * Merger
21   *
22   */
23  public class Merger 
24  {
25    Vector backup_lines = null;
26  
27    PrintWriter pos = null;
28  
29    Vector functions_defined = null;
30    Vector classes_defined = null;
31  
32    public static boolean debug_on = false;
33    public double template_version = 0.0;
34    public boolean template_version_found = false;
35    public boolean nml_mode = false;
36    int last_line_matched = -1;
37    int input_line = 0;
38  
39    boolean disabling_merge = false;
40    boolean merge_disabled = false;
41    boolean enabling_merge = false;
42  
43    boolean cpp_mode = false;
44  
45    int input_brace_count = 0;
46    int input_paren_count = 0;
47    
48    boolean blank_line_needed = false;
49    int output_brace_count = 0;
50    int output_paren_count = 0;
51    boolean Makefile_mode = false;
52    String input_subsection = null;
53    public int input_subsection_number = 0;
54  
55    File mergerLogFile = new File("merger.log");
56    FileOutputStream logFOS;
57    PrintWriter logPS;
58    
59    public Merger()
60    {
61      if(debug_on)
62        {
63    System.out.println(" new Merger()");
64    Thread.dumpStack();
65    try
66      {
67        if(mergerLogFile.exists())
68          {
69      mergerLogFile.delete();
70          }
71        logFOS = new FileOutputStream(mergerLogFile);
72        logPS = new PrintWriter(logFOS);
73      }
74    catch(Exception e)
75      {
76        e.printStackTrace();
77      }
78        }
79    }
80  
81    String MergeNMLBufferLines(String new_line, String old_line)
82    {
83      String buffer_name = "";
84      String bufname_pad = "";
85      String buftype = "SHMEM";
86      String buffer_host = "localhost";
87      String bufhost_pad = "";
88      String bufsize_string = "0x2000";
89      String rpc_string = "0x20002000";
90      String buffer_number_string = "1";
91      String max_procs_string = "16";
92      String shmemkey_string = "1";
93      String neut_string = "0";
94      String tcp_string = "TCP=2000";
95      String nml_attach_string = "";
96      
97      input_line = 0;
98      int token_count = 0;
99      StringTokenizer new_line_tokenizer = new StringTokenizer(new_line, " \t");
100     while(new_line_tokenizer.hasMoreTokens())
101       {
102   String token = new_line_tokenizer.nextToken();
103   switch(token_count)
104     {
105     case 0:    // B
106       break;
107       
108     case 1:    // BufferName
109       buffer_name = token;
110       for(int i = buffer_name.length(); i < 16; i++)
111         {
112     bufname_pad += " ";
113         }
114       break;
115       
116     case 2:    // BufferType
117       break;
118 
119     case 3:    // BufferHost
120       buffer_host =token;
121       for(int i = buffer_host.length(); i < 16; i++)
122         {
123     bufhost_pad += " ";
124         }
125       break;
126       
127     case 4:    // bufsize
128       break;
129       
130     case 5:    // nuet?
131       break;
132       
133       
134     case 6:    // rpc_number
135       rpc_string = token;
136       break;
137       
138     case 7:    // buffer number
139       buffer_number_string = token;
140       break;
141       
142     case 8:    // max_procs
143       max_procs_string = token;
144       break;
145 
146     case 9:    // key
147       shmemkey_string = token; 
148       break;
149 
150     case 10:    // TCP=?
151       tcp_string = token;
152       break;
153       
154     default:
155       if(nml_attach_string.indexOf(token) < 0 && !token.equals("disp"))
156         {
157     nml_attach_string += " "+token;
158         }
159       break;
160     }
161   token_count++;
162       }
163       
164     token_count = 0;
165     StringTokenizer old_line_tokenizer = new StringTokenizer(old_line, " \t");
166     while(old_line_tokenizer.hasMoreTokens())
167       {
168   String token = old_line_tokenizer.nextToken();
169   switch(token_count)
170     {
171     case 0:    // B
172       break;
173       
174     case 1:    // BufferName
175       break;
176       
177     case 2:    // BufferType
178       buftype = token;
179       break;
180 
181     case 3:    // BufferHost
182       break;
183       
184     case 4:    // bufsize
185       bufsize_string = token;
186       break;
187       
188     case 5:    // nuet?
189       neut_string = token;
190       break;
191       
192       
193     case 6:    // rpc_number
194       break;
195       
196     case 7:    // buffer number
197       break;
198       
199     case 8:    // max_procs
200       break;
201 
202     case 9:    // key
203       break;
204 
205     case 10:    // TCP=?
206       break;
207       
208     default:
209       if(nml_attach_string.indexOf(token) < 0)
210         {
211     nml_attach_string += " "+token;
212         }
213       break;
214     }
215   token_count++;
216       }    
217     return "B "+buffer_name+bufname_pad+" \t"+buftype+" \t"+buffer_host+bufhost_pad+" \t"+bufsize_string+"\t"+neut_string+"    \t"+rpc_string+" \t"+buffer_number_string+"        \t"+max_procs_string+"     \t"+shmemkey_string+" "+tcp_string+" "+nml_attach_string;
218   }
219 
220   String NMLMerge(String new_line, String old_line)
221   {
222     if(new_line.startsWith("B"))
223       {
224   return MergeNMLBufferLines(new_line,old_line);
225       }
226     return new_line;
227   }
228 
229   String MakeLineMerge(String new_line, String old_line)
230   {
231     if(new_line.startsWith("#"))
232       {
233   if(debug_on)
234     {
235       System.out.println("Merger.MakeLineMerge("+new_line+", "+old_line+") returning new_line ="+new_line);
236     }
237   return new_line;
238       }
239     if(debug_on)
240       {
241   System.out.println("Merger.MakeLineMerge("+new_line+", "+old_line+") returning old_line ="+old_line);
242       }
243     return old_line;
244   }
245   boolean  MakeLineCompare(String new_line, String old_line)
246   {
247     if(new_line.startsWith("#") || new_line.indexOf('=') >= 0 || new_line.indexOf(':') >= 0 ||
248        new_line.startsWith("if") || new_line.startsWith("end"))
249       {
250   boolean retval = new_line.equals(old_line);
251   if(debug_on)
252     {
253       System.out.println("Merger.MakeLineCompare("+new_line+", "+old_line+") returning "+retval);
254     }
255   return retval;
256       }
257     int tokens_compared = 0;
258     boolean all_tokens_found = true;
259     StringTokenizer new_line_tokenizer = new StringTokenizer(new_line," \t\r\n\b\\");
260     while(new_line_tokenizer.hasMoreTokens())
261       {
262   String token = new_line_tokenizer.nextToken();
263   if(debug_on)
264     {
265       System.out.println("token ="+token+", tokens_compared = "+tokens_compared);
266     }
267   if(old_line.indexOf(token) < 0)
268     {
269       all_tokens_found = false;
270       break;
271     }
272   tokens_compared ++;
273       }
274     if(tokens_compared > 0 && all_tokens_found)
275       {
276   boolean retval = true;
277   if(debug_on)
278     {
279       System.out.println("Merger.MakeLineCompare("+new_line+", "+old_line+") returning "+retval);
280     }
281   return retval;
282       }
283     boolean retval = new_line.equals(old_line);
284     if(debug_on)
285       {
286   System.out.println("Merger.MakeLineCompare("+new_line+", "+old_line+") returning "+retval);
287       }
288     return retval;
289   }
290 
291   public boolean FunctionFoundInExistingFile(String funcname)
292   {
293     if(debug_on)
294       {
295   System.out.println("Merger.FunctionFoundInExistingFile("+funcname+") called.");
296       }
297     if(!cpp_mode)
298       {
299   if(debug_on)
300     {
301       System.out.println("cpp_mode=false");
302     }
303   return false;
304       }
305     if(null == functions_defined)
306       {
307   if(debug_on)
308     {
309       System.out.println("functions_defined=null");
310     }
311   return false;
312       }
313     try
314       {
315   for(int i = 0; i < functions_defined.size(); i++)
316     {
317       String tempfuncname = (String) functions_defined.elementAt(i);
318       if(debug_on)
319         {
320     System.out.println(" String tempfuncname = (String) functions_defined.elementAt("+i+"); ="+tempfuncname);
321         }
322       if(tempfuncname.equals(funcname))
323         {
324     return true;
325         }
326     }
327       }
328     catch(Exception e)
329       {
330   e.printStackTrace();
331       }
332     return false;
333   }
334     
335 
336  public boolean ClassFoundInExistingFile(String funcname)
337   {
338     if(debug_on)
339       {
340   System.out.println("Merger.ClassFoundInExistingFile("+funcname+") called.");
341       }
342     if(!cpp_mode)
343       {
344   if(debug_on)
345     {
346       System.out.println("cpp_mode=false");
347     }
348   return false;
349       }
350     if(null == classes_defined)
351       {
352   if(debug_on)
353     {
354       System.out.println("classes_defined=null");
355     }
356   return false;
357       }
358     try
359       {
360   for(int i = 0; i < classes_defined.size(); i++)
361     {
362       String tempfuncname = (String) classes_defined.elementAt(i);
363       if(debug_on)
364         {
365     System.out.println(" String tempfuncname = (String) classs_defined.elementAt("+i+"); ="+tempfuncname);
366         }
367       if(tempfuncname.equals(funcname))
368         {
369     return true;
370         }
371     }
372       }
373     catch(Exception e)
374       {
375   e.printStackTrace();
376       }
377     return false;
378   }
379     
380 
381   boolean  NMLLineCompare(String linea, String lineb)
382   {
383     boolean retval = false;
384     if(debug_on)
385       {
386   System.out.println("Merger.NMLLineCompare:");
387   System.out.println("\t linea = \""+linea+"\";");
388   System.out.println("\t lineb = \""+lineb+"\";");
389       }
390     if(linea.startsWith("#"))
391       {
392   int paren_index = linea.indexOf('(');
393   if(paren_index > 0 && paren_index == lineb.indexOf('('))
394   {
395     linea = linea.substring(0,paren_index);
396     lineb = lineb.substring(0,paren_index);
397     if(debug_on)
398       {
399         System.out.println("Merger.NMLLineCompare:");
400         System.out.println("paren_index="+paren_index);
401         System.out.println("\t linea = \""+linea+"\";");
402         System.out.println("\t lineb = \""+lineb+"\";");
403       }
404   }
405 
406   retval = linea.equals(lineb);
407   if(debug_on)
408     {
409       System.out.println("Merger.NMLLineCompare() returns "+retval);
410     }
411   return retval;
412       }
413     int max_token_count = 2;
414     if(linea.startsWith("P"))
415       {
416   max_token_count = 3;
417       }
418     StringTokenizer tokenizera = new StringTokenizer(linea, " \t");
419     StringTokenizer tokenizerb = new StringTokenizer(lineb, " \t");
420     int token_count = 0;
421     while(tokenizera.hasMoreTokens() && tokenizerb.hasMoreTokens() && token_count < max_token_count)
422       {
423   token_count++;
424   String tokena = tokenizera.nextToken();
425   String tokenb = tokenizerb.nextToken();
426   if(!tokena.equals(tokenb))
427     {
428       retval = false;
429       if(debug_on)
430         {
431     System.out.println("Merger.NMLLineCompare() returns "+retval);
432         }
433       return retval;
434     }
435       }
436     retval = true;
437     if(debug_on)
438       {
439   System.out.println("Merger.NMLLineCompare() returns "+retval);
440       }
441     return retval;
442   }
443     
444   public void Finish()
445   {
446     try
447       {
448   if(blank_line_needed)
449     {
450       FinalWriteLine("");
451     }
452   if(!merge_disabled && null != backup_lines && !Makefile_mode)
453     {
454       for(int i = last_line_matched+1; i < backup_lines.size(); i++)
455         {
456     BackupLineData bld = (BackupLineData) backup_lines.elementAt(i);
457     if(debug_on && null != logPS)
458       {
459         logPS.println("<"+i+">"+ bld.orig_line);
460       }
461     FinalWriteLine(bld.orig_line);
462         }
463     }
464   if(cpp_mode)
465     {
466       if(output_brace_count != 0)
467         {
468     pos.println("// There are "+output_brace_count+" more open braces than close braces.");
469         }
470       if(output_paren_count != 0)
471         {
472     pos.println("// There are "+output_paren_count+" more open braces than close braces.");
473         }
474       input_brace_count = 0;
475       input_paren_count = 0;
476       output_brace_count = 0;
477       output_paren_count = 0;
478     }
479   if(debug_on)
480     {
481       logPS.close();
482       logPS = null;
483       logFOS.close();
484       logFOS = null;
485     }
486       }
487     catch(Exception e)
488       {
489   e.printStackTrace();
490       }
491   }
492   public void SetPrintWriter(PrintWriter ps)
493   {
494     pos = ps;
495     input_brace_count = 0;
496     input_paren_count = 0;
497     output_brace_count = 0;
498     output_paren_count = 0;
499   }
500 
501   public void GetBackupData(File backup_file)
502   {
503     try
504       {
505   inside_c_comment = false;
506   template_version_found = false;
507   String backup_subsection = null;
508   String last_backup_subsection = null;
509   int backup_subsection_number = 0;
510   if(debug_on)
511     {
512       System.out.println("Merger.GetBackupData("+ backup_file+")");
513     }
514   input_brace_count = 0;
515   input_paren_count = 0;
516   output_brace_count = 0;
517   output_paren_count = 0;
518   int brace_count = 0;
519   int paren_count = 0;
520   merge_disabled = false;
521   if(cpp_mode)
522     {
523       functions_defined = new Vector();
524       classes_defined = new Vector();
525     }
526   if(!backup_file.exists())
527     {
528       System.err.println("Merger.GetBackupFile("+backup_file+"): File does not exist.");
529       return;
530     }
531   if(!backup_file.canRead())
532     {
533       System.err.println("Merger.GetBackupFile("+backup_file+"): File can NOT be read.");
534       return;
535     }
536   if(backup_file.length() < 1)
537     {
538       System.err.println("Merger.GetBackupFile("+backup_file+"): File has "+backup_file.length()+" length.");
539       return;
540     }
541   if(debug_on)
542     {
543       System.out.println("Merger.SetBackup("+backup_file+")");
544     }
545   if(null != backup_file)
546     {
547       if(debug_on)
548         {
549     System.out.println("Creating FileInputStream");
550         }
551       URL_and_FileLoader loader = new URL_and_FileLoader(backup_file.toString());
552       String str = loader.readLine();
553       if(debug_on)
554         {
555     System.out.println(str);
556         }
557       backup_lines = new Vector();
558       String funcname = null;
559       String classname = null;
560       inside_c_comment = false;
561       template_version = 0.0;
562       while(str != null)
563         {
564     if(debug_on)
565       {
566         System.out.println(backup_lines.size()+":"+str);
567       }
568     if(str.indexOf("RCS-Design-MERGE-ENABLE") >= 0)
569       {
570         merge_disabled = false;
571       }
572     if(!merge_disabled)
573       {
574         if(cpp_mode)
575           {
576       String sout = str;
577       if(paren_count == 0 && brace_count == 0 && null == funcname && null == classname)
578         {
579           backup_subsection = null;
580         }
581       int cpp_comment_index = sout.indexOf("//");
582       if(cpp_comment_index >= 0)
583         {
584           sout = sout.substring(0,cpp_comment_index);
585           if(debug_on)
586             {
587         System.out.println("cpp_comment_index="+cpp_comment_index+", sout="+sout+", str="+str);
588             }
589         }
590       int cstart_comment_index = sout.indexOf("/*");
591       int cend_comment_index = sout.indexOf("*/");
592       while(cstart_comment_index >= 0 && cend_comment_index > cstart_comment_index)
593       {
594         if(debug_on)
595           {
596             System.out.println("cstart_comment_index="+cstart_comment_index+", cend_comment_index="+cend_comment_index+", sout="+sout+", str="+str);
597           }
598         sout = sout.substring(0,cstart_comment_index)+ sout.substring(cend_comment_index+2);
599         cstart_comment_index = sout.indexOf("/*");
600         cend_comment_index = sout.indexOf("*/");
601       }
602       if(cend_comment_index >= 0 && inside_c_comment)
603         {
604           sout = sout.substring(cend_comment_index+2);
605           inside_c_comment = false;
606         }
607       if(cstart_comment_index >= 0 && !inside_c_comment)
608         {
609           sout = sout.substring(0,cstart_comment_index);
610           inside_c_comment = true;
611         }
612       if(!inside_c_comment)
613         {
614           int paren_index = sout.indexOf('(');
615           if(brace_count == 0 && (sout.indexOf("class") >= 0 || sout.indexOf("struct") >= 0) && paren_index < 0)
616             {
617         if(debug_on)
618           {
619             System.out.println("brace_count = "+brace_count+", sout="+sout);
620           }
621         int classname_begin = 0;
622         if(classname_begin < sout.indexOf("class")+5)
623           {
624             classname_begin = sout.indexOf("class")+5;
625           }
626         if(classname_begin < sout.indexOf("struct")+6)
627           {
628             classname_begin = sout.indexOf("struct")+6;
629           }
630         while(classname_begin < sout.length())
631           {
632             char c = sout.charAt(classname_begin);
633             if(Character.isLetterOrDigit(c) || c == '_')
634               {
635           break;
636               }
637             classname_begin++;
638           }
639         int classname_end = sout.length()-1;
640         if(sout.indexOf(":") > 0)
641           {
642             classname_end = sout.indexOf(":") -1;
643           }
644         if(sout.indexOf("public") > 0 && classname_end > sout.indexOf("public"))
645           {
646             classname_end = sout.indexOf("public") -1;
647           }
648         if(sout.indexOf("private") > 0 && classname_end > sout.indexOf("private"))
649           {
650             classname_end = sout.indexOf("private") -1;
651           }
652         if(sout.indexOf("protected") > 0 && classname_end > sout.indexOf("protected"))
653           {
654             classname_end = sout.indexOf("protected") -1;
655           }
656         if(sout.indexOf("{") > 0 && classname_end > sout.indexOf("{"))
657           {
658             classname_end = sout.indexOf("protected") -1;
659           }
660         while(classname_end > 0)
661           {
662             char c = sout.charAt(classname_end);
663             if(Character.isLetterOrDigit(c) || c == '_')
664               {
665           classname_end++;
666           break;
667               }
668             classname_end--;
669           }
670         if(debug_on)
671           {
672             System.out.println("classname_begin="+classname_begin+", classname_end="+classname_end);
673           }
674         if(classname_end > classname_begin)
675           {
676             classname = sout.substring(classname_begin, classname_end);
677             if(null != classname && null == backup_subsection)
678               {
679           backup_subsection = classname;
680               }
681             if(debug_on)
682               {
683           System.out.println("classname="+classname);
684               }
685           }
686             }
687           if(brace_count == 0 && paren_index > 1)
688             {
689         if(debug_on)
690           {
691             System.out.println("brace_count="+brace_count+", paren_index="+paren_index+", sout="+sout);
692           }
693         int funcname_end = paren_index;
694         while(funcname_end > 0)
695           {
696             char c = sout.charAt(funcname_end);
697             if(Character.isLetterOrDigit(c) || c == '_')
698               {
699           break;
700               }
701             funcname_end--;
702           }
703         int funcname_begin = funcname_end;
704         while(funcname_begin > 0)
705           {
706             char c = sout.charAt(funcname_begin);
707             if(!Character.isLetterOrDigit(c) && c != '_' && c != ':')
708               {
709           funcname_begin++;
710           break;
711               }
712             funcname_begin--;
713           }
714           
715         if(funcname_begin < funcname_end)
716           {
717             funcname = sout.substring(funcname_begin, funcname_end+1);
718             if(funcname.equals("Parameter"))
719               {
720           funcname = null;
721               }
722             if(null != funcname && null == backup_subsection)
723               {
724           backup_subsection = funcname;
725               }
726             if(debug_on)
727               {
728           System.out.println("funcname = "+funcname);
729               }
730           }
731             }
732           int brace_index = sout.indexOf('{');
733           while(brace_index >= 0)
734             {
735         brace_count++;
736         brace_index = sout.indexOf('{',brace_index+1);
737             }
738           brace_index = sout.indexOf('}');
739           while(brace_index >= 0 )
740             {
741         brace_count--;
742         brace_index = sout.indexOf('}',brace_index+1);
743             }
744           if(funcname != null && brace_count > 0)
745             {
746         functions_defined.addElement(funcname);
747         backup_subsection = funcname;
748         backup_subsection_number++;
749         funcname = null;
750             }
751           if(classname != null && brace_count > 0)
752             {
753         classes_defined.addElement(classname);
754         backup_subsection = classname;
755         backup_subsection_number++;
756         classname=null;
757             }
758           while(paren_index >= 0 )
759             {
760         paren_count++;
761         paren_index = sout.indexOf('(',paren_index+1);
762             }
763           paren_index = sout.indexOf(')');
764           while(paren_index >= 0 )
765             {
766         paren_count--;
767         paren_index = sout.indexOf(')',paren_index+1);
768             }
769         }
770           }
771         if(Makefile_mode)
772           {
773       int eq_index = str.indexOf('=');
774       int colon_index = str.indexOf(':');
775       if(eq_index > 0)
776         {
777           if(debug_on)
778             {
779         System.out.println("eq_index  = "+eq_index);
780             }
781           backup_subsection = RemoveExtraWhiteSpace(str.substring(0,eq_index));
782            backup_subsection_number++;
783         }
784       else if(colon_index > 0)
785         {
786           if(debug_on)
787             {
788         System.out.println("colon_index  = "+colon_index);
789             }
790           backup_subsection = RemoveExtraWhiteSpace(str.substring(0,colon_index));
791            backup_subsection_number++;
792         }
793       if(str.length() < 1)
794         {
795           backup_subsection = null;
796         }
797       else if(str.startsWith("#") || str.startsWith("if") || str.startsWith("else") ||
798          str.startsWith("endif") || str.startsWith("include") || str.startsWith("!"))
799         {
800           backup_subsection = null;
801         }
802       if(null != backup_subsection)
803         {
804           if(backup_subsection.indexOf("PHONY") >= 0 || backup_subsection.startsWith("."))
805             {
806         backup_subsection = null;
807             }
808           else if(backup_subsection.length() < 1)
809             {
810         backup_subsection = null;
811             }
812         }
813       try
814         {
815           if(debug_on && !backup_subsection.equals(last_backup_subsection))
816             {
817         System.out.println("backup_subsection="+backup_subsection);
818         last_backup_subsection = backup_subsection;
819             }
820         }
821       catch(Exception e)
822         {
823         }
824           }
825         BackupLineData bld = new BackupLineData();
826         bld.orig_line = str;
827         bld.line_for_compare = RemoveExtraWhiteSpace(str);
828         int template_version_index = bld.line_for_compare.toUpperCase().indexOf("TEMPLATE VERSION");
829         if(Makefile_mode || cpp_mode)
830           {
831       bld.subsection = backup_subsection;
832       bld.subsection_number = backup_subsection_number;
833           }
834         if(template_version_index >= 0)
835           {
836       int template_version_start_index = template_version_index+new String("TEMPLATE VERSION").length();
837       if(template_version_start_index < bld.line_for_compare.length())
838         {
839           while(true)
840             {
841         if(template_version_start_index >=  bld.line_for_compare.length())
842           {
843             break;
844           }
845         char c =  bld.line_for_compare.charAt(template_version_start_index);
846         if(Character.isDigit(c) ||
847            c == '.'  || c == '+' || c == '-')
848           {
849             break;
850           }
851         template_version_start_index++;
852             }
853           int template_version_stop_index = template_version_start_index;
854           boolean period_occured = false;
855          while(true)
856             {
857         if(template_version_stop_index >=  bld.line_for_compare.length())
858           {
859             break;
860           }
861         char c =  bld.line_for_compare.charAt(template_version_stop_index);
862         if(!Character.isDigit(c) && 
863            c != '.'  && c != '+' && c != '-')
864           {
865             break;
866           }
867         if(c == '.')
868           {
869             if(period_occured)
870               {
871           break;
872               }
873             period_occured = true;
874           }
875         template_version_stop_index++;
876             }
877           if(template_version_stop_index > template_version_start_index)
878             {
879         String vers_string = bld.line_for_compare.substring(template_version_start_index,template_version_stop_index);
880         template_version = Double.valueOf(vers_string).doubleValue();
881         template_version_found = true;
882         if(debug_on)
883           {
884             System.out.println("template_version="+template_version);
885           }
886             }
887         }
888           }
889         bld.brace_count = brace_count;
890         bld.paren_count = paren_count;
891         backup_lines.addElement(bld);
892             
893       }
894     if(str.indexOf("RCS-Design-MERGE-DISABLE") >= 0)      {
895         merge_disabled = true;
896       }
897     str = loader.readLine();
898         }
899       loader.close();
900       merge_disabled = false;
901       brace_count = 0;
902       paren_count = 0;
903     }
904   if(debug_on && null != backup_lines)
905     {
906       System.out.println("backup_lines.size() = "+backup_lines.size());
907     }
908   inside_c_comment = false;
909       }
910     catch(Exception e)
911       {
912   e.printStackTrace();
913       }
914   }
915 
916   String RemoveExtraWhiteSpace(String str)
917   {
918     String newString = "";
919     String separators = "(){}[]=+-*/.;,\"";
920     try
921       {
922   StringTokenizer tokenizer = new StringTokenizer(str," \t\r\n\b");
923   while(tokenizer.hasMoreTokens())
924     {
925       String token = tokenizer.nextToken();
926       if(newString.length() > 0 && token.length() > 0)
927         {
928     char lastChar = newString.charAt(newString.length()-1);
929     char nextChar = token.charAt(0);
930     if(separators.indexOf(lastChar) < 0 && separators.indexOf(nextChar) < 0)
931       {
932         newString += " ";
933       }
934         }
935       newString += token;
936     }
937   if(debug_on)
938     {
939       // System.out.println("Merger.RemoveExtraWhiteSpace("+str+") returning "+newString);
940     }
941       }
942     catch(Exception e)
943       {
944   e.printStackTrace();
945       }
946     return newString;
947   }
948 
949   
950   public void DisableMerge(String msg_head)
951   {
952     try
953       {
954   input_line++;
955   int matching_line = FindDisableLine();
956   if(last_line_matched >= 0 && matching_line > last_line_matched)
957     {
958       boolean blank_line_from_backup_needed = false;
959       for(int i = last_line_matched+1; i < matching_line; i++)
960         {
961     BackupLineData bld = (BackupLineData) backup_lines.elementAt(i);
962     if(bld.line_for_compare.length() < 1)
963       {
964         blank_line_from_backup_needed = true;
965         continue;
966       }
967     if(blank_line_from_backup_needed)
968       {
969         if(debug_on && null != logPS)
970           {
971       logPS.println("");
972           }
973         FinalWriteLine("");
974         if(debug_on)
975           {
976       System.out.println("blank_line_from_backup");
977           }
978         blank_line_from_backup_needed = false;
979       }
980     if(debug_on)
981       {
982         System.out.println("-----\t"+bld.orig_line);
983       }
984     if(debug_on && null != logPS)
985       {
986         logPS.println("<"+i+">"+ bld.orig_line);
987       }
988     FinalWriteLine(bld.orig_line);
989         }
990       last_line_matched = matching_line;
991       if(debug_on)
992         {
993     System.out.println("last_line_matched = "+last_line_matched);
994         }
995     }
996   if(null != msg_head)
997     {
998       if(debug_on && null != logPS)
999         {
1000    logPS.println("("+input_line+")"+ msg_head+" RCS-Design-MERGE-DISABLE Edits to the following area will NOT be preserved by the RCS-Design tool.");
1001    logPS.println("");
1002        }
1003      FinalWriteLine(msg_head+" RCS-Design-MERGE-DISABLE Edits to the following area will NOT be preserved by the RCS-Design tool.");
1004      FinalWriteLine("");
1005    }
1006  else
1007    {
1008      if(debug_on && null != logPS)
1009        {
1010    logPS.println("("+input_line+") RCS-Design-MERGE-DISABLE Edits to the following area will NOT be preserved by the RCS-Design tool.");
1011    logPS.println("");
1012        }
1013      FinalWriteLine(" RCS-Design-MERGE-DISABLE Edits to the following area will NOT be preserved by the RCS-Design tool.");
1014      FinalWriteLine("");
1015    }
1016  merge_disabled = true;
1017      }
1018    catch(Exception e)
1019      {
1020  e.printStackTrace();
1021      }
1022  }
1023
1024  public void EnableMerge(String msg_head)
1025  {
1026    try
1027      {
1028  input_line++;
1029  int matching_line = FindEnableLine();
1030  if(matching_line > 0)
1031    {
1032      if(debug_on && matching_line > last_line_matched)
1033        {
1034    for(int i = last_line_matched; i < matching_line; i++)
1035      {
1036        BackupLineData bld =  (BackupLineData)  backup_lines.elementAt(i);
1037        System.out.println("ddd "+i+" - "+bld.orig_line);
1038      }
1039        }
1040      last_line_matched = matching_line;
1041      if(debug_on)
1042        {
1043    System.out.println("last_line_matched = "+last_line_matched);
1044        }
1045    }
1046  if(null != msg_head)
1047    {
1048      if(debug_on && null != logPS)
1049        {
1050    logPS.println("("+input_line+")"+ msg_head+" RCS-Design-MERGE-ENABLE Edits after this line will be preserved by the RCS-Design tool.");
1051    logPS.println("");
1052        }
1053      FinalWriteLine(msg_head+" RCS-Design-MERGE-ENABLE Edits after this line will be preserved by the RCS-Design tool.");
1054      FinalWriteLine("");
1055    }
1056  else
1057    {
1058      if(debug_on && null != logPS)
1059        {
1060    logPS.println("("+input_line+") RCS-Design-MERGE-ENABLE Edits after this line will be preserved by the RCS-Design tool.");
1061    logPS.println("");
1062        }
1063      FinalWriteLine(" RCS-Design-MERGE-ENABLE Edits after this line will be preserved by the RCS-Design tool.");
1064      FinalWriteLine("");
1065    }
1066  merge_disabled = false;
1067      }
1068    catch(Exception e)
1069      {
1070  e.printStackTrace();
1071      }
1072  }
1073
1074  int FindLine(String str)
1075  {
1076    try
1077      {
1078  if(null == backup_lines)
1079    {
1080      return -1;
1081    }
1082  
1083  if(Makefile_mode)
1084    {
1085      int eq_index = str.indexOf('=');
1086      int colon_index = str.indexOf(':');
1087      if(eq_index > 0)
1088        {
1089    input_subsection = RemoveExtraWhiteSpace(str.substring(0,eq_index));
1090    input_subsection_number++;
1091        }
1092      else if(colon_index > 0)
1093        {
1094    input_subsection = RemoveExtraWhiteSpace(str.substring(0,colon_index));
1095    input_subsection_number++;
1096        }
1097      if(str.length() < 1)
1098        {
1099    input_subsection = null;
1100        }
1101      else if(str.startsWith("#") || str.startsWith("if") || str.startsWith("else") ||
1102        str.startsWith("endif") || str.startsWith("include") || str.startsWith("!"))
1103        {
1104    input_subsection = null;
1105        }
1106      if(null != input_subsection)
1107        {
1108    if(input_subsection.length() < 1)
1109      {
1110        input_subsection = null;
1111      }
1112    else if(input_subsection.indexOf("PHONY") >= 0 || 
1113       input_subsection.startsWith("."))
1114      {
1115        input_subsection = null;
1116      }
1117        }
1118    }
1119  if((Makefile_mode || cpp_mode) && debug_on)
1120    {
1121      System.out.println("input_subsection="+input_subsection);
1122      System.out.println("input_subsection_number="+input_subsection_number);
1123    }
1124
1125    
1126  String str_for_compare = RemoveExtraWhiteSpace(str);
1127  if(debug_on)
1128    {
1129      System.out.println("Merger.FindLine : str_for_compare = "+str_for_compare);
1130      if(cpp_mode)
1131        {
1132    System.out.println("Merger.FindLine : input_brace_count = "+input_brace_count);
1133    System.out.println("Merger.FindLine : input_paren_count = "+input_paren_count);
1134        }
1135    }
1136
1137  for(int i = last_line_matched+1; i < backup_lines.size(); i++)
1138    {
1139      BackupLineData bld = (BackupLineData) backup_lines.elementAt(i);
1140      
1141      if(bld.line_for_compare.length() < 1)
1142        {
1143    continue;
1144        }
1145      if(debug_on)
1146        {
1147    System.out.println("Merger.FindLine : i = "+i);
1148    System.out.println("Merger.FindLine : bld.line_for_compare = "+bld.line_for_compare);
1149    if(cpp_mode)
1150      {
1151        System.out.println("Merger.FindLine : bld.brace_count = "+bld.brace_count);
1152        System.out.println("Merger.FindLine : bld.paren_count = "+bld.paren_count);
1153      }
1154    if(Makefile_mode || cpp_mode)
1155      {
1156        System.out.println("Merger.FindLine : bld.subsection = "+bld.subsection);
1157        System.out.println("Merger.FindLine : bld.subsection_number = "+bld.subsection_number);
1158      }
1159        }
1160      if(cpp_mode && 
1161         (bld.brace_count != input_brace_count 
1162    || bld.paren_count != input_paren_count))
1163        {
1164    continue;
1165        }
1166      if(nml_mode)
1167        {
1168    if(NMLLineCompare(bld.line_for_compare,str_for_compare))
1169      {
1170        return i;
1171      }
1172    continue;
1173        }
1174      if((Makefile_mode || cpp_mode) && input_subsection != null)
1175        {
1176    if(!input_subsection.equals(bld.subsection))
1177      {
1178        if(bld.subsection_number > ((int) (input_subsection_number*1.1)) + 5)
1179          {
1180      if(debug_on)
1181        {
1182          System.out.println("FindLine: Returning -1 : input_subsection = "+input_subsection+", bld.subsection="+bld.subsection+", input_subsection_number ="+input_subsection_number+", bld.subsection_number="+bld.subsection_number);
1183          System.out.println("FindLine: str_for_compare="+str_for_compare+", bld.line_for_compare="+bld.line_for_compare);
1184        }
1185      return -1;
1186          }
1187        else
1188          {
1189      continue;
1190          }
1191      }
1192        }
1193      if((Makefile_mode || cpp_mode) && bld.subsection != null)
1194        {
1195    if(!bld.subsection.equals(input_subsection))
1196      {
1197        if(bld.subsection_number > ((int) (input_subsection_number*1.1)) + 5)
1198          {
1199      if(debug_on)
1200        {
1201          System.out.println("FindLine: Returning -1 : input_subsection = "+input_subsection+", bld.subsection="+bld.subsection+", input_subsection_number ="+input_subsection_number+", bld.subsection_number="+bld.subsection_number);
1202          System.out.println("FindLine: str_for_compare="+str_for_compare+", bld.line_for_compare="+bld.line_for_compare);
1203        }
1204      return -1;
1205          }
1206        else
1207          {
1208      continue;
1209          }
1210      }
1211        }
1212      if(Makefile_mode)
1213        {
1214    if(MakeLineCompare(str_for_compare, bld.line_for_compare))
1215      {
1216        return i;
1217      }
1218    else
1219      {
1220        continue;
1221      }
1222        }
1223      if(bld.line_for_compare.equals(str_for_compare))
1224        {
1225    return i;
1226        }
1227      
1228    }
1229      }
1230    catch(Exception e)
1231      {
1232  e.printStackTrace();
1233      }
1234    return -1;
1235  }
1236
1237  public int DeleteLine(String str)
1238  {
1239    boolean orig_cpp_mode = cpp_mode;
1240    cpp_mode = false;
1241    if(debug_on)
1242      {
1243  System.out.println("Merger.Deleting "+str);
1244      }
1245    int line_num  = FindLine(str);
1246    cpp_mode = orig_cpp_mode;
1247    if(line_num > 0 && line_num < backup_lines.size())
1248      {
1249  if(debug_on)
1250    {
1251      System.out.println("Merger.Deleting line number "+line_num);
1252    }
1253  backup_lines.removeElementAt(line_num);
1254  return 0;
1255      }
1256    return -1;
1257  }
1258
1259  int FindEnableLine()
1260  {
1261    try
1262      {
1263  if(null == backup_lines)
1264    {
1265      return -1;
1266    }
1267  for(int i = last_line_matched+1; i < backup_lines.size(); i++)
1268    {
1269      BackupLineData bld = (BackupLineData) backup_lines.elementAt(i);
1270      if(bld.line_for_compare.length() < 1)
1271        {
1272    continue;
1273        }
1274      if(bld.line_for_compare.indexOf("RCS-Design-MERGE-ENABLE") >= 0)
1275        {
1276    return i;
1277        }
1278    }
1279      }
1280    catch(Exception e)
1281      {
1282  e.printStackTrace();
1283      }
1284    return -1;
1285  }
1286
1287  int FindDisableLine()
1288  {
1289    try
1290      {
1291  if(null == backup_lines)
1292    {
1293      return -1;
1294    }
1295  for(int i = last_line_matched+1; i < backup_lines.size(); i++)
1296    {
1297      BackupLineData bld = (BackupLineData) backup_lines.elementAt(i);
1298      if(bld.line_for_compare.length() < 1)
1299        {
1300    continue;
1301        }
1302      if(bld.line_for_compare.indexOf("RCS-Design-MERGE-DISABLE") >= 0)
1303        {
1304    return i;
1305        }
1306    }
1307      }
1308    catch(Exception e)
1309      {
1310  e.printStackTrace();
1311      }
1312    return -1;
1313  }
1314  
1315  String TabForCpp(String sin)
1316  {
1317    String sout = sin;
1318    try
1319      {
1320  int prev_output_brace_count = output_brace_count;
1321  int prev_output_paren_count = output_paren_count;
1322  if(null == sin)
1323    {
1324      return null;
1325    }
1326  if(sin.length() < 1)
1327    {
1328      return "";
1329    }
1330  int double_slash_index = sout.indexOf("//");
1331  int brace_index = sout.indexOf('{');
1332  while(brace_index >= 0)
1333    {
1334      output_brace_count++;
1335      brace_index = sout.indexOf('{',brace_index+1);
1336    }
1337  brace_index = sout.indexOf('}');
1338  while(brace_index >= 0 )
1339    {
1340      output_brace_count--;
1341      brace_index = sout.indexOf('}',brace_index+1);
1342    }
1343  int paren_index = sout.indexOf('(');
1344  while(paren_index >= 0 )
1345    {
1346      output_paren_count++;
1347      paren_index = sout.indexOf('(',paren_index+1);
1348    }
1349  paren_index = sout.indexOf(')');
1350  while(paren_index >= 0 )
1351    {
1352      output_paren_count--;
1353      paren_index = sout.indexOf(')',paren_index+1);
1354    }
1355  while(sout.length() > 1 )
1356    {
1357      if(sout.charAt(0) != ' ' && sout.charAt(0) != '\t')
1358        {
1359    break;
1360        }
1361      sout = sout.substring(1);
1362    }
1363  if(sout.length() < 1)
1364    {
1365      return "";
1366    }
1367  String tab_string = "";
1368  int start_i = 0;
1369  if(sout.charAt(sout.length()-1) == ':')
1370    {
1371      start_i = 1;
1372    }
1373  for(int i = start_i; i < output_brace_count && i < prev_output_brace_count; i++)
1374    {
1375      tab_string += "\t";
1376    }
1377  if(prev_output_paren_count > 0)
1378    {
1379      tab_string += "\t";
1380    }
1381  sout = tab_string + sout;
1382      }
1383    catch(Exception e)
1384      {
1385  e.printStackTrace();
1386      }
1387    return sout;
1388  }
1389
1390  boolean inside_c_comment = false;
1391  String input_classname = null;
1392  String input_funcname = null;
1393
1394  void SetInputBraceCount(String sin)
1395  {
1396    String sout = sin;
1397
1398    try
1399      {
1400  int prev_input_brace_count = input_brace_count;
1401  if(null == sin)
1402    {
1403      return;
1404    }
1405  if(sin.length() < 1)
1406    {
1407      return;
1408    }
1409
1410  int cpp_comment_index = sout.indexOf("//");
1411  if(cpp_comment_index >= 0)
1412    {
1413      sout = sout.substring(0,cpp_comment_index);
1414      if(debug_on)
1415        {
1416    System.out.println("cpp_comment_index="+cpp_comment_index+", sout="+sout+", sin="+sin);
1417        }
1418    }
1419  int cstart_comment_index = sout.indexOf("/*");
1420  int cend_comment_index = sout.indexOf("*/");
1421  while(cstart_comment_index >= 0 && cend_comment_index > cstart_comment_index)
1422    {
1423      if(debug_on)
1424        {
1425    System.out.println("cstart_comment_index="+cstart_comment_index+", cend_comment_index="+cend_comment_index+", sout="+sout+", sin="+sin);
1426        }
1427      sout = sout.substring(0,cstart_comment_index)+ sout.substring(cend_comment_index+2);
1428      cstart_comment_index = sout.indexOf("/*");
1429      cend_comment_index = sout.indexOf("*/");
1430    }
1431  if(cend_comment_index >= 0 && inside_c_comment)
1432    {
1433      sout = sout.substring(cend_comment_index+2);
1434      inside_c_comment = false;
1435    }
1436  if(cstart_comment_index >= 0 && !inside_c_comment)
1437    {
1438      sout = sout.substring(0,cstart_comment_index);
1439      inside_c_comment = true;
1440    }
1441  if(input_paren_count == 0 && input_brace_count == 0 && null == input_funcname && null == input_classname)
1442    {
1443      input_subsection = null;
1444    }
1445  if(!inside_c_comment)
1446    {
1447      int paren_index = sout.indexOf('(');
1448      if(input_brace_count == 0 && (sout.indexOf("class") >= 0 || sout.indexOf("struct") >= 0) && paren_index < 0)
1449        {
1450    if(debug_on)
1451      {
1452        System.out.println("input_brace_count = "+input_brace_count+", sout="+sout);
1453      }
1454    int input_classname_begin = 0;
1455    if(input_classname_begin < sout.indexOf("class")+5)
1456      {
1457        input_classname_begin = sout.indexOf("class")+5;
1458      }
1459    if(input_classname_begin < sout.indexOf("struct")+6)
1460      {
1461        input_classname_begin = sout.indexOf("struct")+6;
1462      }
1463    while(input_classname_begin < sout.length())
1464      {
1465        char c = sout.charAt(input_classname_begin);
1466        if(Character.isLetterOrDigit(c) || c == '_')
1467          {
1468      break;
1469          }
1470        input_classname_begin++;
1471      }
1472    int input_classname_end = sout.length()-1;
1473    if(sout.indexOf(":") > 0)
1474      {
1475        input_classname_end = sout.indexOf(":") -1;
1476      }
1477    if(sout.indexOf("public") > 0 && input_classname_end > sout.indexOf("public"))
1478      {
1479        input_classname_end = sout.indexOf("public") -1;
1480      }
1481    if(sout.indexOf("private") > 0 && input_classname_end > sout.indexOf("private"))
1482      {
1483        input_classname_end = sout.indexOf("private") -1;
1484      }
1485    if(sout.indexOf("protected") > 0 && input_classname_end > sout.indexOf("protected"))
1486      {
1487        input_classname_end = sout.indexOf("protected") -1;
1488      }
1489    if(sout.indexOf("{") > 0 && input_classname_end > sout.indexOf("{"))
1490      {
1491        input_classname_end = sout.indexOf("protected") -1;
1492      }
1493    while(input_classname_end > 0)
1494      {
1495        char c = sout.charAt(input_classname_end);
1496        if(Character.isLetterOrDigit(c) || c == '_')
1497          {
1498      input_classname_end++;
1499      break;
1500          }
1501        input_classname_end--;
1502      }
1503    if(debug_on)
1504      {
1505        System.out.println("input_classname_begin="+input_classname_begin+", input_classname_end="+input_classname_end);
1506      }
1507    if(input_classname_end > input_classname_begin)
1508      {
1509        input_classname = sout.substring(input_classname_begin, input_classname_end);
1510        if(null != input_classname && null == input_subsection)
1511          {
1512      input_subsection = input_classname;
1513          }
1514        if(debug_on)
1515          {
1516      System.out.println("input_classname="+input_classname);
1517          }
1518      }
1519        }
1520      if(input_brace_count == 0 && paren_index > 1)
1521        {
1522    if(debug_on)
1523      {
1524        System.out.println("input_brace_count="+input_brace_count+", paren_index="+paren_index+", sout="+sout);
1525      }
1526    int input_funcname_end = paren_index;
1527    while(input_funcname_end > 0)
1528      {
1529        char c = sout.charAt(input_funcname_end);
1530        if(Character.isLetterOrDigit(c) || c == '_')
1531          {
1532      break;
1533          }
1534        input_funcna