1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dguihub.core.utils;
10 
11 import std.path;
12 import dguihub.core.winapi;
13 import dguihub.core.charset;
14 
15 enum WindowsVersion {
16    unknown = 0,
17    windows2000 = 1,
18    windowsXP = 2,
19    windowsVista = 4,
20    windows7 = 8,
21 }
22 
23 T winCast(T)(Object o) {
24    return cast(T)(cast(void*)o);
25 }
26 
27 T winCast(T)(size_t st) {
28    return cast(T)(cast(void*)st);
29 }
30 
31 HINSTANCE getHInstance() {
32    static HINSTANCE hInst = null;
33 
34    if (!hInst) {
35       hInst = GetModuleHandleW(null);
36    }
37 
38    return hInst;
39 }
40 
41 string getExecutablePath() {
42    static string exePath;
43 
44    if (!exePath.length) {
45       exePath = getModuleFileName(null);
46    }
47 
48    return exePath;
49 }
50 
51 string getStartupPath() {
52    static string startPath;
53 
54    if (!startPath.length) {
55       startPath = std.path.dirName(getExecutablePath());
56    }
57 
58    return startPath;
59 }
60 
61 string getTempPath() {
62    static string tempPath;
63 
64    if (!tempPath.length) {
65       dguihub.core.charset.getTempPath(tempPath);
66    }
67 
68    return tempPath;
69 }
70 
71 string makeFilter(string userFilter) {
72    char[] newFilter;
73    foreach (char ch; userFilter) {
74       if (ch == '|') {
75          newFilter ~= '\0';
76       } else {
77          newFilter ~= ch;
78       }
79    }
80 
81    newFilter ~= '\0';
82    return newFilter.idup;
83 } unittest {
84    assert(makeFilter("a") == "a\0");
85    assert(makeFilter("a|b") == "a\0b\0");
86 }
87 
88 public WindowsVersion getWindowsVersion() {
89    static WindowsVersion ver = WindowsVersion.unknown;
90    static WindowsVersion[uint][uint] versions;
91 
92    if (ver is WindowsVersion.unknown) {
93       if (!versions.length) {
94          versions[5][0] = WindowsVersion.windows2000;
95          versions[5][1] = WindowsVersion.windowsXP;
96          versions[6][0] = WindowsVersion.windowsVista;
97          versions[6][1] = WindowsVersion.windows7;
98       }
99 
100       OSVERSIONINFOW ovi;
101       ovi.dwOSVersionInfoSize = OSVERSIONINFOW.sizeof;
102 
103       GetVersionExW(&ovi);
104 
105       WindowsVersion[uint]* pMajVer = (ovi.dwMajorVersion in versions);
106 
107       if (pMajVer) {
108          WindowsVersion* pMinVer = (ovi.dwMinorVersion in *pMajVer);
109 
110          if (pMinVer) {
111             ver = versions[ovi.dwMajorVersion][ovi.dwMinorVersion];
112          }
113       }
114    }
115 
116    return ver;
117 }
118 
119 /**
120 * Returns a wstring from a wchar array
121 *
122 * see https://github.com/AndrejMitrovic/DWinProgramming/blob/bbd6cc282468add8c8dbb1de1046a40e5ecb6be0/Samples/Chap22/Drum/DrumFile.d
123 */
124 wstring fromWStringz(const wchar* s) {
125    if (s is null) return null;
126 
127    wchar* ptr;
128    for (ptr = cast(wchar*)s; *ptr; ++ptr) {}
129 
130    return to!wstring(s[0..ptr-s]);
131 } unittest {
132    wchar[] buffer = new wchar[](256);
133    buffer[] = '\0';
134    buffer[0] = 'é';
135    wstring res = fromWStringz(buffer.ptr);
136 
137    import std..string;
138    assert(res.length == 1, "len is %s".format(res.length));
139 }