2003-08-01 21:16:14 +00:00
|
|
|
//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-01 21:16:14 +00:00
|
|
|
//
|
|
|
|
// This file implements a family of utility functions which are useful for doing
|
|
|
|
// various things with files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2004-12-13 17:01:53 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2003-08-01 21:16:14 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2004-12-15 01:46:54 +00:00
|
|
|
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-08-01 21:16:14 +00:00
|
|
|
/// DiffFiles - Compare the two files specified, returning true if they are
|
|
|
|
/// different or if there is a file error. If you specify a string to fill in
|
|
|
|
/// for the error option, it will set the string to an error message if an error
|
|
|
|
/// occurs, allowing the caller to distinguish between a failed diff and a file
|
|
|
|
/// system error.
|
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
|
|
|
|
std::string *Error) {
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::in | std::ios::binary;
|
|
|
|
std::ifstream FileAStream(FileA.c_str(), io_mode);
|
2003-08-01 21:16:14 +00:00
|
|
|
if (!FileAStream) {
|
|
|
|
if (Error) *Error = "Couldn't open file '" + FileA + "'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ifstream FileBStream(FileB.c_str(), io_mode);
|
2003-08-01 21:16:14 +00:00
|
|
|
if (!FileBStream) {
|
|
|
|
if (Error) *Error = "Couldn't open file '" + FileB + "'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the two files...
|
|
|
|
int C1, C2;
|
|
|
|
do {
|
|
|
|
C1 = FileAStream.get();
|
|
|
|
C2 = FileBStream.get();
|
|
|
|
if (C1 != C2) return true;
|
|
|
|
} while (C1 != EOF);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
|
|
|
|
/// or if Old does not exist, move the New file over the Old file. Otherwise,
|
|
|
|
/// remove the New file.
|
|
|
|
///
|
2003-12-14 21:35:53 +00:00
|
|
|
void llvm::MoveFileOverIfUpdated(const std::string &New,
|
|
|
|
const std::string &Old) {
|
2003-08-01 21:16:14 +00:00
|
|
|
if (DiffFiles(New, Old)) {
|
|
|
|
if (std::rename(New.c_str(), Old.c_str()))
|
|
|
|
std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
|
|
|
|
} else {
|
|
|
|
std::remove(New.c_str());
|
|
|
|
}
|
|
|
|
}
|