llvm.org GIT mirror llvm / 15653af
For PR351: * Change use of ReadFileIntoAddressSpace to sys::MappedFile use. * Shorten a line > 80 chars. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18896 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 14 years ago
1 changed file(s) with 57 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/FileUtilities.h"
15 #include "llvm/System/MappedFile.h"
1616 #include
1717 #include
1818
2828 RelTolerance("r", cl::desc("Relative error tolerated"), cl::init(0));
2929 cl::opt
3030 AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
31 }
32
33
34 /// OpenFile - mmap the specified file into the address space for reading, and
35 /// return the length and address of the buffer.
36 static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){
37 BufPtr = (char*)ReadFileIntoAddressSpace(Filename, Len);
38 if (BufPtr == 0) {
39 std::cerr << "Error: cannot open file '" << Filename << "'\n";
40 exit(2);
41 }
4231 }
4332
4433 static bool isNumberChar(char C) {
124113 int main(int argc, char **argv) {
125114 cl::ParseCommandLineOptions(argc, argv);
126115
127 // mmap in the files.
128 unsigned File1Len, File2Len;
129 char *File1Start, *File2Start;
130 OpenFile(File1, File1Len, File1Start);
131 OpenFile(File2, File2Len, File2Start);
116 try {
117 // map in the files into memory
118 sys::Path F1Path(File1);
119 sys::Path F2Path(File2);
120 sys::MappedFile F1 ( F1Path );
121 sys::MappedFile F2 ( F2Path );
122 F1.map();
123 F2.map();
132124
133 // Okay, now that we opened the files, scan them for the first difference.
134 char *File1End = File1Start+File1Len;
135 char *File2End = File2Start+File2Len;
136 char *F1P = File1Start;
137 char *F2P = File2Start;
125 // Okay, now that we opened the files, scan them for the first difference.
126 char *File1Start = F1.charBase();
127 char *File2Start = F2.charBase();
128 char *File1End = File1Start+F1.size();
129 char *File2End = File2Start+F2.size();
130 char *F1P = File1Start;
131 char *F2P = File2Start;
138132
139 // Scan for the end of file or first difference.
140 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
141 ++F1P, ++F2P;
142
143 // Common case: identifical files.
144 if (F1P == File1End && F2P == File2End) return 0;
145
146 // If the files need padding, do so now.
147 PadFileIfNeeded(File1Start, File1End, F1P);
148 PadFileIfNeeded(File2Start, File2End, F2P);
149
150 while (1) {
151 // Scan for the end of file or next difference.
133 // Scan for the end of file or first difference.
152134 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
153135 ++F1P, ++F2P;
154136
155 if (F1P >= File1End || F2P >= File2End) break;
137 // Common case: identifical files.
138 if (F1P == File1End && F2P == File2End) return 0;
156139
157 // Okay, we must have found a difference. Backup to the start of the
158 // current number each stream is at so that we can compare from the
159 // beginning.
140 // If the files need padding, do so now.
141 PadFileIfNeeded(File1Start, File1End, F1P);
142 PadFileIfNeeded(File2Start, File2End, F2P);
143
144 while (1) {
145 // Scan for the end of file or next difference.
146 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
147 ++F1P, ++F2P;
148
149 if (F1P >= File1End || F2P >= File2End) break;
150
151 // Okay, we must have found a difference. Backup to the start of the
152 // current number each stream is at so that we can compare from the
153 // beginning.
154 F1P = BackupNumber(F1P, File1Start);
155 F2P = BackupNumber(F2P, File2Start);
156
157 // Now that we are at the start of the numbers, compare them, exiting if
158 // they don't match.
159 CompareNumbers(F1P, F2P, File1End, File2End);
160 }
161
162 // Okay, we reached the end of file. If both files are at the end, we
163 // succeeded.
164 bool F1AtEnd = F1P >= File1End;
165 bool F2AtEnd = F2P >= File2End;
166 if (F1AtEnd & F2AtEnd) return 0;
167
168 // Else, we might have run off the end due to a number: backup and retry.
169 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
170 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
160171 F1P = BackupNumber(F1P, File1Start);
161172 F2P = BackupNumber(F2P, File2Start);
162173
163174 // Now that we are at the start of the numbers, compare them, exiting if
164175 // they don't match.
165176 CompareNumbers(F1P, F2P, File1End, File2End);
177
178 // If we found the end, we succeeded.
179 if (F1P >= File1End && F2P >= File2End) return 0;
180
181 } catch (const std::string& msg) {
182 std::cerr << argv[0] << ": error: " << msg << "\n";
183 return 2;
166184 }
167
168 // Okay, we reached the end of file. If both files are at the end, we
169 // succeeded.
170 bool F1AtEnd = F1P >= File1End;
171 bool F2AtEnd = F2P >= File2End;
172 if (F1AtEnd & F2AtEnd) return 0;
173
174 // Otherwise, we might have run off the end due to a number: backup and retry.
175 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
176 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
177 F1P = BackupNumber(F1P, File1Start);
178 F2P = BackupNumber(F2P, File2Start);
179
180 // Now that we are at the start of the numbers, compare them, exiting if
181 // they don't match.
182 CompareNumbers(F1P, F2P, File1End, File2End);
183
184 // If we found the end, we succeeded.
185 if (F1P >= File1End && F2P >= File2End) return 0;
186185
187186 return 1;
188187 }