Repository navigation
Expand file tree
/
Copy pathMetroNetUtils.cpp
More file actions
66 lines (57 loc) · 1.49 KB
/
Copy pathMetroNetUtils.cpp
File metadata and controls
66 lines (57 loc) · 1.49 KB
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
#include <iostream>
#include <fstream>
#include <sys/stat.h>
using namespace std;
#include "MetroNetUtils.h"
/**
Auxiliary functions for file manipulation.
*/
bool DirectoryExists(const std::string dirname) {
struct stat st;
return stat(dirname.c_str(), &st) == 0;
}
bool FileExists(const std::string filename) {
struct stat st;
if (stat(filename.c_str(), &st) != 0) return false;
ifstream f(filename);
if (f.good()) {
f.close();
return true;
} else {
f.close();
return false;
}
}
bool FileIsEmpty(const std::string filename) {
struct stat st;
if (stat(filename.c_str(), &st) != 0) return true; // File does not exist; thus it is empty
return st.st_size == 0;
}
bool FileCompare(const std::string leftFileName, const std::string rightFileName) {
ifstream leftFile, rightFile;
char leftRead, rightRead;
bool result;
// Open the two files.
leftFile.open(leftFileName);
if (!leftFile.is_open()) {
return false;
};
rightFile.open(rightFileName);
if (!rightFile.is_open()) {
leftFile.close();
return false;
};
result = true; // files exist and are open; assume equality unless a counterexamples shows up.
while (result && leftFile.good() && rightFile.good()) {
leftFile.get(leftRead);
rightFile.get(rightRead);
result = (leftRead == rightRead);
};
if (result) {
// last read was still equal; are we at the end of both files ?
result = (!leftFile.good()) && (!rightFile.good());
};
leftFile.close();
rightFile.close();
return result;
}