Welcome to mirror list, hosted at ThFree Co, Russian Federation.

globalhelpers.cpp « windirstat - github.com/windirstat/windirstat.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dced8207ee35520ad6c8fb99f8c98bbb9a7f8737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// globalhelpers.cpp - Implementation of global helper functions
//
// WinDirStat - Directory Statistics
// Copyright (C) 2003-2005 Bernhard Seifert
// Copyright (C) 2004-2017 WinDirStat Team (windirstat.net)
// Copyright (C) 2010 Chris Wimmer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "stdafx.h"
#include "windirstat.h"
#include "globalhelpers.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace
{
    CString FormatLongLongNormal(ULONGLONG n)
    {
        // Returns formatted number like "123.456.789".

        ASSERT(n >= 0);

        CString all;

        do
        {
            int rest = (int)(n % 1000);
            n/= 1000;

            CString s;
            if(n > 0)
            {
                s.Format(_T("%s%03d"), GetLocaleThousandSeparator().GetString(), rest);
            }
            else
            {
                s.Format(_T("%d"), rest);
            }

            all = s + all;
        } while(n > 0);

        return all;
    }

    void CacheString(CString& s, UINT resId, LPCTSTR defaultVal)
    {
        ASSERT(_tcslen(defaultVal) > 0);

        if(s.IsEmpty())
        {
            s = LoadString(resId);

            if(s.IsEmpty())
            {
                s = defaultVal;
            }
        }
    }

}

CString GetLocaleString(LCTYPE lctype, LANGID langid)
{
    LCID lcid = MAKELCID(langid, SORT_DEFAULT);

    int len = ::GetLocaleInfo(lcid, lctype, NULL, 0);
    CString s;

    ::GetLocaleInfo(lcid, lctype, s.GetBuffer(len), len);
    s.ReleaseBuffer();

    return s;
}

CString GetLocaleLanguage(LANGID langid)
{
    CString s = GetLocaleString(LOCALE_SNATIVELANGNAME, langid);

    // In the French case, the system returns "francais",
    // but we want "Francais".

    // TODO: need a Unicode version for this function

    if(s.GetLength() > 0)
    {
        s.SetAt(0, (TCHAR)_totupper(s[0])); // FIXME: same holds for Russian, but _toupper won't work there ...
    }

    return s + _T(" - ") + GetLocaleString(LOCALE_SNATIVECTRYNAME, langid);
}

CString GetLocaleThousandSeparator()
{
    return GetLocaleString(LOCALE_STHOUSAND, GetWDSApp()->GetEffectiveLangid());
}

CString GetLocaleDecimalSeparator()
{
    return GetLocaleString(LOCALE_SDECIMAL, GetWDSApp()->GetEffectiveLangid());
}

CString FormatBytes(ULONGLONG const& n)
{
    if(GetOptions()->IsHumanFormat())
    {
        return FormatLongLongHuman(n);
    }
    else
    {
        return FormatLongLongNormal(n);
    }
}

CString FormatLongLongHuman(ULONGLONG n)
{
    // Returns formatted number like "12,4 GB".
    ASSERT(n >= 0);
    const int base = 1024;
    const int half = base / 2;

    CString s;

    double B = (int)(n % base);
    n/= base;

    double KB = (int)(n % base);
    n/= base;

    double MB = (int)(n % base);
    n/= base;

    double GB = (int)(n % base);
    n/= base;

    double TB = (int)(n);

    if(TB != 0 || GB == base - 1 && MB >= half)
    {
        s.Format(_T("%s %s"), FormatDouble(TB + GB/base).GetString(), GetSpec_TB().GetString());
    }
    else if(GB != 0 || MB == base - 1 && KB >= half)
    {
        s.Format(_T("%s %s"), FormatDouble(GB + MB/base).GetString(), GetSpec_GB().GetString());
    }
    else if(MB != 0 || KB == base - 1 && B >= half)
    {
        s.Format(_T("%s %s"), FormatDouble(MB + KB/base).GetString(), GetSpec_MB().GetString());
    }
    else if(KB != 0)
    {
        s.Format(_T("%s %s"), FormatDouble(KB + B/base).GetString(), GetSpec_KB().GetString());
    }
    else if(B != 0)
    {
        s.Format(_T("%d %s"), (int)B, GetSpec_Bytes().GetString());
    }
    else
    {
        s = _T("0");
    }

    return s;
}


CString FormatCount(ULONGLONG const& n)
{
    return FormatLongLongNormal(n);
}

