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/Db3x_mmap/database.cpp | 132 ++++++++++------------------------------- 1 file changed, 31 insertions(+), 101 deletions(-) (limited to 'plugins/Db3x_mmap/database.cpp') 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 -- cgit v1.2.3