|
| 1 | +/* |
| 2 | +** Copyright (C) 2025 nstechbytes. All rights reserved. |
| 3 | +*/ |
| 4 | + |
| 5 | +#include "PathUtils.h" |
| 6 | +#include "Utils.h" |
| 7 | +#include "../API/RainmeterAPI.h" |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +// Path and URI utilities |
| 11 | +std::wstring NormalizeUri(const std::wstring& uri) |
| 12 | +{ |
| 13 | + const std::wstring scheme_sep = L"://"; |
| 14 | + auto scheme_pos = uri.find(scheme_sep); |
| 15 | + if (scheme_pos == std::wstring::npos) |
| 16 | + return uri; |
| 17 | + |
| 18 | + const std::wstring scheme = uri.substr(0, scheme_pos); |
| 19 | + const size_t after_scheme = scheme_pos + scheme_sep.length(); |
| 20 | + |
| 21 | + if (scheme == L"file") |
| 22 | + { |
| 23 | + size_t last_slash = uri.find_last_of(L'/'); |
| 24 | + if (last_slash != std::wstring::npos) |
| 25 | + { |
| 26 | + return uri.substr(0, last_slash + 1); |
| 27 | + } |
| 28 | + return uri; |
| 29 | + } |
| 30 | + |
| 31 | + size_t path_start = uri.find(L'/', after_scheme); |
| 32 | + if (path_start == std::wstring::npos) |
| 33 | + { |
| 34 | + return uri + L"/"; |
| 35 | + } |
| 36 | + |
| 37 | + return uri.substr(0, path_start + 1); |
| 38 | +} |
| 39 | + |
| 40 | +std::wstring NormalizePath(void* rm, LPCWSTR path) |
| 41 | +{ |
| 42 | + if (!path || !*path) |
| 43 | + return {}; |
| 44 | + |
| 45 | + std::wstring value = path; |
| 46 | + |
| 47 | + // Relative path - absolute |
| 48 | + if (value[0] != L'/' && (value.length() < 2 || value[1] != L':')) |
| 49 | + { |
| 50 | + if (LPCWSTR absolutePath = RmPathToAbsolute(rm, value.c_str())) |
| 51 | + { |
| 52 | + value = absolutePath; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Normalize slashes |
| 57 | + for (wchar_t& ch : value) |
| 58 | + { |
| 59 | + if (ch == L'\\') ch = L'/'; |
| 60 | + } |
| 61 | + |
| 62 | + // Enforce extension if required |
| 63 | + auto dotPos = value.find_last_of(L'.'); |
| 64 | + if (dotPos == std::wstring::npos) |
| 65 | + { |
| 66 | + RmLog(rm, LOG_ERROR, L"Execute: File extension is missing, use '.js'."); |
| 67 | + return {}; |
| 68 | + } |
| 69 | + |
| 70 | + std::wstring extension = value.substr(dotPos); |
| 71 | + for (wchar_t& ch : extension) |
| 72 | + ch = towlower(ch); |
| 73 | + |
| 74 | + if (_wcsicmp(extension.c_str(), L".js") != 0) |
| 75 | + { |
| 76 | + std::wstring msg = L"Execute: The file extension '"; |
| 77 | + msg.append(extension); |
| 78 | + msg += L"' is not supported. Use '.js' extension."; |
| 79 | + RmLog(rm, LOG_ERROR, msg.c_str()); |
| 80 | + return {}; |
| 81 | + } |
| 82 | + return value; |
| 83 | +} |
| 84 | + |
| 85 | +bool IsFilePathSyntax(LPCWSTR input) |
| 86 | +{ |
| 87 | + if (!input || input[0] == L'\0') return false; |
| 88 | + |
| 89 | + bool hasExtension = false; |
| 90 | + bool hasSeparator = false; |
| 91 | + |
| 92 | + const wchar_t* lastDot = nullptr; |
| 93 | + const wchar_t* p = input; |
| 94 | + |
| 95 | + while (*p) { |
| 96 | + if (wcschr(L"<>:\"|?*();'", *p)) { |
| 97 | + if (!(*p == L':' && p == input + 1)) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (*p == L'\\' || *p == L'/') { |
| 103 | + hasSeparator = true; |
| 104 | + lastDot = nullptr; |
| 105 | + } |
| 106 | + else if (*p == L'.') { |
| 107 | + lastDot = p; |
| 108 | + } |
| 109 | + p++; |
| 110 | + } |
| 111 | + |
| 112 | + if (lastDot != nullptr && lastDot != input) { |
| 113 | + if (!iswspace(*(lastDot + 1)) && *(lastDot + 1) != L'\0') { |
| 114 | + hasExtension = true; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + bool hasSpace = (wcschr(input, L' ') != nullptr); |
| 119 | + |
| 120 | + if (hasSpace && !hasSeparator) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + return hasSeparator || hasExtension; |
| 125 | +} |
| 126 | + |
| 127 | +// File reading utilities |
| 128 | +std::wstring ReadScriptFile(const std::wstring& path) |
| 129 | +{ |
| 130 | + std::ifstream is(path, std::ios::binary); |
| 131 | + if (!is) { |
| 132 | + throw std::runtime_error("Failed to open file"); |
| 133 | + } |
| 134 | + |
| 135 | + // Read all bytes |
| 136 | + std::vector<char> bytes((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>()); |
| 137 | + size_t n = bytes.size(); |
| 138 | + if (n == 0) return std::wstring(); |
| 139 | + |
| 140 | + const unsigned char* ub = reinterpret_cast<const unsigned char*>(bytes.data()); |
| 141 | + |
| 142 | + // Detect BOMs |
| 143 | + if (n >= 3 && ub[0] == 0xEFu && ub[1] == 0xBBu && ub[2] == 0xBFu) { |
| 144 | + // UTF-8 with BOM -> skip BOM then convert |
| 145 | + return Utf8ToWstring(reinterpret_cast<const char*>(ub + 3), static_cast<int>(n - 3)); |
| 146 | + } |
| 147 | + |
| 148 | + if (n >= 2 && ub[0] == 0xFFu && ub[1] == 0xFEu) { |
| 149 | + // UTF-16 LE with BOM |
| 150 | + std::wstring out; |
| 151 | + out.reserve((n - 2) / 2); |
| 152 | + for (size_t i = 2; i + 1 < n; i += 2) { |
| 153 | + wchar_t ch = static_cast<wchar_t>(ub[i] | (ub[i + 1] << 8)); |
| 154 | + out.push_back(ch); |
| 155 | + } |
| 156 | + return out; |
| 157 | + } |
| 158 | + |
| 159 | + if (n >= 2 && ub[0] == 0xFEu && ub[1] == 0xFFu) { |
| 160 | + // UTF-16 BE with BOM -> swap bytes |
| 161 | + std::wstring out; |
| 162 | + out.reserve((n - 2) / 2); |
| 163 | + for (size_t i = 2; i + 1 < n; i += 2) { |
| 164 | + wchar_t ch = static_cast<wchar_t>((ub[i] << 8) | ub[i + 1]); |
| 165 | + out.push_back(ch); |
| 166 | + } |
| 167 | + return out; |
| 168 | + } |
| 169 | + |
| 170 | + // No BOM: assume UTF-8 and convert |
| 171 | + return Utf8ToWstring(reinterpret_cast<const char*>(ub), static_cast<int>(n)); |
| 172 | +} |
0 commit comments