CString FormatDouble(double d) // "98,4" or "98.4"
{
    ASSERT(d >= 0);

    d += 0.05;

    int i = (int)floor(d);
    int r = (int)(10 * fmod(d, 1));

    CString s;
    s.Format(_T("%d%s%d"), i, GetLocaleDecimalSeparator().GetString(), r);

    return s;
}

CString PadWidthBlanks(CString n, int width)
{
    int blankCount = width - n.GetLength();
    if(blankCount > 0)
    {
        int i = 0;
        CString b;
        LPTSTR psz = b.GetBuffer(blankCount + 1);
        for(i = 0; i < blankCount; i++)
        {
            psz[i]= _T(' ');
        }
        psz[i]= 0;
        b.ReleaseBuffer();

        n = b + n;
    }
    return n;
}

CString FormatFileTime(const FILETIME& t)
{
    SYSTEMTIME st;
    if(!::FileTimeToSystemTime(&t, &st))
    {
        return MdGetWinErrorText(::GetLastError());
    }

    LCID lcid = MAKELCID(GetWDSApp()->GetEffectiveLangid(), SORT_DEFAULT);

    CString date;
    VERIFY(0 < ::GetDateFormat(lcid, DATE_SHORTDATE, &st, NULL, date.GetBuffer(256), 256));
    date.ReleaseBuffer();

    CString time;
    VERIFY(0 < GetTimeFormat(lcid, 0, &st, NULL, time.GetBuffer(256), 256));
    time.ReleaseBuffer();

    return date + _T("  ") + time;
}

CString FormatAttributes(DWORD attr)
{
    if(attr == INVALID_FILE_ATTRIBUTES)
    {
        return wds::strInvalidAttributes;
    }

    CString attributes;
    if(attr & FILE_ATTRIBUTE_READONLY)
    {
        attributes.Append(wds::strAttributeReadonly);
    }

    if(attr & FILE_ATTRIBUTE_HIDDEN)
    {
        attributes.Append(wds::strAttributeHidden);
    }

    if(attr & FILE_ATTRIBUTE_SYSTEM)
    {
        attributes.Append(wds::strAttributeSystem);
    }


    if(attr & FILE_ATTRIBUTE_ARCHIVE)
    {
        attributes.Append(wds::strAttributeArchive);
    }

    if(attr & FILE_ATTRIBUTE_COMPRESSED)
    {
        attributes.Append(wds::strAttributeCompressed   );
    }

    if(attr & FILE_ATTRIBUTE_ENCRYPTED)
    {
        attributes.Append(wds::strAttributeEncrypted);
    }

    return attributes;
}

CString FormatMilliseconds(DWORD ms)
{
    CString ret;
    DWORD sec = (ms + 500) / 1000;

    DWORD s = sec % 60;
    DWORD min = sec / 60;

    DWORD m = min % 60;

    DWORD h = min / 60;

    if(h > 0)
    {
        ret.Format(_T("%u:%02u:%02u"), h, m, s);
    }
    else
    {
        ret.Format(_T("%u:%02u"), m, s);
    }
    return ret;
}

bool GetVolumeName(LPCTSTR rootPath, CString& volumeName)
{
    CString ret;
    DWORD dummy;

    UINT old = SetErrorMode(SEM_FAILCRITICALERRORS);

    bool b = (FALSE != GetVolumeInformation(rootPath, volumeName.GetBuffer(256), 256, &dummy, &dummy, &dummy, NULL, 0));
    volumeName.ReleaseBuffer();

    if(!b)
    {
        VTRACE(_T("GetVolumeInformation(%s) failed: %u"), rootPath, ::GetLastError());
    }

    ::SetErrorMode(old);

    return b;
}

// Given a root path like "C:\", this function
// obtains the volume name and returns a complete display string
// like "BOOT (C:)".
CString FormatVolumeNameOfRootPath(CString rootPath)
{
    CString ret;
    CString volumeName;
    bool b = GetVolumeName(rootPath, volumeName);
    if(b)
    {
        ret = FormatVolumeName(rootPath, volumeName);
    }
    else
    {
        ret = rootPath;
    }
    return ret;
}

CString FormatVolumeName(CString rootPath, CString volumeName)
{
    CString ret;
    ret.Format(_T("%s (%s)"), volumeName.GetString(), rootPath.Left(2).GetString());
    return ret;
}

