123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- // Util.cpp : 实现文件
- //
-
- #include "stdafx.h"
- #include "Util.h"
- #include <odbcinst.h>
- #include <comdef.h>
- #include <afxdb.h>
- #include <regex>
- #include <tlhelp32.h>
- #include <psapi.h>
- #pragma comment(lib,"Psapi.lib")
- string UnicodeToGB2312(const wchar_t* pData)
- {
- string str;
- int len = WideCharToMultiByte(936, 0, pData, -1, NULL, 0, NULL, NULL); // 先取得转换后的ANSI字符串所需的长度
- if (len <= 0) return str;
- char* pStr = (char*)calloc(len + 1, sizeof(char)); // 分配缓冲区
- WideCharToMultiByte(936, 0, pData, -1, pStr, len, NULL, NULL); // 开始转换
- str = pStr;
- if (pStr) free(pStr);
- return str;
- }
- wstring GB2312ToUnicode(const char *pData)
- {
- wstring str;
- int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); // 先取得转换后的UNICODE字符串所需的长度
- if (len <= 0) return str;
- wchar_t* pWstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t));// 分配缓冲区
- MultiByteToWideChar(936, 0, pData, -1, pWstr, len); // 开始转换
- str = pWstr;
- if (pWstr) free(pWstr);
- return str;
- }
- string UnicodeToUtf8(const wchar_t* unicode)
- {
- string str;
- int len;
- len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
- if (len <= 0) return str;
- char *szUtf8 = (char*)calloc(len + 1, sizeof(char));
- WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
- szUtf8[len] = '\0';
- str = szUtf8;
- if (szUtf8) free(szUtf8);
- return str;
- }
- wstring Utf8ToUnicode(const char* szU8)
- {
- wstring str;
- if (szU8 == NULL)
- {
- return str;
- }
- //预转换,得到所需空间的大小;
- int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
- if (wcsLen <= 0) return str;
- //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
- wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t));
- //转换
- ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen);
- pWStr[wcsLen] = L'\0';
- str = pWStr;
- if (pWStr) free(pWStr);
- return str;
- }
- string ConvertUTF8toGB2312(const char* pData)
- {
- size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, strlen(pData), NULL, 0);
- WCHAR* pChar = new WCHAR[n + 1];
- n = MultiByteToWideChar(CP_UTF8, 0, pData, strlen(pData), pChar, n);
- pChar[n] = 0;
- n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0);
- char* p = new char[n + 1];
- n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0);
- string result(p);
- delete[]pChar;
- delete[]p;
- return result;
- }
- void GetModuleDir(wstring& dir)
- {
- TCHAR Path[MAX_PATH];
- TCHAR Driver[_MAX_DRIVE];
- TCHAR Dir[_MAX_DIR];
- TCHAR FileName[_MAX_FNAME];
- TCHAR Ext[_MAX_EXT];
- ::GetModuleFileName(NULL, Path, MAX_PATH - 1);
- _wsplitpath(Path, Driver, Dir, FileName, Ext);
- dir.append(Driver);
- dir.append(Dir);
- }
- BOOL ProgramExist(const TCHAR* szKey)
- {
- HANDLE hSysSnapshot = NULL;
- PROCESSENTRY32 proc;
- hSysSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSysSnapshot == (HANDLE)-1)
- return FALSE;
- proc.dwSize = sizeof(proc);
- if (Process32First(hSysSnapshot, &proc))
- {
- do
- {
- if (_tcsicmp(proc.szExeFile, szKey) == 0)
- {
- CloseHandle(hSysSnapshot);
- return TRUE;
- }
- } while (Process32Next(hSysSnapshot, &proc));
- }
- CloseHandle(hSysSnapshot);
- return FALSE;
- }
- void KillProgress(const TCHAR* szKey)
- {
- HANDLE hSysSnapshot = NULL;
- PROCESSENTRY32 proc;
- hSysSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSysSnapshot == (HANDLE)-1)
- return;
- proc.dwSize = sizeof(proc);
- if (Process32First(hSysSnapshot, &proc))
- {
- do
- {
- if (_tcsicmp(proc.szExeFile, szKey) == 0)
- {
- HANDLE Proc_handle = OpenProcess(PROCESS_TERMINATE, FALSE, proc.th32ProcessID);
- if (Proc_handle != NULL)
- {
- TerminateProcess(Proc_handle, 0);
- ::WaitForSingleObject(Proc_handle, INFINITE);
- CloseHandle(Proc_handle);
- Proc_handle = NULL;
- }
- }
- } while (Process32Next(hSysSnapshot, &proc));
- }
- CloseHandle(hSysSnapshot);
- }
- void DetectDirectory(const wstring& strIamgePath, vector<string>& listImage)
- {
- CFileFind finder;
- CString path;
- if (PathFileExists(strIamgePath.c_str()))
- {
- vector<string> _list_source;
- path.Format(_T("%s/*.*"), strIamgePath.c_str());
- bool bWorking = finder.FindFile(path);
- while (bWorking)
- {
- bWorking = finder.FindNextFile();
- if ((finder.GetFileName() == _T(".")) || (finder.GetFileName() == _T("..")))
- continue;
- CString strFilePath = finder.GetFilePath();
- strFilePath.Replace(L"\\", L"/");
- _list_source.push_back(UnicodeToUtf8(strFilePath));
- }
- for (auto iter : _list_source)
- {
- int index = iter.rfind(".jpg");
- if (index > 0 && index == iter.length() - 4)
- {
- listImage.push_back(iter);
- }
- }
- if (listImage.size() != _list_source.size())
- {
- listImage.clear();
- }
- }
- }
- void DeleteDirectory(const wstring &strPath)
- {
- if (strPath.length() == 0)
- return;
- CFileFind ff;
- CString _path(strPath.c_str());
- _path.Replace('/', '\\');
- _path.Append(_T("\\*"));
- BOOL bFound = ff.FindFile(_path, 0);
- while (bFound)
- {
- bFound = ff.FindNextFile();
- if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T("..")))
- continue;
- SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
- if (ff.IsDirectory())
- {
- DeleteDirectory((wstring)ff.GetFilePath());
- RemoveDirectory(ff.GetFilePath());
- }
- else
- {
- DeleteFile(ff.GetFilePath());
- }
- }
- ff.Close();
- RemoveDirectory(strPath.c_str());
- }
- bool split(const string& src, string delimit, vector<string> &v, string null_subst)
- {
- if (src.empty() || delimit.empty())
- return false;
- int deli_len = delimit.size();
- long index = -1, last_search_position = 0;
- while ((index = src.find(delimit, last_search_position)) != -1)
- {
- if (index == last_search_position)
- v.push_back(null_subst);
- else
- v.push_back(src.substr(last_search_position, index - last_search_position));
- last_search_position = index + deli_len;
- }
- string last_one = src.substr(last_search_position);
- v.push_back(last_one.empty() ? null_subst : last_one);
- return true;
- }
|