-std::string llvm::getUniqueFilename(const std::string &FilenameBase) {
- if (!std::ifstream(FilenameBase.c_str()))
- return FilenameBase; // Couldn't open the file? Use it!
-
- // Create a pattern for mkstemp...
- char *FNBuffer = new char[FilenameBase.size()+8];
- strcpy(FNBuffer, FilenameBase.c_str());
- strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
-
- // Agree on a temporary file name to use....
-#if defined(HAVE_MKSTEMP) && !defined(_MSC_VER)
- int TempFD;
- if ((TempFD = mkstemp(FNBuffer)) == -1) {
- // FIXME: this should return an emtpy string or something and allow the
- // caller to deal with the error!
- std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
- << " directory!\n";
- exit(1);
- }
-
- // We don't need to hold the temp file descriptor... we will trust that no one
- // will overwrite/delete the file while we are working on it...
- close(TempFD);
-#else
- // If we don't have mkstemp, use the old and obsolete mktemp function.
- if (mktemp(FNBuffer) == 0) {
- // FIXME: this should return an emtpy string or something and allow the
- // caller to deal with the error!
- std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
- << " directory!\n";
- exit(1);
- }
-#endif
-
- std::string Result(FNBuffer);
- delete[] FNBuffer;
- return Result;
-}
+int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
+ const sys::Path &FileB,
+ double AbsTol, double RelTol,
+ std::string *Error) {
+ try {
+ // 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+A_size;
+ char *File2End = File2Start+B_size;
+ char *F1P = File1Start;
+ char *F2P = File2Start;
+
+ if (A_size == B_size) {
+ // Are the buffers identical?
+ if (std::memcmp(File1Start, File2Start, A_size) == 0)
+ return 0;
+
+ if (AbsTol == 0 && RelTol == 0)
+ return 1; // Files different!
+ }