-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlmisc.h
1352 lines (1105 loc) · 30.2 KB
/
atlmisc.h
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Windows Template Library - WTL version 10.0
// Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __ATLMISC_H__
#define __ATLMISC_H__
#pragma once
#ifndef __ATLAPP_H__
#error atlmisc.h requires atlapp.h to be included first
#endif
#ifndef _WTL_NO_COMPATIBILITY_INCLUDES
#include <atlstr.h>
#include <atltypes.h>
#endif // _WTL_NO_COMPATIBILITY_INCLUDES
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CRecentDocumentListBase<T, t_cchItemLen, t_nFirstID, t_nLastID>
// CRecentDocumentList
// CFindFile
// CRegProperty
// CRegPropertyImpl<T>
//
// Global functions:
// AtlGetStockPen()
// AtlGetStockBrush()
// AtlGetStockFont()
// AtlGetStockPalette()
//
// AtlCompactPath()
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// CSize scalar operators
#if !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
template <class Num>
inline CSize operator *(SIZE s, Num n)
{
return CSize((int)(s.cx * n), (int)(s.cy * n));
};
template <class Num>
inline void operator *=(SIZE & s, Num n)
{
s = s * n;
};
template <class Num>
inline CSize operator /(SIZE s, Num n)
{
return CSize((int)(s.cx / n), (int)(s.cy / n));
};
template <class Num>
inline void operator /=(SIZE & s, Num n)
{
s = s / n;
};
#endif // !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
///////////////////////////////////////////////////////////////////////////////
// CRecentDocumentList - MRU List Support
#ifndef _WTL_MRUEMPTY_TEXT
#define _WTL_MRUEMPTY_TEXT _T("(empty)")
#endif
// forward declaration
inline bool AtlCompactPath(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen);
template <class T, int t_cchItemLen = MAX_PATH, int t_nFirstID = ID_FILE_MRU_FIRST, int t_nLastID = ID_FILE_MRU_LAST>
class CRecentDocumentListBase
{
public:
// Declarations
struct _DocEntry
{
TCHAR szDocName[t_cchItemLen];
bool operator ==(const _DocEntry& de) const
{ return (lstrcmpi(szDocName, de.szDocName) == 0); }
};
enum
{
m_nMaxEntries_Min = 2,
m_nMaxEntries_Max = t_nLastID - t_nFirstID + 1,
m_cchMaxItemLen_Min = 6,
m_cchMaxItemLen_Max = t_cchItemLen,
m_cchItemNameLen = 11
};
// Data members
ATL::CSimpleArray<_DocEntry> m_arrDocs;
int m_nMaxEntries; // default is 4
HMENU m_hMenu;
TCHAR m_szNoEntries[t_cchItemLen];
int m_cchMaxItemLen;
// Constructor
CRecentDocumentListBase() : m_nMaxEntries(4), m_hMenu(NULL), m_cchMaxItemLen(-1)
{
m_szNoEntries[0] = 0;
// These ASSERTs verify values of the template arguments
ATLASSERT(t_cchItemLen > m_cchMaxItemLen_Min);
ATLASSERT(m_nMaxEntries_Max > m_nMaxEntries_Min);
}
// Attributes
HMENU GetMenuHandle() const
{
return m_hMenu;
}
void SetMenuHandle(HMENU hMenu)
{
ATLASSERT((hMenu == NULL) || ::IsMenu(hMenu));
m_hMenu = hMenu;
if((m_hMenu == NULL) || (::GetMenuString(m_hMenu, t_nFirstID, m_szNoEntries, t_cchItemLen, MF_BYCOMMAND) == 0))
{
T* pT = static_cast<T*>(this);
(void)pT; // avoid level 4 warning
ATL::Checked::tcsncpy_s(m_szNoEntries, _countof(m_szNoEntries), pT->GetMRUEmptyText(), _TRUNCATE);
}
}
int GetMaxEntries() const
{
return m_nMaxEntries;
}
void SetMaxEntries(int nMaxEntries)
{
ATLASSERT((nMaxEntries >= m_nMaxEntries_Min) && (nMaxEntries <= m_nMaxEntries_Max));
if(nMaxEntries < m_nMaxEntries_Min)
nMaxEntries = m_nMaxEntries_Min;
else if(nMaxEntries > m_nMaxEntries_Max)
nMaxEntries = m_nMaxEntries_Max;
m_nMaxEntries = nMaxEntries;
}
int GetMaxItemLength() const
{
return m_cchMaxItemLen;
}
void SetMaxItemLength(int cchMaxLen)
{
ATLASSERT(((cchMaxLen >= m_cchMaxItemLen_Min) && (cchMaxLen <= m_cchMaxItemLen_Max)) || (cchMaxLen == -1));
if(cchMaxLen != -1)
{
if(cchMaxLen < m_cchMaxItemLen_Min)
cchMaxLen = m_cchMaxItemLen_Min;
else if(cchMaxLen > m_cchMaxItemLen_Max)
cchMaxLen = m_cchMaxItemLen_Max;
}
m_cchMaxItemLen = cchMaxLen;
T* pT = static_cast<T*>(this);
pT->UpdateMenu();
}
// Operations
BOOL AddToList(LPCTSTR lpstrDocName)
{
_DocEntry de;
errno_t nRet = ATL::Checked::tcsncpy_s(de.szDocName, _countof(de.szDocName), lpstrDocName, _TRUNCATE);
if((nRet != 0) && (nRet != STRUNCATE))
return FALSE;
for(int i = 0; i < m_arrDocs.GetSize(); i++)
{
if(lstrcmpi(m_arrDocs[i].szDocName, lpstrDocName) == 0)
{
m_arrDocs.RemoveAt(i);
break;
}
}
if(m_arrDocs.GetSize() == m_nMaxEntries)
m_arrDocs.RemoveAt(0);
BOOL bRet = m_arrDocs.Add(de);
if(bRet)
{
T* pT = static_cast<T*>(this);
bRet = pT->UpdateMenu();
}
return bRet;
}
// This function is deprecated because it is not safe.
// Use the version below that accepts the buffer length.
__declspec(deprecated)
BOOL GetFromList(int /*nItemID*/, LPTSTR /*lpstrDocName*/)
{
ATLASSERT(FALSE);
return FALSE;
}
BOOL GetFromList(int nItemID, LPTSTR lpstrDocName, int cchLength)
{
int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
return FALSE;
if(lstrlen(m_arrDocs[nIndex].szDocName) >= cchLength)
return FALSE;
ATL::Checked::tcscpy_s(lpstrDocName, cchLength, m_arrDocs[nIndex].szDocName);
return TRUE;
}
#ifdef __ATLSTR_H__
BOOL GetFromList(int nItemID, ATL::CString& strDocName)
{
int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
return FALSE;
strDocName = m_arrDocs[nIndex].szDocName;
return TRUE;
}
#endif // __ATLSTR_H__
BOOL RemoveFromList(int nItemID)
{
int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
BOOL bRet = m_arrDocs.RemoveAt(nIndex);
if(bRet)
{
T* pT = static_cast<T*>(this);
bRet = pT->UpdateMenu();
}
return bRet;
}
BOOL MoveToTop(int nItemID)
{
int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
return FALSE;
_DocEntry de;
de = m_arrDocs[nIndex];
m_arrDocs.RemoveAt(nIndex);
BOOL bRet = m_arrDocs.Add(de);
if(bRet)
{
T* pT = static_cast<T*>(this);
bRet = pT->UpdateMenu();
}
return bRet;
}
BOOL ReadFromRegistry(LPCTSTR lpstrRegKey)
{
T* pT = static_cast<T*>(this);
ATL::CRegKey rkParent;
ATL::CRegKey rk;
LONG lRet = rkParent.Open(HKEY_CURRENT_USER, lpstrRegKey);
if(lRet != ERROR_SUCCESS)
return FALSE;
lRet = rk.Open(rkParent, pT->GetRegKeyName());
if(lRet != ERROR_SUCCESS)
return FALSE;
DWORD dwRet = 0;
lRet = rk.QueryDWORDValue(pT->GetRegCountName(), dwRet);
if(lRet != ERROR_SUCCESS)
return FALSE;
SetMaxEntries(dwRet);
m_arrDocs.RemoveAll();
TCHAR szRetString[t_cchItemLen] = {};
_DocEntry de;
for(int nItem = m_nMaxEntries; nItem > 0; nItem--)
{
TCHAR szBuff[m_cchItemNameLen] = {};
_stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
ULONG ulCount = t_cchItemLen;
lRet = rk.QueryStringValue(szBuff, szRetString, &ulCount);
if(lRet == ERROR_SUCCESS)
{
ATL::Checked::tcscpy_s(de.szDocName, _countof(de.szDocName), szRetString);
m_arrDocs.Add(de);
}
}
rk.Close();
rkParent.Close();
return pT->UpdateMenu();
}
BOOL WriteToRegistry(LPCTSTR lpstrRegKey)
{
T* pT = static_cast<T*>(this);
(void)pT; // avoid level 4 warning
ATL::CRegKey rkParent;
ATL::CRegKey rk;
LONG lRet = rkParent.Create(HKEY_CURRENT_USER, lpstrRegKey);
if(lRet != ERROR_SUCCESS)
return FALSE;
lRet = rk.Create(rkParent, pT->GetRegKeyName());
if(lRet != ERROR_SUCCESS)
return FALSE;
lRet = rk.SetDWORDValue(pT->GetRegCountName(), m_nMaxEntries);
ATLASSERT(lRet == ERROR_SUCCESS);
// set new values
int nItem;
for(nItem = m_arrDocs.GetSize(); nItem > 0; nItem--)
{
TCHAR szBuff[m_cchItemNameLen] = {};
_stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
TCHAR szDocName[t_cchItemLen] = {};
GetFromList(t_nFirstID + nItem - 1, szDocName, t_cchItemLen);
lRet = rk.SetStringValue(szBuff, szDocName);
ATLASSERT(lRet == ERROR_SUCCESS);
}
// delete unused keys
for(nItem = m_arrDocs.GetSize() + 1; nItem <= m_nMaxEntries_Max; nItem++)
{
TCHAR szBuff[m_cchItemNameLen] = {};
_stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
rk.DeleteValue(szBuff);
}
rk.Close();
rkParent.Close();
return TRUE;
}
// Implementation
BOOL UpdateMenu()
{
if(m_hMenu == NULL)
return FALSE;
ATLASSERT(::IsMenu(m_hMenu));
int nItems = ::GetMenuItemCount(m_hMenu);
int nInsertPoint = 0;
for(int i = 0; i < nItems; i++)
{
CMenuItemInfo mi;
mi.fMask = MIIM_ID;
::GetMenuItemInfo(m_hMenu, i, TRUE, &mi);
if (mi.wID == t_nFirstID)
{
nInsertPoint = i;
break;
}
}
ATLASSERT((nInsertPoint < nItems) && "You need a menu item with an ID = t_nFirstID");
for(int j = t_nFirstID; j < (t_nFirstID + m_nMaxEntries); j++)
{
// keep the first one as an insertion point
if (j != t_nFirstID)
::DeleteMenu(m_hMenu, j, MF_BYCOMMAND);
}
TCHAR szItemText[t_cchItemLen + 6] = {}; // add space for &, 2 digits, and a space
int nSize = m_arrDocs.GetSize();
int nItem = 0;
if(nSize > 0)
{
for(nItem = 0; nItem < nSize; nItem++)
{
if(m_cchMaxItemLen == -1)
{
_stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, m_arrDocs[nSize - 1 - nItem].szDocName);
}
else
{
TCHAR szBuff[t_cchItemLen] = {};
T* pT = static_cast<T*>(this);
(void)pT; // avoid level 4 warning
bool bRet = pT->CompactDocumentName(szBuff, m_arrDocs[nSize - 1 - nItem].szDocName, m_cchMaxItemLen);
(void)bRet; // avoid level 4 warning
ATLASSERT(bRet);
_stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, szBuff);
}
::InsertMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION | MF_STRING, t_nFirstID + nItem, szItemText);
}
}
else // empty
{
::InsertMenu(m_hMenu, nInsertPoint, MF_BYPOSITION | MF_STRING, t_nFirstID, m_szNoEntries);
::EnableMenuItem(m_hMenu, t_nFirstID, MF_GRAYED);
nItem++;
}
::DeleteMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION);
return TRUE;
}
// Overrideables
// override to provide a different method of compacting document names
static bool CompactDocumentName(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen)
{
return AtlCompactPath(lpstrOut, lpstrIn, cchLen);
}
static LPCTSTR GetRegKeyName()
{
return _T("Recent Document List");
}
static LPCTSTR GetRegCountName()
{
return _T("DocumentCount");
}
static LPCTSTR GetRegItemName()
{
// Note: This string is a format string used with wsprintf().
// Resulting formatted string must be m_cchItemNameLen or less
// characters long, including the terminating null character.
return _T("Document%i");
}
static LPCTSTR GetMRUEmptyText()
{
return _WTL_MRUEMPTY_TEXT;
}
};
class CRecentDocumentList : public CRecentDocumentListBase<CRecentDocumentList>
{
public:
// nothing here
};
///////////////////////////////////////////////////////////////////////////////
// CFindFile - file search helper class
class CFindFile
{
public:
// Data members
HANDLE m_hFind;
WIN32_FIND_DATA m_fd;
LPTSTR m_lpszRoot;
const TCHAR m_chDirSeparator;
BOOL m_bFound;
// Constructor/destructor
CFindFile() : m_hFind(NULL), m_lpszRoot(NULL), m_chDirSeparator(_T('\\')), m_bFound(FALSE)
{
memset(&m_fd, 0, sizeof(m_fd));
}
~CFindFile()
{
Close();
}
// Attributes
ULONGLONG GetFileSize() const
{
ATLASSERT(m_hFind != NULL);
ULARGE_INTEGER nFileSize = {};
if(m_bFound)
{
nFileSize.LowPart = m_fd.nFileSizeLow;
nFileSize.HighPart = m_fd.nFileSizeHigh;
}
else
{
nFileSize.QuadPart = 0;
}
return nFileSize.QuadPart;
}
BOOL GetFileName(LPTSTR lpstrFileName, int cchLength) const
{
ATLASSERT(m_hFind != NULL);
if(lstrlen(m_fd.cFileName) >= cchLength)
return FALSE;
if(m_bFound)
ATL::Checked::tcscpy_s(lpstrFileName, cchLength, m_fd.cFileName);
return m_bFound;
}
BOOL GetFilePath(LPTSTR lpstrFilePath, int cchLength) const
{
ATLASSERT(m_hFind != NULL);
int nLen = lstrlen(m_lpszRoot);
ATLASSERT(nLen > 0);
if(nLen == 0)
return FALSE;
bool bAddSep = (m_lpszRoot[nLen - 1] != m_chDirSeparator);
if((lstrlen(m_lpszRoot) + (bAddSep ? 1 : 0)) >= cchLength)
return FALSE;
ATL::Checked::tcscpy_s(lpstrFilePath, cchLength, m_lpszRoot);
if(bAddSep)
{
TCHAR szSeparator[2] = { m_chDirSeparator, 0 };
ATL::Checked::tcscat_s(lpstrFilePath, cchLength, szSeparator);
}
ATL::Checked::tcscat_s(lpstrFilePath, cchLength, m_fd.cFileName);
return TRUE;
}
BOOL GetFileTitle(LPTSTR lpstrFileTitle, int cchLength) const
{
ATLASSERT(m_hFind != NULL);
TCHAR szBuff[MAX_PATH] = {};
if(!GetFileName(szBuff, MAX_PATH))
return FALSE;
if(lstrlen(szBuff) >= cchLength)
return FALSE;
// find the last dot
LPTSTR pstrDot = _tcsrchr(szBuff, _T('.'));
if(pstrDot != NULL)
*pstrDot = 0;
ATL::Checked::tcscpy_s(lpstrFileTitle, cchLength, szBuff);
return TRUE;
}
BOOL GetFileURL(LPTSTR lpstrFileURL, int cchLength) const
{
ATLASSERT(m_hFind != NULL);
LPCTSTR lpstrFileURLPrefix = _T("file://");
const int cchPrefix = lstrlen(lpstrFileURLPrefix);
if(cchPrefix >= cchLength)
return FALSE;
ATL::Checked::tcscpy_s(lpstrFileURL, cchLength, lpstrFileURLPrefix);
return GetFilePath(&lpstrFileURL[cchPrefix], cchLength - cchPrefix);
}
BOOL GetRoot(LPTSTR lpstrRoot, int cchLength) const
{
ATLASSERT(m_hFind != NULL);
if(lstrlen(m_lpszRoot) >= cchLength)
return FALSE;
ATL::Checked::tcscpy_s(lpstrRoot, cchLength, m_lpszRoot);
return TRUE;
}
#ifdef __ATLSTR_H__
ATL::CString GetFileName() const
{
ATLASSERT(m_hFind != NULL);
ATL::CString ret;
if(m_bFound)
ret = m_fd.cFileName;
return ret;
}
ATL::CString GetFilePath() const
{
ATLASSERT(m_hFind != NULL);
ATL::CString strResult = m_lpszRoot;
int nLen = strResult.GetLength();
ATLASSERT(nLen > 0);
if(nLen == 0)
return strResult;
if(strResult[nLen - 1] != m_chDirSeparator)
strResult += m_chDirSeparator;
strResult += GetFileName();
return strResult;
}
ATL::CString GetFileTitle() const
{
ATLASSERT(m_hFind != NULL);
ATL::CString strResult;
GetFileTitle(strResult.GetBuffer(MAX_PATH), MAX_PATH);
strResult.ReleaseBuffer();
return strResult;
}
ATL::CString GetFileURL() const
{
ATLASSERT(m_hFind != NULL);
ATL::CString strResult("file://");
strResult += GetFilePath();
return strResult;
}
ATL::CString GetRoot() const
{
ATLASSERT(m_hFind != NULL);
ATL::CString str = m_lpszRoot;
return str;
}
#endif // __ATLSTR_H__
BOOL GetLastWriteTime(FILETIME* pTimeStamp) const
{
ATLASSERT(m_hFind != NULL);
ATLASSERT(pTimeStamp != NULL);
if(m_bFound && (pTimeStamp != NULL))
{
*pTimeStamp = m_fd.ftLastWriteTime;
return TRUE;
}
return FALSE;
}
BOOL GetLastAccessTime(FILETIME* pTimeStamp) const
{
ATLASSERT(m_hFind != NULL);
ATLASSERT(pTimeStamp != NULL);
if(m_bFound && (pTimeStamp != NULL))
{
*pTimeStamp = m_fd.ftLastAccessTime;
return TRUE;
}
return FALSE;
}
BOOL GetCreationTime(FILETIME* pTimeStamp) const
{
ATLASSERT(m_hFind != NULL);
if(m_bFound && (pTimeStamp != NULL))
{
*pTimeStamp = m_fd.ftCreationTime;
return TRUE;
}
return FALSE;
}
BOOL MatchesMask(DWORD dwMask) const
{
ATLASSERT(m_hFind != NULL);
if(m_bFound)
return ((m_fd.dwFileAttributes & dwMask) != 0);
return FALSE;
}
BOOL IsDots() const
{
ATLASSERT(m_hFind != NULL);
// return TRUE if the file name is "." or ".." and
// the file is a directory
BOOL bResult = FALSE;
if(m_bFound && IsDirectory())
{
if((m_fd.cFileName[0] == _T('.')) && ((m_fd.cFileName[1] == _T('\0')) || ((m_fd.cFileName[1] == _T('.')) && (m_fd.cFileName[2] == _T('\0')))))
bResult = TRUE;
}
return bResult;
}
BOOL IsReadOnly() const
{
return MatchesMask(FILE_ATTRIBUTE_READONLY);
}
BOOL IsDirectory() const
{
return MatchesMask(FILE_ATTRIBUTE_DIRECTORY);
}
BOOL IsCompressed() const
{
return MatchesMask(FILE_ATTRIBUTE_COMPRESSED);
}
BOOL IsSystem() const
{
return MatchesMask(FILE_ATTRIBUTE_SYSTEM);
}
BOOL IsHidden() const
{
return MatchesMask(FILE_ATTRIBUTE_HIDDEN);
}
BOOL IsTemporary() const
{
return MatchesMask(FILE_ATTRIBUTE_TEMPORARY);
}
BOOL IsNormal() const
{
return MatchesMask(FILE_ATTRIBUTE_NORMAL);
}
BOOL IsArchived() const
{
return MatchesMask(FILE_ATTRIBUTE_ARCHIVE);
}
// Operations
BOOL FindFile(LPCTSTR pstrName = NULL, bool bAutoLongPath = false)
{
Close();
if(pstrName == NULL)
pstrName = _T("*.*");
if(bAutoLongPath && (lstrlen(pstrName) >= MAX_PATH))
{
LPCTSTR lpstrPrefix = _T("\\\\?\\");
int cchLongPath = lstrlen(lpstrPrefix) + lstrlen(pstrName) + 1;
ATL::CTempBuffer<TCHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
LPTSTR lpstrLongPath = buff.Allocate(cchLongPath);
if(lpstrLongPath != NULL)
{
ATL::Checked::tcscpy_s(lpstrLongPath, cchLongPath, lpstrPrefix);
ATL::Checked::tcscat_s(lpstrLongPath, cchLongPath, pstrName);
m_hFind = ::FindFirstFile(lpstrLongPath, &m_fd);
}
}
else
{
m_hFind = ::FindFirstFile(pstrName, &m_fd);
}
if(m_hFind == INVALID_HANDLE_VALUE)
return FALSE;
int cchRoot = ::GetFullPathName(pstrName, 0, NULL, NULL);
if(cchRoot > 0)
ATLTRY(m_lpszRoot = new TCHAR[cchRoot]);
if(m_lpszRoot == NULL)
return FALSE;
bool bFullPath = (::GetFullPathName(pstrName, cchRoot, m_lpszRoot, NULL) != 0);
// passed name isn't a valid path but was found by the API
ATLASSERT(bFullPath);
if(!bFullPath)
{
Close();
::SetLastError(ERROR_INVALID_NAME);
return FALSE;
}
else
{
// find the last separator
LPTSTR pstrSep = _tcsrchr(m_lpszRoot, m_chDirSeparator);
if(pstrSep != NULL)
*pstrSep = _T('\0');
}
m_bFound = TRUE;
return TRUE;
}
BOOL FindNextFile()
{
ATLASSERT(m_hFind != NULL);
if(m_hFind == NULL)
return FALSE;
if(!m_bFound)
return FALSE;
m_bFound = ::FindNextFile(m_hFind, &m_fd);
return m_bFound;
}
void Close()
{
m_bFound = FALSE;
delete [] m_lpszRoot;
m_lpszRoot = NULL;
if((m_hFind != NULL) && (m_hFind != INVALID_HANDLE_VALUE))
{
::FindClose(m_hFind);
m_hFind = NULL;
}
}
};
///////////////////////////////////////////////////////////////////////////////
// CRegProperty and CRegPropertyImpl<> - properties stored in registry
// How to use: Derive a class from CRegPropertyImpl, add data members
// for properties, and add REGPROP map to map properties to registry value names.
// You can then call Read() and Write() methods to read and write properties to/from registry.
// You can also use CRegProperty class directly, for one time read/write, or for custom stuff.
#define REGPROP_CURRENTUSER 0x0000
#define REGPROP_LOCALMACHINE 0x0001
#define REGPROP_READONLY 0x0002
#define REGPROP_WRITEONLY 0x0004
class CRegProperty
{
public:
// Type declarations
struct BinaryProp
{
void* pBinary;
ULONG uSize; // buffer size in bytes, used size after read
BinaryProp() : pBinary(NULL), uSize(0U)
{ }
};
struct CharArrayProp
{
LPTSTR lpstrText;
ULONG uSize; // buffer size in chars
CharArrayProp() : lpstrText(NULL), uSize(0U)
{ }
};
// Data members
ATL::CRegKey m_regkey;
WORD m_wFlags;
// Constructor
CRegProperty() : m_wFlags(REGPROP_CURRENTUSER)
{ }
// Registry key methods
LSTATUS OpenRegKey(LPCTSTR lpstrRegKey, bool bWrite)
{
ATLASSERT(m_regkey.m_hKey == NULL);
HKEY hKey = ((m_wFlags & REGPROP_LOCALMACHINE) != 0) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
REGSAM sam = KEY_READ | KEY_WRITE;
LSTATUS lRet = -1;
if(bWrite)
lRet = m_regkey.Create(hKey, lpstrRegKey, NULL, 0, ((m_wFlags & REGPROP_WRITEONLY) != 0) ? KEY_WRITE : sam);
else
lRet = m_regkey.Open(hKey, lpstrRegKey, ((m_wFlags & REGPROP_READONLY) != 0) ? KEY_READ : sam);
return lRet;
}
void CloseRegKey()
{
LSTATUS lRet = m_regkey.Close();
(void)lRet; // avoid level 4 warning
ATLASSERT(lRet == ERROR_SUCCESS);
}
// Flag methods
WORD GetFlags() const
{
return m_wFlags;
}
WORD SetFlags(WORD wFlags, WORD wMask = 0)
{
WORD wPrevFlags = m_wFlags;
if(wMask == 0)
m_wFlags = wFlags;
else
m_wFlags = (m_wFlags & ~wMask) | (wFlags & wMask);
return wPrevFlags;
}
// Generic read/write methods
template <class TProp>
LSTATUS ReadProp(LPCTSTR lpstrRegValue, TProp& prop)
{
ATLASSERT(m_regkey.m_hKey != NULL);
DWORD dwRet = 0;
LSTATUS lRet = m_regkey.QueryDWORDValue(lpstrRegValue, dwRet);
if(lRet == ERROR_SUCCESS)
prop = static_cast<TProp>(dwRet);
return lRet;
}
template <class TProp>
LSTATUS WriteProp(LPCTSTR lpstrRegValue, TProp& prop)
{
ATLASSERT(m_regkey.m_hKey != NULL);
return m_regkey.SetDWORDValue(lpstrRegValue, (DWORD)prop);
}
// Specialization for bool
template <>
LSTATUS ReadProp(LPCTSTR lpstrRegValue, bool& bProp)
{
ATLASSERT(m_regkey.m_hKey != NULL);
DWORD dwRet = 0;
LSTATUS lRet = m_regkey.QueryDWORDValue(lpstrRegValue, dwRet);
if(lRet == ERROR_SUCCESS)
bProp = (dwRet != 0);
return lRet;
}
template <>
LSTATUS WriteProp(LPCTSTR lpstrRegValue, bool& bProp)
{
ATLASSERT(m_regkey.m_hKey != NULL);
return m_regkey.SetDWORDValue(lpstrRegValue, bProp ? 1 : 0);
}
// Specialization for HFONT
template <>
LSTATUS ReadProp(LPCTSTR lpstrRegValue, HFONT& hFont)
{
ATLASSERT(m_regkey.m_hKey != NULL);
LOGFONT lf = {};
ULONG uSize = sizeof(lf);
LSTATUS lRet = m_regkey.QueryBinaryValue(lpstrRegValue, &lf, &uSize);
if(lRet == ERROR_SUCCESS)
{
if(hFont != NULL)
::DeleteObject(hFont);
hFont = ::CreateFontIndirect(&lf);
if(hFont == NULL)
lRet = ERROR_INVALID_DATA;
}
return lRet;
}
template <>
LSTATUS WriteProp(LPCTSTR lpstrRegValue, HFONT& hFont)
{
ATLASSERT(m_regkey.m_hKey != NULL);
CLogFont lf(hFont);
return m_regkey.SetBinaryValue(lpstrRegValue, &lf, sizeof(lf));
}
// Specialization for BinaryProp