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"
22 static bool isNumberChar(char C) {
24 case '0': case '1': case '2': case '3': case '4':
25 case '5': case '6': case '7': case '8': case '9':
26 case '.': case '+': case '-':
28 case 'E': return true;
29 default: return false;
33 static char *BackupNumber(char *Pos, char *FirstChar) {
34 // If we didn't stop in the middle of a number, don't backup.
35 if (!isNumberChar(*Pos)) return Pos;
37 // Otherwise, return to the start of the number.
38 while (Pos > FirstChar && isNumberChar(Pos[-1]))
43 /// CompareNumbers - compare two numbers, returning true if they are different.
44 static bool CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End,
45 double AbsTolerance, double RelTolerance,
46 std::string *ErrorMsg) {
47 char *F1NumEnd, *F2NumEnd;
48 double V1 = 0.0, V2 = 0.0;
49 // If we stop on numbers, compare their difference.
50 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
51 V1 = strtod(F1P, &F1NumEnd);
52 V2 = strtod(F2P, &F2NumEnd);
54 // Otherwise, the diff failed.
59 if (F1NumEnd == F1P || F2NumEnd == F2P) {
60 if (ErrorMsg) *ErrorMsg = "Comparison failed, not a numeric difference.";
64 // Check to see if these are inside the absolute tolerance
65 if (AbsTolerance < std::abs(V1-V2)) {
66 // Nope, check the relative tolerance...
69 Diff = std::abs(V1/V2 - 1.0);
71 Diff = std::abs(V2/V1 - 1.0);
73 Diff = 0; // Both zero.
74 if (Diff > RelTolerance) {
76 *ErrorMsg = "Compared: " + ftostr(V1) + " and " + ftostr(V2) +
77 ": diff = " + ftostr(Diff) + "\n";
78 *ErrorMsg += "Out of tolerance: rel/abs: " + ftostr(RelTolerance) +
79 "/" + ftostr(AbsTolerance);
85 // Otherwise, advance our read pointers to the end of the numbers.
86 F1P = F1NumEnd; F2P = F2NumEnd;
90 // PadFileIfNeeded - If the files are not identical, we will have to be doing
91 // numeric comparisons in here. There are bad cases involved where we (i.e.,
92 // strtod) might run off the beginning or end of the file if it starts or ends
93 // with a number. Because of this, if needed, we pad the file so that it starts
94 // and ends with a null character.
95 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
96 if (isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
97 unsigned FileLen = FileEnd-FileStart;
98 char *NewFile = new char[FileLen+2];
99 NewFile[0] = 0; // Add null padding
100 NewFile[FileLen+1] = 0; // Add null padding
101 memcpy(NewFile+1, FileStart, FileLen);
102 FP = NewFile+(FP-FileStart)+1;
103 FileStart = NewFile+1;
104 FileEnd = FileStart+FileLen;
108 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
109 /// files match, 1 if they are different, and 2 if there is a file error. This
110 /// function differs from DiffFiles in that you can specify an absolete and
111 /// relative FP error that is allowed to exist. If you specify a string to fill
112 /// in for the error option, it will set the string to an error message if an
113 /// error occurs, allowing the caller to distinguish between a failed diff and a
114 /// file system error.
116 int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
117 const sys::Path &FileB,
118 double AbsTol, double RelTol,
119 std::string *Error) {
121 // Map in the files into memory.
122 sys::MappedFile F1(FileA);
123 sys::MappedFile F2(FileB);
127 // Okay, now that we opened the files, scan them for the first difference.
128 char *File1Start = F1.charBase();
129 char *File2Start = F2.charBase();
130 char *File1End = File1Start+F1.size();
131 char *File2End = File2Start+F2.size();
132 char *F1P = File1Start;
133 char *F2P = File2Start;
135 // Scan for the end of file or first difference.
136 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
139 // Common case: identifical files.
140 if (F1P == File1End && F2P == File2End) return 0;
142 if (AbsTol == 0 && RelTol == 0)
143 return 1; // Files different!
145 char *OrigFile1Start = File1Start;
146 char *OrigFile2Start = File2Start;
148 // If the files need padding, do so now.
149 PadFileIfNeeded(File1Start, File1End, F1P);
150 PadFileIfNeeded(File2Start, File2End, F2P);
152 bool CompareFailed = false;
154 // Scan for the end of file or next difference.
155 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
158 if (F1P >= File1End || F2P >= File2End) break;
160 // Okay, we must have found a difference. Backup to the start of the
161 // current number each stream is at so that we can compare from the
163 F1P = BackupNumber(F1P, File1Start);
164 F2P = BackupNumber(F2P, File2Start);
166 // Now that we are at the start of the numbers, compare them, exiting if
168 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
169 CompareFailed = true;
174 // Okay, we reached the end of file. If both files are at the end, we
176 bool F1AtEnd = F1P >= File1End;
177 bool F2AtEnd = F2P >= File2End;
178 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
179 // Else, we might have run off the end due to a number: backup and retry.
180 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
181 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
182 F1P = BackupNumber(F1P, File1Start);
183 F2P = BackupNumber(F2P, File2Start);
185 // Now that we are at the start of the numbers, compare them, exiting if
187 if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
188 CompareFailed = true;
190 // If we found the end, we succeeded.
191 if (F1P < File1End || F2P < File2End)
192 CompareFailed = true;
195 if (OrigFile1Start != File1Start)
196 delete[] (File1Start-1); // Back up past null byte
197 if (OrigFile2Start != File2Start)
198 delete[] (File2Start-1); // Back up past null byte
199 return CompareFailed;
200 } catch (const std::string &Msg) {
201 if (Error) *Error = Msg;