forked from ColinPitrat/caprice32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileutils.cpp
More file actions
89 lines (77 loc) · 2.23 KB
/
fileutils.cpp
File metadata and controls
89 lines (77 loc) · 2.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Caprice 32
// File IO functions
#include <dirent.h>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctime>
int file_size (int fd) {
struct stat s;
if (!fstat(fd, &s)) {
return s.st_size;
}
return 0;
}
bool file_copy(FILE *in, FILE *out) {
size_t read;
char buffer[1024];
while((read = fread(buffer, 1, 1024, in)) > 0) {
if (fwrite(buffer, 1, read, out) != read) {
break;
}
}
return !(ferror(in) || ferror(out));
}
bool is_directory(std::string filepath) {
struct stat _stat;
return ( (stat(filepath.c_str(), &_stat) == 0) && (S_ISDIR(_stat.st_mode)) );
}
// Returns a vector containing the names of the files in the specified directory
std::vector<std::string> listDirectory(std::string &directory) {
std::vector<std::string> s;
if (directory[directory.size() - 1] != '/') {
directory += "/";
}
DIR* pDir;
struct dirent *pent;
pDir = opendir(directory.c_str());
if (!pDir){
printf ("opendir(%s) failed; terminating\n", directory.c_str());
return s;
}
while ((pent = readdir(pDir))){
std::string fileName = std::string(pent->d_name);
if (fileName != ".." and fileName != ".") {
s.push_back(fileName);
}
}
closedir(pDir);
sort(s.begin(), s.end()); // sort elements
return s;
}
// Returns a vector containing the names of the files having extension "ext" in
// the specified directory
std::vector<std::string> listDirectoryExt(std::string &directory, const std::string &ext) {
std::vector<std::string> allFiles = listDirectory(directory);
std::vector<std::string> matchingFiles;
std::string extension;
for (const auto& fileName : allFiles) {
extension = fileName.substr(fileName.find_last_of('.') + 1);
if (ext == extension) {
matchingFiles.push_back(fileName);
}
}
return matchingFiles;
}
#define TIME_STRING_MAX_LENGTH 80
std::string getDateString() {
char dateString[TIME_STRING_MAX_LENGTH]; // Should be more than enough
time_t t = std::time(nullptr);
if (std::strftime(dateString, sizeof(dateString), "%Y%m%d_%H%M%S", std::localtime(&t))) {
return std::string(dateString);
}
return "unknown_date";
}