Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 1 | //===- Support/FileUtilities.cpp - File System Utilities ------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a family of utility functions which are useful for doing |
| 11 | // various things with files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileUtilities.h" |
Reid Spencer | 9d88d1a | 2004-12-13 17:01:53 +0000 | [diff] [blame] | 16 | #include "llvm/System/Path.h" |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <iostream> |
Reid Spencer | 67f6d3a | 2004-12-15 01:46:54 +0000 | [diff] [blame^] | 19 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 22 | /// DiffFiles - Compare the two files specified, returning true if they are |
| 23 | /// different or if there is a file error. If you specify a string to fill in |
| 24 | /// for the error option, it will set the string to an error message if an error |
| 25 | /// occurs, allowing the caller to distinguish between a failed diff and a file |
| 26 | /// system error. |
| 27 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 28 | bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB, |
| 29 | std::string *Error) { |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 30 | std::ifstream FileAStream(FileA.c_str()); |
| 31 | if (!FileAStream) { |
| 32 | if (Error) *Error = "Couldn't open file '" + FileA + "'"; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | std::ifstream FileBStream(FileB.c_str()); |
| 37 | if (!FileBStream) { |
| 38 | if (Error) *Error = "Couldn't open file '" + FileB + "'"; |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | // Compare the two files... |
| 43 | int C1, C2; |
| 44 | do { |
| 45 | C1 = FileAStream.get(); |
| 46 | C2 = FileBStream.get(); |
| 47 | if (C1 != C2) return true; |
| 48 | } while (C1 != EOF); |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 53 | /// MoveFileOverIfUpdated - If the file specified by New is different than Old, |
| 54 | /// or if Old does not exist, move the New file over the Old file. Otherwise, |
| 55 | /// remove the New file. |
| 56 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 57 | void llvm::MoveFileOverIfUpdated(const std::string &New, |
| 58 | const std::string &Old) { |
Chris Lattner | 7a6ff2b | 2003-08-01 21:16:14 +0000 | [diff] [blame] | 59 | if (DiffFiles(New, Old)) { |
| 60 | if (std::rename(New.c_str(), Old.c_str())) |
| 61 | std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n"; |
| 62 | } else { |
| 63 | std::remove(New.c_str()); |
| 64 | } |
| 65 | } |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 66 | |
| 67 | /// removeFile - Delete the specified file |
| 68 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 69 | void llvm::removeFile(const std::string &Filename) { |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 70 | std::remove(Filename.c_str()); |
| 71 | } |
| 72 | |
John Criswell | 6991a03 | 2003-09-02 20:14:57 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // FDHandle class implementation |
| 76 | // |
| 77 | |
Chris Lattner | 9b448b7 | 2003-12-29 21:43:58 +0000 | [diff] [blame] | 78 | FDHandle::~FDHandle() throw() { |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 79 | if (FD != -1) close(FD); |
| 80 | } |
| 81 | |
Chris Lattner | 9b448b7 | 2003-12-29 21:43:58 +0000 | [diff] [blame] | 82 | FDHandle &FDHandle::operator=(int fd) throw() { |
Chris Lattner | 2d6481c | 2003-12-29 21:35:05 +0000 | [diff] [blame] | 83 | if (FD != -1) close(FD); |
| 84 | FD = fd; |
| 85 | return *this; |
| 86 | } |