#! @file paths.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains turning a path into a `file` URI and back, `std::io`'s paths being #! read here so that `std::io` reads nothing of URIs. import std::io; module std::net; #! Makes the `file` URI of an absolute path, as #! `file:///usr/lib/my%20notes.txt`. #! @param at The path, which must be absolute. #! @param alloc The optional allocator for the text the URI holds. #! @returns The URI, with each byte of the path but its delimiters and the #! unreserved bytes percent-encoded, or why there is none. #! @remarks On Windows, `C:\Users` is `file:///C:/Users`, and `\\server\share\x` #! is `file://server/share/x`. pub def uri::from_path(at: &path, alloc: maybe<&mut allocator> = nothing{}): result { if !at.is_absolute { return failure { message = $"`{at.view()}` is relative, and only an absolute path has a `file` URI" }; } var made := string::new(alloc); made.append("file://"); when os == windows { uri::windows_uri(at.view(), &mut made); return uri::split(move(made)); } for byte in at.view() { uri::encode_byte(byte, "/", &mut made); } return uri::split(move(made)); } #! Makes the path a `file` URI names. #! @param this The URI, whose scheme must be `file`. #! @param alloc The optional allocator for the text the path holds. #! @returns The path, decoded from the URI's, or why there is none: the URI #! names another scheme, or a file on another machine, or a path no #! path here can be. A query or fragment is left out. #! @remarks On Windows, a URI naming a machine other than `localhost` names a #! path on its share, as `file://server/share/x` names #! `\\server\share\x`. pub def uri::to_path(&this, alloc: maybe<&mut allocator> = nothing{}): result { if !uri::same_letters(this.scheme, "file") { return failure { message = $"`{this.view()}` is not a `file` URI" }; } var decoded := uri::decode(this.path, alloc); if decoded is not value(&text) { return failure { message = $"`{this.view()}` has a `%` that two hexadecimal digits do not follow" }; } val host := this.host; when os == windows { return uri::windows_path(host, text.view(), alloc); } if !host.is_empty() && !uri::same_letters(host, "localhost") { return failure { message = $"`{this.view()}` names a file on `{host}`, which no path here can" }; } return path::parse(text.view(), alloc); } # The Windows path a `file` URI names, from its host and decoded path: # `\\host\share\x` for a host other than `localhost`, and `C:\x` for `/C:/x`. def uri::windows_path(host: strview, text: strview, alloc: maybe<&mut allocator>): result { if !host.is_empty() && !uri::same_letters(host, "localhost") { var share := string::new(alloc); share.append("\\\\"); share.append(host); share.append(text); return path::parse(share.view(), alloc); } if text.length >= 3 && text[0] == '/' && text[1].is_alpha && text[2] == ':' { return path::parse(text.substr(1, text.length - 1), alloc); } return path::parse(text, alloc); } # Writes what follows `file://` in the URI of an absolute Windows path: the host # and share of `\\host\share\x` as `host/share/x`, and `C:\x` as `/C:/x`. def uri::windows_uri(text: strview, into: &mut string): void { var at := 2u; if text.starts_with("\\\\") { while at < text.length && text[at] != '\\' { uri::encode_byte(text[at], "", into); at++; } } else { into.push('/'); into.push(text[0]); into.push(':'); } while at < text.length { val byte := text[at]; at++; if byte == '\\' { into.push('/'); continue; } uri::encode_byte(byte, "", into); } }