-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfss.cpp
More file actions
153 lines (127 loc) · 4.36 KB
/
fss.cpp
File metadata and controls
153 lines (127 loc) · 4.36 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "fss.h"
#if WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "str.h"
#include <fstream>
#include <vector>
#include <filesystem>
using namespace std;
namespace fss
{
std::string get_current_dir() {
#if WIN32
TCHAR buffer[MAX_PATH] = {0};
::GetModuleFileName(nullptr, buffer, MAX_PATH);
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
auto r = std::wstring(buffer).substr(0, pos);
return str::to_str(r);
#else
//long size = pathconf(".", _PC_PATH_MAX);
//getcwd()
return "";
#endif
}
std::string get_current_exec_path() {
#if WIN32
TCHAR szFileName[MAX_PATH];
::GetModuleFileName(nullptr, szFileName, MAX_PATH);
return str::to_str(szFileName);
#else
return "";
#endif
}
bool file_exists(const std::string& name) {
std::ifstream f{name};
return f.is_open();
}
size_t get_file_size(const std::string& name) {
std::ifstream fl(name, std::ifstream::ate | std::ifstream::binary);
if(!fl) return -1;
return fl.tellg();
}
std::string get_full_path(const std::string& path) {
if (path.empty() || !path.starts_with('.')) return path;
string r = path.substr(1);
r = get_current_dir() + r;
return r;
}
bool read_binary_file(const std::string& name, unsigned char* buffer) {
std::ifstream fl(name, std::ifstream::ate | std::ifstream::binary);
if(!fl) return false;
auto size = fl.tellg();
fl.seekg(fl.beg);
fl.read((char*)buffer, size);
return true;
}
std::string read_all_text(const std::string& name) {
ifstream ifs(name, ios::in | ios::binary | ios::ate);
if(!ifs) return "";
ifstream::pos_type sz = ifs.tellg();
ifs.seekg(0, ios::beg);
vector<char> bytes(sz);
ifs.read(bytes.data(), sz);
return string(bytes.data(), sz);
}
void write_all_text(const std::string& filename, const std::string& contents) {
ofstream ofs(filename, ios::out | ios::binary | ios::ate);
if(!ofs) return;
ofs << contents;
ofs.flush();
}
void append_all_text(const std::string& filename, const std::string& contents) {
ofstream ofs(filename, ios::out | ios::binary | ios::ate | ios::app);
if(!ofs) return;
ofs << contents;
ofs.flush();
}
unsigned int get_age_in_seconds(const std::string& filename) {
#if WIN32
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
// Retrieve file attributes
if(!::GetFileAttributesExA(filename.c_str(), GetFileExInfoStandard, &fileInfo)) {
//throw std::runtime_error("Failed to get file attributes");
return 0;
}
FILETIME creationTime = fileInfo.ftCreationTime;
// get time now
SYSTEMTIME now;
::GetSystemTime(&now);
FILETIME nowFileTime;
::SystemTimeToFileTime(&now, &nowFileTime);
// get the difference in seconds
ULARGE_INTEGER nowTime;
ULARGE_INTEGER creationTimeInt;
nowTime.LowPart = nowFileTime.dwLowDateTime;
nowTime.HighPart = nowFileTime.dwHighDateTime;
creationTimeInt.LowPart = creationTime.dwLowDateTime;
creationTimeInt.HighPart = creationTime.dwHighDateTime;
ULARGE_INTEGER diff;
diff.QuadPart = nowTime.QuadPart - creationTimeInt.QuadPart;
unsigned int seconds = static_cast<unsigned int>(diff.QuadPart / 10000000ULL);
return seconds;
#endif
return 0;
}
std::string get_temp_file_path(const string& prefix) {
WCHAR temp_path[MAX_PATH];
WCHAR temp_file[MAX_PATH];
// Get the path to the temporary folder
DWORD path_len = ::GetTempPath(MAX_PATH, temp_path);
if (path_len == 0 || path_len > MAX_PATH) {
throw std::runtime_error("Failed to get temp path");
}
// Generate a unique temporary file name
UINT unique_num = ::GetTempFileName(temp_path, str::to_wstr(prefix).c_str(), 0, temp_file);
if (unique_num == 0) {
throw std::runtime_error("Failed to get temp file name");
}
return str::to_str(wstring(temp_file));
}
bool delete_file(const std::string& path) {
// use native c++ file system
return std::remove(path.c_str());
}
}