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
|
#ifndef __KHELPERS_H
#define __KHELPERS_H
#include <shlobj.h>
extern "C" WINSHELLAPI void WINAPI SHFree( LPVOID);
namespace Klever {
inline BOOL BrowseForFolder(CString& folder,LPCTSTR title=NULL,CWnd* pParent=NULL) {
BROWSEINFO bi;
memset(&bi,0,sizeof(bi));
if(pParent)
bi.hwndOwner=pParent->GetSafeHwnd();
CString rv;
bi.pszDisplayName=rv.GetBuffer(MAX_PATH);
bi.lpszTitle=title;
bi.ulFlags=BIF_RETURNONLYFSDIRS;
LPITEMIDLIST lpidl = SHBrowseForFolder(&bi);
if(lpidl){
SHGetPathFromIDList(lpidl,bi.pszDisplayName);
SHFree(lpidl);
rv.ReleaseBuffer();
folder=rv;
return TRUE;
}
rv.ReleaseBuffer();
return FALSE;
}
inline BOOL BrowseForFolder(CString& folder,UINT idTitle,CWnd* pParent=NULL) {
CString title;
VERIFY(title.LoadString(idTitle));
return BrowseForFolder(folder,title,pParent);
}
inline CString GluePathAndFile(LPCTSTR path,LPCTSTR file) {
CString rv = path;
while((!rv.IsEmpty()) && rv[rv.GetLength()-1]=='\\')
rv=rv.Left(rv.GetLength()-1);
rv+='\\';
while(*file && *file=='\\')
file++;
rv+=file;
return rv;
}
inline UINT TokenizeString(CStringList& rv,LPCTSTR string,LPCTSTR delimiter) {
CString s = string;
int found;
int delength = strlen(delimiter);
int rvc = 0;
while((found=s.Find(delimiter))>=0){
rv.AddTail(s.Left(found));
rvc++;
s=s.Mid(found+delength);
}
if(!s.IsEmpty()){
rv.AddTail(s);
rvc++;
}
return rvc;
}
inline BOOL LogRecord(LPCTSTR logFile,LPCTSTR logRecord) {
try{
CFile f(logFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
f.SeekToEnd();
CString s = CTime::GetCurrentTime().Format("[%c] ")+logRecord+"\r\n";
f.Write((LPCTSTR)s,s.GetLength());
}catch(CException* e){
e->Delete();
return FALSE;
}
return TRUE;
}
inline BOOL ReadString(CFile* file,CString& rv) {
rv.Empty();
int nBuffer = 256;
TCHAR* ch = rv.GetBuffer(nBuffer);
int nPos = 0;
BOOL bRV = FALSE;
for(;;){
TCHAR c;
try{
if(file->Read(&c,sizeof(c))!=sizeof(c))
break;
bRV=TRUE;
}catch(CException* e){
e->Delete();
TRACE0("Exception in ReadString\n");
return FALSE;
}
if(nPos>=(nBuffer-1)){
rv.ReleaseBuffer();
ch = rv.GetBuffer(nBuffer=nBuffer+256);
ASSERT(ch);
}
if(c=='\n')
break;
ch[nPos++]=c;
}
ch[nPos]=0;
for(;;){
nPos--;
if(nPos<0)
break;
if(ch[nPos]!='\r')
break;
ch[nPos]=0;
}
rv.ReleaseBuffer();
rv.FreeExtra();
return bRV;
}
inline int LoadStringList(CStringList& list,LPCTSTR section) {
CString n;
list.RemoveAll();
CWinApp* app = AfxGetApp();
ASSERT(app);
for(int tmp=0;;tmp++){
n.Format("%d",tmp);
CString str = app->GetProfileString(section,n,NULL);
if(str.IsEmpty())
break;
list.AddTail(str);
}
return tmp;
}
inline int SaveStringList(CStringList& list,LPCTSTR section) {
CString n;
CWinApp* app = AfxGetApp();
ASSERT(app);
POSITION p = list.GetHeadPosition();
for(int tmp=0;p;tmp++){
n.Format("%d",tmp);
app->WriteProfileString(section,n,list.GetNext(p));
}
n.Format("%d",tmp);
app->WriteProfileString(section,n,NULL);
return tmp;
}
inline BOOL WriteProfileString(LPCTSTR section,LPCTSTR entry,LPCTSTR str) {
CWinApp* app = AfxGetApp();
return app->WriteProfileBinary(section,entry,(LPBYTE)str,strlen(str)+1);
}
inline CString GetProfileString(LPCTSTR section,LPCTSTR entry,LPCTSTR defval) {
CWinApp* app = AfxGetApp();
LPBYTE pData;
UINT nCount;
CString rv = defval;
if(app->GetProfileBinary(section,entry,&pData,&nCount)){
rv = (LPCTSTR)pData;
delete pData;
}
return rv;
}
};
#endif // __KHELPERS_H
|