-
-/// 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());
- }
-}
-
-/// removeFile - Delete the specified file
-///
-void llvm::removeFile(const std::string &Filename) {
- std::remove(Filename.c_str());
-}
-
-/// getUniqueFilename - Return a filename with the specified prefix. If the
-/// file does not exist yet, return it, otherwise add a suffix to make it
-/// unique.
-///
-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);
+// PadFileIfNeeded - If the files are not identical, we will have to be doing
+// numeric comparisons in here. There are bad cases involved where we (i.e.,
+// strtod) might run off the beginning or end of the file if it starts or ends
+// 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 (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
+ NewFile[FileLen+1] = 0; // Add null padding
+ memcpy(NewFile+1, FileStart, FileLen);
+ FP = NewFile+(FP-FileStart)+1;
+ FileStart = NewFile+1;
+ FileEnd = FileStart+FileLen;