From bfe1bd0fc087be44c70904aee0fe4276643d206d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 20 Jul 2012 15:56:25 +0000 Subject: - db3x_mmap is completely moved to a class; - the old nightmare in the core "How to detect a db plugin and load it" is eliminated forever; - databases are the usual plugins now (loadable via Load) - dynamic DATABASELINK registration git-svn-id: http://svn.miranda-ng.org/main/trunk@1082 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Clist_modern/src/modern_skinengine.cpp | 110 +++++----- plugins/Db3x/src/init.cpp | 27 +-- plugins/Db3x_mmap/commonheaders.h | 6 +- plugins/Db3x_mmap/database.cpp | 132 +++-------- plugins/Db3x_mmap/database.h | 65 +----- plugins/Db3x_mmap/dbcache.cpp | 162 ++++++-------- plugins/Db3x_mmap/dbcontacts.cpp | 127 +++++------ plugins/Db3x_mmap/dbevents.cpp | 98 ++++----- plugins/Db3x_mmap/dbheaders.cpp | 49 ++--- plugins/Db3x_mmap/dbintf.cpp | 118 +++++++++- plugins/Db3x_mmap/dbintf.h | 98 ++++++++- plugins/Db3x_mmap/dbmodulechain.cpp | 139 ++++-------- plugins/Db3x_mmap/dbsettings.cpp | 290 ++++++++++--------------- plugins/Db3x_mmap/init.cpp | 166 +++++++------- plugins/Db3x_mmap/version.rc | 6 +- plugins/Dbx_mmap_SA/init.cpp | 32 +-- plugins/Dbx_tree/Compatibility.cpp | 5 - plugins/Dbx_tree/Compatibility.h | 1 - plugins/Dbx_tree/DatabaseLink.cpp | 57 +---- plugins/Dbx_tree/init.cpp | 15 +- plugins/UserInfoEx/svc_avatar.cpp | 111 ++-------- 21 files changed, 789 insertions(+), 1025 deletions(-) (limited to 'plugins') diff --git a/plugins/Clist_modern/src/modern_skinengine.cpp b/plugins/Clist_modern/src/modern_skinengine.cpp index 11c8391c4d..d51972abc3 100644 --- a/plugins/Clist_modern/src/modern_skinengine.cpp +++ b/plugins/Clist_modern/src/modern_skinengine.cpp @@ -516,7 +516,7 @@ int SkinEngineUnloadModule() if (pEffectStack) { - for (int i=0; i < pEffectStack->realCount; i++) + for (int i=0; i < pEffectStack->realCount; i++) if (pEffectStack->items[i]) { EFFECTSSTACKITEM * effect = (EFFECTSSTACKITEM*)(pEffectStack->items[i]); mir_free(effect); @@ -1695,7 +1695,7 @@ INT_PTR ske_Service_DrawGlyph(WPARAM wParam,LPARAM lParam) if (pgl->Data == NULL) return -1; gl = (LPGLYPHOBJECT)pgl->Data; if ((gl->Style&7) == ST_SKIP) return ST_SKIP; - if (gl->hGlyph == NULL && gl->hGlyph != (HBITMAP)-1 && + if (gl->hGlyph == NULL && gl->hGlyph != (HBITMAP)-1 && ( (gl->Style&7) == ST_IMAGE || (gl->Style&7) == ST_FRAGMENT || (gl->Style&7) == ST_SOLARIZE )) @@ -1953,65 +1953,57 @@ static HBITMAP ske_LoadGlyphImage_TGA(char * szFilename) //this function is required to load PNG to dib buffer myself HBITMAP ske_LoadGlyphImage_Png2Dib(char * szFilename) { + HANDLE hFile, hMap = NULL; + BYTE* ppMap = NULL; + long cbFileSize = 0; + BITMAPINFOHEADER* pDib; + BYTE* pDibBits; + + if ( !ServiceExists( MS_PNG2DIB )) { + MessageBox( NULL, TranslateT( "You need an image services plugin to process PNG images." ), TranslateT( "Error" ), MB_OK ); + return (HBITMAP)NULL; + } + + if (( hFile = CreateFileA( szFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL )) != INVALID_HANDLE_VALUE ) + if (( hMap = CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, 0, NULL )) != NULL ) + if (( ppMap = ( BYTE* )MapViewOfFile( hMap, FILE_MAP_READ, 0, 0, 0 )) != NULL ) + cbFileSize = GetFileSize( hFile, NULL ); + + if ( cbFileSize != 0 ) { + PNG2DIB param; + param.pSource = ppMap; + param.cbSourceSize = cbFileSize; + param.pResult = &pDib; + if ( CallService( MS_PNG2DIB, 0, ( LPARAM )¶m )) + pDibBits = ( BYTE* )( pDib+1 ); + else + cbFileSize = 0; + } - { - HANDLE hFile, hMap = NULL; - BYTE* ppMap = NULL; - long cbFileSize = 0; - BITMAPINFOHEADER* pDib; - BYTE* pDibBits; - - if ( !ServiceExists( MS_PNG2DIB )) { - MessageBox( NULL, TranslateT( "You need an image services plugin to process PNG images." ), TranslateT( "Error" ), MB_OK ); - return (HBITMAP)NULL; - } - - if (( hFile = CreateFileA( szFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL )) != INVALID_HANDLE_VALUE ) - if (( hMap = CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, 0, NULL )) != NULL ) - if (( ppMap = ( BYTE* )MapViewOfFile( hMap, FILE_MAP_READ, 0, 0, 0 )) != NULL ) - cbFileSize = GetFileSize( hFile, NULL ); - - if ( cbFileSize != 0 ) { - PNG2DIB param; - param.pSource = ppMap; - param.cbSourceSize = cbFileSize; - param.pResult = &pDib; - if ( CallService( MS_PNG2DIB, 0, ( LPARAM )¶m )) - pDibBits = ( BYTE* )( pDib+1 ); - else - cbFileSize = 0; - } - - if ( ppMap != NULL ) UnmapViewOfFile( ppMap ); - if ( hMap != NULL ) CloseHandle( hMap ); - if ( hFile != NULL ) CloseHandle( hFile ); + if ( ppMap != NULL ) UnmapViewOfFile( ppMap ); + if ( hMap != NULL ) CloseHandle( hMap ); + if ( hFile != NULL ) CloseHandle( hFile ); - if ( cbFileSize == 0 ) - return (HBITMAP)NULL; + if ( cbFileSize == 0 ) + return (HBITMAP)NULL; - { - BITMAPINFO* bi = ( BITMAPINFO* )pDib; - BYTE *pt = (BYTE*)bi; - pt += bi->bmiHeader.biSize; - if (bi->bmiHeader.biBitCount != 32) - { - HDC sDC = GetDC( NULL ); - HBITMAP hBitmap = CreateDIBitmap( sDC, pDib, CBM_INIT, pDibBits, bi, DIB_PAL_COLORS ); - SelectObject( sDC, hBitmap ); - DeleteDC( sDC ); - GlobalFree( pDib ); - return hBitmap; - } - else - { - BYTE * ptPixels = pt; - HBITMAP hBitmap = CreateDIBSection(NULL,bi, DIB_RGB_COLORS, (void **)&ptPixels, NULL, 0); - memcpy(ptPixels,pt,bi->bmiHeader.biSizeImage); - GlobalFree( pDib ); - return hBitmap; - } - } - } + HBITMAP hBitmap; + BITMAPINFO* bi = ( BITMAPINFO* )pDib; + BYTE *pt = (BYTE*)bi; + pt += bi->bmiHeader.biSize; + if (bi->bmiHeader.biBitCount != 32) { + HDC sDC = GetDC( NULL ); + hBitmap = CreateDIBitmap( sDC, pDib, CBM_INIT, pDibBits, bi, DIB_PAL_COLORS ); + SelectObject( sDC, hBitmap ); + DeleteDC( sDC ); + } + else { + BYTE *ptPixels = pt; + HBITMAP hBitmap = CreateDIBSection(NULL,bi, DIB_RGB_COLORS, (void **)&ptPixels, NULL, 0); + memcpy(ptPixels,pt,bi->bmiHeader.biSizeImage); + } + GlobalFree( pDib ); + return hBitmap; } static HBITMAP ske_LoadGlyphImageByDecoders(char * szFileName) @@ -2166,7 +2158,7 @@ int ske_UnloadSkin(SKINOBJECTSLIST * Skin) DWORD i; ske_LockSkin(); ClearMaskList(Skin->pMaskList); - + //clear font list if (gl_plSkinFonts && gl_plSkinFonts->realCount > 0) { for (int i=0; i < gl_plSkinFonts->realCount; i++) { diff --git a/plugins/Db3x/src/init.cpp b/plugins/Db3x/src/init.cpp index b2b3f1fa72..ab0e6b4357 100644 --- a/plugins/Db3x/src/init.cpp +++ b/plugins/Db3x/src/init.cpp @@ -44,13 +44,8 @@ CDdxMmap* g_Db = NULL; ///////////////////////////////////////////////////////////////////////////////////////// -static int getCapability( int flag ) -{ - return 0; -} - // returns 0 if the profile is created, EMKPRF* -static int makeDatabase(TCHAR *profile, int * error) +static int makeDatabase(const TCHAR *profile, int * error) { HANDLE hFile = CreateFile(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if ( hFile != INVALID_HANDLE_VALUE ) { @@ -63,7 +58,7 @@ static int makeDatabase(TCHAR *profile, int * error) } // returns 0 if the given profile has a valid header -static int grokHeader(TCHAR *profile, int * error ) +static int grokHeader(const TCHAR *profile, int * error ) { int rc = 1; int chk = 0; @@ -115,7 +110,7 @@ static int grokHeader(TCHAR *profile, int * error ) } // returns 0 if all the APIs are injected otherwise, 1 -static MIDatabase* LoadDatabase(TCHAR *profile) +static MIDatabase* LoadDatabase(const TCHAR *profile) { if (g_Db) delete g_Db; g_Db = new CDdxMmap(profile); @@ -131,23 +126,16 @@ static MIDatabase* LoadDatabase(TCHAR *profile) return g_Db; } -static int UnloadDatabase(int wasLoaded) +static int UnloadDatabase(MIDatabase* db) { - if ( !wasLoaded) return 0; UnloadDatabaseModule(); return 0; } -static int getFriendlyName( TCHAR *buf, size_t cch, int shortName ) -{ - _tcsncpy(buf, shortName ? _T("db3x driver") : _T("db3x database support"), cch); - return 0; -} - static DATABASELINK dblink = { sizeof(DATABASELINK), - getCapability, - getFriendlyName, + "db3x driver", + _T("db3x database support"), makeDatabase, grokHeader, LoadDatabase, @@ -174,7 +162,8 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_DATABAS extern "C" __declspec(dllexport) int Load(void) { - return 1; + RegisterDatabasePlugin(&dblink); + return 0; } extern "C" __declspec(dllexport) int Unload(void) diff --git a/plugins/Db3x_mmap/commonheaders.h b/plugins/Db3x_mmap/commonheaders.h index 7dc31a1615..acdc888725 100644 --- a/plugins/Db3x_mmap/commonheaders.h +++ b/plugins/Db3x_mmap/commonheaders.h @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -21,8 +21,6 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#define MIRANDA_VER 0x0A00 - #define _WIN32_WINNT 0x0501 #include "m_stdhdr.h" diff --git a/plugins/Db3x_mmap/database.cpp b/plugins/Db3x_mmap/database.cpp index a69c902772..889b13fd02 100644 --- a/plugins/Db3x_mmap/database.cpp +++ b/plugins/Db3x_mmap/database.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -20,73 +20,50 @@ 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 "commonheaders.h" -int ProfileManager(char *szDbDest,int cbDbDest); -int ShouldAutoCreate(void); -int CreateDbHeaders(HANDLE hFile); -int InitialiseDbHeaders(void); -int InitSettings(void); -void UninitSettings(void); -int InitContacts(void); -int InitEvents(void); int InitModuleNames(void); -void UninitModuleNames(void); int InitCache(void); -void UninitCache(void); int InitIni(void); void UninitIni(void); -HANDLE hDbFile = INVALID_HANDLE_VALUE; -CRITICAL_SECTION csDbAccess; -struct DBHeader dbHeader; -TCHAR szDbPath[MAX_PATH]; - -static void UnloadDatabase(void) -{ - // update profile last modified time - DWORD bytesWritten; - SetFilePointer(hDbFile,0,NULL,FILE_BEGIN); - WriteFile(hDbFile,&dbSignature,1,&bytesWritten,NULL); - - CloseHandle(hDbFile); -} - -DWORD CreateNewSpace(int bytes) +DWORD CDdxMmap::CreateNewSpace(int bytes) { - DWORD ofsNew; - ofsNew = dbHeader.ofsFileEnd; - dbHeader.ofsFileEnd += bytes; - DBWrite(0,&dbHeader,sizeof(dbHeader)); - log2("newspace %d@%08x",bytes,ofsNew); + DWORD ofsNew = m_dbHeader.ofsFileEnd; + m_dbHeader.ofsFileEnd += bytes; + DBWrite(0, &m_dbHeader, sizeof(m_dbHeader)); + log2("newspace %d@%08x", bytes, ofsNew); return ofsNew; } -void DeleteSpace(DWORD ofs,int bytes) +void CDdxMmap::DeleteSpace(DWORD ofs, int bytes) { - if (ofs+bytes == dbHeader.ofsFileEnd) { + if (ofs+bytes == m_dbHeader.ofsFileEnd) { log2("freespace %d@%08x",bytes,ofs); - dbHeader.ofsFileEnd = ofs; - } else { + m_dbHeader.ofsFileEnd = ofs; + } + else { log2("deletespace %d@%08x",bytes,ofs); - dbHeader.slackSpace += bytes; + m_dbHeader.slackSpace += bytes; } - DBWrite(0,&dbHeader,sizeof(dbHeader)); - DBFill(ofs,bytes); + DBWrite(0, &m_dbHeader, sizeof(m_dbHeader)); + DBFill(ofs, bytes); } -DWORD ReallocSpace(DWORD ofs,int oldSize,int newSize) +DWORD CDdxMmap::ReallocSpace(DWORD ofs, int oldSize, int newSize) { - DWORD ofsNew; + if (oldSize >= newSize) + return ofs; - if (oldSize >= newSize) return ofs; - - if (ofs+oldSize == dbHeader.ofsFileEnd) { + DWORD ofsNew; + if (ofs+oldSize == m_dbHeader.ofsFileEnd) { ofsNew = ofs; - dbHeader.ofsFileEnd += newSize-oldSize; - DBWrite(0,&dbHeader,sizeof(dbHeader)); - log3("adding newspace %d@%08x+%d",newSize,ofsNew,oldSize); - } else { + m_dbHeader.ofsFileEnd += newSize-oldSize; + DBWrite(0,&m_dbHeader,sizeof(m_dbHeader)); + log3("adding newspace %d@%08x+%d", newSize, ofsNew, oldSize); + } + else { ofsNew = CreateNewSpace(newSize); DBMoveChunk(ofsNew,ofs,oldSize); DeleteSpace(ofs,oldSize); @@ -94,38 +71,7 @@ DWORD ReallocSpace(DWORD ofs,int oldSize,int newSize) return ofsNew; } -void UnloadDatabaseModule(void) -{ - UninitSettings(); - UninitModuleNames(); - UninitCache(); - UnloadDatabase(); - DeleteCriticalSection(&csDbAccess); -} - -int LoadDatabaseModule(void) -{ - InitializeCriticalSection(&csDbAccess); - log0("DB logging running"); - { - DWORD dummy = 0; - hDbFile = CreateFile(szDbPath,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); - if ( hDbFile == INVALID_HANDLE_VALUE ) { - return 1; - } - if ( !ReadFile(hDbFile,&dbHeader,sizeof(dbHeader),&dummy,NULL)) { - CloseHandle(hDbFile); - return 1; - } - } - - if (InitCache()) return 1; - if (InitModuleNames()) return 1; - if (InitContacts()) return 1; - if (InitSettings()) return 1; - if (InitEvents()) return 1; - return 0; -} +///////////////////////////////////////////////////////////////////////////////////////// static DWORD DatabaseCorrupted = 0; static TCHAR *msg = NULL; @@ -150,11 +96,11 @@ void __cdecl dbpanic(void *arg) TerminateProcess(GetCurrentProcess(),255); } -void DatabaseCorruption(TCHAR *text) +void CDdxMmap::DatabaseCorruption(TCHAR *text) { int kill = 0; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (DatabaseCorrupted == 0) { DatabaseCorrupted++; kill++; @@ -163,29 +109,13 @@ void DatabaseCorruption(TCHAR *text) } else { /* db is already corrupted, someone else is dealing with it, wait here so that we don't do any more damage */ - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); Sleep(INFINITE); return; } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); if (kill) { _beginthread(dbpanic,0,NULL); Sleep(INFINITE); } } - -#ifdef DBLOGGING -void DBLog(const char *file,int line,const char *fmt,...) -{ - FILE *fp; - va_list vararg; - char str[1024]; - - va_start(vararg,fmt); - mir_vsnprintf(str,sizeof(str),fmt,vararg); - va_end(vararg); - fp = fopen("c:\\mirandadatabase.log.txt","at"); - fprintf(fp,"%u: %s %d: %s\n",GetTickCount(),file,line,str); - fclose(fp); -} -#endif diff --git a/plugins/Db3x_mmap/database.h b/plugins/Db3x_mmap/database.h index 3760711db3..1a10285c01 100644 --- a/plugins/Db3x_mmap/database.h +++ b/plugins/Db3x_mmap/database.h @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -153,73 +153,26 @@ struct DBEvent #include -typedef struct -{ - BYTE bIsResident; - char name[1]; -} - DBCachedSettingName; - -typedef struct +struct DBCachedGlobalValue { char* name; DBVARIANT value; -} - DBCachedGlobalValue; +}; -typedef struct DBCachedContactValue_tag +struct DBCachedContactValue { char* name; DBVARIANT value; - struct DBCachedContactValue_tag* next; -} - DBCachedContactValue; + DBCachedContactValue* next; +}; -typedef struct +struct DBCachedContactValueList { HANDLE hContact; HANDLE hNext; DBCachedContactValue* first; DBCachedContactValue* last; -} - DBCachedContactValueList; - -//databasecorruption: with NULL called if any signatures are broken. very very fatal -void DatabaseCorruption(TCHAR *text); -PBYTE DBRead(DWORD ofs,int bytesRequired,int *bytesAvail); //any preview result could be invalidated by the next call -__forceinline PBYTE DBRead(HANDLE hContact,int bytesRequired,int *bytesAvail) -{ return DBRead((DWORD)hContact, bytesRequired, bytesAvail); -} -void DBWrite(DWORD ofs,PVOID pData,int count); -void DBFill(DWORD ofs,int bytes); -void DBFlush(int setting); -void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes); -DWORD CreateNewSpace(int bytes); -void DeleteSpace(DWORD ofs,int bytes); -DWORD ReallocSpace(DWORD ofs,int oldSize,int newSize); -void GetProfileDirectory(char *szPath,int cbPath); -int GetDefaultProfilePath(char *szPath,int cbPath,int *specified); -int ShouldShowProfileManager(void); -int CheckDbHeaders(struct DBHeader * hdr); -int CreateDbHeaders(HANDLE hFile); -int LoadDatabaseModule(void); -void UnloadDatabaseModule(void); - -DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index); -int CheckProto(HANDLE hContact, const char *proto); - -void FreeCachedVariant( DBVARIANT* V ); - -extern CRITICAL_SECTION csDbAccess; -extern struct DBHeader dbHeader; -extern HANDLE hDbFile; - -extern HANDLE hCacheHeap; -extern SortedList lContacts; -extern HANDLE hLastCachedContact; -extern HANDLE hContactDeletedEvent,hContactAddedEvent; - -extern BOOL safetyMode; +}; #define MAXCACHEDREADSIZE 65536 diff --git a/plugins/Db3x_mmap/dbcache.cpp b/plugins/Db3x_mmap/dbcache.cpp index 271de9d512..4b48a3c907 100644 --- a/plugins/Db3x_mmap/dbcache.cpp +++ b/plugins/Db3x_mmap/dbcache.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -23,172 +23,154 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" -BOOL safetyMode = TRUE; -static UINT_PTR flushBuffersTimerId; - -static PBYTE pNull = 0; -static PBYTE pDbCache = NULL; -static HANDLE hMap = NULL; -static DWORD dwFileSize = 0; -static DWORD ChunkSize = 65536; -static DWORD flushFailTick = 0; - - -void Map() +void CDdxMmap::Map() { - hMap = CreateFileMapping(hDbFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL); + m_hMap = CreateFileMapping(m_hDbFile, NULL, PAGE_READWRITE, 0, m_dwFileSize, NULL); - if (hMap) + if (m_hMap) { - pDbCache = (PBYTE)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS/*FILE_MAP_WRITE*/, 0, 0 ,0); - if (!pDbCache) + m_pDbCache = (PBYTE)MapViewOfFile(m_hMap, FILE_MAP_ALL_ACCESS/*FILE_MAP_WRITE*/, 0, 0 ,0); + if (!m_pDbCache) DatabaseCorruption( _T("%s (MapViewOfFile failed. Code: %d)")); } else DatabaseCorruption( _T("%s (CreateFileMapping failed. Code: %d)")); } -void ReMap(DWORD needed) +void CDdxMmap::ReMap(DWORD needed) { - KillTimer(NULL,flushBuffersTimerId); + KillTimer(NULL,m_flushBuffersTimerId); - log3("remapping %d + %d (file end: %d)",dwFileSize,needed,dbHeader.ofsFileEnd); + log3("remapping %d + %d (file end: %d)",m_dwFileSize,needed,m_dbHeader.ofsFileEnd); - if (needed > ChunkSize) + if (needed > m_ChunkSize) { - if (needed + dwFileSize > dbHeader.ofsFileEnd + ChunkSize) + if (needed + m_dwFileSize > m_dbHeader.ofsFileEnd + m_ChunkSize) DatabaseCorruption( _T("%s (Too large increment)")); else { - DWORD x = dbHeader.ofsFileEnd/ChunkSize; - dwFileSize = (x+1)*ChunkSize; + DWORD x = m_dbHeader.ofsFileEnd/m_ChunkSize; + m_dwFileSize = (x+1)*m_ChunkSize; } } else - dwFileSize += ChunkSize; + m_dwFileSize += m_ChunkSize; -// FlushViewOfFile(pDbCache, 0); - UnmapViewOfFile(pDbCache); - pDbCache = NULL; - CloseHandle(hMap); +// FlushViewOfFile(m_pDbCache, 0); + UnmapViewOfFile(m_pDbCache); + m_pDbCache = NULL; + CloseHandle(m_hMap); Map(); } -void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes) +void CDdxMmap::DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes) { - int x = 0; + int x = 0; log3("move %d %08x->%08x",bytes,ofsSource,ofsDest); - if (ofsDest+bytes>dwFileSize) ReMap(ofsDest+bytes-dwFileSize); - if (ofsSource+bytes>dwFileSize) { - x = ofsSource+bytes-dwFileSize; + if (ofsDest+bytes>m_dwFileSize) ReMap(ofsDest+bytes-m_dwFileSize); + if (ofsSource+bytes>m_dwFileSize) { + x = ofsSource+bytes-m_dwFileSize; log0("buggy move!"); _ASSERT(0); } if (x > 0) - ZeroMemory(pDbCache+ofsDest+bytes-x, x); - if (ofsSource < dwFileSize) - MoveMemory(pDbCache+ofsDest,pDbCache+ofsSource, bytes-x); + ZeroMemory(m_pDbCache+ofsDest+bytes-x, x); + if (ofsSource < m_dwFileSize) + MoveMemory(m_pDbCache+ofsDest,m_pDbCache+ofsSource, bytes-x); logg(); } //we are assumed to be in a mutex here -PBYTE DBRead(DWORD ofs,int bytesRequired,int *bytesAvail) +PBYTE CDdxMmap::DBRead(DWORD ofs,int bytesRequired,int *bytesAvail) { // buggy read - if (ofs>= dwFileSize) { + if (ofs>= m_dwFileSize) { log2("read from outside %d@%08x",bytesRequired,ofs); - if (bytesAvail!=NULL) *bytesAvail = ChunkSize; - return pNull; + if (bytesAvail!=NULL) *bytesAvail = m_ChunkSize; + return m_pNull; } - log3((ofs+bytesRequired>dwFileSize)?"read %d@%08x, only %d avaliable":"read %d@%08x",bytesRequired,ofs,dwFileSize-ofs); - if (bytesAvail!=NULL) *bytesAvail = dwFileSize - ofs; - return pDbCache+ofs; + log3((ofs+bytesRequired > m_dwFileSize)?"read %d@%08x, only %d avaliable":"read %d@%08x",bytesRequired,ofs,m_dwFileSize-ofs); + if (bytesAvail!=NULL) *bytesAvail = m_dwFileSize - ofs; + return m_pDbCache+ofs; } //we are assumed to be in a mutex here -void DBWrite(DWORD ofs,PVOID pData,int bytes) +void CDdxMmap::DBWrite(DWORD ofs,PVOID pData,int bytes) { log2("write %d@%08x",bytes,ofs); - if (ofs+bytes>dwFileSize) ReMap(ofs+bytes-dwFileSize); - MoveMemory(pDbCache+ofs,pData,bytes); + if (ofs+bytes>m_dwFileSize) ReMap(ofs+bytes-m_dwFileSize); + MoveMemory(m_pDbCache+ofs,pData,bytes); logg(); } //we are assumed to be in a mutex here -void DBFill(DWORD ofs,int bytes) +void CDdxMmap::DBFill(DWORD ofs,int bytes) { log2("zerofill %d@%08x",bytes,ofs); - if (ofs+bytes <= dwFileSize) - ZeroMemory(pDbCache+ofs,bytes); + if (ofs+bytes <= m_dwFileSize) + ZeroMemory(m_pDbCache+ofs,bytes); logg(); } static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd, UINT message, UINT_PTR idEvent, DWORD dwTime) { - if (!pDbCache) return; - - KillTimer(NULL,flushBuffersTimerId); - log0("tflush1"); - if (FlushViewOfFile(pDbCache, 0) == 0) { - if (flushFailTick == 0) - flushFailTick = GetTickCount(); - else if (GetTickCount() - flushFailTick > 5000) - DatabaseCorruption(NULL); + for (int i=0; i < g_Dbs.getCount(); i++) { + CDdxMmap* db = g_Dbs[i]; + if (db->m_flushBuffersTimerId != idEvent) + continue; + + if (!db->m_pDbCache) + return; + + KillTimer(NULL, db->m_flushBuffersTimerId); + log0("tflush1"); + if (FlushViewOfFile(db->m_pDbCache, 0) == 0) { + if (db->m_flushFailTick == 0) + db->m_flushFailTick = GetTickCount(); + else if (GetTickCount() - db->m_flushFailTick > 5000) + db->DatabaseCorruption(NULL); + } + else db->m_flushFailTick = 0; + log0("tflush2"); } - else - flushFailTick = 0; - log0("tflush2"); } -void DBFlush(int setting) +void CDdxMmap::DBFlush(int setting) { if (!setting) { log0("nflush1"); - if (safetyMode && pDbCache) { - if (FlushViewOfFile(pDbCache, 0) == 0) { - if (flushFailTick == 0) - flushFailTick = GetTickCount(); - else if (GetTickCount() - flushFailTick > 5000) + if (m_safetyMode && m_pDbCache) { + if (FlushViewOfFile(m_pDbCache, 0) == 0) { + if (m_flushFailTick == 0) + m_flushFailTick = GetTickCount(); + else if (GetTickCount() - m_flushFailTick > 5000) DatabaseCorruption(NULL); } else - flushFailTick = 0; + m_flushFailTick = 0; } log0("nflush2"); return; } - KillTimer(NULL,flushBuffersTimerId); - flushBuffersTimerId = SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); + KillTimer(NULL, m_flushBuffersTimerId); + m_flushBuffersTimerId = SetTimer(NULL, m_flushBuffersTimerId, 50, DoBufferFlushTimerProc); } -int InitCache(void) +int CDdxMmap::InitCache(void) { DWORD x; - SYSTEM_INFO sinf; - GetSystemInfo(&sinf); - ChunkSize = sinf.dwAllocationGranularity; - - dwFileSize = GetFileSize(hDbFile, NULL); + m_dwFileSize = GetFileSize(m_hDbFile, NULL); // Align to chunk - x = dwFileSize % ChunkSize; - if (x) dwFileSize += ChunkSize - x; + x = m_dwFileSize % m_ChunkSize; + if (x) m_dwFileSize += m_ChunkSize - x; Map(); // zero region for reads outside the file - pNull = (PBYTE)calloc(ChunkSize, 1); + m_pNull = (PBYTE)calloc(m_ChunkSize, 1); return 0; } - -void UninitCache(void) -{ - KillTimer(NULL,flushBuffersTimerId); - FlushViewOfFile(pDbCache, 0); - UnmapViewOfFile(pDbCache); - CloseHandle(hMap); - if (pNull) free(pNull); -} diff --git a/plugins/Db3x_mmap/dbcontacts.cpp b/plugins/Db3x_mmap/dbcontacts.cpp index 554fe7bd4e..c23db78f30 100644 --- a/plugins/Db3x_mmap/dbcontacts.cpp +++ b/plugins/Db3x_mmap/dbcontacts.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -23,15 +23,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" -HANDLE hContactDeletedEvent, hContactAddedEvent; - -DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index) +DBCachedContactValueList* CDdxMmap::AddToCachedContactList(HANDLE hContact, int index) { - DBCachedContactValueList* VL; - VL = (DBCachedContactValueList*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList)); + DBCachedContactValueList* VL = (DBCachedContactValueList*)HeapAlloc(m_hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList)); VL->hContact = hContact; - if (index == -1) List_GetIndex(&lContacts,VL,&index); - List_Insert(&lContacts,VL,index); + if (index == -1) + m_lContacts.insert(VL); + else + m_lContacts.insert(VL,index); return VL; } @@ -54,23 +53,16 @@ int CDdxMmap::CheckProto(HANDLE hContact, const char *proto) return !strcmp(protobuf,proto); } -int InitContacts(void) -{ - hContactDeletedEvent = CreateHookableEvent(ME_DB_CONTACT_DELETED); - hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED); - return 0; -} - STDMETHODIMP_(LONG) CDdxMmap::GetContactCount(void) { - mir_cslock lck(csDbAccess); - return dbHeader.contactCount; + mir_cslock lck(m_csDbAccess); + return m_dbHeader.contactCount; } STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstContact(const char *szProto) { - mir_cslock lck(csDbAccess); - HANDLE ret = (HANDLE)dbHeader.ofsFirstContact; + mir_cslock lck(m_csDbAccess); + HANDLE ret = (HANDLE)m_dbHeader.ofsFirstContact; if (szProto && !CheckProto(ret, szProto)) ret = FindNextContact(ret, szProto); return ret; @@ -83,10 +75,10 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindNextContact(HANDLE hContact, const char *szP DBCachedContactValueList VLtemp, *VL = NULL; VLtemp.hContact = hContact; - mir_cslock lck(csDbAccess); - while(VLtemp.hContact) { - if ( List_GetIndex(&lContacts,&VLtemp,&index)) { - VL = ( DBCachedContactValueList* )lContacts.items[index]; + mir_cslock lck(m_csDbAccess); + while (VLtemp.hContact) { + if (( index = m_lContacts.getIndex(&VLtemp)) != -1) { + VL = m_lContacts[index]; if (VL->hNext != NULL) { if (!szProto || CheckProto(VL->hNext, szProto)) return VL->hNext; @@ -96,18 +88,18 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindNextContact(HANDLE hContact, const char *szP } } dbc = (DBContact*)DBRead(VLtemp.hContact,sizeof(DBContact),NULL); - if (dbc->signature!=DBCONTACT_SIGNATURE) + if (dbc->signature != DBCONTACT_SIGNATURE) break; - else { - if ( VL == NULL ) - VL = AddToCachedContactList(VLtemp.hContact,index); - VL->hNext = (HANDLE)dbc->ofsNext; - if (VL->hNext != NULL && (!szProto || CheckProto(VL->hNext, szProto))) - return VL->hNext; + if ( VL == NULL ) + VL = AddToCachedContactList(VLtemp.hContact,index); - VLtemp.hContact = VL->hNext; - } } + VL->hNext = (HANDLE)dbc->ofsNext; + if (VL->hNext != NULL && (!szProto || CheckProto(VL->hNext, szProto))) + return VL->hNext; + + VLtemp.hContact = VL->hNext; + } return NULL; } @@ -123,56 +115,55 @@ STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact) return 1; { - mir_cslock lck(csDbAccess); + mir_cslock lck(m_csDbAccess); DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); if (dbc->signature != DBCONTACT_SIGNATURE) return 1; - if (hContact == (HANDLE)dbHeader.ofsUser) { + if (hContact == (HANDLE)m_dbHeader.ofsUser) { log0("FATAL: del of user chain attempted."); return 1; } log0("del contact"); } - //call notifier while outside mutex + // call notifier while outside mutex NotifyEventHooks(hContactDeletedEvent, (WPARAM)hContact, 0); - //get back in - mir_cslock lck(csDbAccess); + // get back in + mir_cslock lck(m_csDbAccess); DBCachedContactValueList VLtemp; VLtemp.hContact = hContact; - if ( List_GetIndex(&lContacts,&VLtemp,&index)) - { - DBCachedContactValueList *VL = ( DBCachedContactValueList* )lContacts.items[index]; + if ((index = m_lContacts.getIndex(&VLtemp)) != -1) { + DBCachedContactValueList *VL = m_lContacts[index]; DBCachedContactValue* V = VL->first; while ( V != NULL ) { DBCachedContactValue* V1 = V->next; FreeCachedVariant(&V->value); - HeapFree( hCacheHeap, 0, V ); + HeapFree( m_hCacheHeap, 0, V ); V = V1; } - HeapFree( hCacheHeap, 0, VL ); + HeapFree( m_hCacheHeap, 0, VL ); - if (VLtemp.hContact == hLastCachedContact) - hLastCachedContact = NULL; - List_Remove(&lContacts,index); + if (VLtemp.hContact == m_hLastCachedContact) + m_hLastCachedContact = NULL; + m_lContacts.remove(index); } DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); //delete settings chain ofsThis = dbc->ofsFirstSettings; ofsFirstEvent = dbc->ofsFirstEvent; - while(ofsThis) { - dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); + while (ofsThis) { + dbcs = (DBContactSettings*)DBRead(ofsThis,sizeof(DBContactSettings),NULL); ofsNext = dbcs->ofsNext; - DeleteSpace(ofsThis,offsetof(struct DBContactSettings,blob)+dbcs->cbBlob); + DeleteSpace(ofsThis,offsetof(DBContactSettings,blob)+dbcs->cbBlob); ofsThis = ofsNext; } //delete event chain ofsThis = ofsFirstEvent; - while(ofsThis) { + while (ofsThis) { dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL); ofsNext = dbe->ofsNext; DeleteSpace(ofsThis,offsetof(DBEvent,blob)+dbe->cbBlob); @@ -180,15 +171,15 @@ STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact) } //find previous contact in chain and change ofsNext dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); - if (dbHeader.ofsFirstContact == (DWORD)hContact) { - dbHeader.ofsFirstContact = dbc->ofsNext; - DBWrite(0,&dbHeader,sizeof(dbHeader)); + if (m_dbHeader.ofsFirstContact == (DWORD)hContact) { + m_dbHeader.ofsFirstContact = dbc->ofsNext; + DBWrite(0,&m_dbHeader,sizeof(m_dbHeader)); } else { ofsNext = dbc->ofsNext; - ofsThis = dbHeader.ofsFirstContact; + ofsThis = m_dbHeader.ofsFirstContact; DBContact *dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL); - while(dbcPrev->ofsNext != (DWORD)hContact) { + while (dbcPrev->ofsNext != (DWORD)hContact) { if (dbcPrev->ofsNext == 0) DatabaseCorruption(NULL); ofsThis = dbcPrev->ofsNext; dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL); @@ -198,17 +189,17 @@ STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact) DBCachedContactValueList VLtemp; VLtemp.hContact = (HANDLE)ofsThis; - if ( List_GetIndex(&lContacts,&VLtemp,&index)) { - DBCachedContactValueList *VL = ( DBCachedContactValueList* )lContacts.items[index]; - VL->hNext = ( HANDLE )ofsNext; + if ((index = m_lContacts.getIndex(&VLtemp)) != -1) { + DBCachedContactValueList *VL = m_lContacts[index]; + VL->hNext = (HANDLE)ofsNext; } } //delete contact DeleteSpace((DWORD)hContact, sizeof(DBContact)); //decrement contact count - dbHeader.contactCount--; - DBWrite(0,&dbHeader,sizeof(dbHeader)); + m_dbHeader.contactCount--; + DBWrite(0,&m_dbHeader,sizeof(m_dbHeader)); DBFlush(0); return 0; } @@ -218,16 +209,16 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddContact() DWORD ofsNew; log0("add contact"); { - mir_cslock lck(csDbAccess); + mir_cslock lck(m_csDbAccess); ofsNew = CreateNewSpace(sizeof(DBContact)); DBContact dbc = { 0 }; dbc.signature = DBCONTACT_SIGNATURE; - dbc.ofsNext = dbHeader.ofsFirstContact; - dbHeader.ofsFirstContact = ofsNew; - dbHeader.contactCount++; + dbc.ofsNext = m_dbHeader.ofsFirstContact; + m_dbHeader.ofsFirstContact = ofsNew; + m_dbHeader.contactCount++; DBWrite(ofsNew,&dbc,sizeof(DBContact)); - DBWrite(0,&dbHeader,sizeof(dbHeader)); + DBWrite(0,&m_dbHeader,sizeof(m_dbHeader)); DBFlush(0); AddToCachedContactList((HANDLE)ofsNew, -1); @@ -243,12 +234,12 @@ STDMETHODIMP_(BOOL) CDdxMmap::IsDbContact(HANDLE hContact) DWORD ofsContact = (DWORD)hContact; int ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); { int index; DBCachedContactValueList VLtemp; VLtemp.hContact = hContact; - if ( List_GetIndex(&lContacts,&VLtemp,&index)) + if ((index = m_lContacts.getIndex(&VLtemp)) != -1) ret = TRUE; else { dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); @@ -257,6 +248,6 @@ STDMETHODIMP_(BOOL) CDdxMmap::IsDbContact(HANDLE hContact) AddToCachedContactList(hContact, index); } } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } diff --git a/plugins/Db3x_mmap/dbevents.cpp b/plugins/Db3x_mmap/dbevents.cpp index c7a44d2e4e..11325e8cae 100644 --- a/plugins/Db3x_mmap/dbevents.cpp +++ b/plugins/Db3x_mmap/dbevents.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" -extern BOOL safetyMode; +extern BOOL m_safetyMode; DWORD GetModuleNameOfs(const char *szName); char *GetModuleNameByOfs(DWORD ofs); @@ -34,13 +34,13 @@ STDMETHODIMP_(LONG) CDdxMmap::GetEventCount(HANDLE hContact) { LONG ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL); if (dbc->signature != DBCONTACT_SIGNATURE) ret = -1; else ret = dbc->eventCount; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -56,12 +56,12 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei) if (NotifyEventHooks(hEventFilterAddedEvent, (WPARAM)hContact, (LPARAM)dbei)) { return 0; } - EnterCriticalSection(&csDbAccess); - if (hContact == 0) ofsContact = dbHeader.ofsUser; + EnterCriticalSection(&m_csDbAccess); + if (hContact == 0) ofsContact = m_dbHeader.ofsUser; else ofsContact = (DWORD)hContact; dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); if (dbc.signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 0; } ofsNew = CreateNewSpace(offsetof(DBEvent,blob)+dbei->cbBlob); @@ -128,14 +128,14 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei) } neednotify = TRUE; } - else neednotify = safetyMode; + else neednotify = m_safetyMode; DBWrite(ofsContact,&dbc,sizeof(DBContact)); DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob)); DBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob); DBFlush(0); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); log1("add event @ %08x",ofsNew); // Notify only in safe mode or on really new events @@ -151,21 +151,21 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent) DWORD ofsContact,ofsThis; DBEvent dbe,*dbeNext,*dbePrev; - EnterCriticalSection(&csDbAccess); - if (hContact == 0) ofsContact = dbHeader.ofsUser; + EnterCriticalSection(&m_csDbAccess); + if (hContact == 0) ofsContact = m_dbHeader.ofsUser; else ofsContact = (DWORD)hContact; dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); dbe = *(DBEvent*)DBRead(hContact,sizeof(DBEvent),NULL); if (dbc.signature!=DBCONTACT_SIGNATURE || dbe.signature!=DBEVENT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } log1("delete event @ %08x",wParam); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); //call notifier while outside mutex NotifyEventHooks(hEventDeletedEvent,(WPARAM)hContact, (LPARAM)hDbEvent); //get back in - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); //check if this was the first unread, if so, recalc the first unread @@ -222,7 +222,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent) DBWrite(ofsContact,&dbc,sizeof(DBContact)); DBFlush(0); //quit - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 0; } @@ -231,11 +231,11 @@ STDMETHODIMP_(LONG) CDdxMmap::GetBlobSize(HANDLE hDbEvent) INT_PTR ret; DBEvent *dbe; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL); if (dbe->signature!=DBEVENT_SIGNATURE) ret = -1; else ret = dbe->cbBlob; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -249,10 +249,10 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) dbei->cbBlob = 0; return 1; } - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); if (dbe->signature!=DBEVENT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName); @@ -272,7 +272,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) CopyMemory(dbei->pBlob+i,DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,MAXCACHEDREADSIZE,NULL),MAXCACHEDREADSIZE); } } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 0; } @@ -283,18 +283,18 @@ STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent) DBContact dbc; DWORD ofsThis; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; dbc = *(DBContact*)DBRead(hContact,sizeof(DBContact),NULL); dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); if (dbe->signature!=DBEVENT_SIGNATURE || dbc.signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return -1; } if (dbe->flags&DBEF_READ || dbe->flags&DBEF_SENT) { ret = (INT_PTR)dbe->flags; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } log1("mark read @ %08x",wParam); @@ -319,23 +319,23 @@ STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent) } DBWrite((DWORD)hContact,&dbc,sizeof(DBContact)); DBFlush(0); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } STDMETHODIMP_(HANDLE) CDdxMmap::GetEventContact(HANDLE hDbEvent) { - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); if (dbe->signature != DBEVENT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return (HANDLE)-1; } - while(!(dbe->flags & DBEF_FIRST)) + while (!(dbe->flags & DBEF_FIRST)) dbe = (DBEvent*)DBRead(dbe->ofsPrev,sizeof(DBEvent),NULL); HANDLE ret = (HANDLE)dbe->ofsPrev; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -343,16 +343,16 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstEvent(HANDLE hContact) { HANDLE ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); if (dbc->signature != DBCONTACT_SIGNATURE) ret = 0; else ret = (HANDLE)dbc->ofsFirstEvent; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -360,13 +360,13 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstUnreadEvent(HANDLE hContact) { HANDLE ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL); if (dbc->signature!=DBCONTACT_SIGNATURE) ret = 0; else ret = (HANDLE)dbc->ofsFirstUnreadEvent; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -374,13 +374,13 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindLastEvent(HANDLE hContact) { HANDLE ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL); if (dbc->signature!=DBCONTACT_SIGNATURE) ret = 0; else ret = (HANDLE)dbc->ofsLastEvent; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -388,11 +388,11 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindNextEvent(HANDLE hDbEvent) { HANDLE ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); if (dbe->signature!=DBEVENT_SIGNATURE) ret = 0; else ret = (HANDLE)dbe->ofsNext; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } @@ -400,21 +400,11 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindPrevEvent(HANDLE hDbEvent) { HANDLE ret; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); if (dbe->signature!=DBEVENT_SIGNATURE) ret = 0; else if (dbe->flags&DBEF_FIRST) ret = 0; else ret = (HANDLE)dbe->ofsPrev; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ret; } - -/////////////////////////////////////////////////////////////////////////////// - -int InitEvents(void) -{ - hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED); - hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED); - hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD); - return 0; -} diff --git a/plugins/Db3x_mmap/dbheaders.cpp b/plugins/Db3x_mmap/dbheaders.cpp index a7eede6475..32ed0091cb 100644 --- a/plugins/Db3x_mmap/dbheaders.cpp +++ b/plugins/Db3x_mmap/dbheaders.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -25,44 +25,39 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //the cache has not been loaded when these functions are used -int CreateDbHeaders(HANDLE hFile) +int CDdxMmap::CreateDbHeaders() { DBContact user; DWORD bytesWritten; - CopyMemory(dbHeader.signature,&dbSignature,sizeof(dbHeader.signature)); - dbHeader.version = DB_THIS_VERSION; - dbHeader.ofsFileEnd = sizeof(struct DBHeader); - dbHeader.slackSpace = 0; - dbHeader.contactCount = 0; - dbHeader.ofsFirstContact = 0; - dbHeader.ofsFirstModuleName = 0; - dbHeader.ofsUser = 0; + CopyMemory(m_dbHeader.signature,&dbSignature,sizeof(m_dbHeader.signature)); + m_dbHeader.version = DB_THIS_VERSION; + m_dbHeader.ofsFileEnd = sizeof(struct DBHeader); + m_dbHeader.slackSpace = 0; + m_dbHeader.contactCount = 0; + m_dbHeader.ofsFirstContact = 0; + m_dbHeader.ofsFirstModuleName = 0; + m_dbHeader.ofsUser = 0; //create user - dbHeader.ofsUser = dbHeader.ofsFileEnd; - dbHeader.ofsFileEnd += sizeof(DBContact); - SetFilePointer(hFile,0,NULL,FILE_BEGIN); - WriteFile(hFile,&dbHeader,sizeof(dbHeader),&bytesWritten,NULL); + m_dbHeader.ofsUser = m_dbHeader.ofsFileEnd; + m_dbHeader.ofsFileEnd += sizeof(DBContact); + SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN); + WriteFile(m_hDbFile,&m_dbHeader,sizeof(m_dbHeader),&bytesWritten,NULL); user.signature = DBCONTACT_SIGNATURE; user.ofsNext = 0; user.ofsFirstSettings = 0; user.eventCount = 0; user.ofsFirstEvent = user.ofsLastEvent = 0; - SetFilePointer(hFile,dbHeader.ofsUser,NULL,FILE_BEGIN); - WriteFile(hFile,&user,sizeof(DBContact),&bytesWritten,NULL); - FlushFileBuffers(hFile); + SetFilePointer(m_hDbFile,m_dbHeader.ofsUser,NULL,FILE_BEGIN); + WriteFile(m_hDbFile,&user,sizeof(DBContact),&bytesWritten,NULL); + FlushFileBuffers(m_hDbFile); return 0; } -int CheckDbHeaders(struct DBHeader * hdr) -{ - if (memcmp(hdr->signature,&dbSignature,sizeof(hdr->signature))) return 1; - if (hdr->version!=DB_THIS_VERSION) return 2; - if (hdr->ofsUser == 0) return 3; - return 0; -} - -int InitialiseDbHeaders(void) +int CDdxMmap::CheckDbHeaders() { + if (memcmp(m_dbHeader.signature, &dbSignature, sizeof(m_dbHeader.signature))) return 1; + if (m_dbHeader.version != DB_THIS_VERSION) return 2; + if (m_dbHeader.ofsUser == 0) return 3; return 0; } diff --git a/plugins/Db3x_mmap/dbintf.cpp b/plugins/Db3x_mmap/dbintf.cpp index c2074b3e84..831b147ea5 100644 --- a/plugins/Db3x_mmap/dbintf.cpp +++ b/plugins/Db3x_mmap/dbintf.cpp @@ -23,14 +23,126 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" -CDdxMmap::CDdxMmap(const TCHAR* tszFileName) +static int stringCompare(const char* p1, const char* p2) { + return strcmp(p1+1, p2+1); +} + +static int stringCompare2(const char* p1, const char* p2) +{ + return strcmp(p1, p2); +} + +static int compareGlobals(const DBCachedGlobalValue* p1, const DBCachedGlobalValue* p2) +{ + return strcmp(p1->name, p2->name); +} + +static int ModCompare(const ModuleName *mn1, const ModuleName *mn2 ) +{ + return strcmp( mn1->name, mn2->name ); +} + +static int OfsCompare(const ModuleName *mn1, const ModuleName *mn2 ) +{ + return ( mn1->ofs - mn2->ofs ); +} + +CDdxMmap::CDdxMmap(const TCHAR* tszFileName) : + m_hDbFile(INVALID_HANDLE_VALUE), + m_safetyMode(TRUE), + m_lSettings(100, stringCompare), + m_lContacts(50, LIST::FTSortFunc(HandleKeySort)), + m_lGlobalSettings(50, compareGlobals), + m_lResidentSettings(50, stringCompare2), + m_lMods(50, ModCompare), + m_lOfs(50, OfsCompare) +{ + m_tszProfileName = mir_tstrdup(tszFileName); + + InitializeCriticalSection(&m_csDbAccess); + + SYSTEM_INFO sinf; + GetSystemInfo(&sinf); + m_ChunkSize = sinf.dwAllocationGranularity; + + m_codePage = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0); + m_hCacheHeap = HeapCreate(0, 0, 0); + m_hModHeap = HeapCreate(0,0,0); + + hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED); + hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED); + hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED); + hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD); +} + +CDdxMmap::~CDdxMmap() +{ + // destroy settings + HeapDestroy(m_hCacheHeap); + m_lContacts.destroy(); + m_lSettings.destroy(); + m_lGlobalSettings.destroy(); + m_lResidentSettings.destroy(); + + // destroy modules + HeapDestroy(m_hModHeap); + m_lMods.destroy(); + m_lOfs.destroy(); + + // destroy map + KillTimer(NULL,m_flushBuffersTimerId); + if (m_pDbCache) { + FlushViewOfFile(m_pDbCache, 0); + UnmapViewOfFile(m_pDbCache); + } + if (m_hMap) + CloseHandle(m_hMap); + if (m_pNull) + free(m_pNull); + + // update profile last modified time + DWORD bytesWritten; + SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN); + WriteFile(m_hDbFile,&dbSignature,1,&bytesWritten,NULL); + CloseHandle(m_hDbFile); + + DeleteCriticalSection(&m_csDbAccess); + + mir_free(m_tszProfileName); +} + +int CDdxMmap::Load(bool bSkipInit) +{ + log0("DB logging running"); + + DWORD dummy = 0; + m_hDbFile = CreateFile(m_tszProfileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); + if ( m_hDbFile == INVALID_HANDLE_VALUE ) + return 1; + + if ( !ReadFile(m_hDbFile,&m_dbHeader,sizeof(m_dbHeader),&dummy,NULL)) { + CloseHandle(m_hDbFile); + return 1; + } + + if ( !bSkipInit) { + if (InitCache()) return 1; + if (InitModuleNames()) return 1; + } + return 0; +} + +int CDdxMmap::Create() +{ + m_hDbFile = CreateFile(m_tszProfileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL); + return ( m_hDbFile == INVALID_HANDLE_VALUE ); } STDMETHODIMP_(void) CDdxMmap::SetCacheSafetyMode(BOOL bIsSet) { - { mir_cslock lck(csDbAccess); - safetyMode = bIsSet; + { mir_cslock lck(m_csDbAccess); + m_safetyMode = bIsSet; } DBFlush(1); } diff --git a/plugins/Db3x_mmap/dbintf.h b/plugins/Db3x_mmap/dbintf.h index 518d6b4e22..ac5a1c25b5 100644 --- a/plugins/Db3x_mmap/dbintf.h +++ b/plugins/Db3x_mmap/dbintf.h @@ -23,10 +23,24 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include -struct CDdxMmap : public MIDatabase +struct ModuleName +{ + char *name; + DWORD ofs; +}; + +struct CDdxMmap : public MIDatabase, public MZeroedObject { CDdxMmap(const TCHAR* tszFileName); + ~CDdxMmap(); + int Load(bool bSkipInit); + int Create(void); + int CreateDbHeaders(); + int CheckDbHeaders(); + void DatabaseCorruption(TCHAR *text); + +protected: STDMETHODIMP_(void) SetCacheSafetyMode(BOOL); STDMETHODIMP_(LONG) GetContactCount(void); @@ -62,5 +76,85 @@ struct CDdxMmap : public MIDatabase STDMETHODIMP_(BOOL) EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam); private: - int CheckProto(HANDLE hContact, const char *proto); + TCHAR* m_tszProfileName; + HANDLE m_hDbFile; + DBHeader m_dbHeader; + DWORD m_ChunkSize; + BOOL m_safetyMode; + + //////////////////////////////////////////////////////////////////////////// + // database stuff +public: + UINT_PTR m_flushBuffersTimerId; + DWORD m_flushFailTick; + PBYTE m_pDbCache; + +private: + PBYTE m_pNull; + HANDLE m_hMap; + DWORD m_dwFileSize; + + CRITICAL_SECTION m_csDbAccess; + + int CheckProto(HANDLE hContact, const char *proto); + DWORD CreateNewSpace(int bytes); + void DeleteSpace(DWORD ofs, int bytes); + DWORD ReallocSpace(DWORD ofs, int oldSize, int newSize); + + void Map(); + void ReMap(DWORD needed); + void DBMoveChunk(DWORD ofsDest, DWORD ofsSource, int bytes); + PBYTE DBRead(DWORD ofs, int bytesRequired, int *bytesAvail); + void DBWrite(DWORD ofs, PVOID pData, int bytes); + void DBFill(DWORD ofs, int bytes); + void DBFlush(int setting); + int InitCache(void); + + __forceinline PBYTE DBRead(HANDLE hContact, int bytesRequired, int *bytesAvail) + { return DBRead((DWORD)hContact, bytesRequired, bytesAvail); + } + + //////////////////////////////////////////////////////////////////////////// + // settings + + int m_codePage; + + HANDLE m_hCacheHeap; + HANDLE m_hLastCachedContact; + char* m_lastSetting; + DBCachedContactValueList *m_lastVL; + + LIST m_lContacts; + LIST m_lGlobalSettings; + LIST m_lSettings, m_lResidentSettings; + HANDLE hSettingChangeEvent, hContactDeletedEvent, hContactAddedEvent; + + DWORD GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleName); + char* InsertCachedSetting(const char* szName, size_t cbNameLen); + char* GetCachedSetting(const char *szModuleName,const char *szSettingName, int moduleNameLen, int settingNameLen); + void SetCachedVariant(DBVARIANT* s, DBVARIANT* d); + void FreeCachedVariant(DBVARIANT* V); + DBVARIANT* GetCachedValuePtr(HANDLE hContact, char* szSetting, int bAllocate); + int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs,int isStatic); + + //////////////////////////////////////////////////////////////////////////// + // contacts + + DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index); + + //////////////////////////////////////////////////////////////////////////// + // modules + + HANDLE m_hModHeap; + LIST m_lMods, m_lOfs; + HANDLE hEventAddedEvent, hEventDeletedEvent, hEventFilterAddedEvent; + ModuleName *m_lastmn; + + void AddToList(char *name, DWORD len, DWORD ofs); + DWORD FindExistingModuleNameOfs(const char *szName); + int InitModuleNames(void); + DWORD GetModuleNameOfs(const char *szName); + char *GetModuleNameByOfs(DWORD ofs); }; + +extern LIST g_Dbs; diff --git a/plugins/Db3x_mmap/dbmodulechain.cpp b/plugins/Db3x_mmap/dbmodulechain.cpp index ce835c3f94..f8b25ef879 100644 --- a/plugins/Db3x_mmap/dbmodulechain.cpp +++ b/plugins/Db3x_mmap/dbmodulechain.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -23,67 +23,33 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" -static INT_PTR EnumModuleNames(WPARAM wParam,LPARAM lParam); - -typedef struct { - char *name; - DWORD ofs; -} ModuleName; - -HANDLE hModHeap = NULL; -static SortedList lMods, lOfs; - -static int ModCompare( ModuleName *mn1, ModuleName *mn2 ) +void CDdxMmap::AddToList(char *name, DWORD len, DWORD ofs) { - return strcmp( mn1->name, mn2->name ); -} - -static int OfsCompare( ModuleName *mn1, ModuleName *mn2 ) -{ - return ( mn1->ofs - mn2->ofs ); -} - -void AddToList(char *name, DWORD len, DWORD ofs) -{ - int index; - ModuleName *mn = (ModuleName*)HeapAlloc(hModHeap,0,sizeof(ModuleName)); + ModuleName *mn = (ModuleName*)HeapAlloc(m_hModHeap,0,sizeof(ModuleName)); mn->name = name; mn->ofs = ofs; - if (List_GetIndex(&lMods,mn,&index)) + if (m_lMods.getIndex(mn) != -1) DatabaseCorruption( _T("%s (Module Name not unique)")); + m_lMods.insert(mn); - List_Insert(&lMods,mn,index); - - if (List_GetIndex(&lOfs,mn,&index)) + if (m_lOfs.getIndex(mn) != -1) DatabaseCorruption( _T("%s (Module Offset not unique)")); - - List_Insert(&lOfs,mn,index); + m_lOfs.insert(mn); } - -int InitModuleNames(void) +int CDdxMmap::InitModuleNames(void) { - struct DBModuleName *dbmn; - DWORD ofsThis; - int nameLen; - char *mod; - - hModHeap = HeapCreate(0, 0, 0); - lMods.sortFunc = (FSortFunc)ModCompare; - lMods.increment = 50; - lOfs.sortFunc = (FSortFunc)OfsCompare; - lOfs.increment = 50; - - ofsThis = dbHeader.ofsFirstModuleName; - dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); - while(ofsThis) { - if (dbmn->signature != DBMODULENAME_SIGNATURE) DatabaseCorruption(NULL); + DWORD ofsThis = m_dbHeader.ofsFirstModuleName; + DBModuleName *dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); + while (ofsThis) { + if (dbmn->signature != DBMODULENAME_SIGNATURE) + DatabaseCorruption(NULL); - nameLen = dbmn->cbName; + int nameLen = dbmn->cbName; - mod = (char*)HeapAlloc(hModHeap,0,nameLen+1); - CopyMemory(mod,DBRead(ofsThis+offsetof(struct DBModuleName,name),nameLen,NULL),nameLen); + char *mod = (char*)HeapAlloc(m_hModHeap,0,nameLen+1); + CopyMemory(mod,DBRead(ofsThis + offsetof(struct DBModuleName,name),nameLen,NULL),nameLen); mod[nameLen] = 0; AddToList(mod, nameLen, ofsThis); @@ -94,28 +60,16 @@ int InitModuleNames(void) return 0; } -void UninitModuleNames(void) -{ - HeapDestroy(hModHeap); - List_Destroy(&lMods); - List_Destroy(&lOfs); -} - -static DWORD FindExistingModuleNameOfs(const char *szName) +DWORD CDdxMmap::FindExistingModuleNameOfs(const char *szName) { - static ModuleName *lastmn = NULL; - ModuleName mn, *pmn; - int index; - - mn.name = (char*)szName; - mn.ofs = 0; - - if (lastmn && ModCompare(&mn,lastmn) == 0) - return lastmn->ofs; - - if (List_GetIndex(&lMods,&mn,&index)) { - pmn = (ModuleName*)lMods.items[index]; - lastmn = pmn; + ModuleName mn = { (char*)szName, 0 }; + if (m_lastmn && !strcmp(mn.name, m_lastmn->name)) + return m_lastmn->ofs; + + int index = m_lMods.getIndex(&mn); + if (index != -1) { + ModuleName *pmn = m_lMods[index]; + m_lastmn = pmn; return pmn->ofs; } @@ -123,7 +77,7 @@ static DWORD FindExistingModuleNameOfs(const char *szName) } //will create the offset if it needs to -DWORD GetModuleNameOfs(const char *szName) +DWORD CDdxMmap::GetModuleNameOfs(const char *szName) { struct DBModuleName dbmn; int nameLen; @@ -139,15 +93,15 @@ DWORD GetModuleNameOfs(const char *szName) ofsNew = CreateNewSpace(nameLen+offsetof(struct DBModuleName,name)); dbmn.signature = DBMODULENAME_SIGNATURE; dbmn.cbName = nameLen; - dbmn.ofsNext = dbHeader.ofsFirstModuleName; - dbHeader.ofsFirstModuleName = ofsNew; - DBWrite(0,&dbHeader,sizeof(dbHeader)); + dbmn.ofsNext = m_dbHeader.ofsFirstModuleName; + m_dbHeader.ofsFirstModuleName = ofsNew; + DBWrite(0,&m_dbHeader,sizeof(m_dbHeader)); DBWrite(ofsNew,&dbmn,offsetof(struct DBModuleName,name)); DBWrite(ofsNew+offsetof(struct DBModuleName,name),(PVOID)szName,nameLen); DBFlush(0); //add to cache - mod = (char*)HeapAlloc(hModHeap,0,nameLen+1); + mod = (char*)HeapAlloc(m_hModHeap,0,nameLen+1); strcpy(mod,szName); AddToList(mod, nameLen, ofsNew); @@ -155,21 +109,16 @@ DWORD GetModuleNameOfs(const char *szName) return ofsNew; } -char *GetModuleNameByOfs(DWORD ofs) +char* CDdxMmap::GetModuleNameByOfs(DWORD ofs) { - static ModuleName *lastmn = NULL; - ModuleName mn, *pmn; - int index; - - if (lastmn && lastmn->ofs == ofs) - return lastmn->name; - - mn.name = NULL; - mn.ofs = ofs; - - if (List_GetIndex(&lOfs,&mn,&index)) { - pmn = (ModuleName*)lOfs.items[index]; - lastmn = pmn; + if (m_lastmn && m_lastmn->ofs == ofs) + return m_lastmn->name; + + ModuleName mn = {NULL, ofs}; + int index = m_lOfs.getIndex(&mn); + if (index != -1) { + ModuleName *pmn = m_lOfs[index]; + m_lastmn = pmn; return pmn->name; } @@ -179,11 +128,9 @@ char *GetModuleNameByOfs(DWORD ofs) STDMETHODIMP_(BOOL) CDdxMmap::EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam) { - int ret; - ModuleName *pmn; - for(int i = 0; i < lMods.realCount; i++) { - pmn = (ModuleName *)lMods.items[i]; - ret = pFunc(pmn->name, pmn->ofs, (LPARAM)pParam); + for (int i = 0; i < m_lMods.getCount(); i++) { + ModuleName *pmn = m_lMods[i]; + int ret = pFunc(pmn->name, pmn->ofs, (LPARAM)pParam); if (ret) return ret; } diff --git a/plugins/Db3x_mmap/dbsettings.cpp b/plugins/Db3x_mmap/dbsettings.cpp index d55f71d189..918bc9ab44 100644 --- a/plugins/Db3x_mmap/dbsettings.cpp +++ b/plugins/Db3x_mmap/dbsettings.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -26,24 +26,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. DWORD GetModuleNameOfs(const char *szName); DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index); -static int mirCp = CP_ACP; - -HANDLE hCacheHeap = NULL; -SortedList lContacts = {0}; -HANDLE hLastCachedContact = NULL; -static DBCachedContactValueList *LastVL = NULL; - -static SortedList lSettings = {0}, lGlobalSettings = {0}, lResidentSettings = {0}; -static HANDLE hSettingChangeEvent = NULL; - -static DWORD GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleName) +DWORD CDdxMmap::GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleName) { - struct DBContactSettings *dbcs; - DWORD ofsThis; - - ofsThis = dbc->ofsFirstSettings; - while(ofsThis) { - dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); + DWORD ofsThis = dbc->ofsFirstSettings; + while (ofsThis) { + DBContactSettings *dbcs = (DBContactSettings*)DBRead(ofsThis,sizeof(DBContactSettings),NULL); if (dbcs->signature!=DBCONTACTSETTINGS_SIGNATURE) DatabaseCorruption(NULL); if (dbcs->ofsModuleName == ofsModuleName) return ofsThis; @@ -53,56 +40,55 @@ static DWORD GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleNa return 0; } -static DWORD __inline GetSettingValueLength(PBYTE pSetting) +DWORD __forceinline GetSettingValueLength(PBYTE pSetting) { - if (pSetting[0]&DBVTF_VARIABLELENGTH) return 2+*(PWORD)(pSetting+1); + if (pSetting[0] & DBVTF_VARIABLELENGTH) + return 2+*(PWORD)(pSetting+1); return pSetting[0]; } -static char* InsertCachedSetting( const char* szName, size_t cbNameLen, int index ) +char* CDdxMmap::InsertCachedSetting(const char* szName, size_t cbNameLen) { - char* newValue = (char*)HeapAlloc( hCacheHeap, 0, cbNameLen ); + char* newValue = (char*)HeapAlloc(m_hCacheHeap, 0, cbNameLen); *newValue = 0; - strcpy(newValue+1,szName+1); - List_Insert(&lSettings,newValue,index); + strcpy(newValue+1, szName+1); + m_lSettings.insert(newValue); return newValue; } -static char* GetCachedSetting(const char *szModuleName,const char *szSettingName, int moduleNameLen, int settingNameLen) +char* CDdxMmap::GetCachedSetting(const char *szModuleName,const char *szSettingName, int moduleNameLen, int settingNameLen) { - static char *lastsetting = NULL; - int index; char szFullName[512]; - strcpy(szFullName+1,szModuleName); szFullName[moduleNameLen+1] = '/'; strcpy(szFullName+moduleNameLen+2,szSettingName); - if (lastsetting && strcmp(szFullName+1,lastsetting) == 0) - return lastsetting; + if (m_lastSetting && strcmp(szFullName+1, m_lastSetting) == 0) + return m_lastSetting; - if (List_GetIndex(&lSettings,szFullName,&index)) - lastsetting = (char*)lSettings.items[index]+1; + int index = m_lSettings.getIndex(szFullName); + if (index != -1) + m_lastSetting = m_lSettings[index]+1; else - lastsetting = InsertCachedSetting( szFullName, settingNameLen+moduleNameLen+3, index )+1; + m_lastSetting = InsertCachedSetting( szFullName, settingNameLen+moduleNameLen+3)+1; - return lastsetting; + return m_lastSetting; } -static void SetCachedVariant( DBVARIANT* s /* new */, DBVARIANT* d /* cached */ ) +void CDdxMmap::SetCachedVariant( DBVARIANT* s /* new */, DBVARIANT* d /* cached */ ) { char* szSave = ( d->type == DBVT_UTF8 || d->type == DBVT_ASCIIZ ) ? d->pszVal : NULL; memcpy( d, s, sizeof( DBVARIANT )); if (( s->type == DBVT_UTF8 || s->type == DBVT_ASCIIZ ) && s->pszVal != NULL ) { if ( szSave != NULL ) - d->pszVal = (char*)HeapReAlloc(hCacheHeap,0,szSave,strlen(s->pszVal)+1); + d->pszVal = (char*)HeapReAlloc(m_hCacheHeap,0,szSave,strlen(s->pszVal)+1); else - d->pszVal = (char*)HeapAlloc(hCacheHeap,0,strlen(s->pszVal)+1); + d->pszVal = (char*)HeapAlloc(m_hCacheHeap,0,strlen(s->pszVal)+1); strcpy(d->pszVal,s->pszVal); } else if ( szSave != NULL ) - HeapFree(hCacheHeap,0,szSave); + HeapFree(m_hCacheHeap,0,szSave); #ifdef DBLOGGING switch( d->type ) { @@ -116,34 +102,34 @@ static void SetCachedVariant( DBVARIANT* s /* new */, DBVARIANT* d /* cached */ #endif } -void FreeCachedVariant( DBVARIANT* V ) +void CDdxMmap::FreeCachedVariant( DBVARIANT* V ) { if (( V->type == DBVT_ASCIIZ || V->type == DBVT_UTF8 ) && V->pszVal != NULL ) - HeapFree(hCacheHeap,0,V->pszVal); + HeapFree(m_hCacheHeap,0,V->pszVal); } -static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllocate ) +DBVARIANT* CDdxMmap::GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllocate ) { - int index; - if ( hContact == 0 ) { DBCachedGlobalValue Vtemp, *V; Vtemp.name = szSetting; - if ( List_GetIndex(&lGlobalSettings,&Vtemp,&index)) { - V = (DBCachedGlobalValue*)lGlobalSettings.items[index]; + int index = m_lGlobalSettings.getIndex(&Vtemp); + if (index != -1) { + V = m_lGlobalSettings[index]; if ( bAllocate == -1 ) { FreeCachedVariant( &V->value ); - List_Remove(&lGlobalSettings,index); - HeapFree(hCacheHeap,0,V); + m_lGlobalSettings.remove(index); + HeapFree(m_hCacheHeap,0,V); return NULL; - } } + } + } else { if ( bAllocate != 1 ) return NULL; - V = (DBCachedGlobalValue*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedGlobalValue)); + V = (DBCachedGlobalValue*)HeapAlloc(m_hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedGlobalValue)); V->name = szSetting; - List_Insert(&lGlobalSettings,V,index); + m_lGlobalSettings.insert(V); } return &V->value; @@ -152,23 +138,22 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo DBCachedContactValue *V, *V1; DBCachedContactValueList VLtemp,*VL; - if (hLastCachedContact == hContact && LastVL) { - VL = LastVL; - } + if (m_hLastCachedContact == hContact && m_lastVL) + VL = m_lastVL; else { VLtemp.hContact = hContact; - if ( !List_GetIndex(&lContacts,&VLtemp,&index)) - { + int index = m_lContacts.getIndex(&VLtemp); + if (index == -1) { if ( bAllocate != 1 ) return NULL; - VL = AddToCachedContactList(hContact,index); + VL = AddToCachedContactList(hContact, index); } - else VL = (DBCachedContactValueList*)lContacts.items[index]; + else VL = m_lContacts[index]; - LastVL = VL; - hLastCachedContact = hContact; + m_lastVL = VL; + m_hLastCachedContact = hContact; } for ( V = VL->first; V != NULL; V = V->next) @@ -179,7 +164,7 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo if ( bAllocate != 1 ) return NULL; - V = (DBCachedContactValue *)HeapAlloc(hCacheHeap, HEAP_ZERO_MEMORY, sizeof(DBCachedContactValue)); + V = (DBCachedContactValue *)HeapAlloc(m_hCacheHeap, HEAP_ZERO_MEMORY, sizeof(DBCachedContactValue)); if (VL->last) VL->last->next = V; else @@ -188,7 +173,7 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo V->name = szSetting; } else if ( bAllocate == -1 ) { - LastVL = NULL; + m_lastVL = NULL; FreeCachedVariant(&V->value); if ( VL->first == V ) { VL->first = V->next; @@ -203,7 +188,7 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo VL->last = V1; break; } - HeapFree(hCacheHeap,0,V); + HeapFree(m_hCacheHeap,0,V); return NULL; } @@ -213,7 +198,8 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo #define NeedBytes(n) if (bytesRemaining<(n)) pBlob = (PBYTE)DBRead(ofsBlobPtr,(n),&bytesRemaining) #define MoveAlong(n) {int x = n; pBlob += (x); ofsBlobPtr += (x); bytesRemaining -= (x);} #define VLT(n) ((n == DBVT_UTF8)?DBVT_ASCIIZ:n) -static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs,int isStatic) + +int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs,int isStatic) { DBContact *dbc; DWORD ofsModuleName,ofsContact,ofsSettingsGroup,ofsBlobPtr; @@ -242,7 +228,7 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING return 1; } - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); log3("get [%08p] %s/%s",hContact,dbcgs->szModule,dbcgs->szSetting); @@ -283,36 +269,36 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING default: log1( "get cached crap: %d", dbcgs->pValue->type ); break; } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return ( pCachedValue->type == DBVT_DELETED ) ? 1 : 0; } } ofsModuleName = GetModuleNameOfs(dbcgs->szModule); - if (hContact == NULL) ofsContact = dbHeader.ofsUser; + if (hContact == NULL) ofsContact = m_dbHeader.ofsUser; else ofsContact = (DWORD)hContact; dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); if (dbc->signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName); if (ofsSettingsGroup) { - ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); - pBlob = DBRead(ofsBlobPtr,sizeof(struct DBContactSettings),&bytesRemaining); - while(pBlob[0]) { + ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob); + pBlob = DBRead(ofsBlobPtr,sizeof(DBContactSettings),&bytesRemaining); + while (pBlob[0]) { NeedBytes(1+settingNameLen); if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen)) { MoveAlong(1+settingNameLen); NeedBytes(5); if (isStatic && pBlob[0]&DBVTF_VARIABLELENGTH && VLT(dbcgs->pValue->type) != VLT(pBlob[0])) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } dbcgs->pValue->type = pBlob[0]; switch(pBlob[0]) { case DBVT_DELETED: { /* this setting is deleted */ dbcgs->pValue->type = DBVT_DELETED; - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 2; } case DBVT_BYTE: dbcgs->pValue->bVal = pBlob[1]; break; @@ -356,7 +342,7 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING SetCachedVariant(dbcgs->pValue,pCachedValue); } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); logg(); return 0; } @@ -375,7 +361,7 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING pCachedValue->type = DBVT_DELETED; } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); logg(); return 1; } @@ -391,7 +377,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetContactSetting(HANDLE hContact, DBCONTACTGETSET char* p = NEWSTR_ALLOCA(dgs->pValue->pszVal); if ( mir_utf8decode( p, &tmp ) != NULL ) { BOOL bUsed = FALSE; - int result = WideCharToMultiByte( mirCp, WC_NO_BEST_FIT_CHARS, tmp, -1, NULL, 0, NULL, &bUsed ); + int result = WideCharToMultiByte( m_codePage, WC_NO_BEST_FIT_CHARS, tmp, -1, NULL, 0, NULL, &bUsed ); mir_free( dgs->pValue->pszVal ); @@ -402,7 +388,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetContactSetting(HANDLE hContact, DBCONTACTGETSET else { dgs->pValue->type = DBVT_ASCIIZ; dgs->pValue->pszVal = (char *)mir_alloc(result); - WideCharToMultiByte( mirCp, WC_NO_BEST_FIT_CHARS, tmp, -1, dgs->pValue->pszVal, result, NULL, NULL ); + WideCharToMultiByte( m_codePage, WC_NO_BEST_FIT_CHARS, tmp, -1, dgs->pValue->pszVal, result, NULL, NULL ); mir_free( tmp ); } } @@ -500,30 +486,29 @@ STDMETHODIMP_(BOOL) CDdxMmap::FreeVariant(DBVARIANT *dbv) STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *pszSettingName) { size_t cbSettingNameLen = strlen(pszSettingName) + 2; - if (cbSettingNameLen < 512) - { + if (cbSettingNameLen < 512) { char* szSetting; - int idx; char szTemp[512]; strcpy( szTemp+1, pszSettingName); - EnterCriticalSection(&csDbAccess); - if ( !List_GetIndex( &lSettings, szTemp, &idx )) - szSetting = InsertCachedSetting( szTemp, cbSettingNameLen, idx ); + EnterCriticalSection(&m_csDbAccess); + int idx = m_lSettings.getIndex(szTemp); + if (idx == -1) + szSetting = InsertCachedSetting( szTemp, cbSettingNameLen); else - szSetting = (char *)lSettings.items[idx]; + szSetting = m_lSettings[idx]; *szSetting = (char)bIsResident; - if ( !List_GetIndex( &lResidentSettings, szSetting+1, &idx )) - { + idx = m_lResidentSettings.getIndex(szSetting+1); + if (idx == -1) { if (bIsResident) - List_Insert(&lResidentSettings,szSetting+1,idx); + m_lResidentSettings.insert(szSetting+1); } else if (!bIsResident) - List_Remove(&lResidentSettings,idx); + m_lResidentSettings.remove(idx); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); } return 0; } @@ -533,7 +518,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT DBCONTACTWRITESETTING tmp; DBContact dbc; DWORD ofsModuleName; - struct DBContactSettings dbcs; + DBContactSettings dbcs; PBYTE pBlob; int settingNameLen = 0; int moduleNameLen = 0; @@ -596,7 +581,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT } } - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); { char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, moduleNameLen, settingNameLen); if ( tmp.value.type != DBVT_BLOB ) { @@ -612,14 +597,14 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT case DBVT_ASCIIZ: bIsIdentical = strcmp( pCachedValue->pszVal, tmp.value.pszVal ) == 0; break; } if ( bIsIdentical ) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 0; } } SetCachedVariant(&tmp.value, pCachedValue); } if ( szCachedSettingName[-1] != 0 ) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp); return 0; } @@ -628,12 +613,12 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT } ofsModuleName = GetModuleNameOfs(tmp.szModule); - if (hContact == 0) ofsContact = dbHeader.ofsUser; + if (hContact == 0) ofsContact = m_dbHeader.ofsUser; else ofsContact = (DWORD)hContact; dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); if (dbc.signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } log0("write setting"); @@ -647,7 +632,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT else bytesRequired = tmp.value.type; bytesRequired += 2+settingNameLen; bytesRequired += (DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY; - ofsSettingsGroup = CreateNewSpace(bytesRequired+offsetof(struct DBContactSettings,blob)); + ofsSettingsGroup = CreateNewSpace(bytesRequired+offsetof(DBContactSettings,blob)); dbcs.signature = DBCONTACTSETTINGS_SIGNATURE; dbcs.ofsNext = dbc.ofsFirstSettings; dbcs.ofsModuleName = ofsModuleName; @@ -655,16 +640,16 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT dbcs.blob[0] = 0; dbc.ofsFirstSettings = ofsSettingsGroup; DBWrite(ofsContact,&dbc,sizeof(DBContact)); - DBWrite(ofsSettingsGroup,&dbcs,sizeof(struct DBContactSettings)); - ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); + DBWrite(ofsSettingsGroup,&dbcs,sizeof(DBContactSettings)); + ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob); pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); } else { - dbcs = *(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),&bytesRemaining); + dbcs = *(DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(DBContactSettings),&bytesRemaining); //find if the setting exists - ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); + ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob); pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); - while(pBlob[0]) { + while (pBlob[0]) { NeedBytes(settingNameLen+1); if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,tmp.szSetting,settingNameLen)) break; @@ -688,7 +673,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT ofsSettingToCut = ofsBlobPtr-nameLen; MoveAlong(valLen); NeedBytes(1); - while(pBlob[0]) { + while (pBlob[0]) { MoveAlong(pBlob[0]+1); NeedBytes(3); MoveAlong(1+GetSettingValueLength(pBlob)); @@ -711,7 +696,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT } //quit DBFlush(1); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); //notify NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp); return 0; @@ -727,10 +712,10 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT } else bytesRequired = tmp.value.type; bytesRequired += 2+settingNameLen; - bytesRequired += ofsBlobPtr+1-(ofsSettingsGroup+offsetof(struct DBContactSettings,blob)); + bytesRequired += ofsBlobPtr+1-(ofsSettingsGroup+offsetof(DBContactSettings,blob)); if ((DWORD)bytesRequired>dbcs.cbBlob) { //doesn't fit: move entire group - struct DBContactSettings *dbcsPrev; + DBContactSettings *dbcsPrev; DWORD ofsDbcsPrev,ofsNew; bytesRequired += (DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY; @@ -738,28 +723,28 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT ofsDbcsPrev = dbc.ofsFirstSettings; if (ofsDbcsPrev == ofsSettingsGroup) ofsDbcsPrev = 0; else { - dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); - while(dbcsPrev->ofsNext!=ofsSettingsGroup) { + dbcsPrev = (DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(DBContactSettings),NULL); + while (dbcsPrev->ofsNext!=ofsSettingsGroup) { if (dbcsPrev->ofsNext == 0) DatabaseCorruption(NULL); ofsDbcsPrev = dbcsPrev->ofsNext; - dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); + dbcsPrev = (DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(DBContactSettings),NULL); } } //create the new one - ofsNew = ReallocSpace(ofsSettingsGroup, dbcs.cbBlob+offsetof(struct DBContactSettings,blob), bytesRequired+offsetof(struct DBContactSettings,blob)); + ofsNew = ReallocSpace(ofsSettingsGroup, dbcs.cbBlob+offsetof(DBContactSettings,blob), bytesRequired+offsetof(DBContactSettings,blob)); dbcs.cbBlob = bytesRequired; - DBWrite(ofsNew,&dbcs,offsetof(struct DBContactSettings,blob)); + DBWrite(ofsNew,&dbcs,offsetof(DBContactSettings,blob)); if (ofsDbcsPrev == 0) { dbc.ofsFirstSettings = ofsNew; DBWrite(ofsContact,&dbc,sizeof(DBContact)); } else { - dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); + dbcsPrev = (DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(DBContactSettings),NULL); dbcsPrev->ofsNext = ofsNew; - DBWrite(ofsDbcsPrev,dbcsPrev,offsetof(struct DBContactSettings,blob)); + DBWrite(ofsDbcsPrev,dbcsPrev,offsetof(DBContactSettings,blob)); } ofsBlobPtr += ofsNew-ofsSettingsGroup; ofsSettingsGroup = ofsNew; @@ -794,7 +779,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT } //quit DBFlush(1); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); //notify NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp ); return 0; @@ -829,26 +814,26 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET return 1; } - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); ofsModuleName = GetModuleNameOfs(dbcgs->szModule); if (hContact == 0) - hContact = (HANDLE)dbHeader.ofsUser; + hContact = (HANDLE)m_dbHeader.ofsUser; dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL); if (dbc->signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } //make sure the module group exists ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName); if (ofsSettingsGroup == 0) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } //find if the setting exists - ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); + ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob); pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); - while(pBlob[0]) { + while (pBlob[0]) { NeedBytes(settingNameLen+1); if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen)) break; @@ -859,7 +844,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET NeedBytes(1); } if (!pBlob[0]) { //setting didn't exist - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return 1; } { //bin it @@ -872,7 +857,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET ofsSettingToCut = ofsBlobPtr-nameLen; MoveAlong(valLen); NeedBytes(1); - while(pBlob[0]) { + while (pBlob[0]) { MoveAlong(pBlob[0]+1); NeedBytes(3); MoveAlong(1+GetSettingValueLength(pBlob)); @@ -886,7 +871,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET //quit DBFlush(1); - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); { //notify DBCONTACTWRITESETTING dbcws = {0}; dbcws.szModule = dbcgs->szModule; @@ -908,29 +893,29 @@ STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUM if (!dbces->szModule) return -1; - EnterCriticalSection(&csDbAccess); + EnterCriticalSection(&m_csDbAccess); ofsModuleName = GetModuleNameOfs(dbces->szModule); - if (hContact == 0) ofsContact = dbHeader.ofsUser; + if (hContact == 0) ofsContact = m_dbHeader.ofsUser; else ofsContact = (DWORD)hContact; dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); if (dbc->signature != DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return -1; } dbces->ofsSettings = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName); if (!dbces->ofsSettings) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return -1; } - ofsBlobPtr = dbces->ofsSettings+offsetof(struct DBContactSettings,blob); + ofsBlobPtr = dbces->ofsSettings+offsetof(DBContactSettings,blob); pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); if (pBlob[0] == 0) { - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return -1; } result = 0; - while(pBlob[0]) { + while (pBlob[0]) { NeedBytes(1); NeedBytes(1+pBlob[0]); CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0; @@ -940,56 +925,15 @@ STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUM MoveAlong(1+GetSettingValueLength(pBlob)); NeedBytes(1); } - LeaveCriticalSection(&csDbAccess); + LeaveCriticalSection(&m_csDbAccess); return result; } STDMETHODIMP_(BOOL) CDdxMmap::EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam) { - for(int i = 0; i < lResidentSettings.realCount; i++) { - int ret = pFunc((char *)lResidentSettings.items[i], 0, (LPARAM)pParam); + for(int i = 0; i < m_lResidentSettings.getCount(); i++) { + int ret = pFunc(m_lResidentSettings[i], 0, (LPARAM)pParam); if (ret) return ret; } return 0; } - -///////////////////////////////////////////////////////////////////////////////////////// -// -// Module initialization procedure - -static int stringCompare( DBCachedSettingName* p1, DBCachedSettingName* p2 ) -{ - return strcmp( p1->name, p2->name ); -} - -static int stringCompare2( char* p1, char* p2 ) -{ - return strcmp( p1, p2); -} - -int InitSettings(void) -{ - hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED); - - hCacheHeap = HeapCreate(0, 0, 0); - lSettings.sortFunc = (FSortFunc)stringCompare; - lSettings.increment = 100; - lContacts.sortFunc = HandleKeySort; - lContacts.increment = 50; - lGlobalSettings.sortFunc = HandleKeySort; - lGlobalSettings.increment = 50; - lResidentSettings.sortFunc = (FSortFunc)stringCompare2; - lResidentSettings.increment = 50; - - mirCp = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0); - return 0; -} - -void UninitSettings(void) -{ - HeapDestroy(hCacheHeap); - List_Destroy(&lContacts); - List_Destroy(&lSettings); - List_Destroy(&lGlobalSettings); - List_Destroy(&lResidentSettings); -} diff --git a/plugins/Db3x_mmap/init.cpp b/plugins/Db3x_mmap/init.cpp index b655e61499..03f4f3c3bb 100644 --- a/plugins/Db3x_mmap/init.cpp +++ b/plugins/Db3x_mmap/init.cpp @@ -1,8 +1,8 @@ /* -Miranda IM: the free IM client for Microsoft* Windows* +Miranda NG: the free IM client for Microsoft* Windows* -Copyright 2000-2003 Miranda ICQ/IM project, +Copyright 2012 Miranda NG project, all portions of this codebase are copyrighted to the people listed in contributors.txt. @@ -25,19 +25,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int hLangpack; -extern TCHAR szDbPath[MAX_PATH]; - -CDdxMmap* g_Db = NULL; - static PLUGININFOEX pluginInfo = { sizeof(PLUGININFOEX), - "Miranda mmap database driver", + "Miranda NG mmap database driver", __VERSION_DWORD, "Provides Miranda database support: global settings, contacts, history, settings per contact.", - "Miranda-IM project", + "Miranda-NG project", "bio@msx.ru; ghazan@miranda.im", - "Copyright 2000-2011 Miranda IM project", + "Copyright 2012 Miranda NG project", "", UNICODE_AWARE, {0xf7a6b27c, 0x9d9c, 0x4a42, { 0xbe, 0x86, 0xa4, 0x48, 0xae, 0x10, 0x91, 0x61 }} //{F7A6B27C-9D9C-4a42-BE86-A448AE109161} @@ -45,129 +41,105 @@ static PLUGININFOEX pluginInfo = HINSTANCE g_hInst = NULL; +LIST g_Dbs(1, (LIST::FTSortFunc)HandleKeySort); + +///////////////////////////////////////////////////////////////////////////////////////// + static int getCapability( int flag ) { return 0; } // returns 0 if the profile is created, EMKPRF* -static int makeDatabase(TCHAR *profile, int *error) +static int makeDatabase(const TCHAR *profile, int *error) { - HANDLE hFile = CreateFile(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); - if ( hFile != INVALID_HANDLE_VALUE ) { - CreateDbHeaders(hFile); - CloseHandle(hFile); + CDdxMmap *tmp = new CDdxMmap(profile); + if (tmp->Create() == ERROR_SUCCESS) { + tmp->CreateDbHeaders(); + delete tmp; return 0; } - if ( error != NULL ) *error = EMKPRF_CREATEFAILED; + delete tmp; + if (error != NULL) *error = EMKPRF_CREATEFAILED; return 1; } // returns 0 if the given profile has a valid header -static int grokHeader(TCHAR *profile, int *error) +static int grokHeader(const TCHAR *profile, int *error) { - int rc = 1; - int chk = 0; - struct DBHeader hdr; - HANDLE hFile = INVALID_HANDLE_VALUE; - DWORD dummy = 0; - - hFile = CreateFile(profile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); - if ( hFile == INVALID_HANDLE_VALUE ) { - if ( error != NULL ) *error = EGROKPRF_CANTREAD; + CDdxMmap *tmp = new CDdxMmap(profile); + if (tmp->Load(true) != ERROR_SUCCESS) { + delete tmp; + if (error != NULL) *error = EGROKPRF_CANTREAD; return 1; } - // read the header, which can fail (for various reasons) - if ( !ReadFile(hFile, &hdr, sizeof(struct DBHeader), &dummy, NULL)) { - if ( error != NULL) *error = EGROKPRF_CANTREAD; - CloseHandle(hFile); - return 1; - } - chk = CheckDbHeaders(&hdr); + + int chk = tmp->CheckDbHeaders(); + delete tmp; if ( chk == 0 ) { // all the internal tests passed, hurrah - rc = 0; - if ( error != NULL ) *error = 0; - } else { - // didn't pass at all, or some did. - switch ( chk ) { - case 1: - { - // "Miranda ICQ DB" wasn't present - if ( error != NULL ) *error = EGROKPRF_UNKHEADER; - break; - } - case 2: - { - // header was present, but version information newer - if ( error != NULL ) *error = EGROKPRF_VERNEWER; - break; - } - case 3: - { - // header/version OK, internal data missing - if ( error != NULL ) *error = EGROKPRF_DAMAGED; - break; - } - } // switch - } //if - CloseHandle(hFile); - return rc; + if (error != NULL) *error = 0; + return 0; + } + + // didn't pass at all, or some did. + switch ( chk ) { + case 1: + // "Miranda ICQ DB" wasn't present + if (error != NULL) *error = EGROKPRF_UNKHEADER; + break; + + case 2: + // header was present, but version information newer + if (error != NULL) *error = EGROKPRF_VERNEWER; + break; + + case 3: + // header/version OK, internal data missing + if (error != NULL) *error = EGROKPRF_DAMAGED; + break; + } + + return 1; } // returns 0 if all the APIs are injected otherwise, 1 -static MIDatabase* LoadDatabase(TCHAR *profile) +static MIDatabase* LoadDatabase(const TCHAR *profile) { - if (g_Db) delete g_Db; - g_Db = new CDdxMmap(profile); - - // don't need thread notifications - _tcsncpy(szDbPath, profile, SIZEOF(szDbPath)); - // set the memory, lists & UTF8 manager mir_getLP( &pluginInfo ); - // inject all APIs and hooks into the core - LoadDatabaseModule(); + CDdxMmap* db = new CDdxMmap(profile); + if (db->Load(false) != ERROR_SUCCESS) { + delete db; + return NULL; + } - return g_Db; + g_Dbs.insert(db); + return db; } -static int UnloadDatabase(int wasLoaded) +static int UnloadDatabase(MIDatabase* db) { - if ( !wasLoaded) return 0; - UnloadDatabaseModule(); + g_Dbs.remove((CDdxMmap*)db); + delete (CDdxMmap*)db; return 0; } -static int getFriendlyName( TCHAR *buf, size_t cch, int shortName ) +static DATABASELINK dblink = { - _tcsncpy(buf,shortName ? _T("db3x mmap driver") : _T("db3x mmap database support"), cch); - return 0; -} - -static DATABASELINK dblink = { sizeof(DATABASELINK), - getCapability, - getFriendlyName, + "db3x mmap driver", + _T("db3x mmap database support"), makeDatabase, grokHeader, LoadDatabase, - UnloadDatabase, + UnloadDatabase }; -BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved) -{ - g_hInst = hInstDLL; - return TRUE; -} - -extern "C" __declspec(dllexport) DATABASELINK* DatabasePluginInfo(void * reserved) -{ - return &dblink; -} +///////////////////////////////////////////////////////////////////////////////////////// -extern "C" __declspec(dllexport) PLUGININFOEX * MirandaPluginInfoEx(DWORD mirandaVersion) +extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion) { return &pluginInfo; } @@ -176,10 +148,18 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_DATABAS extern "C" __declspec(dllexport) int Load(void) { - return 1; + RegisterDatabasePlugin(&dblink); + return 0; } extern "C" __declspec(dllexport) int Unload(void) { + g_Dbs.destroy(); return 0; } + +BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved) +{ + g_hInst = hInstDLL; + return TRUE; +} diff --git a/plugins/Db3x_mmap/version.rc b/plugins/Db3x_mmap/version.rc index d2567f55ae..21a71416b5 100644 --- a/plugins/Db3x_mmap/version.rc +++ b/plugins/Db3x_mmap/version.rc @@ -27,11 +27,11 @@ BEGIN BEGIN BLOCK "041904b0" BEGIN - VALUE "FileDescription", "Miranda IM Mmap DataBase Engine 3x" + VALUE "FileDescription", "Miranda NG Mmap DataBase Engine 3x" VALUE "FileVersion", __VERSION_STRING - VALUE "LegalCopyright", "Copyright (C) 2000-2007" + VALUE "LegalCopyright", "Copyright (C) 2012" VALUE "OriginalFilename", "dbx_mmap.dll" - VALUE "ProductName", "Miranda IM Mmap DataBase Engine 3x" + VALUE "ProductName", "Miranda NG Mmap DataBase Engine 3x" VALUE "ProductVersion", __VERSION_STRING END END diff --git a/plugins/Dbx_mmap_SA/init.cpp b/plugins/Dbx_mmap_SA/init.cpp index 97c6353d69..cf06474dde 100644 --- a/plugins/Dbx_mmap_SA/init.cpp +++ b/plugins/Dbx_mmap_SA/init.cpp @@ -45,13 +45,10 @@ PLUGININFOEX pluginInfo = { { 0x28ff9b91, 0x3e4d, 0x4f1c, { 0xb4, 0x7c, 0xc6, 0x41, 0xb0, 0x37, 0xff, 0x40 } } }; -static int getCapability( int flag ) -{ - return 0; -} +///////////////////////////////////////////////////////////////////////////////////////// // returns 0 if the profile is created, EMKPRF* -static int makeDatabase(TCHAR *profile, int *error) +static int makeDatabase(const TCHAR *profile, int *error) { HANDLE hFile = CreateFile(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if ( hFile != INVALID_HANDLE_VALUE ) { @@ -64,7 +61,7 @@ static int makeDatabase(TCHAR *profile, int *error) } // returns 0 if the given profile has a valid header -static int grokHeader(TCHAR *profile, int *error ) +static int grokHeader(const TCHAR *profile, int *error ) { int rc = 1; int chk = 0; @@ -116,7 +113,7 @@ static int grokHeader(TCHAR *profile, int *error ) } // returns 0 if all the APIs are injected otherwise, 1 -static MIDatabase* LoadDatabase(TCHAR *profile) +static MIDatabase* LoadDatabase(const TCHAR *profile) { if (g_Db) delete g_Db; g_Db = new CDdxMmap(profile); @@ -152,23 +149,17 @@ static MIDatabase* LoadDatabase(TCHAR *profile) return g_Db; } -static int UnloadDatabase(int wasLoaded) +static int UnloadDatabase(MIDatabase* db) { - if ( !wasLoaded) return 0; UnloadDatabaseModule(); - return 0; -} - -static int getFriendlyName( TCHAR* buf, size_t cch, int shortName ) -{ - _tcsncpy(buf,shortName ? _T("db3x secured_mmap driver") : _T("db3x mmap database support"), cch); + delete g_Db; g_Db = 0; return 0; } static DATABASELINK dblink = { sizeof(DATABASELINK), - getCapability, - getFriendlyName, + "db3x secured_mmap driver", + _T("db3x mmap database support"), makeDatabase, grokHeader, LoadDatabase, @@ -181,11 +172,6 @@ BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved) return TRUE; } -extern "C" __declspec(dllexport) DATABASELINK* DatabasePluginInfo(void * reserved) -{ - return &dblink; -} - extern "C" __declspec(dllexport) PLUGININFOEX * MirandaPluginInfoEx(DWORD mirandaVersion) { return &pluginInfo; @@ -195,7 +181,7 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_DATABAS extern "C" __declspec(dllexport) int Load(void) { - return 1; + return 0; } extern "C" __declspec(dllexport) int Unload(void) diff --git a/plugins/Dbx_tree/Compatibility.cpp b/plugins/Dbx_tree/Compatibility.cpp index 9247e4d744..759e44012c 100644 --- a/plugins/Dbx_tree/Compatibility.cpp +++ b/plugins/Dbx_tree/Compatibility.cpp @@ -797,8 +797,3 @@ bool CompatibilityRegister() hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED); return true; } - -bool CompatibilityUnRegister() -{ - return true; -} diff --git a/plugins/Dbx_tree/Compatibility.h b/plugins/Dbx_tree/Compatibility.h index 4d89ca6cda..a23fa9284b 100644 --- a/plugins/Dbx_tree/Compatibility.h +++ b/plugins/Dbx_tree/Compatibility.h @@ -27,5 +27,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Services.h" bool CompatibilityRegister(); -bool CompatibilityUnRegister(); diff --git a/plugins/Dbx_tree/DatabaseLink.cpp b/plugins/Dbx_tree/DatabaseLink.cpp index 850e798ef9..e2947bb72c 100644 --- a/plugins/Dbx_tree/DatabaseLink.cpp +++ b/plugins/Dbx_tree/DatabaseLink.cpp @@ -25,42 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "savestrings_gcc.h" #endif -HANDLE hSystemModulesLoaded = 0; - -static int SystemModulesLoaded(WPARAM wParam, LPARAM lParam) -{ - - UnhookEvent(hSystemModulesLoaded); - hSystemModulesLoaded = 0; - - return 0; -} - -/* -returns what the driver can do given the flag -*/ -static int getCapability(int flag) -{ - return 0; -} - -/* - buf: pointer to a string buffer - cch: length of buffer - shortName: if true, the driver should return a short but descriptive name, e.g. "3.xx profile" - Affect: The database plugin must return a "friendly name" into buf and not exceed cch bytes, - e.g. "Database driver for 3.xx profiles" - Returns: 0 on success, non zero on failure -*/ - -static int getFriendlyName(TCHAR *buf, size_t cch, int shortName) -{ - if (shortName) - _tcsncpy_s(buf, cch, _T(gInternalName), SIZEOF(gInternalName)); - else - _tcsncpy_s(buf, cch, _T(gInternalNameLong), SIZEOF(gInternalNameLong)); - return 0; -} /* profile: pointer to a string which contains full path + name @@ -70,7 +34,7 @@ static int getFriendlyName(TCHAR *buf, size_t cch, int shortName) Note: Do not initialise internal data structures at this point! Returns: 0 on success, non zero on failure - error contains extended error information, see EMKPRF_* */ -static int makeDatabase(TCHAR *profile, int* error) +static int makeDatabase(const TCHAR *profile, int* error) { if (gDataBase) delete gDataBase; gDataBase = new CDataBase(profile); @@ -89,7 +53,7 @@ static int makeDatabase(TCHAR *profile, int* error) etc. Returns: 0 on success, non zero on failure */ -static int grokHeader(TCHAR *profile, int* error) +static int grokHeader(const TCHAR *profile, int* error) { if (gDataBase) delete gDataBase; gDataBase = new CDataBase(profile); @@ -104,16 +68,11 @@ Affect: Tell the database to create all services/hooks that a 3.xx legecy databa Returns: 0 on success, nonzero on failure */ -static MIDatabase* Load(TCHAR *profile) +static MIDatabase* LoadDatabase(const TCHAR *profile) { if (gDataBase) delete gDataBase; gDataBase = new CDataBase(profile); - RegisterServices(); - CompatibilityRegister(); - - hSystemModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, SystemModulesLoaded); - gDataBase->OpenDB(); return gDataBase; } @@ -123,7 +82,7 @@ Affect: The database plugin should shutdown, unloading things from the core and Returns: 0 on success, nonzero on failure Note: Unload() might be called even if Load(void) was never called, wasLoaded is set to 1 if Load(void) was ever called. */ -static int Unload(int wasLoaded) +static int UnloadDatabase(MIDatabase* db) { if (gDataBase) delete gDataBase; @@ -134,10 +93,10 @@ static int Unload(int wasLoaded) DATABASELINK gDBLink = { sizeof(DATABASELINK), - getCapability, - getFriendlyName, + gInternalName, + _T(gInternalNameLong), makeDatabase, grokHeader, - Load, - Unload, + LoadDatabase, + UnloadDatabase, }; diff --git a/plugins/Dbx_tree/init.cpp b/plugins/Dbx_tree/init.cpp index 0ad9e6721b..eca96fac97 100644 --- a/plugins/Dbx_tree/init.cpp +++ b/plugins/Dbx_tree/init.cpp @@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "Interface.h" +#include "DatabaseLink.h" HINSTANCE hInstance = NULL; int hLangpack; @@ -34,7 +35,7 @@ static PLUGININFOEX gPluginInfoEx = { sizeof(PLUGININFOEX), gInternalNameLong, gVersion, - gDescription " - build " __DATE__ " @ " __TIME__, + gDescription, gAutor, gAutorEmail, gCopyright, @@ -43,11 +44,6 @@ static PLUGININFOEX gPluginInfoEx = { gGUID }; -extern "C" __declspec(dllexport) DATABASELINK* DatabasePluginInfo(void * Reserved) -{ - return &gDBLink; -} - extern "C" __declspec(dllexport) PLUGININFOEX * MirandaPluginInfoEx(DWORD MirandaVersion) { return &gPluginInfoEx; @@ -58,7 +54,11 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_DATABAS extern "C" __declspec(dllexport) int Load(void) { mir_getLP(&gPluginInfoEx); - return 1; + + RegisterDatabasePlugin(&gDBLink); + RegisterServices(); + CompatibilityRegister(); + return 0; } extern "C" __declspec(dllexport) int Unload(void) @@ -66,7 +66,6 @@ extern "C" __declspec(dllexport) int Unload(void) return 0; } - BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved) { hInstance = hInstDLL; diff --git a/plugins/UserInfoEx/svc_avatar.cpp b/plugins/UserInfoEx/svc_avatar.cpp index 0010ec54b1..f9d5515f52 100644 --- a/plugins/UserInfoEx/svc_avatar.cpp +++ b/plugins/UserInfoEx/svc_avatar.cpp @@ -33,93 +33,22 @@ Last change by : $Author: ing.u.horn $ #include "m_png.h" #include "m_avatars.h" -namespace NServices +namespace NServices { - namespace NAvatar + namespace NAvatar { static HANDLE ghChangedHook = NULL; -/* - int SaveBitmapAsAvatar(HBITMAP hBitmap, LPCSTR szFileName) - { - BITMAPINFO* bmi; - HDC hdc; - HBITMAP hOldBitmap; - BITMAPINFOHEADER* pDib; - BYTE* pDibBits; - long dwPngSize = 0; - DIB2PNG convertor; - FILE* out; - - // file exists? - out = fopen(szFileName, "rb"); - if (out != NULL) { - fclose(out); - return ERROR_SUCCESS; - } - - if (!ServiceExists(MS_DIB2PNG)) { - MsgErr(NULL, TranslateT("Your png2dib.dll is either obsolete or damaged.")); - return 1; - } - - hdc = CreateCompatibleDC(NULL); - hOldBitmap = (HBITMAP)SelectObject(hdc, hBitmap); - - bmi = (BITMAPINFO*)_alloca(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256); - memset(bmi, 0, sizeof(BITMAPINFO)); - bmi->bmiHeader.biSize = 0x28; - if (GetDIBits(hdc, hBitmap, 0, 96, NULL, bmi, DIB_RGB_COLORS) == 0) { - return 2; - } - - pDib = (BITMAPINFOHEADER*)GlobalAlloc(LPTR, sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256 + bmi->bmiHeader.biSizeImage); - if (pDib == NULL) - return 3; - - memcpy(pDib, bmi, sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256); - pDibBits = ((BYTE*)pDib) + sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256; - - GetDIBits(hdc, hBitmap, 0, pDib->biHeight, pDibBits, (BITMAPINFO*)pDib, DIB_RGB_COLORS); - SelectObject(hdc, hOldBitmap); - DeleteDC(hdc); - - convertor.pbmi = (BITMAPINFO*)pDib; - convertor.pDiData = pDibBits; - convertor.pResult = NULL; - convertor.pResultLen = &dwPngSize; - if (!CallService(MS_DIB2PNG, 0, (LPARAM)&convertor)) { - GlobalFree(pDib); - return 2; - } - - convertor.pResult = (PBYTE)mir_alloc(dwPngSize); - CallService(MS_DIB2PNG, 0, (LPARAM)&convertor); - - GlobalFree(pDib); - - out = fopen(szFileName, "wb"); - if (out != NULL) { - fwrite(convertor.pResult, dwPngSize, 1, out); - fclose(out); - } - - mir_free(convertor.pResult); - - return ERROR_SUCCESS; - } -*/ - static INT GetContactAvatarFileName(LPCTSTR zodiac, LPSTR szFileName, INT cchFileName) { - if (!CallService(MS_DB_GETPROFILEPATH, (WPARAM)cchFileName, (LPARAM)szFileName)) + if (!CallService(MS_DB_GETPROFILEPATH, (WPARAM)cchFileName, (LPARAM)szFileName)) { size_t len = mir_strlen(szFileName); - + CHAR tmp[64]; - if (WideCharToMultiByte(CP_ACP, 0, zodiac, 64, tmp, SIZEOF(tmp),0,0) > 0) + if (WideCharToMultiByte(CP_ACP, 0, zodiac, 64, tmp, SIZEOF(tmp),0,0) > 0) { mir_snprintf(szFileName + len, cchFileName - len, "\\avatars\\%s.png", tmp); } @@ -133,7 +62,7 @@ namespace NServices * * **/ - static VOID SetZodiacAvatar(HANDLE hContact) + static VOID SetZodiacAvatar(HANDLE hContact) { MAnnivDate mtb; @@ -143,17 +72,17 @@ namespace NServices MZodiac zodiac; //ICONINFO iinfo; CHAR szFileName[MAX_PATH]; - + // get zodiac for birthday zodiac = mtb.Zodiac(); - if (!GetContactAvatarFileName(zodiac.pszName, szFileName, SIZEOF(szFileName))) + if (!GetContactAvatarFileName(zodiac.pszName, szFileName, SIZEOF(szFileName))) { // extract the bitmap from the icon //GetIconInfo(zodiac.hIcon, &iinfo); // save the bitmap to a file used as avatar later - //if (!SaveBitmapAsAvatar(iinfo.hbmColor, szFileName)) + //if (!SaveBitmapAsAvatar(iinfo.hbmColor, szFileName)) { if (!CallService(MS_AV_SETAVATAR, (WPARAM)hContact, (LPARAM)szFileName)) { @@ -177,7 +106,7 @@ namespace NServices DB::Setting::Delete(hContact, "ContactPhoto", "ImageHash"); DB::Setting::WriteByte(hContact, "ContactPhoto", "IsZodiac", 0); - + /* ace = (AVATARCACHEENTRY *)CallService(MS_AV_GETMYAVATAR, NULL, (LPARAM)szProto); if (ace) @@ -204,7 +133,7 @@ namespace NServices if (ace) { if (// check for correct structure - ace->cbSize == sizeof(AVATARCACHEENTRY) && + ace->cbSize == sizeof(AVATARCACHEENTRY) && // set zodiac as avatar either if the desired avatar is invalid or a general protocol picture ((ace->dwFlags & AVS_PROTOPIC) || !(ace->dwFlags & AVS_BITMAP_VALID))) { @@ -232,12 +161,12 @@ namespace NServices * * **/ - VOID Enable(BOOLEAN bEnable) + VOID Enable(BOOLEAN bEnable) { HANDLE hContact; DBVARIANT dbv; - if (bEnable && !ghChangedHook) + if (bEnable && !ghChangedHook) { //walk through all the contacts stored in the DB @@ -246,18 +175,18 @@ namespace NServices hContact = DB::Contact::FindNext(hContact)) { // don't set if avatar is locked! - if (!DB::Setting::GetByte(hContact, "ContactPhoto", "Locked", 0)) + if (!DB::Setting::GetByte(hContact, "ContactPhoto", "Locked", 0)) { BOOLEAN bInvalidAvatar = TRUE; - + // the relative file is valid - if (!DB::Setting::GetAString(hContact, "ContactPhoto", "RFile", &dbv)) + if (!DB::Setting::GetAString(hContact, "ContactPhoto", "RFile", &dbv)) { CHAR absolute[MAX_PATH]; absolute[0] = '\0'; // check if file exists - if (!CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)dbv.pszVal, (LPARAM)absolute)) + if (!CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)dbv.pszVal, (LPARAM)absolute)) { FILE *f = fopen(absolute, "rb"); if (f) { @@ -269,7 +198,7 @@ namespace NServices } // the absolute file is valid - if (bInvalidAvatar && !DBGetContactSetting(hContact, "ContactPhoto", "File", &dbv)) + if (bInvalidAvatar && !DBGetContactSetting(hContact, "ContactPhoto", "File", &dbv)) { FILE *f = fopen(dbv.pszVal, "rb"); if (f) { @@ -278,7 +207,7 @@ namespace NServices } DB::Variant::Free(&dbv); } - + // set the zodiac as avatar if (bInvalidAvatar) { SetZodiacAvatar(hContact); @@ -287,7 +216,7 @@ namespace NServices } ghChangedHook = HookEvent(ME_AV_AVATARCHANGED, (MIRANDAHOOK) OnAvatarChanged); } - else if (!bEnable && ghChangedHook) + else if (!bEnable && ghChangedHook) { UnhookEvent(ghChangedHook); ghChangedHook = NULL; -- cgit v1.2.3