From 9d777637239fed893c26c7d8aee3f929937311ba Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@4f64403b-2f21-0410-a795-97e2b3489a10> Date: Tue, 6 Apr 2010 10:42:54 +0000 Subject: More optimizations git-svn-id: https://server.scottellis.com.au/svn/mim_plugs@501 4f64403b-2f21-0410-a795-97e2b3489a10 --- updater/extern.cpp | 125 +++++++++++++++++++++++------------------------------ 1 file changed, 53 insertions(+), 72 deletions(-) (limited to 'updater/extern.cpp') diff --git a/updater/extern.cpp b/updater/extern.cpp index ccd7fcf..f34f24b 100644 --- a/updater/extern.cpp +++ b/updater/extern.cpp @@ -28,38 +28,33 @@ int ExternProcess(bool restart) { return 1; } - TCHAR *mir_exe = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)), - *plugins_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)); - char *db_name = (char *)malloc(MAX_PATH), - *db_path = (char *)malloc(MAX_PATH); + char db_name[100], db_path[MAX_PATH]; // Get Miranda exe path and profile CallService(MS_DB_GETPROFILENAME, MAX_PATH, (WPARAM)(char*) db_name); CallService(MS_DB_GETPROFILEPATH, MAX_PATH, (WPARAM)(char*) db_path); - strcat(db_path, "\\"); - strcat(db_path, db_name); - //lstrcpyn(db_path+lstrlen(db_path), db_name, lstrlen(db_name)-3); // Pointer arithmetic - //*(db_path + lstrlen(db_name) - 3) = 0; + + TCHAR db_pathT[MAX_PATH], *t1, *t2; + mir_sntprintf(db_pathT, SIZEOF(db_pathT), _T("%s\\%s"), (t1=mir_a2t(db_path)), (t2=mir_a2t(db_name))); + mir_free(t1); mir_free(t2); // get plugin folder - GetModuleFileName(0, mir_exe, MAX_PATH); - _tcscpy(plugins_folder, mir_exe); - TCHAR *p = _tcsrchr(plugins_folder, _T('\\')); - if(p) *p = 0; - _tcscat(plugins_folder, _T("\\Plugins")); + REPLACEVARSDATA dat = {0}; + dat.cbSize = sizeof(dat); + dat.dwFlags = RVF_TCHAR; + + TCHAR* mir_exe = (TCHAR*)CallService(MS_UTILS_REPLACEVARS, (WPARAM)_T("%miranda_path%"), (LPARAM)&dat); + TCHAR* plugins_folder = (TCHAR*)CallService(MS_UTILS_REPLACEVARS, (WPARAM)_T("%miranda_path%\\Plugins"), (LPARAM)&dat); unsigned long bytes_written; - char *nl = "\r\n"; - TCHAR *tnl = _T("\r\n"); - char buf[64]; + const TCHAR *tnl = _T("\r\n"); + WriteFile(hDatFile, mir_exe, (unsigned)_tcslen(mir_exe) * sizeof(TCHAR), &bytes_written, FALSE); WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE); WriteFile(hDatFile, plugins_folder, (unsigned)_tcslen(plugins_folder) * sizeof(TCHAR), &bytes_written, FALSE); WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE); - WriteFile(hDatFile, db_name, (unsigned)strlen(db_name), &bytes_written, FALSE); - WriteFile(hDatFile, nl, 2, &bytes_written, FALSE); - WriteFile(hDatFile, db_path, (unsigned)strlen(db_path), &bytes_written, FALSE); - WriteFile(hDatFile, nl, 2, &bytes_written, FALSE); + WriteFile(hDatFile, db_pathT, (unsigned)_tcslen(db_pathT), &bytes_written, FALSE); + WriteFile(hDatFile, tnl, 2, &bytes_written, FALSE); WriteFile(hDatFile, options.temp_folder, (unsigned)_tcslen(options.temp_folder) * sizeof(TCHAR), &bytes_written, FALSE); WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE); if(options.backup) { @@ -69,21 +64,21 @@ int ExternProcess(bool restart) { WriteFile(hDatFile, _T("no backups"),(unsigned) _tcslen(_T("no backups")) * sizeof(TCHAR), &bytes_written, FALSE); WriteFile(hDatFile, tnl, 2 * sizeof(TCHAR), &bytes_written, FALSE); } - sprintf(buf, "%d", (unsigned int)GetCurrentProcessId()); + + TCHAR buf[64]; + mir_sntprintf(buf, SIZEOF(buf), _T("%d"), (unsigned int)GetCurrentProcessId()); //MessageBox(0, buf, "Writing process id", MB_OK); - WriteFile(hDatFile, buf, (unsigned)strlen(buf), &bytes_written, FALSE); - WriteFile(hDatFile, nl, 2, &bytes_written, FALSE); + WriteFile(hDatFile, buf, (unsigned)_tcslen(buf), &bytes_written, FALSE); + WriteFile(hDatFile, tnl, 2, &bytes_written, FALSE); - sprintf(buf, (restart ? "restart" : "no_restart")); - WriteFile(hDatFile, buf, (unsigned)strlen(buf), &bytes_written, FALSE); - WriteFile(hDatFile, nl, 2, &bytes_written, FALSE); + mir_sntprintf(buf, SIZEOF(buf), restart ? _T("restart") : _T("no_restart")); + WriteFile(hDatFile, buf, (unsigned)_tcslen(buf), &bytes_written, FALSE); + WriteFile(hDatFile, tnl, 2, &bytes_written, FALSE); CloseHandle(hDatFile); - free(mir_exe); - free(plugins_folder); - free(db_name); - free(db_path); + mir_free(mir_exe); + mir_free(plugins_folder); /* if(!CallService(MS_SYSTEM_OKTOEXIT,0,0)) { @@ -96,11 +91,10 @@ int ExternProcess(bool restart) { TCHAR szParams[MAX_PATH], szBuf[MAX_PATH], *szProcDir; // try to fire up external process from new dll (if present), so we can overwrite the old one - _tcscpy(szBuf, options.temp_folder); #ifdef PLUGDIR - _tcscat(szBuf, _T("\\plugins\\updater.dll")); + mir_sntprintf(szBuf, SIZEOF(szBuf), _T("%s\\plugins\\updater.dll"), options.temp_folder); #else - _tcscat(szBuf, _T("\\updater.dll")); + mir_sntprintf(szBuf, SIZEOF(szBuf), _T("%s\\updater.dll"), options.temp_folder); #endif HANDLE hFile = CreateFile(szBuf, 0, 0, 0, OPEN_EXISTING, 0, 0); @@ -126,8 +120,7 @@ int ExternProcess(bool restart) { } else { - p = _tcsrchr(szBuf, _T('\\')); - if (p) *p = 0; + TCHAR* p = _tcsrchr(szBuf, _T('\\')); if (p) *p = 0; szProcDir = szBuf; // rundll32 hates spaces in the arg, but quotes aren't allowed in earlier versions... @@ -174,7 +167,8 @@ void mWriteFile(HANDLE hFile, char *line) { // move all files in src_folder to dst_folder - put replaced files in backup folder // if a file in src_folder is a directory, copy it's contents to the same dir in the root folder and // set that dir as the new root (so that dirs in dirs go in the right place) -void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *backup_folder, TCHAR *root_folder) { +void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *backup_folder, TCHAR *root_folder)\ +{ // move files from src_folder to dst_folder if(!src_folder || _tcslen(src_folder) == 0) { @@ -191,8 +185,8 @@ void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *bac return; } - _tcscpy(szFilesPath, src_folder); - _tcscat(szFilesPath, _T("\\*.*")); + _sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), src_folder); + szFilesPath[SIZEOF(szFilesPath) - 1] = 0; bool move_file; @@ -201,16 +195,14 @@ void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *bac if(hFileSearch != INVALID_HANDLE_VALUE) { do { if(findData.cFileName[0] != _T('.')) { - //_tcslwr(findData.cFileName); - _tcscpy(szOldFileName, src_folder); - _tcscat(szOldFileName, _T("\\")); - _tcscat(szOldFileName, findData.cFileName); + _sntprintf(szOldFileName, SIZEOF(szOldFileName), _T("%s\\%s"), src_folder, findData.cFileName); + szOldFileName[SIZEOF(szOldFileName) - 1] = 0; if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // use szNewFileName as destination and new root folder _tcscpy(szNewFileName, root_folder); - if(_tcslen(findData.cFileName) < _tcslen(ROOT_FILES_FOLDER) || _tcsicmp(findData.cFileName, ROOT_FILES_FOLDER)) { + if (_tcslen(findData.cFileName) < _tcslen(ROOT_FILES_FOLDER) || _tcsicmp(findData.cFileName, ROOT_FILES_FOLDER)) { _tcscat(szNewFileName, _T("\\")); _tcscat(szNewFileName, findData.cFileName); } @@ -234,9 +226,9 @@ void MoveFiles(HANDLE hLogFile, TCHAR *src_folder, TCHAR *dst_folder, TCHAR *bac move_file = false; if(do_backups) { - _tcscpy(szBackupFileName, backup_folder); - _tcscat(szBackupFileName, _T("\\")); - _tcscat(szBackupFileName, findData.cFileName); + _sntprintf(szBackupFileName, SIZEOF(szBackupFileName), _T("%s\\%s"), + backup_folder, findData.cFileName); + szBackupFileName[SIZEOF(szBackupFileName) - 1] = 0; move_file = true; DeleteFile(szBackupFileName); @@ -296,7 +288,7 @@ void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, HANDLE hDatFile = CreateFileA(lpszCmdLine, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if(hDatFile == INVALID_HANDLE_VALUE) { char msg[1024]; - sprintf(msg, "Could not read data file:\n%s", lpszCmdLine); + _snprintf(msg, SIZEOF(msg), "Could not read data file:\n%s", lpszCmdLine); MessageBoxA(0, msg, "Updater Error", MB_OK | MB_ICONERROR); } else { @@ -304,38 +296,29 @@ void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, *plugins_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)), *temp_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)), *backup_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)), - *root_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)); - char *db_name = (char *)malloc(MAX_PATH), - *db_path = (char *)malloc(MAX_PATH), - *pid = (char *)malloc(64); + *root_folder = (TCHAR *)malloc(MAX_PATH * sizeof(TCHAR)), + *db_path = (TCHAR *)malloc(MAX_PATH), + *pid = (TCHAR *)malloc(64); bool restart = true; { int i = 0, offset; - unsigned long bytes_read; - char ans_line[MAX_PATH]; TCHAR line[MAX_PATH]; BOOL bResult = TRUE; do { offset = 0; - if(i == 2 || i == 3 || i == 6 || i == 7) { - while((bResult = ReadFile(hDatFile, ans_line + offset, 1, &bytes_read, 0)) && offset < MAX_PATH && bytes_read == 1 && ans_line[offset] && (ans_line[offset] != '\n' || (offset > 0 && ans_line[offset - 1] != '\r'))) offset++; - if(offset > 0) ans_line[offset - 1] = 0; // cut off /r/n - //MessageBoxA(0, ans_line, "Read ANSI text line", MB_OK); - } else { - ReadTLine(hDatFile, line, MAX_PATH, offset); - } + ReadTLine(hDatFile, line, MAX_PATH, offset); + switch(i) { case 0: _tcsncpy(mir_exe, line, MAX_PATH); break; case 1: _tcsncpy(plugins_folder, line, MAX_PATH); break; - case 2: strncpy(db_name, ans_line, MAX_PATH); break; - case 3: strncpy(db_path, ans_line, MAX_PATH); break; - case 4: _tcsncpy(temp_folder, line, MAX_PATH); break; - case 5: _tcsncpy(backup_folder, line, MAX_PATH); break; - case 6: strncpy(pid, ans_line, 64); break; - case 7: - restart = (strncmp(ans_line, "restart", strlen("restart")) == 0); + case 2: _tcsncpy(db_path, line, MAX_PATH); break; + case 3: _tcsncpy(temp_folder, line, MAX_PATH); break; + case 4: _tcsncpy(backup_folder, line, MAX_PATH); break; + case 5: _tcsncpy(pid, line, 64); break; + case 6: + restart = (_tcsncmp(line, _T("restart"), 7) == 0); offset = 0; // end loop break; default: @@ -361,7 +344,7 @@ void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, if(p) *p = 0; // ensure miranda has exited - DWORD mpi = (DWORD)atol(pid); + DWORD mpi = (DWORD)_ttol(pid); bool exited = false; @@ -452,10 +435,9 @@ void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, #ifdef _UD_LOGGING mWriteFile(hDatFile, "Restarting"); #endif - TCHAR szArgs[MAX_PATH], *temp_str; + TCHAR szArgs[MAX_PATH]; //wsprintf(szArgs, "\"%s\" \"%s\"", db_path, db_name); - mir_sntprintf(szArgs, SIZEOF(szArgs), _T("\"%s\" \"%s\""), mir_exe, temp_str = mir_a2t(db_path)); // includes name, dummy instead of executable? - mir_free(temp_str); + _sntprintf(szArgs, SIZEOF(szArgs), _T("\"%s\" \"%s\""), mir_exe, db_path); // includes name, dummy instead of executable? //wsprintf(szArgs, "\"%s\"", db_name); @@ -475,7 +457,6 @@ void CALLBACK ExternalUpdate(HWND hwnd, HINSTANCE hInstance, LPSTR lpszCmdLine, #endif free(plugins_folder); - free(db_name); free(db_path); free(temp_folder); free(backup_folder); -- cgit v1.2.3