1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <unistd.h>
#include "Support.h"
namespace Test {
string
readAll(int fd) {
string result;
char buf[1024 * 32];
ssize_t ret;
while (true) {
do {
ret = read(fd, buf, sizeof(buf));
} while (ret == -1 && errno == EINTR);
if (ret == 0) {
break;
} else if (ret == -1) {
throw SystemException("Cannot read from socket", errno);
} else {
result.append(buf, ret);
}
}
return result;
}
string
replaceString(const string &str, const string &toFind, const string &replaceWith) {
string::size_type pos = str.find(toFind);
if (pos == string::npos) {
return str;
} else {
string result(str);
return result.replace(pos, toFind.size(), replaceWith);
}
}
void
replaceStringInFile(const char *filename, const string &toFind, const string &replaceWith) {
FILE *f = fopen(filename, "r");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for reading");
throw FileSystemException(message, e, filename);
}
string content(readAll(fileno(f)));
fclose(f);
f = fopen(filename, "w");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for writing");
throw FileSystemException(message, e, filename);
}
content = replaceString(content, toFind, replaceWith);
fwrite(content.data(), 1, content.size(), f);
fclose(f);
}
void
touchFile(const char *filename, time_t timestamp) {
FILE *f = fopen(filename, "a");
if (f != NULL) {
fclose(f);
} else if (errno != EISDIR) {
int e = errno;
string message = "Cannot touch file '";
message.append(filename);
message.append("'");
throw FileSystemException(message, e, filename);
}
if (timestamp != (time_t) -1) {
struct utimbuf times;
times.actime = timestamp;
times.modtime = timestamp;
utime(filename, ×);
}
}
} // namespace Test
|