00001 namespace JLibDiff
00002 {
00003 using System;
00004 using System.IO;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 public class diff3
00053 {
00054
00055 internal System.Collections.ArrayList v = new System.Collections.ArrayList();
00056
00057
00058
00059
00060 public diff3()
00061 {
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 public diff3(System.String s0, System.String s1, System.String s2)
00073 {
00074 diffFile(s0, s1, s2);
00075 }
00076
00077
00078
00079
00080
00081
00082
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
00091 diffBuffer(in0, in1, in2);
00092 in0.Close();
00093 in1.Close();
00094 in2.Close();
00095 }
00096
00097
00098
00099
00100
00101
00102
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
00110
00111
00112
00113
00114 d0.diffBuffer(in0, in2);
00115
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
00138 Hunk3 tmpblock;
00139 Hunk3 last_diff3;
00140 Hunk3 zero_diff3 = null;
00141
00142 System.Collections.ArrayList v = new System.Collections.ArrayList();
00143
00144
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
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
00279
00280 public virtual System.Collections.ArrayList getHunk3()
00281 {
00282 return v;
00283 }
00284
00285
00286
00287
00288
00289
00290 public virtual Hunk3 hunk3At(int i)
00291 {
00292 return (Hunk3) v[i];
00293 }
00294
00295
00296
00297
00298 public virtual int numberOfHunk3()
00299 {
00300 return v.Count;
00301 }
00302
00303
00304
00305
00306 public virtual void print()
00307 {
00308
00309 for (System.Collections.IEnumerator e = v.GetEnumerator(); e.MoveNext(); )
00310 {
00311
00312 System.Console.Out.Write(((Hunk3) e.Current).convert());
00313 }
00314 }
00315
00316
00317
00318
00319
00320
00321 public virtual void save(System.String file)
00322 {
00323
00324 StreamWriter out_Renamed = new StreamWriter(file, false);
00325
00326
00327 for (System.Collections.IEnumerator e = v.GetEnumerator(); e.MoveNext(); )
00328 {
00329
00330 out_Renamed.WriteLine(((Hunk3) e.Current).convert());
00331 }
00332 out_Renamed.Flush();
00333 out_Renamed.Close();
00334 }
00335 }
00336 }