From d123e0ce94bf90b2adb0a4000930eb467e293226 Mon Sep 17 00:00:00 2001 From: sje Date: Wed, 1 Nov 2006 14:48:34 +0000 Subject: git-svn-id: https://server.scottellis.com.au/svn/mim_plugs@16 4f64403b-2f21-0410-a795-97e2b3489a10 --- updater/utils.cpp | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 updater/utils.cpp (limited to 'updater/utils.cpp') diff --git a/updater/utils.cpp b/updater/utils.cpp new file mode 100644 index 0000000..f370e68 --- /dev/null +++ b/updater/utils.cpp @@ -0,0 +1,247 @@ +#include "common.h" +#include "utils.h" + +bool VersionFromString(const char *szVer, DWORD *pdwVer) { + char *p = (char *)szVer; + *pdwVer = 0; + + int bytes = 1; // we start in the first 'byte' + int digit_count = 0; + while(*p && bytes <= 4 && digit_count <= 3) { + if(*p >= '0' && *p <= '9') { + *pdwVer = (*pdwVer & 0xFFFFFF00) + (*pdwVer & 0xFF) * 10 + (*p - '0'); + digit_count++; + } else if(*p == '.') { + *pdwVer = *pdwVer << 8; + bytes++; + digit_count = 0; + } else { + if(bytes < 3) // allow other chars on the end (e.g. space) + return false; // incompatible version string + else + return true; + } + p++; + } + + // version must be x.x.x.x format (for now - until a convention is established + // whereby we assume '0' bytes as either prefix or suffix) + // 15/3/06 - allowing 3 digit version numbers (ostensibly for spamfilter definition files which use date for version) + return (bytes >= 3); + +} + +int CheckForFileID(char *update_url, char *version_url, char *name) { + if(strlen(update_url) > 45 && strncmp(update_url, MIM_DOWNLOAD_URL_PREFIX, 45) == 0) { + char *p = update_url + 45; + return atoi(p); + } + if(strlen(update_url) > 51 && strncmp(update_url, "http://www.miranda-im.org/download/feed.php?dlfile=", 51) == 0) { + char *p = update_url + 51; + return atoi(p); + } + if(strlen(update_url) > 47 && strncmp(update_url, "http://miranda-im.org/download/feed.php?dlfile=", 47) == 0) { + char *p = update_url + 47; + return atoi(p); + } + return -1; +} + +bool CreatePath(const TCHAR *path) { + if(!path) return false; + + TCHAR folder[MAX_PATH]; + _tcscpy(folder, path); + int i = 0; + + if(folder[0] && folder[1] && folder[1] == ':') i += 2; // skip drive letter + + SetLastError(ERROR_SUCCESS); + TCHAR *p = &folder[i]; + while(folder[i]) { + p = _tcschr(p, _T('\\')); + if(p) { + i = p - folder; + p++; + + if(folder[i]) folder[i] = 0; + } else + i = _tcslen(folder); + + /* + // this fails when the user does not have permission to create intermediate folders + if(!CreateDirectory(folder, 0)) { + DWORD err = GetLastError(); + if(err != ERROR_ALREADY_EXISTS) { + //MessageBox(0, Translate("Could not create temporary folder."), Translate("Error"), MB_OK | MB_ICONERROR); + //return 1; + return false; + } + } + */ + CreateDirectory(folder, 0); + + folder[i] = path[i]; + } + + DWORD lerr = GetLastError(); + return (lerr == ERROR_SUCCESS || lerr == ERROR_ALREADY_EXISTS); + + /* + // this function seems to be unavailable in shell32.dll under NT4 + int ret = SHCreateDirectoryEx(0, path, 0); + return (ret == ERROR_SUCCESS) || (ret == ERROR_FILE_EXISTS) || (ret == ERROR_ALREADY_EXISTS); + */ +} + +// must 'free' return val +TCHAR *GetTString(const char *asc) { + if(!asc) return 0; + +#ifdef _UNICODE + wchar_t *ret; + int code_page = CP_ACP; + if(ServiceExists(MS_LANGPACK_GETCODEPAGE)) code_page = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0); + int size = MultiByteToWideChar(code_page, MB_PRECOMPOSED, asc, -1, 0, 0); + ret = (wchar_t *)malloc(sizeof(wchar_t) * size); + MultiByteToWideChar(code_page, MB_PRECOMPOSED, asc, -1, ret, size); + return ret; +#else + return _strdup(asc); +#endif +} + +TCHAR *GetTStringACP(const char *asc) { + if(!asc) return 0; + +#ifdef _UNICODE + wchar_t *ret; + + int size = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, asc, -1, 0, 0); + ret = (wchar_t *)malloc(sizeof(wchar_t) * size); + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, asc, -1, ret, size); + return ret; +#else + return _strdup(asc); +#endif +} + +char *GetAString(const TCHAR *t) { + if(!t) return 0; + +#ifdef _UNICODE + char *ret; + + int code_page = CP_ACP; + if(ServiceExists(MS_LANGPACK_GETCODEPAGE)) code_page = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0); + int size = WideCharToMultiByte(code_page, 0, t, -1, 0, 0, 0, 0); + ret = (char *)malloc(size); + WideCharToMultiByte(code_page, 0, t, -1, ret, size, 0, 0); + return ret; +#else + return _strdup(t); +#endif +} + +void RemoveFolder(const TCHAR *src_folder) { + TCHAR szFilesPath[MAX_PATH], szNewFileName[MAX_PATH]; + + _tcscpy(szFilesPath, src_folder); + _tcscat(szFilesPath, _T("\\*.*")); + + WIN32_FIND_DATA findData; + HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData); + if(hFileSearch != INVALID_HANDLE_VALUE) { + do { + if(findData.cFileName[0] != _T('.')) { + _tcslwr(findData.cFileName); + _tcscpy(szNewFileName, src_folder); + _tcscat(szNewFileName, _T("\\")); + _tcscat(szNewFileName, findData.cFileName); + + if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + // recurse + RemoveFolder(szNewFileName); + else + DeleteFile(szNewFileName); + } + } while(FindNextFile(hFileSearch, &findData)); + FindClose(hFileSearch); + } + + RemoveDirectory(src_folder); +} + +bool FolderIsEmpty(const TCHAR *folder) { + TCHAR szFilesPath[MAX_PATH]; + + _tcscpy(szFilesPath, folder); + _tcscat(szFilesPath, _T("\\*.*")); + + WIN32_FIND_DATA findData; + HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData); + if(hFileSearch != INVALID_HANDLE_VALUE) { + do { + if(_tcscmp(findData.cFileName, _T(".")) != 0 && _tcscmp(findData.cFileName, _T("..")) != 0) { + FindClose(hFileSearch); + return false; + } + } while(FindNextFile(hFileSearch, &findData)); + FindClose(hFileSearch); + } + + return true; +} + +bool DeleteNonDlls(const TCHAR *folder) { + TCHAR szFilesPath[MAX_PATH], szFilename[MAX_PATH]; + + { + char buff[200]; +#ifdef _UNICODE + char mbFname[MAX_PATH]; + WideCharToMultiByte(CP_ACP, 0, folder, -1, mbFname, MAX_PATH, 0, 0); + sprintf(buff, "Deleting non-dlls in %s", mbFname); +#else + sprintf(buff, "Deleting non-dlls in %s", folder); +#endif + NLog(buff); + } + + _tcscpy(szFilesPath, folder); + _tcscpy(szFilename, folder); + _tcscat(szFilename, _T("\\")); + TCHAR *p = szFilename + _tcslen(szFilename); + _tcscat(szFilesPath, _T("\\*.*")); + + WIN32_FIND_DATA findData; + HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData); + if(hFileSearch != INVALID_HANDLE_VALUE) { + do { + if(_tcscmp(findData.cFileName, _T(".")) != 0 && _tcscmp(findData.cFileName, _T("..")) != 0) { + _tcscpy(p, findData.cFileName); + if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + DeleteNonDlls(szFilename); + } else { + if(_tcsstr(findData.cFileName, _T(".dll")) == 0) { + { + char buff[200]; +#ifdef _UNICODE + char mbFname[MAX_PATH]; + WideCharToMultiByte(CP_ACP, 0, szFilename, -1, mbFname, MAX_PATH, 0, 0); + sprintf(buff, "Deleting %s", mbFname); +#else + sprintf(buff, "Deleting %s", szFilename); +#endif + NLog(buff); + } + DeleteFile(szFilename); + } + } + } + } while(FindNextFile(hFileSearch, &findData)); + FindClose(hFileSearch); + } + + return true; +} \ No newline at end of file -- cgit v1.2.3