1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a family of utility functions which are useful for doing
11 // various things with files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/FileUtilities.h"
16 #include "llvm/System/Path.h"
17 #include "llvm/System/MappedFile.h"
18 #include "llvm/ADT/StringExtras.h"
23 static bool isNumberChar(char C) {
25 case '0': case '1': case '2': case '3': case '4':
26 case '5': case '6': case '7': case '8': case '9':
27 case '.': case '+': case '-':
29 case 'E': return true;
30 default: return false;
34 static char *BackupNumber(char *Pos, char *FirstChar) {
35 // If we didn't stop in the middle of a number, don't backup.
36 if (!isNumberChar(*Pos)) return Pos;
38 // Otherwise, return to the start of the number.
39 while (Pos > FirstChar && isNumberChar(Pos[-1]))
44 /// CompareNumbers - compare two numbers, returning true if they are different.
45 static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
46 double AbsTolerance, double RelTolerance,
47 std::string *ErrorMsg) {
48 char *F1NumEnd, *F2NumEnd;
49 double V1 = 0.0, V2 = 0.0;
50 // If we stop on numbers, compare their difference.
51 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
52 V1 = strtod(F1P, &F1NumEnd);
53 V2 = strtod(F2P, &F2NumEnd);
55 // Otherwise, the diff failed.
60 if (F1NumEnd == F1P || F2NumEnd == F2P) {
61 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
65 // Check to see if these are inside the absolute tolerance
66 if (AbsTolerance < std::abs(V1-V2)) {
67 // Nope, check the relative tolerance...
70 Diff = std::abs(V1/V2 - 1.0);
72 Diff = std::abs(V2/V1 - 1.0);
74 Diff = 0; // Both zero.
75 if (Diff > RelTolerance) {
77 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
78 ": diff = " + ftostr(Diff) + "\n";
79 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
80 "/" + ftostr(AbsTolerance);
86 // Otherwise, advance our read pointers to the end of the numbers.
87 F1P = F1NumEnd; F2P = F2NumEnd;
91 // PadFileIfNeeded - If the files are not identical, we will have to be doing
92 // numeric comparisons in here. There are bad cases involved where we (i.e.,
93 // strtod) might run off the beginning or end of the file if it starts or ends
94 // with a number. Because of this, if needed, we pad the file so that it starts
95 // and ends with a null character.
96 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
97 if (FileStart-FileEnd < 2 ||
98 isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
99 unsigned FileLen = FileEnd-FileStart;
100 char *NewFile = new char[FileLen+2];
101 NewFile[0] = 0; // Add null padding
102 NewFile[FileLen+1] = 0; // Add null padding
103 memcpy(NewFile+1, FileStart, FileLen);
104 FP = NewFile+(FP-FileStart)+1;
105 FileStart = NewFile+1;
106 FileEnd = FileStart+FileLen;
110 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
111 /// files match, 1 if they are different, and 2 if there is a file error. This
112 /// function differs from DiffFiles in that you can specify an absolete and
113 /// relative FP error that is allowed to exist. If you specify a string to fill
114 /// in for the error option, it will set the string to an error message if an
115 /// error occurs, allowing the caller to distinguish between a failed diff and a
116 /// file system error.
118 int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
119 const sys::Path &FileB,
120 double AbsTol, double RelTol,
121 std::string *Error) {
123 // Check for zero length files because some systems croak when you try to
124 // mmap an empty file.
125 size_t A_size = FileA.getSize();
126 size_t B_size = FileB.getSize();
128 // If they are both zero sized then they're the same
129 if (A_size == 0 && B_size == 0)
131 // If only one of them is zero sized then they can't be the same
132 if ((A_size == 0 || B_size == 0))
135 // Now its safe to mmap the files into memory becasue both files
136 // have a non-zero size.
137 sys::MappedFile F1(FileA);
138 sys::MappedFile F2(FileB);
142 // Okay, now that we opened the files, scan them for the first difference.
143 char *File1Start = F1.charBase();
144 char *File2Start = F2.charBase();
145 char *File1End = File1Start+A_size;
146 char *File2End = File2Start+B_size;
147 char *F1P = File1Start;
148 char *F2P = File2Start;
150 if (A_size == B_size) {
151 // Are the buffers identical?
152 if (std::memcmp(File1Start, File2Start, A_size) == 0)
155 if (AbsTol == 0 && RelTol == 0)
156 return 1; // Files different!
159 char *OrigFile1Start = File1Start;
160 char *OrigFile2Start = File2Start;
162 // If the files need padding, do so now.
163 PadFileIfNeeded(File1Start, File1End, F1P);
164 PadFileIfNeeded(File2Start, File2End, F2P);
166 bool CompareFailed = false;
168 // Scan for the end of file or next difference.
169 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
172 if (F1P >= File1End || F2P >= File2End) break;
174 // Okay, we must have found a difference. Backup to the start of the
175 // current number each stream is at so that we can compare from the
177 F1P = BackupNumber(F1P, File1Start);
178 F2P = BackupNumber(F2P, File2Start);
180 // Now that we are at the start of the numbers, compare them, exiting if
182 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
183 CompareFailed = true;
188 // Okay, we reached the end of file. If both files are at the end, we
190 bool F1AtEnd = F1P >= File1End;
191 bool F2AtEnd = F2P >= File2End;
192 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
193 // Else, we might have run off the end due to a number: backup and retry.
194 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
195 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
196 F1P = BackupNumber(F1P, File1Start);
197 F2P = BackupNumber(F2P, File2Start);
199 // Now that we are at the start of the numbers, compare them, exiting if
201 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
202 CompareFailed = true;
204 // If we found the end, we succeeded.
205 if (F1P < File1End || F2P < File2End)
206 CompareFailed = true;
209 if (OrigFile1Start != File1Start)
210 delete[] (File1Start-1); // Back up past null byte
211 if (OrigFile2Start != File2Start)
212 delete[] (File2Start-1); // Back up past null byte
213 return CompareFailed;
214 } catch (const std::string &Msg) {
215 if (Error) *Error = Msg;