// The inverse of FormatVolumeNameOfRootPath().
// Given a name like "BOOT (C:)", it returns "C:" (without trailing backslash).
// Or, if name like "C:\", it returns "C:".
CString PathFromVolumeName(CString name)
{
    int i = name.ReverseFind(wds::chrBracketClose);
    if(i == -1)
    {
        ASSERT(name.GetLength() == 3);
        return name.Left(2);
    }

    ASSERT(i != -1);
    int k = name.ReverseFind(wds::chrBracketOpen);
    ASSERT(k != -1);
    ASSERT(k < i);
    CString path = name.Mid(k + 1, i - k - 1);
    ASSERT(path.GetLength() == 2);
    ASSERT(path[1] == wds::chrColon);

    return path;
}

// Retrieve the "fully qualified parse name" of "My Computer"
CString GetParseNameOfMyComputer()
{
    CComPtr<IShellFolder> sf;
    HRESULT hr = ::SHGetDesktopFolder(&sf);
    MdThrowFailed(hr, _T("::SHGetDesktopFolder"));

    CCoTaskMem<LPITEMIDLIST> pidl;
    hr = ::SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidl);
    MdThrowFailed(hr, _T("SHGetSpecialFolderLocation(CSIDL_DRIVES)"));

    STRRET name;
    ZeroMemory(&name, sizeof(name));
    name.uType = STRRET_CSTR;
    hr = sf->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &name);
    MdThrowFailed(hr, _T("GetDisplayNameOf(My Computer)"));

    return MyStrRetToString(pidl, &name);
}

void GetPidlOfMyComputer(LPITEMIDLIST *ppidl)
{
    CComPtr<IShellFolder> sf;
    HRESULT hr = ::SHGetDesktopFolder(&sf);
    MdThrowFailed(hr, _T("SHGetDesktopFolder"));

    hr = ::SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, ppidl);
    MdThrowFailed(hr, _T("SHGetSpecialFolderLocation(CSIDL_DRIVES)"));
}

void ShellExecuteWithAssocDialog(HWND hwnd, LPCTSTR filename)
{
    CWaitCursor wc;

    BOOL bExecuted = ShellExecuteNoThrow(hwnd, NULL, filename, NULL, NULL, SW_SHOWNORMAL);
    if((!bExecuted) && (ERROR_NO_ASSOCIATION == ::GetLastError()))
    {
        // Q192352
        CString sysDir;
        //-- Get the system directory so that we know where Rundll32.exe resides.
        ::GetSystemDirectory(sysDir.GetBuffer(_MAX_PATH), _MAX_PATH);
        sysDir.ReleaseBuffer();

        CString parameters = _T("shell32.dll,OpenAs_RunDLL ");
        bExecuted = ShellExecuteNoThrow(hwnd, _T("open"), _T("RUNDLL32.EXE"), parameters + filename, sysDir, SW_SHOWNORMAL);
    }

    if(!bExecuted)
    {
        MdThrowStringExceptionF(_T("ShellExecute failed: %1!s!"), MdGetWinErrorText(::GetLastError()).GetString());
    }
}

CString GetFolderNameFromPath(LPCTSTR path)
{
    CString s = path;
    int i = s.ReverseFind(wds::chrBackslash);
    if(i < 0)
    {
        return s;
    }
    return s.Left(i);
}

CString GetCOMSPEC()
{
    CString cmd;

    DWORD dw = ::GetEnvironmentVariable(_T("COMSPEC"), cmd.GetBuffer(_MAX_PATH), _MAX_PATH);
    cmd.ReleaseBuffer();

    if(dw == 0)
    {
        VTRACE(_T("COMSPEC not set."));
        cmd = _T("cmd.exe");
    }
    return cmd;
}

DWORD WaitForHandleWithRepainting(HANDLE h, DWORD TimeOut /*= INFINITE*/)
{
    DWORD r = 0;
    // Code derived from MSDN sample "Waiting in a Message Loop".

    while(true)
    {
        // Read all of the messages in this next loop, removing each message as we read it.
        MSG msg;
        while(::PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE))
        {
            ::DispatchMessage(&msg);
        }

        // Wait for WM_PAINT message sent or posted to this queue
        // or for one of the passed handles be set to signaled.
        r = ::MsgWaitForMultipleObjects(1, &h, FALSE, TimeOut, QS_PAINT);

        // The result tells us the type of event we have.
        if(r == WAIT_OBJECT_0 + 1)
        {
            // New messages have arrived.
            // Continue to the top of the always while loop to dispatch them and resume waiting.
            continue;
        }
        else
        {
            // The handle became signaled.
            break;
        }
    }

    return r;
}

