From fb8b43dbce8bc310718614384297aeaabb9a3cbb Mon Sep 17 00:00:00 2001 From: Michael Krelin Date: Mon, 05 Jul 2004 01:53:09 +0000 Subject: initial commit into svn repository git-svn-id: http://svn.klever.net/kin/bigbrother/trunk@1 fe716a7a-6dde-0310-88d9-d003556173a8 --- (limited to 'shared-code/kHelpers.h') 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 @@ +#ifndef __KHELPERS_H +#define __KHELPERS_H + +#include + +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 -- cgit v0.9.0.2