digitalmars.D.learn - Getting environment variables?
- Christopher Wright (4/4) Nov 22 2008 Hey all,
- Jarrett Billingsley (7/11) Nov 22 2008 In Tango, there's tango.sys.Environment
- Christopher Wright (6/22) Nov 22 2008 Thanks, that's exactly what I was looking for.
- BCS (2/4) Nov 22 2008 It does.
- Mark Fischer (19/21) May 02 2013 Better late than never...
- Stewart Gordon (23/26) Nov 22 2008 std.c.stdlib.getenv
- novice2 (3/5) Nov 23 2008 don't forget, that D char[] is utf8 and windows char* is 8-bit chars, no...
- Stewart Gordon (15/22) Nov 23 2008 Under what setups can the drive letter be a non-ASCII character?
- novice2 (6/10) Nov 23 2008 i am afraid that windows API named *W works with UCS2 string.
- John C (2/4) Nov 24 2008 Wrong - Windows has used UTF-16 as native since Windows 2000.
- Christopher Wright (2/8) Nov 24 2008 Actually, you're both right. UCS2 is UTF-16.
- Denis Koroskin (12/19) Nov 24 2008 No. A quote from Wikipedia:
- torhu (7/13) Nov 23 2008 I think the 'correct' way on Windows is to use SHGetSpecialFolderPathA.
- Lars Ivar Igesund (7/24) Nov 23 2008 In tango this is available in tango.sys.win32.SpecialPath
Hey all, How do I get environment variables in a D program? I specifically want the path to a user's home folder. Ta muchly.
Nov 22 2008
On Sat, Nov 22, 2008 at 12:55 PM, Christopher Wright <dhasenan gmail.com> wrote:Hey all, How do I get environment variables in a D program? I specifically want the path to a user's home folder. Ta muchly.In Tango, there's tango.sys.Environment (http://www.dsource.org/projects/tango/docs/current/tango.sys.Environment.html) which provides a nice interface to environment variables. In Phobos, I think you have to use the C functions to get at environment variables, but std.path.expandTilde can be abused to get the home folder ;)
Nov 22 2008
Jarrett Billingsley wrote:On Sat, Nov 22, 2008 at 12:55 PM, Christopher Wright <dhasenan gmail.com> wrote:Thanks, that's exactly what I was looking for. I thought (perhaps wrongly) C allowed you to declare main as taking a list of environment variables, which is why I asked here rather than Tango's forums. But D doesn't, so I probably should have asked at the library-specific forums.Hey all, How do I get environment variables in a D program? I specifically want the path to a user's home folder. Ta muchly.In Tango, there's tango.sys.Environment (http://www.dsource.org/projects/tango/docs/current/tango.sys.Environment.html) which provides a nice interface to environment variables. In Phobos, I think you have to use the C functions to get at environment variables, but std.path.expandTilde can be abused to get the home folder ;)
Nov 22 2008
Reply to Christopher,I thought (perhaps wrongly) C allowed you to declare main as taking a list of environment variables,It does.
Nov 22 2008
Better late than never... On Sunday, 23 November 2008 at 02:28:30 UTC, Christopher Wright wrote: ...I thought (perhaps wrongly) C allowed you to declare main as taking a list of environment variables, which is why I askedIndeed, on Unix { not POSIX } and Windows: From Wiki: ====================== Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must always be int;[3] for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h: int main(int argc, char **argv, char **envp); Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:[4] ====================== Mark ~~~~
May 02 2013
"Christopher Wright" <dhasenan gmail.com> wrote in message news:gg9h3f$9uo$1 digitalmars.com...Hey all, How do I get environment variables in a D program?std.c.stdlib.getenv http://www.cplusplus.com/reference/clibrary/cstdlib/getenv.htmlI specifically want the path to a user's home folder.Platform-dependent. For Windows, you can use the concatenation of HOMEDRIVE and HOMEPATH, but I don't know under which versions of Windows these variables are actually set. ---------- import std.c.stdlib, std.string, std.stdio; void main() { string homeDrive, homePath; homeDrive = toString(getenv("HOMEDRIVE")).dup; homePath = toString(getenv("HOMEPATH")).dup; writefln("%s%s", homeDrive, homePath); } ---------- For Unix, I'm informed that the HOME environment variable does it. http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2005-09/0092.html HTH Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Nov 22 2008
homeDrive = toString(getenv("HOMEDRIVE")).dup; homePath = toString(getenv("HOMEPATH")).dup;don't forget, that D char[] is utf8 and windows char* is 8-bit chars, not utf8. so you should import std.windows.charset and use toMBSz() as D->WindowsAPI and fromMBSz as WindowsAPI->D string converter. for example: homeDrive = fromMBSz(getenv("HOMEDRIVE")).dup;
Nov 23 2008
"novice2" <sorry noem.ail> wrote in message news:ggbd96$2drl$1 digitalmars.com...Under what setups can the drive letter be a non-ASCII character? But according to my experiments, getenv works in the OEM encoding rather than the Windows encoding, so you'd need homePath = fromMBSz(getenv("HOMEPATH"), 1).dup; If you want to make sure all Unicode characters are preserved, you're best off using the Windows API wchar[] wpath; wpath.length = GetEnvironmentVariableW("HOMEPATH", null, 0); GetEnvironmentVariableW("HOMEPATH", wpath.ptr, wpath.length); Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.homeDrive = toString(getenv("HOMEDRIVE")).dup; homePath = toString(getenv("HOMEPATH")).dup;don't forget, that D char[] is utf8 and windows char* is 8-bit chars, not utf8. so you should import std.windows.charset and use toMBSz() as D->WindowsAPI and fromMBSz as WindowsAPI->D string converter. for example: homeDrive = fromMBSz(getenv("HOMEDRIVE")).dup;
Nov 23 2008
Under what setups can the drive letter be a non-ASCII character?any non-english windows have folders, usernames, etc with non-ascii chars, therefore this names presents in environment, registry, file API etc.wchar[] wpath; wpath.length = GetEnvironmentVariableW("HOMEPATH", null, 0); GetEnvironmentVariableW("HOMEPATH", wpath.ptr, wpath.length);i am afraid that windows API named *W works with UCS2 string. but D wchar[] is UTF-16. since UCS2 is some sort of subset of UTF-16, then your code above is correct (then lvalue is D wchar[]). but problems can appear in reverse situation - pass D wchar[] to windows API. that is why utf strings in D annoy me - because all strings exchange D<->WindowsAPI shoud be passed thru to-utf and from-utf convertion. other programming languages works fine with windows, not required utf-support editors etc. sorry for offtopic.
Nov 23 2008
novice2 Wrote:i am afraid that windows API named *W works with UCS2 string. but D wchar[] is UTF-16.Wrong - Windows has used UTF-16 as native since Windows 2000.
Nov 24 2008
John C wrote:novice2 Wrote:Actually, you're both right. UCS2 is UTF-16.i am afraid that windows API named *W works with UCS2 string. but D wchar[] is UTF-16.Wrong - Windows has used UTF-16 as native since Windows 2000.
Nov 24 2008
On Mon, 24 Nov 2008 16:53:20 +0300, Christopher Wright <dhasenan gmail.com> wrote:John C wrote:No. A quote from Wikipedia: "Because of the technical similarities and upwards compatibility from UCS-2 to UTF-16, the two encodings are often erroneously conflated and used as if interchangeable, so that strings encoded in UTF-16 are sometimes misidentified as being encoded in UCS-2." "UTF-16 is the native internal representation of text in the Microsoft Windows 2000/XP/2003/Vista/CE; Qualcomm BREW operating systems; the Java and .NET bytecode environments; Mac OS X's Cocoa and Core Foundation frameworks; and the Qt cross-platform graphical widget toolkit." "Older Windows NT systems (prior to Windows 2000) only support UCS-2."novice2 Wrote:Actually, you're both right. UCS2 is UTF-16.i am afraid that windows API named *W works with UCS2 string. but D wchar[] is UTF-16.Wrong - Windows has used UTF-16 as native since Windows 2000.
Nov 24 2008
Christopher Wright wrote:Hey all, How do I get environment variables in a D program? I specifically want the path to a user's home folder. Ta muchly.I think the 'correct' way on Windows is to use SHGetSpecialFolderPathA. Something like this: char[MAX_PATH] buf; SHGetSpecialFolderPathA(null, buf.ptr, CSIDL_PERSONAL, false); char[] dir = toString(buf.ptr); or CSIDL_APPDATA, etc.
Nov 23 2008
torhu wrote:Christopher Wright wrote:In tango this is available in tango.sys.win32.SpecialPath -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the TangoHey all, How do I get environment variables in a D program? I specifically want the path to a user's home folder. Ta muchly.I think the 'correct' way on Windows is to use SHGetSpecialFolderPathA. Something like this: char[MAX_PATH] buf; SHGetSpecialFolderPathA(null, buf.ptr, CSIDL_PERSONAL, false); char[] dir = toString(buf.ptr); or CSIDL_APPDATA, etc.
Nov 23 2008