summaryrefslogtreecommitdiffabout
path: root/shared-code/kHelpers.h
Unidiff
Diffstat (limited to 'shared-code/kHelpers.h') (more/less context) (ignore whitespace changes)
-rw-r--r--shared-code/kHelpers.h159
1 files changed, 159 insertions, 0 deletions
diff --git a/shared-code/kHelpers.h b/shared-code/kHelpers.h
new file mode 100644
index 0000000..209c6b0
--- a/dev/null
+++ b/shared-code/kHelpers.h
@@ -0,0 +1,159 @@
1 #ifndef__KHELPERS_H
2#define __KHELPERS_H
3
4#include <shlobj.h>
5
6extern "C" WINSHELLAPI void WINAPI SHFree( LPVOID);
7
8namespace Klever {
9
10 inline BOOL BrowseForFolder(CString& folder,LPCTSTR title=NULL,CWnd* pParent=NULL) {
11 BROWSEINFO bi;
12 memset(&bi,0,sizeof(bi));
13 if(pParent)
14 bi.hwndOwner=pParent->GetSafeHwnd();
15 CString rv;
16 bi.pszDisplayName=rv.GetBuffer(MAX_PATH);
17 bi.lpszTitle=title;
18 bi.ulFlags=BIF_RETURNONLYFSDIRS;
19 LPITEMIDLIST lpidl = SHBrowseForFolder(&bi);
20 if(lpidl){
21 SHGetPathFromIDList(lpidl,bi.pszDisplayName);
22 SHFree(lpidl);
23 rv.ReleaseBuffer();
24 folder=rv;
25 return TRUE;
26 }
27 rv.ReleaseBuffer();
28 return FALSE;
29 }
30 inline BOOL BrowseForFolder(CString& folder,UINT idTitle,CWnd* pParent=NULL) {
31 CString title;
32 VERIFY(title.LoadString(idTitle));
33 return BrowseForFolder(folder,title,pParent);
34 }
35 inline CString GluePathAndFile(LPCTSTR path,LPCTSTR file) {
36 CString rv = path;
37 while((!rv.IsEmpty()) && rv[rv.GetLength()-1]=='\\')
38 rv=rv.Left(rv.GetLength()-1);
39 rv+='\\';
40 while(*file && *file=='\\')
41 file++;
42 rv+=file;
43 return rv;
44 }
45 inline UINT TokenizeString(CStringList& rv,LPCTSTR string,LPCTSTR delimiter) {
46 CString s = string;
47 int found;
48 int delength = strlen(delimiter);
49 int rvc = 0;
50 while((found=s.Find(delimiter))>=0){
51 rv.AddTail(s.Left(found));
52 rvc++;
53 s=s.Mid(found+delength);
54 }
55 if(!s.IsEmpty()){
56 rv.AddTail(s);
57 rvc++;
58 }
59 return rvc;
60 }
61 inline BOOL LogRecord(LPCTSTR logFile,LPCTSTR logRecord) {
62 try{
63 CFile f(logFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
64 f.SeekToEnd();
65 CString s = CTime::GetCurrentTime().Format("[%c] ")+logRecord+"\r\n";
66 f.Write((LPCTSTR)s,s.GetLength());
67 }catch(CException* e){
68 e->Delete();
69 return FALSE;
70 }
71 return TRUE;
72 }
73 inline BOOL ReadString(CFile* file,CString& rv) {
74 rv.Empty();
75 int nBuffer = 256;
76 TCHAR* ch = rv.GetBuffer(nBuffer);
77 int nPos = 0;
78 BOOL bRV = FALSE;
79 for(;;){
80 TCHAR c;
81 try{
82 if(file->Read(&c,sizeof(c))!=sizeof(c))
83 break;
84 bRV=TRUE;
85 }catch(CException* e){
86 e->Delete();
87 TRACE0("Exception in ReadString\n");
88 return FALSE;
89 }
90 if(nPos>=(nBuffer-1)){
91 rv.ReleaseBuffer();
92 ch = rv.GetBuffer(nBuffer=nBuffer+256);
93 ASSERT(ch);
94 }
95 if(c=='\n')
96 break;
97 ch[nPos++]=c;
98 }
99 ch[nPos]=0;
100 for(;;){
101 nPos--;
102 if(nPos<0)
103 break;
104 if(ch[nPos]!='\r')
105 break;
106 ch[nPos]=0;
107 }
108 rv.ReleaseBuffer();
109 rv.FreeExtra();
110 return bRV;
111 }
112
113 inline int LoadStringList(CStringList& list,LPCTSTR section) {
114 CString n;
115 list.RemoveAll();
116 CWinApp* app = AfxGetApp();
117 ASSERT(app);
118 for(int tmp=0;;tmp++){
119 n.Format("%d",tmp);
120 CString str = app->GetProfileString(section,n,NULL);
121 if(str.IsEmpty())
122 break;
123 list.AddTail(str);
124 }
125 return tmp;
126 }
127 inline int SaveStringList(CStringList& list,LPCTSTR section) {
128 CString n;
129 CWinApp* app = AfxGetApp();
130 ASSERT(app);
131 POSITION p = list.GetHeadPosition();
132 for(int tmp=0;p;tmp++){
133 n.Format("%d",tmp);
134 app->WriteProfileString(section,n,list.GetNext(p));
135 }
136 n.Format("%d",tmp);
137 app->WriteProfileString(section,n,NULL);
138 return tmp;
139 }
140
141 inline BOOL WriteProfileString(LPCTSTR section,LPCTSTR entry,LPCTSTR str) {
142 CWinApp* app = AfxGetApp();
143 return app->WriteProfileBinary(section,entry,(LPBYTE)str,strlen(str)+1);
144 }
145 inline CString GetProfileString(LPCTSTR section,LPCTSTR entry,LPCTSTR defval) {
146 CWinApp* app = AfxGetApp();
147 LPBYTE pData;
148 UINT nCount;
149 CString rv = defval;
150 if(app->GetProfileBinary(section,entry,&pData,&nCount)){
151 rv = (LPCTSTR)pData;
152 delete pData;
153 }
154 return rv;
155 }
156
157};
158
159#endif // __KHELPERS_H