www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert path to file system path on windows

reply Dr.No <jckj33 gmail.com> writes:
How can I do that with D?



var filename =  "C:\path\to\my\file.txt";
var file = new Uri(filename).AbsoluteUri;
// file is "file:///C:/path/to/my/file.txt"

How can I do that in D?
Jun 21 2018
next sibling parent Timoses <timosesu gmail.com> writes:
On Thursday, 21 June 2018 at 18:46:05 UTC, Dr.No wrote:
 How can I do that with D?



 var filename =  "C:\path\to\my\file.txt";
 var file = new Uri(filename).AbsoluteUri;
 // file is "file:///C:/path/to/my/file.txt"

 How can I do that in D?
I don't know of a specific implementation that does the same, but I can point you to some spots you might look into: std.path: https://dlang.org/phobos/std_path.html std.uri: https://dlang.org/phobos/std_uri.html vibe.inet.url: http://vibed.org/api/vibe.inet.url/URL vibe.core.path: http://vibed.org/api/vibe.core.path/ I really feel like vibed's documentation api site is missing some nice examples of usage to get a quick feel of the provided api.
Jun 21 2018
prev sibling parent reply FreeSlave <freeslave93 gmail.com> writes:
On Thursday, 21 June 2018 at 18:46:05 UTC, Dr.No wrote:
 How can I do that with D?



 var filename =  "C:\path\to\my\file.txt";
 var file = new Uri(filename).AbsoluteUri;
 // file is "file:///C:/path/to/my/file.txt"

 How can I do that in D?
import std.stdio; import std.exception; import core.sys.windows.windows; import std.windows.syserror; safe void henforce(HRESULT hres, lazy string msg = null, string file = __FILE__, size_t line = __LINE__) { if (hres != S_OK) throw new WindowsException(hres, msg, file, line); } trusted wstring absoluteUri(string path) { import std.path : absolutePath; import std.utf : toUTF16z; import core.sys.windows.shlwapi; import core.sys.windows.wininet; auto shlwapi = wenforce(LoadLibraryA("Shlwapi"), "Failed to load shlwapi"); enforce(shlwapi !is null); auto urlCreateFromPath = cast(typeof(&UrlCreateFromPathW))wenforce(shlwapi.GetProcAddress("U lCreateFromPathW"), "Failed to find UrlCreateFromPathW"); scope(exit) FreeLibrary(shlwapi); wchar[INTERNET_MAX_URL_LENGTH] buf; auto size = cast(DWORD)buf.length; henforce(urlCreateFromPath(path.absolutePath.toUTF16z, buf.ptr, &size, 0)); return buf[0..size].idup; } int main(string[] args) { foreach(path; args) { writeln(absoluteUri(path)); } return 0; }
Jun 21 2018
parent Dr.No <jckj33 gmail.com> writes:
Thank you very much u all guys.
Jun 30 2018