X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FFileUtilities.cpp;h=6a65ccae3e40e7a3c7caf624b151e7f2f5cff2dc;hb=ef9531efedd2233269f670227fb0e6aae7480d53;hp=8ce9602872a8790b8aefbab8496026e9f9a76794;hpb=c95b5604e0a248f59c2cf2d4ed93122d8c42bd45;p=oota-llvm.git diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp index 8ce9602872a..6a65ccae3e4 100644 --- a/lib/Support/FileUtilities.cpp +++ b/lib/Support/FileUtilities.cpp @@ -17,57 +17,10 @@ #include "llvm/System/MappedFile.h" #include "llvm/ADT/StringExtras.h" #include -#include -#include - +#include +#include using namespace llvm; -/// DiffFiles - Compare the two files specified, returning true if they are -/// different or if there is a file error. If you specify a string to fill in -/// for the error option, it will set the string to an error message if an error -/// occurs, allowing the caller to distinguish between a failed diff and a file -/// system error. -/// -bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB, - std::string *Error) { - std::ios::openmode io_mode = std::ios::in | std::ios::binary; - std::ifstream FileAStream(FileA.c_str(), io_mode); - if (!FileAStream) { - if (Error) *Error = "Couldn't open file '" + FileA + "'"; - return true; - } - - std::ifstream FileBStream(FileB.c_str(), io_mode); - if (!FileBStream) { - if (Error) *Error = "Couldn't open file '" + FileB + "'"; - return true; - } - - // Compare the two files... - int C1, C2; - do { - C1 = FileAStream.get(); - C2 = FileBStream.get(); - if (C1 != C2) return true; - } while (C1 != EOF); - - return false; -} - -/// MoveFileOverIfUpdated - If the file specified by New is different than Old, -/// or if Old does not exist, move the New file over the Old file. Otherwise, -/// remove the New file. -/// -void llvm::MoveFileOverIfUpdated(const std::string &New, - const std::string &Old) { - if (DiffFiles(New, Old)) { - if (std::rename(New.c_str(), Old.c_str())) - std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n"; - } else { - std::remove(New.c_str()); - } -} - static bool isNumberChar(char C) { switch (C) { case '0': case '1': case '2': case '3': case '4': @@ -95,6 +48,14 @@ static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End, std::string *ErrorMsg) { char *F1NumEnd, *F2NumEnd; double V1 = 0.0, V2 = 0.0; + + // If one of the positions is at a space and the other isn't, chomp up 'til + // the end of the space. + while (isspace(*F1P) && F1P != F1End) + ++F1P; + while (isspace(*F2P) && F2P != F2End) + ++F2P; + // If we stop on numbers, compare their difference. if (isNumberChar(*F1P) && isNumberChar(*F2P)) { V1 = strtod(F1P, &F1NumEnd); @@ -142,7 +103,8 @@ static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End, // with a number. Because of this, if needed, we pad the file so that it starts // and ends with a null character. static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) { - if (isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) { + if (FileStart-FileEnd < 2 || + isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) { unsigned FileLen = FileEnd-FileStart; char *NewFile = new char[FileLen+2]; NewFile[0] = 0; // Add null padding @@ -162,31 +124,46 @@ static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) { /// error occurs, allowing the caller to distinguish between a failed diff and a /// file system error. /// -int llvm::DiffFilesWithTolerance(const std::string &FileA, - const std::string &FileB, +int llvm::DiffFilesWithTolerance(const sys::Path &FileA, + const sys::Path &FileB, double AbsTol, double RelTol, std::string *Error) { try { - // Map in the files into memory. - sys::MappedFile F1((sys::Path(FileA))); - sys::MappedFile F2((sys::Path(FileB))); + // Check for zero length files because some systems croak when you try to + // mmap an empty file. + size_t A_size = FileA.getSize(); + size_t B_size = FileB.getSize(); + + // If they are both zero sized then they're the same + if (A_size == 0 && B_size == 0) + return 0; + // If only one of them is zero sized then they can't be the same + if ((A_size == 0 || B_size == 0)) + return 1; + + // Now its safe to mmap the files into memory becasue both files + // have a non-zero size. + sys::MappedFile F1(FileA); + sys::MappedFile F2(FileB); 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 *File1End = File1Start+A_size; + char *File2End = File2Start+B_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 (A_size == B_size) { + // Are the buffers identical? + if (std::memcmp(File1Start, File2Start, A_size) == 0) + return 0; - // Common case: identifical files. - if (F1P == File1End && F2P == File2End) return 0; + if (AbsTol == 0 && RelTol == 0) + return 1; // Files different! + } char *OrigFile1Start = File1Start; char *OrigFile2Start = File2Start; @@ -239,9 +216,9 @@ int llvm::DiffFilesWithTolerance(const std::string &FileA, } if (OrigFile1Start != File1Start) - delete[] File1Start-1; // Back up past null byte + delete[] (File1Start-1); // Back up past null byte if (OrigFile2Start != File2Start) - delete[] File2Start-1; // Back up past null byte + delete[] (File2Start-1); // Back up past null byte return CompareFailed; } catch (const std::string &Msg) { if (Error) *Error = Msg;