blob: 234cf7d2604d43706ad0d84632ae4a35abda41fb [file] [log] [blame]
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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 Lattner7a6ff2b2003-08-01 21:16:14 +00009//
10// This file implements a family of utility functions which are useful for doing
11// various things with files.
12//
13//===----------------------------------------------------------------------===//
14
Reid Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/FileUtilities.h"
Reid Spencer9d88d1a2004-12-13 17:01:53 +000016#include "llvm/System/Path.h"
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000017#include <fstream>
18#include <iostream>
Reid Spencer67f6d3a2004-12-15 01:46:54 +000019
Chris Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000022/// 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 Lattner2cdd21c2003-12-14 21:35:53 +000028bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
29 std::string *Error) {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000030 std::ios::openmode io_mode = std::ios::in | std::ios::binary;
31 std::ifstream FileAStream(FileA.c_str(), io_mode);
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000032 if (!FileAStream) {
33 if (Error) *Error = "Couldn't open file '" + FileA + "'";
34 return true;
35 }
36
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000037 std::ifstream FileBStream(FileB.c_str(), io_mode);
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000038 if (!FileBStream) {
39 if (Error) *Error = "Couldn't open file '" + FileB + "'";
40 return true;
41 }
42
43 // Compare the two files...
44 int C1, C2;
45 do {
46 C1 = FileAStream.get();
47 C2 = FileBStream.get();
48 if (C1 != C2) return true;
49 } while (C1 != EOF);
50
51 return false;
52}
53
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000054/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
55/// or if Old does not exist, move the New file over the Old file. Otherwise,
56/// remove the New file.
57///
Chris Lattner2cdd21c2003-12-14 21:35:53 +000058void llvm::MoveFileOverIfUpdated(const std::string &New,
59 const std::string &Old) {
Chris Lattner7a6ff2b2003-08-01 21:16:14 +000060 if (DiffFiles(New, Old)) {
61 if (std::rename(New.c_str(), Old.c_str()))
62 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
63 } else {
64 std::remove(New.c_str());
65 }
66}