Something went wrong. Try again.
🪄 dd's hex editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218/// OS path utilities.////// Copyright: dd86k <dd@dax.moe>/// License: MIT/// Authors: $(LINK2 https://github.com/dd86k, dd86k)module os.path;
version (Windows){ import core.sys.windows.windef : S_OK; import core.sys.windows.shlobj : CSIDL_PROFILE, CSIDL_LOCAL_APPDATA, CSIDL_APPDATA, SHGetFolderPathA, SHGetFolderPathW; import core.stdc.stdlib : malloc, free; import core.stdc.wchar_ : wcslen; import std.encoding : transcode;}else version (Posix){ import core.sys.posix.unistd : getuid; import core.sys.posix.pwd : getpwuid, passwd; import core.stdc.string : strlen;}
import std.process : environment;import std.path : buildPath;
// NOTE: As of Windows Vista, the SHGetSpecialFolderPathW function is a wrapper// for SHGetKnownFolderPath. The latter not defined in the shlobj module.
/// Short name to show for a target path.////// std.path.baseName reads `\\.\PhysicalDrive0` as a UNC root, share and all,/// and hands back a lone separator, so the device namespaces are taken off/// before asking it./// Params: path = Target path./// Returns: Name to display, or the path itself when it has no shorter form.string displayName(string path){ import std.path : baseName;
version (Windows) { // \\.\ (devices) and \\?\ (unparsed paths, e.g. \\?\C:\dir\file) if (path.length > 4 && path[0..2] == `\\` && (path[2] == '.' || path[2] == '?') && path[3] == '\\') path = path[4..$]; }
string name = baseName(path); return name.length ? name : path; // roots ("C:", "/") have no basename}
/// Device paths keep their device nameunittest{ version (Windows) { assert(displayName(`\\.\PhysicalDrive0`) == "PhysicalDrive0"); assert(displayName(`\\?\PhysicalDrive0`) == "PhysicalDrive0"); assert(displayName(`\\.\C:`) == `C:`); assert(displayName(`\\?\C:\dir\file.bin`) == "file.bin"); assert(displayName(`C:\dir\file.bin`) == "file.bin"); assert(displayName(`\\server\share\file.bin`) == "file.bin"); } else { assert(displayName("/dev/sda") == "sda"); assert(displayName("/dir/file.bin") == "file.bin"); assert(displayName("file.bin") == "file.bin"); }}
/// Get the path to the current user's home folder./// This does not verify if the path exists./// Windows: Typically C:\\Users\\%USERNAME%/// Posix: Typically /home/$USERNAME/// Returns: Path or null on failure.string getHomeFolder(){version (Windows){ // 1. %USERPROFILE% if (string userprofile = environment.get("USERPROFILE")) return userprofile; // 2. %HOMEDRIVE% and %HOMEPATH% string homedrive = environment.get("HOMEDRIVE"); string homepath = environment.get("HOMEPATH"); if (homedrive && homepath) return homedrive ~ homepath; // 3. SHGetFolderPath wchar *buffer = cast(wchar*)malloc(1024); assert(buffer, "malloc failed"); scope(exit) free(buffer); // transcode allocates, so it's safe to free if (SHGetFolderPathW(null, CSIDL_PROFILE, null, 0, buffer) == S_OK) { string path; transcode(buffer[0..wcslen(buffer)], path); return path; }}else version (Posix){ // 1. $HOME if (string home = environment.get("HOME")) return home; // 2. getpwuid+getuid passwd *wd = getpwuid(getuid()); if (wd && wd.pw_dir) return cast(string)wd.pw_dir[0..strlen(wd.pw_dir)];} return null;}
/// Get the path to the current user data folder./// This does not verify if the path exists./// Windows: Typically C:\\Users\\%USERNAME%\\AppData\\Local/// Posix: Typically /home/$USERNAME/.config/// Returns: Path or null on failure.string getUserConfigFolder(){version (Windows){ // 1. %LOCALAPPDATA% if (string localappdata = environment.get("LOCALAPPDATA")) return localappdata; // 2. SHGetFolderPath wchar *buffer = cast(wchar*)malloc(1024); assert(buffer, "malloc failed"); scope(exit) free(buffer); // transcode allocates if (SHGetFolderPathW(null, CSIDL_LOCAL_APPDATA, null, 0, buffer) == S_OK) { string path; transcode(buffer[0..wcslen(buffer)], path); return path; }}else version (Posix){ if (string xdg_config_home = environment.get("XDG_CONFIG_HOME")) return xdg_config_home;} // Fallback string base = getHomeFolder; if (base is null) return null; version (Windows) return buildPath(base, "AppData", "Local");else version (Posix) return buildPath(base, ".config");else return null;}
/// Attempt to get the system folder./// Windows: Usually C:\\ProgramData/// Posix: Usually /etc/// Returns: System config path.string getSystemFolder(){version (Windows){ // 1. %ProgramData% if (string programdata = environment.get("ProgramData")) return programdata; // 2. Fallback (Windows Vista+) return `C:\ProgramData`;}else version (Posix){ return "/etc";}}
/// Attempt to find existing config file on the system./// Params:/// appname = Application name, used for user config folder./// filename = File to get./// Returns: Path to config file, or null.string findConfig(string appname, string filename){ import std.file : exists; // 1. Check in user home folder directly string homedir = getHomeFolder(); if (homedir) { string path = buildPath(homedir, filename); if (exists(path)) return path; } // 2. Check in app config directory string appdir = getUserConfigFolder(); if (appdir) { string path = buildPath(appdir, appname, filename); if (exists(path)) return path; } // 3. Check in system folder string sysdir = getSystemFolder(); if (sysdir) { string path = buildPath(sysdir, appname, filename); if (exists(path)) return path; } return null;}