Main Page | Class Hierarchy | Class List | File List | Class Members

diff3.cs

00001 namespace JLibDiff
00002 {
00003         using System;
00004         using System.IO;
00005         
00006         /*
00007         * The <code>diff3</code> class compares three files or three BufferedReaders.
00008         * after comparison, the vector will represente a list of Hunk3 
00009         * corresponding with blocks of difference.
00010         * <p>
00011         * to generate Hunk3, one can instanciate as follows the class <code>diff3</code>:
00012         * <p><blockquote><pre>
00013         *     diff3 d = new diff3(file1,file2,file3);
00014         * </pre></blockquote><p>
00015         * which is equivalent to:
00016         * <p><blockquote><pre>
00017         *     diff3 d = new diff3();
00018         *     d.diffFile(file1,file2,file3);
00019         * </pre></blockquote><p>
00020         * To compaire three BufferedReader we have to instanciate as follows:
00021         * <p><blockquote><pre>
00022         *     diff3 d = new diff3();
00023         *     d.diffBuffer(BufferedReader1,BufferedReader2,BufferedReader3);
00024         * </pre></blockquote><p>
00025         * The class <code>diff3</code> includes methods for examining, printing
00026         * or saveing  blocks of difference:(Hunk3).
00027         * Here are some more examples of how <code>diff3</code> can be used:
00028         * <p><blockquote><pre>
00029         *       diff3 d = new diff3(file1,file2,file3);
00030         *       d.print();
00031         *     d.save("diff.txt");
00032         * </pre></blockquote><p>
00033         * Example using BufferedReader and ED_format:
00034         * <p><blockquote><pre> 
00035         *       BufferedReader in0=new BufferedReader(new FileReader(file1));
00036         *       BufferedReader in1=new BufferedReader(new FileReader(file2));
00037         *     BufferedReader in2=new BufferedReader(new FileReader(file3)); 
00038         *       diff3 d = new diff3();
00039         *       d.diffBuffer(in0,in1,in2);
00040         *       d.print_ED();
00041         *       d.save_ED("diff.txt");
00042         * </pre></blockquote><p>
00043         *
00044         *
00045         *
00046         * @see diff.diff3#getHunk3()
00047         * @see diff.diff3#hunk3At()
00048         * @see diff.diff3#numberOfHunk3()
00049         * @see diff.diff3#print()
00050         * @see diff.diff3#save()
00051         */
00052         public class diff3
00053         {
00054                 
00055                 internal System.Collections.ArrayList v = new System.Collections.ArrayList();
00056                 
00057                 /*
00058                 *  Allocates a new <code>diff3</code> containing no Hunks.
00059                 */
00060                 public diff3()
00061                 {
00062                 }
00063                 
00064                 /*
00065                 * Allocates a new <code>diff3</code> which contains Hunk3 corresponding 
00066                 * to the differences between the three files passed in arguments.
00067                 *
00068                 * @param s0  first file.
00069                 * @param s1  second file.
00070                 * @param s2  therd file.
00071                 */
00072                 public diff3(System.String s0, System.String s1, System.String s2)
00073                 {
00074                         diffFile(s0, s1, s2);
00075                 }
00076                 
00077                 /*
00078                 * Compaires three files and updates the vector of Hunk3.
00079                 *
00080                 * @param s0  first file.
00081                 * @param s1  second file.
00082                 * @param s2  therd file.
00083                 */
00084                 public virtual void  diffFile(System.String s0, System.String s1, System.String s2)
00085                 {
00086                         
00087                         System.IO.StreamReader in0 = new System.IO.StreamReader(new System.IO.StreamReader(s0).BaseStream);
00088                         System.IO.StreamReader in1 = new System.IO.StreamReader(new System.IO.StreamReader(s1).BaseStream);
00089                         System.IO.StreamReader in2 = new System.IO.StreamReader(new System.IO.StreamReader(s2).BaseStream);
00090                         // BufferedReader in22=new BufferedReader(new FileReader(s2));
00091                         diffBuffer(in0, in1, in2);
00092                         in0.Close();
00093                         in1.Close();
00094                         in2.Close();
00095                 }
00096                 
00097                 /*
00098                 * Compaires three BufferedReaders and updates the vector of Hunk3.
00099                 *
00100                 * @param in0  first Buffer.
00101                 * @param in1  second Buffer.
00102                 * @param in2  therd Buffer.
00103                 */
00104                 public virtual void  diffBuffer(System.IO.StreamReader in0, System.IO.StreamReader in1, System.IO.StreamReader in2)
00105                 {
00106                         
00107                         diff d0 = new diff();
00108                         diff d1 = new diff();
00109                         //BufferedReader in22=new BufferedReader(new BufferedReader(in2));
00110                         //in22=in2;
00111                         // if(in2.markSupported())
00112                         //System.out.println("-----ssssss--------");
00113                         //in2.mark(0);
00114                         d0.diffBuffer(in0, in2);
00115                         //in2.reset();
00116                         d1.diffBuffer(in1, in2);
00117                         d1.print();
00118                         d0.print();
00119                         System.Collections.ArrayList v0, v1;
00120                         v0 = d0.getHunk();
00121                         v1 = d1.getHunk();
00122                         v = getHunk3(v0, v1);
00123                 }
00124                 
00125                 private System.Collections.ArrayList getHunk3(System.Collections.ArrayList v0, System.Collections.ArrayList v1)
00126                 {
00127                         
00128                         Hunk[] using_Renamed = new Hunk[2];
00129                         Hunk[] last_using = new Hunk[2];
00130                         Hunk[] current = new Hunk[2];
00131                         
00132                         int high_water_mark;
00133                         int high_water_thread, base_water_thread, other_thread;
00134                         
00135                         Hunk high_water_diff, other_diff;
00136                         
00137                         //Hunk3 result;
00138                         Hunk3 tmpblock;
00139                         Hunk3 last_diff3;
00140                         Hunk3 zero_diff3 = null;
00141                         
00142                         System.Collections.ArrayList v = new System.Collections.ArrayList();
00143                         
00144                         //result = null;
00145                         
00146                         last_diff3 = zero_diff3;
00147                         
00148                         if (v0.Count != 0)
00149                                 current[0] = (Hunk) v0[0];
00150                         else
00151                                 current[0] = null;
00152                         if (v1.Count != 0)
00153                                 current[1] = (Hunk) v1[0];
00154                         else
00155                                 current[1] = null;
00156                         
00157                         while (current[0] != null || current[1] != null)
00158                         {
00159                                 using_Renamed[0] = null;
00160                                 using_Renamed[1] = null;
00161                                 last_using[0] = null;
00162                                 last_using[1] = null;
00163                                 
00164                                 if (current[0] == null)
00165                                         base_water_thread = 1;
00166                                 else if (current[1] == null)
00167                                         base_water_thread = 0;
00168                                 else if (current[0].lowLine(1) > current[1].lowLine(1))
00169                                         base_water_thread = 1;
00170                                 else
00171                                         base_water_thread = 0;
00172                                 high_water_thread = base_water_thread;
00173                                 high_water_diff = current[high_water_thread];
00174                                 
00175                                 high_water_mark = high_water_diff.highLine(1);
00176                                 last_using[high_water_thread] = high_water_diff;
00177                                 using_Renamed[high_water_thread] = high_water_diff;
00178                                 current[high_water_thread] = high_water_diff.next;
00179                                 (last_using[high_water_thread]).next = null;
00180                                 other_thread = high_water_thread ^ 0x1;
00181                                 other_diff = current[other_thread];
00182                                 
00183                                 while (other_diff != null && other_diff.lowLine(1) <= high_water_mark + 1)
00184                                 {
00185                                         if (using_Renamed[other_thread] != null)
00186                                                 (last_using[other_thread]).next = other_diff;
00187                                         else
00188                                                 using_Renamed[other_thread] = other_diff;
00189                                         last_using[other_thread] = other_diff;
00190                                         current[other_thread] = (current[other_thread]).next;
00191                                         other_diff.next = null;
00192                                         if (high_water_mark < other_diff.highLine(1))
00193                                         {
00194                                                 high_water_thread ^= 1;
00195                                                 high_water_diff = other_diff;
00196                                                 high_water_mark = other_diff.highLine(1);
00197                                         }
00198                                         other_thread = high_water_thread ^ 0x1;
00199                                         other_diff = current[other_thread];
00200                                 }
00201                                 tmpblock = using_to_diff3_block(using_Renamed, last_using, base_water_thread, high_water_thread, last_diff3);
00202                                 if (tmpblock != null)
00203                                 {
00204                                         v.Add(tmpblock);
00205                                         last_diff3 = tmpblock;
00206                                 }
00207                         }
00208                         return v;
00209                 }
00210                 
00211                 private Hunk3 using_to_diff3_block(Hunk[] using_Renamed, Hunk[] last_using, int low_thread, int high_thread, Hunk3 last_diff3)
00212                 {
00213                         
00214                         int d, i;
00215                         int[] low = new int[2];
00216                         int[] high = new int[2];
00217                         Hunk3 result = new Hunk3();
00218                         
00219                         int lowc = using_Renamed[low_thread].lowLine(1);
00220                         int highc = last_using[high_thread].highLine(1);
00221                         
00222                          for (d = 0; d < 2; d++)
00223                                 if (using_Renamed[d] != null)
00224                                 {
00225                                         low[d] = lowc - using_Renamed[d].lowLine(1) + using_Renamed[d].lowLine(0);
00226                                         high[d] = highc - last_using[d].highLine(1) + last_using[d].highLine(0);
00227                                 }
00228                                 else
00229                                 {
00230                                         if (last_diff3 == null)
00231                                         {
00232                                                 low[d] = lowc;
00233                                                 high[d] = highc;
00234                                         }
00235                                         else
00236                                         {
00237                                                 low[d] = lowc - last_diff3.highLine(2) + last_diff3.highLine(d);
00238                                                 high[d] = highc - last_diff3.highLine(2) + last_diff3.highLine(d);
00239                                         }
00240                                 }
00241                         result.setRange(low[0], high[0], low[1], high[1], lowc, highc);
00242                          for (d = 0; d < 2; d++)
00243                         {
00244                                 
00245                         }
00246                          for (d = 0; d < 2; d++)
00247                         {
00248                                 Hunk u = using_Renamed[d];
00249                                 int lo = low[d];
00250                                 int hi = high[d];
00251                                  for (i = 0; i + lo < ((u != null)?u.lowLine(1):hi + 1); i++)
00252                                 {
00253                                         
00254                                 }
00255                                 //for()
00256                                 {
00257                                         
00258                                 }
00259                                 
00260                         }
00261                         if (using_Renamed[0] == null)
00262                                 result.diff = DiffType.DIFF_2ND;
00263                         else if (using_Renamed[1] == null)
00264                                 result.diff = DiffType.DIFF_1ST;
00265                         else
00266                         {
00267                                 int nl0 = result.numLines(0);
00268                                 int nl1 = result.numLines(1);
00269                                 if (nl0 != nl1)
00270                                         result.diff = DiffType.DIFF_ALL;
00271                                 else
00272                                         result.diff = DiffType.DIFF_3RD;
00273                         }
00274                         return result;
00275                 }
00276                 
00277                 /*
00278                 * Returns a vector containing Hunk3.
00279                 */
00280                 public virtual System.Collections.ArrayList getHunk3()
00281                 {
00282                         return v;
00283                 }
00284                 
00285                 /*
00286                 * Return the hunk3 at the specified index.
00287                 *
00288                 * @param i  index of the Hunk that will be returned.
00289                 */
00290                 public virtual Hunk3 hunk3At(int i)
00291                 {
00292                         return (Hunk3) v[i];
00293                 }
00294                 
00295                 /*
00296                 * Returns the number of hunk3 in the vector.
00297                 */
00298                 public virtual int numberOfHunk3()
00299                 {
00300                         return v.Count;
00301                 }
00302                 
00303                 /*
00304                 * Print Hunk3 with normal format.
00305                 */
00306                 public virtual void  print()
00307                 {
00308                         //UPGRADE_TODO: The method hasMoreElements has a different behavior in .NET.;
00309                          for (System.Collections.IEnumerator e = v.GetEnumerator(); e.MoveNext(); )
00310                         {
00311                                 //UPGRADE_TODO: The method nextElement has a different behavior in .NET.;
00312                                 System.Console.Out.Write(((Hunk3) e.Current).convert());
00313                         }
00314                 }
00315                 
00316                 /*
00317                 * Save Hunk3 with normal format at the specified file.
00318                 *
00319                 * @param file   file in which Hunks will be saved.
00320                 */
00321                 public virtual void  save(System.String file)
00322                 {
00323                         //FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
00324                         StreamWriter out_Renamed = new StreamWriter(file, false);
00325                         //System.IO.StreamWriter out_Renamed = new PrintWriter(new BufferedWriter(new System.IO.StreamWriter(file)));
00326                         //UPGRADE_TODO: The method hasMoreElements has a different behavior in .NET.;
00327                          for (System.Collections.IEnumerator e = v.GetEnumerator(); e.MoveNext(); )
00328                         {
00329                                 //UPGRADE_TODO: The method nextElement has a different behavior in .NET.;
00330                                 out_Renamed.WriteLine(((Hunk3) e.Current).convert());
00331                         }
00332                         out_Renamed.Flush();
00333                         out_Renamed.Close();
00334                 }
00335         }
00336 }

Generated on Mon May 8 22:07:27 2006 by  doxygen 1.3.9.1