| ... | ... | @@ -1263,40 +1263,80 @@ pub const PathSpace = struct { |
| 1263 | 1263 | pub fn span(self: PathSpace) [:0]const u16 { |
| 1264 | 1264 | return self.data[0..self.len :0]; |
| 1265 | 1265 | } |
| 1266 | |
| 1267 | fn ensureNtStyle(self: *PathSpace) void { |
| 1268 | // > File I/O functions in the Windows API convert "/" to "\" as part of |
| 1269 | // > converting the name to an NT-style name, except when using the "\\?\" |
| 1270 | // > prefix as detailed in the following sections. |
| 1271 | // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation |
| 1272 | // Because we want the larger maximum path length for absolute paths, we |
| 1273 | // convert forward slashes to backward slashes here. |
| 1274 | for (self.data[0..self.len]) |*elem| { |
| 1275 | if (elem.* == '/') { |
| 1276 | elem.* = '\\'; |
| 1277 | } |
| 1278 | } |
| 1279 | self.data[self.len] = 0; |
| 1280 | } |
| 1266 | 1281 | }; |
| 1267 | 1282 | |
| 1283 | /// Same as `sliceToPrefixedFileW` but accepts a pointer |
| 1284 | /// to a null-terminated path. |
| 1268 | 1285 | pub fn cStrToPrefixedFileW(s: [*:0]const u8) !PathSpace { |
| 1269 | 1286 | return sliceToPrefixedFileW(mem.spanZ(s)); |
| 1270 | 1287 | } |
| 1271 | 1288 | |
| 1289 | /// Same as `sliceToWin32PrefixedFileW` but accepts a pointer |
| 1290 | /// to a null-terminated path. |
| 1291 | pub fn cStrToWin32PrefixedFileW(s: [*:0]const u8) !PathSpace { |
| 1292 | return sliceToWin32PrefixedFileW(mem.spanZ(s)); |
| 1293 | } |
| 1294 | |
| 1295 | /// Converts the path `s` to WTF16, null-terminated. If the path is absolute, |
| 1296 | /// it will get NT-style prefix `\??\` prepended automatically. For prepending |
| 1297 | /// Win32-style prefix, see `sliceToWin32PrefixedFileW` instead. |
| 1272 | 1298 | pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { |
| 1273 | 1299 | // TODO https://github.com/ziglang/zig/issues/2765 |
| 1274 | 1300 | var path_space: PathSpace = undefined; |
| 1275 | | for (s) |byte| { |
| 1301 | const prefix_index: usize = if (mem.startsWith(u8, s, "\\??\\")) 4 else 0; |
| 1302 | for (s[prefix_index..]) |byte| { |
| 1276 | 1303 | switch (byte) { |
| 1277 | 1304 | '*', '?', '"', '<', '>', '|' => return error.BadPathName, |
| 1278 | 1305 | else => {}, |
| 1279 | 1306 | } |
| 1280 | 1307 | } |
| 1281 | | const start_index = if (mem.startsWith(u8, s, "\\?") or !std.fs.path.isAbsolute(s)) 0 else blk: { |
| 1308 | const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { |
| 1282 | 1309 | const prefix = [_]u16{ '\\', '?', '?', '\\' }; |
| 1283 | 1310 | mem.copy(u16, path_space.data[0..], &prefix); |
| 1284 | 1311 | break :blk prefix.len; |
| 1285 | 1312 | }; |
| 1286 | 1313 | path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); |
| 1287 | 1314 | if (path_space.len > path_space.data.len) return error.NameTooLong; |
| 1288 | | // > File I/O functions in the Windows API convert "/" to "\" as part of |
| 1289 | | // > converting the name to an NT-style name, except when using the "\\?\" |
| 1290 | | // > prefix as detailed in the following sections. |
| 1291 | | // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation |
| 1292 | | // Because we want the larger maximum path length for absolute paths, we |
| 1293 | | // convert forward slashes to backward slashes here. |
| 1294 | | for (path_space.data[0..path_space.len]) |*elem| { |
| 1295 | | if (elem.* == '/') { |
| 1296 | | elem.* = '\\'; |
| 1315 | path_space.ensureNtStyle(); |
| 1316 | return path_space; |
| 1317 | } |
| 1318 | |
| 1319 | /// Converts the path `s` to WTF16, null-terminated. If the path is absolute, |
| 1320 | /// it will get Win32-style extended prefix `\\?\` prepended automatically. For prepending |
| 1321 | /// NT-style prefix, see `sliceToPrefixedFileW` instead. |
| 1322 | pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { |
| 1323 | // TODO https://github.com/ziglang/zig/issues/2765 |
| 1324 | var path_space: PathSpace = undefined; |
| 1325 | const prefix_index: usize = if (mem.startsWith(u8, s, "\\\\?\\")) 4 else 0; |
| 1326 | for (s[prefix_index..]) |byte| { |
| 1327 | switch (byte) { |
| 1328 | '*', '?', '"', '<', '>', '|' => return error.BadPathName, |
| 1329 | else => {}, |
| 1297 | 1330 | } |
| 1298 | 1331 | } |
| 1299 | | path_space.data[path_space.len] = 0; |
| 1332 | const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { |
| 1333 | const prefix = [_]u16{ '\\', '\\', '?', '\\' }; |
| 1334 | mem.copy(u16, path_space.data[0..], &prefix); |
| 1335 | break :blk prefix.len; |
| 1336 | }; |
| 1337 | path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); |
| 1338 | if (path_space.len > path_space.data.len) return error.NameTooLong; |
| 1339 | path_space.ensureNtStyle(); |
| 1300 | 1340 | return path_space; |
| 1301 | 1341 | } |
| 1302 | 1342 | |
| ... | ... | @@ -1313,12 +1353,7 @@ pub fn wToPrefixedFileW(s: []const u16) !PathSpace { |
| 1313 | 1353 | path_space.len = start_index + s.len; |
| 1314 | 1354 | if (path_space.len > path_space.data.len) return error.NameTooLong; |
| 1315 | 1355 | mem.copy(u16, path_space.data[start_index..], s); |
| 1316 | | for (path_space.data[0..path_space.len]) |*elem| { |
| 1317 | | if (elem.* == '/') { |
| 1318 | | elem.* = '\\'; |
| 1319 | | } |
| 1320 | | } |
| 1321 | | path_space.data[path_space.len] = 0; |
| 1356 | path_space.ensureNtStyle(); |
| 1322 | 1357 | return path_space; |
| 1323 | 1358 | } |
| 1324 | 1359 | |