Skip to content

Commit

Permalink
For PR351:
Browse files Browse the repository at this point in the history
* Change use of ReadFileIntoAddressSpace to sys::MappedFile use.
* Shorten a line > 80 chars.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18896 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Reid Spencer committed Dec 13, 2004
1 parent c0354c9 commit 15653af
Showing 1 changed file with 58 additions and 59 deletions.
117 changes: 58 additions & 59 deletions utils/fpcmp/fpcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/System/MappedFile.h"
#include <iostream>
#include <cmath>

Expand All @@ -31,17 +31,6 @@ namespace {
AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
}


/// OpenFile - mmap the specified file into the address space for reading, and
/// return the length and address of the buffer.
static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){
BufPtr = (char*)ReadFileIntoAddressSpace(Filename, Len);
if (BufPtr == 0) {
std::cerr << "Error: cannot open file '" << Filename << "'\n";
exit(2);
}
}

static bool isNumberChar(char C) {
switch (C) {
case '0': case '1': case '2': case '3': case '4':
Expand Down Expand Up @@ -125,65 +114,75 @@ static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);

// mmap in the files.
unsigned File1Len, File2Len;
char *File1Start, *File2Start;
OpenFile(File1, File1Len, File1Start);
OpenFile(File2, File2Len, File2Start);

// Okay, now that we opened the files, scan them for the first difference.
char *File1End = File1Start+File1Len;
char *File2End = File2Start+File2Len;
char *F1P = File1Start;
char *F2P = File2Start;

// Scan for the end of file or first difference.
while (F1P < File1End && F2P < File2End && *F1P == *F2P)
++F1P, ++F2P;

// Common case: identifical files.
if (F1P == File1End && F2P == File2End) return 0;

// If the files need padding, do so now.
PadFileIfNeeded(File1Start, File1End, F1P);
PadFileIfNeeded(File2Start, File2End, F2P);

while (1) {
// Scan for the end of file or next difference.
try {
// map in the files into memory
sys::Path F1Path(File1);
sys::Path F2Path(File2);
sys::MappedFile F1 ( F1Path );
sys::MappedFile F2 ( F2Path );
F1.map();
F2.map();

// Okay, now that we opened the files, scan them for the first difference.
char *File1Start = F1.charBase();
char *File2Start = F2.charBase();
char *File1End = File1Start+F1.size();
char *File2End = File2Start+F2.size();
char *F1P = File1Start;
char *F2P = File2Start;

// Scan for the end of file or first difference.
while (F1P < File1End && F2P < File2End && *F1P == *F2P)
++F1P, ++F2P;

if (F1P >= File1End || F2P >= File2End) break;
// Common case: identifical files.
if (F1P == File1End && F2P == File2End) return 0;

// If the files need padding, do so now.
PadFileIfNeeded(File1Start, File1End, F1P);
PadFileIfNeeded(File2Start, File2End, F2P);

while (1) {
// Scan for the end of file or next difference.
while (F1P < File1End && F2P < File2End && *F1P == *F2P)
++F1P, ++F2P;

if (F1P >= File1End || F2P >= File2End) break;

// Okay, we must have found a difference. Backup to the start of the
// current number each stream is at so that we can compare from the
// beginning.
F1P = BackupNumber(F1P, File1Start);
F2P = BackupNumber(F2P, File2Start);

// Now that we are at the start of the numbers, compare them, exiting if
// they don't match.
CompareNumbers(F1P, F2P, File1End, File2End);
}

// Okay, we reached the end of file. If both files are at the end, we
// succeeded.
bool F1AtEnd = F1P >= File1End;
bool F2AtEnd = F2P >= File2End;
if (F1AtEnd & F2AtEnd) return 0;

// Okay, we must have found a difference. Backup to the start of the
// current number each stream is at so that we can compare from the
// beginning.
// Else, we might have run off the end due to a number: backup and retry.
if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
F1P = BackupNumber(F1P, File1Start);
F2P = BackupNumber(F2P, File2Start);

// Now that we are at the start of the numbers, compare them, exiting if
// they don't match.
CompareNumbers(F1P, F2P, File1End, File2End);
}

// Okay, we reached the end of file. If both files are at the end, we
// succeeded.
bool F1AtEnd = F1P >= File1End;
bool F2AtEnd = F2P >= File2End;
if (F1AtEnd & F2AtEnd) return 0;

// Otherwise, we might have run off the end due to a number: backup and retry.
if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
F1P = BackupNumber(F1P, File1Start);
F2P = BackupNumber(F2P, File2Start);
// If we found the end, we succeeded.
if (F1P >= File1End && F2P >= File2End) return 0;

// Now that we are at the start of the numbers, compare them, exiting if
// they don't match.
CompareNumbers(F1P, F2P, File1End, File2End);

// If we found the end, we succeeded.
if (F1P >= File1End && F2P >= File2End) return 0;
} catch (const std::string& msg) {
std::cerr << argv[0] << ": error: " << msg << "\n";
return 2;
}

return 1;
}
Expand Down

0 comments on commit 15653af

Please sign in to comment.