Repository navigation
Expand file tree
/
Copy pathCommon.cpp
More file actions
134 lines (117 loc) · 4.27 KB
/
Copy pathCommon.cpp
File metadata and controls
134 lines (117 loc) · 4.27 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
#include "Common.h"
std::wstring Widen(const std::string& utf8)
{
if (utf8.empty())
return {};
int len = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), nullptr, 0);
std::wstring wide(len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), &wide[0], len);
return wide;
}
std::string Narrow(const std::wstring& wide)
{
if (wide.empty())
return {};
int len = WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), nullptr, 0, nullptr, nullptr);
std::string utf8(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, wide.data(), static_cast<int>(wide.size()), &utf8[0], len, nullptr, nullptr);
return utf8;
}
std::wstring UrlEncode(const std::wstring& text)
{
std::wstring encoded;
for (unsigned char c : Narrow(text)) {
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encoded += static_cast<wchar_t>(c);
} else {
wchar_t hex[4];
swprintf_s(hex, L"%%%02X", c);
encoded += hex;
}
}
return encoded;
}
bool FileExists(const std::wstring& path)
{
DWORD attributes = GetFileAttributesW(path.c_str());
return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY);
}
std::wstring SanitizeFileName(const std::wstring& name)
{
std::wstring safe;
for (wchar_t c : name) {
if (iswalnum(c) || c == L'-' || c == L'_' || c == L'.')
safe += c;
}
return safe;
}
std::string ReadFileBytes(const std::wstring& path)
{
HANDLE file = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE)
throw AppError{ L"Could not open file:\n" + path };
LARGE_INTEGER size = {};
if (!GetFileSizeEx(file, &size) || size.QuadPart <= 0 || size.QuadPart > (256LL << 20)) {
CloseHandle(file);
throw AppError{ L"Could not read file:\n" + path };
}
std::string bytes(static_cast<size_t>(size.QuadPart), '\0');
DWORD read = 0;
BOOL ok = ReadFile(file, &bytes[0], static_cast<DWORD>(bytes.size()), &read, nullptr);
CloseHandle(file);
if (!ok || read != bytes.size())
throw AppError{ L"Could not read file:\n" + path };
return bytes;
}
std::vector<std::wstring> ListImageFiles(const std::wstring& folder)
{
static const wchar_t* const kExtensions[] = {
L".jpg", L".jpeg", L".png", L".bmp", L".gif", L".webp"
};
std::vector<std::wstring> files;
if (folder.empty())
return files;
WIN32_FIND_DATAW found = {};
HANDLE search = FindFirstFileW((folder + L"\\*").c_str(), &found);
if (search == INVALID_HANDLE_VALUE)
return files;
do {
if (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
std::wstring name = found.cFileName;
size_t dot = name.find_last_of(L'.');
if (dot == std::wstring::npos)
continue;
std::wstring extension = name.substr(dot);
for (wchar_t& c : extension)
c = towlower(c);
for (const wchar_t* candidate : kExtensions) {
if (extension == candidate) {
files.push_back(folder + L"\\" + name);
break;
}
}
} while (FindNextFileW(search, &found));
FindClose(search);
return files;
}
void WriteFileAtomic(const std::wstring& path, const std::string& data)
{
std::wstring staging = path + L"." + std::to_wstring(GetCurrentThreadId()) + L".part";
HANDLE file = CreateFileW(staging.c_str(), GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file == INVALID_HANDLE_VALUE)
throw AppError{ L"Could not create file:\n" + staging };
DWORD written = 0;
BOOL ok = WriteFile(file, data.data(), static_cast<DWORD>(data.size()), &written, nullptr);
CloseHandle(file);
if (!ok || written != data.size()) {
DeleteFileW(staging.c_str());
throw AppError{ L"Could not write file:\n" + staging };
}
if (!MoveFileExW(staging.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING)) {
DeleteFileW(staging.c_str());
throw AppError{ L"Could not finish writing file:\n" + path };
}
}