-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathUtils.cpp
More file actions
172 lines (143 loc) · 3.82 KB
/
PathUtils.cpp
File metadata and controls
172 lines (143 loc) · 3.82 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
** Copyright (C) 2025 nstechbytes. All rights reserved.
*/
#include "PathUtils.h"
#include "Utils.h"
#include "../API/RainmeterAPI.h"
#include <vector>
// Path and URI utilities
std::wstring NormalizeUri(const std::wstring& uri)
{
const std::wstring scheme_sep = L"://";
auto scheme_pos = uri.find(scheme_sep);
if (scheme_pos == std::wstring::npos)
return uri;
const std::wstring scheme = uri.substr(0, scheme_pos);
const size_t after_scheme = scheme_pos + scheme_sep.length();
if (scheme == L"file")
{
size_t last_slash = uri.find_last_of(L'/');
if (last_slash != std::wstring::npos)
{
return uri.substr(0, last_slash + 1);
}
return uri;
}
size_t path_start = uri.find(L'/', after_scheme);
if (path_start == std::wstring::npos)
{
return uri + L"/";
}
return uri.substr(0, path_start + 1);
}
std::wstring NormalizePath(void* rm, LPCWSTR path)
{
if (!path || !*path)
return {};
std::wstring value = path;
// Relative path - absolute
if (value[0] != L'/' && (value.length() < 2 || value[1] != L':'))
{
if (LPCWSTR absolutePath = RmPathToAbsolute(rm, value.c_str()))
{
value = absolutePath;
}
}
// Normalize slashes
for (wchar_t& ch : value)
{
if (ch == L'\\') ch = L'/';
}
// Enforce extension if required
auto dotPos = value.find_last_of(L'.');
if (dotPos == std::wstring::npos)
{
RmLog(rm, LOG_ERROR, L"Execute: File extension is missing, use '.js'.");
return {};
}
std::wstring extension = value.substr(dotPos);
for (wchar_t& ch : extension)
ch = towlower(ch);
if (_wcsicmp(extension.c_str(), L".js") != 0)
{
std::wstring msg = L"Execute: The file extension '";
msg.append(extension);
msg += L"' is not supported. Use '.js' extension.";
RmLog(rm, LOG_ERROR, msg.c_str());
return {};
}
return value;
}
bool IsFilePathSyntax(LPCWSTR input)
{
if (!input || input[0] == L'\0') return false;
bool hasExtension = false;
bool hasSeparator = false;
const wchar_t* lastDot = nullptr;
const wchar_t* p = input;
while (*p) {
if (wcschr(L"<>:\"|?*();'", *p)) {
if (!(*p == L':' && p == input + 1)) {
return false;
}
}
if (*p == L'\\' || *p == L'/') {
hasSeparator = true;
lastDot = nullptr;
}
else if (*p == L'.') {
lastDot = p;
}
p++;
}
if (lastDot != nullptr && lastDot != input) {
if (!iswspace(*(lastDot + 1)) && *(lastDot + 1) != L'\0') {
hasExtension = true;
}
}
bool hasSpace = (wcschr(input, L' ') != nullptr);
if (hasSpace && !hasSeparator) {
return false;
}
return hasSeparator || hasExtension;
}
// File reading utilities
std::wstring ReadScriptFile(const std::wstring& path)
{
std::ifstream is(path, std::ios::binary);
if (!is) {
throw std::runtime_error("Failed to open file");
}
// Read all bytes
std::vector<char> bytes((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
size_t n = bytes.size();
if (n == 0) return std::wstring();
const unsigned char* ub = reinterpret_cast<const unsigned char*>(bytes.data());
// Detect BOMs
if (n >= 3 && ub[0] == 0xEFu && ub[1] == 0xBBu && ub[2] == 0xBFu) {
// UTF-8 with BOM -> skip BOM then convert
return Utf8ToWstring(reinterpret_cast<const char*>(ub + 3), static_cast<int>(n - 3));
}
if (n >= 2 && ub[0] == 0xFFu && ub[1] == 0xFEu) {
// UTF-16 LE with BOM
std::wstring out;
out.reserve((n - 2) / 2);
for (size_t i = 2; i + 1 < n; i += 2) {
wchar_t ch = static_cast<wchar_t>(ub[i] | (ub[i + 1] << 8));
out.push_back(ch);
}
return out;
}
if (n >= 2 && ub[0] == 0xFEu && ub[1] == 0xFFu) {
// UTF-16 BE with BOM -> swap bytes
std::wstring out;
out.reserve((n - 2) / 2);
for (size_t i = 2; i + 1 < n; i += 2) {
wchar_t ch = static_cast<wchar_t>((ub[i] << 8) | ub[i + 1]);
out.push_back(ch);
}
return out;
}
// No BOM: assume UTF-8 and convert
return Utf8ToWstring(reinterpret_cast<const char*>(ub), static_cast<int>(n));
}