1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
/*
Derby - Class org.apache.derbyTesting.functionTests.harness.SimpleDiff
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//SimpleDiff.java
package org.apache.derbyTesting.functionTests.harness;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.util.Vector;
import java.io.File;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
public class SimpleDiff
{
PrintWriter pw;
boolean debugOn = Boolean.getBoolean("simplediff.debug");
int debugLevel = 1;
boolean diffsFound = true;
int lookAhead = 20;
public void debug(int level, String msg)
{
if (debugLevel >= level)
{
debug(msg);
}
}
public void debug(String msg)
{
if (debugOn)
{
System.out.println("DEBUG: " + msg);
}
}
int lineCount(String file) throws IOException
{
BufferedReader input = new BufferedReader(new FileReader(file));
int count = 0;
String aLine = input.readLine();
while (aLine != null)
{
count++;
aLine = input.readLine();
}
input.close();
return count;
}
public String[] readFile(BufferedReader input) throws IOException
{
Vector<String> vec = new Vector<String>();
String aLine = "";
//int count = 0;
aLine = input.readLine();
while (aLine != null)
{
vec.addElement(aLine);
//count++;
aLine = input.readLine();
}
input.close();
String rV[] = new String[vec.size()];
//debug(2, ""+count + " lines in " + filename);
debug(2, ""+vec.size() + " lines in input");
vec.copyInto(rV);
return rV;
}
public void printFile(String file[])
{
for (int i = 0; i < file.length; i++)
{
pw.println(i + ": " + file[i]);
System.out.println(i + ": " + file[i]);
}
}
public static String usage = "java SimpleDiff <file1> <file2>";
/**
@param file1 Name of first file to be read
@param file2 Name of file with which to compare
@return String array of file contents
@exception IOException thrown by underlying calls
to the file system
*/
public String[] diffFiles( DiffBuffer file1,
DiffBuffer file2)
throws IOException
{
int currentLine1 = 0;
int currentLine2 = 0;
Vector<String> returnVec = new Vector<String>();
while ( file1.isValidOffset(currentLine1) &&
file2.isValidOffset(currentLine2))
{
String f1 = file1.lineAt(currentLine1);
String f2 = file2.lineAt(currentLine2);
if (f1.equals(f2))
{
debug(1, currentLine1 + ": match");
currentLine1++;
currentLine2++;
file1.setLowWater(currentLine1);
file2.setLowWater(currentLine2);
}
else
{
boolean foundMatch = false;
int checkLine2 = currentLine2;
int checkCount = 1;
// while ( (currentLine2 + checkCount) < (file2Length - 1) &&
while ( file2.isValidOffset(currentLine2 + checkCount) &&
checkCount < lookAhead)
{
debug(1, "currentLine1 " + currentLine1 + " currentLine2 " + (currentLine2 +checkCount));
debug(1, "about to reference file2[" + (currentLine2 + checkCount) + "]");
f2 = file2.lineAt(currentLine2 + checkCount);
debug(2, "did");
if (f1.equals(f2))
{
foundMatch = true;
if (checkCount > 1)
{
returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1) + "," + (currentLine2 + checkCount));
}
else
{
returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1));
}
for (int j = 0; j < checkCount; j++)
{
returnVec.addElement("> " +
file2.lineAt(currentLine2 + j) );
}
currentLine2 = currentLine2 + checkCount;
checkCount = 0;
// This break statement was commented out, which
// caused problems in the diff output. I don't
// know why it was commented out, and uncommenting
// it fixed the problem.
//
// - Jeff Lichtman
// March 24, 1999
break;
}
checkCount ++;
}
if (!foundMatch && file2.isValidOffset(currentLine2))
{
int checkLine1 = currentLine1;
checkCount = 1;
f2 = file2.lineAt(currentLine2);
while ( file1.isValidOffset(currentLine1 + checkCount) &&
checkCount < lookAhead)
{
debug(1, "currentLine2 " + currentLine2 + " currentLine1 " + (currentLine1 + checkCount));
f1 = file1.lineAt(currentLine1 + checkCount);
if ( f2.equals(f1))
{
foundMatch = true;
if (checkCount > 1)
{
returnVec.addElement((currentLine1 + 1) + "," + (currentLine1 + checkCount) + "d" + currentLine2);
}
else
{
returnVec.addElement((currentLine1 + 1) + "d" + currentLine2);
}
for (int j = 0; j < checkCount; j++)
{
returnVec.addElement("< " +
file1.lineAt(currentLine1 + j) );
}
currentLine1 = currentLine1 + checkCount;
checkCount = 0;
debug(1, "continuing");
break;
}
checkCount ++;
}
}
if (!foundMatch)
{
debug(1, currentLine1 + ": NOMATCH");
returnVec.addElement((currentLine1 + 1) +" del");// + (currentLine2 + 1));
returnVec.addElement("< " + file1.lineAt(currentLine1));
currentLine1++;
}
else
{
currentLine1++;
currentLine2++;
file1.setLowWater(currentLine1);
file2.setLowWater(currentLine2);
}
}
}
if (file1.isValidOffset(currentLine1))
{
returnVec.addElement((currentLine2) + " del");
for (int i = currentLine1; file1.isValidOffset(i); i++)
{
returnVec.addElement("< " + file1.lineAt(i));
}
}
if (file2.isValidOffset(currentLine2))
{
returnVec.addElement((currentLine1) + " add");
for (int i = currentLine2; file2.isValidOffset(i); i++)
{
returnVec.addElement("> " + file2.lineAt(i));
}
}
file1.close();
file2.close();
if (returnVec.isEmpty())
{
return null;
}
String [] returnArray = new String[returnVec.size()];
returnVec.copyInto(returnArray);
return returnArray;
}
private void reportMemory()
{
reportMemory(null);
}
private void reportMemory(String header)
{
if (header != null)
{
System.out.println(header);
}
long free = Runtime.getRuntime().freeMemory();
long total = Runtime.getRuntime().totalMemory();
System.out.println("total: " + total );
System.out.println("free: " + free );
System.out.println("used: " + (total - free));
System.gc();
System.out.println("used: <postgc> " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
System.out.println(" ");
}
public boolean doWork(BufferedReader in1, BufferedReader in2, PrintWriter localPW) throws IOException
{
this.pw=(localPW==null?new PrintWriter(System.out):localPW);
try
{
DiffBuffer db1 = new DiffBuffer(in1, "1");
DiffBuffer db2 = new DiffBuffer(in2, "2");
String diffs[] = diffFiles(db1, db2);
if (diffs == null)
{
debug(1, "no diff");
return false;
}
else
{
for (int i = 0; i < diffs.length; i++)
{
this.pw.println(diffs[i]);
System.out.println(diffs[i]);
}
}
}
catch (IOException ioe)
{
System.err.println("IOException comparing <" + in1 +
"> and <" + in2 + ">");
System.err.println(ioe);
this.pw.println("IOException comparing <" + in1 +
"> and <" + in2 + ">");
this.pw.println(ioe);
}
return true;
}
public static void main(String args[]) throws IOException
{
if (args.length < 2)
{
System.err.println("Invalid number of arguments");
System.err.println("Usage: " + usage);
System.exit(1);
}
SimpleDiff me = new SimpleDiff();
try
{
BufferedReader br1 = new BufferedReader(new FileReader(args[0]));
BufferedReader br2 = new BufferedReader(new FileReader(args[1]));
me.doWork(br1, br2, null);
}
catch (IOException ioe)
{
System.out.println("IOExeption: " + ioe);
}
}
public void pause()
{
BufferedInputStream bis = new BufferedInputStream(System.in);
try
{
bis.read();
}
catch (IOException ioe)
{
pw.println("Error trying to pause...");
System.out.println("Error trying to pause...");
}
}
class DiffBuffer extends Vector<String>
{
public boolean atEOF()
{
return atEnd;
}
public DiffBuffer(BufferedReader rb)
{
this(rb, "");
}
public DiffBuffer(BufferedReader rb, String name)
{
this (rb, name, 1024);
}
public DiffBuffer(BufferedReader rb, String name, int size)
{
super(size);
readBuffer = rb;
currentLowWater = 0;
currentHighWater = -1;
oldLow = 0;
myName = name;
atEnd = false;
}
public boolean isValidOffset(int lineNumber) throws IOException
{
if (atEnd)
{
return lineNumber <= actualEndOfFile;
}
if (lineAt(lineNumber) == null)
{
return false;
}
return true;
}
public String lineAt(int offset) throws IOException
{
/*
System.out.println("offset: " + offset);
System.out.println("currentHighWater: " + currentHighWater);
System.out.println("");
*/
if (offset > currentHighWater)
{
for (int i = 0; i < offset - currentHighWater; i++)
{
String aLine = readBuffer.readLine();
addElement(aLine);
/*
System.out.println("aLine: " + aLine);
*/
if (aLine == null)
{
if (!atEnd)
{
//first time we've tried to read past the EOF
actualEndOfFile = currentHighWater + i;
//System.out.println(myName + ": length " + actualEndOfFile);
atEnd = true;
}
}
}
currentHighWater = offset;
}
return (String) elementAt(offset);
}
public final String EMPTY = null;
protected BufferedReader readBuffer;
protected int currentLowWater;
protected int currentHighWater;
private int oldLow;
protected String myName;
protected boolean atEnd;
protected int actualEndOfFile;
/**
Useful to keep memory requirements low
*/
public void setLowWater(int newLow)
{
for (int i = oldLow; i < newLow; i++)
{
setElementAt(EMPTY, i);
}
currentLowWater = newLow;
oldLow = newLow -1;
}
public void iterate(boolean verbose)
{
int nulls = 0;
int nonnulls = 0;
for (int i = 0; i < this.size(); i++)
{
if (elementAt(i) == null)
{
if (verbose)
{
System.out.print("[" + i + "] ");
System.out.println("null");
}
nulls++;
}
else
{
if (verbose)
{
System.out.print("[" + i + "] ");
System.out.println("NotNULL");
}
nonnulls++;
}
}
System.out.println("nulls: " + nulls + " nonNull: " + nonnulls);
}
public void close() throws IOException
{
// System.out.println("Closing BufferedReader");
readBuffer.close();
readBuffer = null;
}
}
}
|