bool FolderExists(LPCTSTR path)
{
    CFileFind finder;
    BOOL b = finder.FindFile(path);
    if(b)
    {
        finder.FindNextFile();
        return (FALSE != finder.IsDirectory());
    }
    else
    {
        // Here we land, if path is an UNC drive. In this case we
        // try another FindFile:
        b = finder.FindFile(CString(path) + _T("\\*.*"));
        return (b != false);
    }
}

bool DriveExists(const CString& path)
{
    if(path.GetLength() != 3 || path[1] != wds::chrColon || path[2] != wds::chrBackslash)
    {
        return false;
    }

    CString letter = path.Left(1);
    letter.MakeLower();
    int d = letter[0] - wds::chrSmallA;

    DWORD mask = 0x1 << d;

    if((mask & ::GetLogicalDrives()) == 0)
    {
        return false;
    }

    CString dummy;
    if(!::GetVolumeName(path, dummy))
    {
        return false;
    }

    return true;
}

CString GetUserName()
{
    CString s;
    DWORD size = UNLEN + 1;
    (void)::GetUserName(s.GetBuffer(size), &size);
    s.ReleaseBuffer();
    return s;
}

// drive is a drive spec like C: or C:\ or C:\path (path is ignored).
//
// This function returns
// "", if QueryDosDevice is unsupported or drive doesn't begin with a drive letter,
// 'Information about MS-DOS device names' otherwise:
// Something like
//
// \Device\Harddisk\Volume1                               for a local drive
// \Device\LanmanRedirector\;T:0000000011e98\spock\temp   for a network drive
// \??\C:\programme                                       for a SUBSTed local path
// \??\T:\Neuer Ordner                                    for a SUBSTed SUBSTed path
// \??\UNC\spock\temp                                     for a SUBSTed UNC path
//
// As always, I had to experimentally determine these strings, Microsoft
// didn't think it necessary to document them. (Sometimes I think, they
// even don't document such things internally...)
//
// I hope that a drive is SUBSTed iff this string starts with \??\.
//
// assarbad:
//   It cannot be safely determined whether a path is or is not SUBSTed on NT
//   via this API. You would have to look up the volume mount points because
//   SUBST only works per session by definition whereas volume mount points
//   work across sessions (after restarts).
//
CString MyQueryDosDevice(LPCTSTR drive)
{
    CString d = drive;

    if(d.GetLength() < 2 || d[1] != wds::chrColon)
    {
        return wds::strEmpty;
    }

    d = d.Left(2);

    CString info;
    DWORD dw = ::QueryDosDevice(d, info.GetBuffer(512), 512);
    info.ReleaseBuffer();

    if(dw == 0)
    {
        VTRACE(_T("QueryDosDevice(%s) failed: %s"), d.GetString(), MdGetWinErrorText(::GetLastError()).GetString());
        return wds::strEmpty;
    }

    return info;
}

// drive is a drive spec like C: or C:\ or C:\path (path is ignored).
// 
// This function returnes true, if QueryDosDevice() is supported
// and drive is a SUBSTed drive.
//
bool IsSUBSTedDrive(LPCTSTR drive)
{
    CString info = MyQueryDosDevice(drive);
    return (info.GetLength() >= 4 && info.Left(4) == "\\??\\");
}

CString GetSpec_Bytes()
{
    static CString s;
    CacheString(s, IDS_SPEC_BYTES, _T("Bytes"));
    return s;
}

CString GetSpec_KB()
{
    static CString s;
    CacheString(s, IDS_SPEC_KB, _T("KiB"));
    return s;
}

CString GetSpec_MB()
{
    static CString s;
    CacheString(s, IDS_SPEC_MB, _T("MiB"));
    return s;
}

CString GetSpec_GB()
{
    static CString s;
    CacheString(s, IDS_SPEC_GB, _T("GiB"));
    return s;
}

CString GetSpec_TB()
{
    static CString s;
    CacheString(s, IDS_SPEC_TB, _T("TiB"));
    return s;
}

BOOL IsAdmin()
{
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    PSID pSid;
    if (::AllocateAndInitializeSid(&NtAuthority,
        2,
        SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS,
        0, 0, 0, 0, 0, 0,
        &pSid))
    {
        BOOL bResult = FALSE;
        if (!::CheckTokenMembership( NULL, pSid, &bResult))
        {
            ::FreeSid(pSid);
            return FALSE;
        }
        ::FreeSid(pSid);
        return bResult;
    }

    return FALSE;
}