blob: 9f9ef30686839e1c659b6a3277001af84b2afa8c [file] [log] [blame]
Chris Lattner7a6ff2b2003-08-01 21:16:14 +00001//===- Support/FileUtilities.cpp - File System Utilities ------------------===//
2//
3// This file implements a family of utility functions which are useful for doing
4// various things with files.
5//
6//===----------------------------------------------------------------------===//
7
8#include "Support/FileUtilities.h"
9#include <fstream>
10#include <iostream>
11#include <cstdio>
12
13/// DiffFiles - Compare the two files specified, returning true if they are
14/// different or if there is a file error. If you specify a string to fill in
15/// for the error option, it will set the string to an error message if an error
16/// occurs, allowing the caller to distinguish between a failed diff and a file
17/// system error.
18///
19bool DiffFiles(const std::string &FileA, const std::string &FileB,
20 std::string *Error) {
21 std::ifstream FileAStream(FileA.c_str());
22 if (!FileAStream) {
23 if (Error) *Error = "Couldn't open file '" + FileA + "'";
24 return true;
25 }
26
27 std::ifstream FileBStream(FileB.c_str());
28 if (!FileBStream) {
29 if (Error) *Error = "Couldn't open file '" + FileB + "'";
30 return true;
31 }
32
33 // Compare the two files...
34 int C1, C2;
35 do {
36 C1 = FileAStream.get();
37 C2 = FileBStream.get();
38 if (C1 != C2) return true;
39 } while (C1 != EOF);
40
41 return false;
42}
43
44
45/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
46/// or if Old does not exist, move the New file over the Old file. Otherwise,
47/// remove the New file.
48///
49void MoveFileOverIfUpdated(const std::string &New, const std::string &Old) {
50 if (DiffFiles(New, Old)) {
51 if (std::rename(New.c_str(), Old.c_str()))
52 std::cerr << "Error renaming '" << New << "' to '" << Old << "'!\n";
53 } else {
54 std::remove(New.c_str());
55 }
56}
Misha Brukman3d1b0c72003-08-07 21:28:50 +000057
58/// removeFile - Delete the specified file
59///
60void removeFile(const std::string &Filename) {
61 std::remove(Filename.c_str());
62}
63
64/// getUniqueFilename - Return a filename with the specified prefix. If the
65/// file does not exist yet, return it, otherwise add a suffix to make it
66/// unique.
67///
68std::string getUniqueFilename(const std::string &FilenameBase) {
69 if (!std::ifstream(FilenameBase.c_str()))
70 return FilenameBase; // Couldn't open the file? Use it!
71
72 // Create a pattern for mkstemp...
73 char *FNBuffer = new char[FilenameBase.size()+8];
74 strcpy(FNBuffer, FilenameBase.c_str());
75 strcpy(FNBuffer+FilenameBase.size(), "-XXXXXX");
76
77 // Agree on a temporary file name to use....
78 int TempFD;
79 if ((TempFD = mkstemp(FNBuffer)) == -1) {
80 std::cerr << "bugpoint: ERROR: Cannot create temporary file in the current "
81 << " directory!\n";
82 exit(1);
83 }
84
85 // We don't need to hold the temp file descriptor... we will trust that noone
86 // will overwrite/delete the file while we are working on it...
87 close(TempFD);
88 std::string Result(FNBuffer);
89 delete[] FNBuffer;
90 return Result;
91}