📄 otherfunctions.cpp
字号:
if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_AUDIO)) == 0) return GetResString(IDS_SEARCH_AUDIO);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_VIDEO)) == 0) return GetResString(IDS_SEARCH_VIDEO);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_IMAGE)) == 0) return GetResString(IDS_SEARCH_PICS);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_DOCUMENT)) == 0) return GetResString(IDS_SEARCH_DOC);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_PROGRAM)) == 0) return GetResString(IDS_SEARCH_PRG);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_ARCHIVE)) == 0) return GetResString(IDS_SEARCH_ARC);
else if (_tcscmp(pszED2KFileType, _T(ED2KFTSTR_CDIMAGE)) == 0) return GetResString(IDS_SEARCH_CDIMG);
}
return _T("");
}
class CED2KFileTypes{
public:
CED2KFileTypes(){
qsort(_aED2KFileTypes, ARRSIZE(_aED2KFileTypes), sizeof _aED2KFileTypes[0], CompareE2DKFileType);
#ifdef _DEBUG
// check for duplicate entries
LPCTSTR pszLast = _aED2KFileTypes[0].pszExt;
for (int i = 1; i < ARRSIZE(_aED2KFileTypes); i++){
ASSERT( _tcscmp(pszLast, _aED2KFileTypes[i].pszExt) != 0 );
pszLast = _aED2KFileTypes[i].pszExt;
}
#endif
}
};
CED2KFileTypes theED2KFileTypes; // get the list sorted *before* any code is accessing it
TCHAR *stristr(const TCHAR *str1, const TCHAR *str2)
{
const TCHAR *cp = str1;
const TCHAR *s1;
const TCHAR *s2;
if (!*str2)
return (TCHAR *)str1;
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 && *s2 && _totlower(*s1) == _totlower(*s2))
s1++, s2++;
if (!*s2)
return (TCHAR *)cp;
cp++;
}
return NULL;
}
int GetSystemErrorString(DWORD dwError, CString &rstrError)
{
// FormatMessage language flags:
//
// - MFC uses: MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT)
// SUBLANG_SYS_DEFAULT = 0x02 (system default)
//
// - SDK uses: MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
// SUBLANG_DEFAULT = 0x01 (user default)
//
//
// Found in "winnt.h"
// ------------------
// Language IDs.
//
// The following two combinations of primary language ID and
// sublanguage ID have special semantics:
//
// Primary Language ID Sublanguage ID Result
// ------------------- --------------- ------------------------
// LANG_NEUTRAL SUBLANG_NEUTRAL Language neutral
// LANG_NEUTRAL SUBLANG_DEFAULT User default language
// LANG_NEUTRAL SUBLANG_SYS_DEFAULT System default language
//
// *** SDK notes also:
// If you pass in zero, 'FormatMessage' looks for a message for LANGIDs in
// the following order:
//
// 1) Language neutral
// 2) Thread LANGID, based on the thread's locale value
// 3) User default LANGID, based on the user's default locale value
// 4) System default LANGID, based on the system default locale value
// 5) US English
LPTSTR pszSysMsg = NULL;
DWORD dwLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
(LPTSTR)&pszSysMsg, 0, NULL);
if (pszSysMsg != NULL && dwLength != 0)
{
if (dwLength >= 2 && pszSysMsg[dwLength - 2] == _T('\r'))
pszSysMsg[dwLength - 2] = _T('\0');
rstrError = pszSysMsg;
rstrError.Replace(_T("\r\n"), _T(" ")); // some messages contain CRLF within the message!?
}
else {
rstrError.Empty();
}
if (pszSysMsg)
LocalFree(pszSysMsg);
return rstrError.GetLength();
}
int GetModuleErrorString(DWORD dwError, CString &rstrError, LPCTSTR pszModule)
{
LPTSTR pszSysMsg = NULL;
DWORD dwLength = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
GetModuleHandle(pszModule), dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
(LPTSTR)&pszSysMsg, 0, NULL);
if (pszSysMsg != NULL && dwLength != 0)
{
if (dwLength >= 2 && pszSysMsg[dwLength - 2] == _T('\r'))
pszSysMsg[dwLength - 2] = _T('\0');
rstrError = pszSysMsg;
rstrError.Replace(_T("\r\n"), _T(" ")); // some messages contain CRLF within the message!?
}
else {
rstrError.Empty();
}
if (pszSysMsg)
LocalFree(pszSysMsg);
return rstrError.GetLength();
}
int GetErrorMessage(DWORD dwError, CString &rstrErrorMsg, DWORD dwFlags)
{
int iMsgLen = GetSystemErrorString(dwError, rstrErrorMsg);
if (iMsgLen == 0)
{
if ((long)dwError >= 0)
rstrErrorMsg.Format(_T("Error %u"), dwError);
else
rstrErrorMsg.Format(_T("Error 0x%08x"), dwError);
}
else if (dwFlags & 1)
{
CString strFullErrorMsg;
if ((long)dwError >= 0)
strFullErrorMsg.Format(_T("Error %u: %s"), dwError, rstrErrorMsg);
else
strFullErrorMsg.Format(_T("Error 0x%08x: %s"), dwError, rstrErrorMsg);
rstrErrorMsg = strFullErrorMsg;
}
return rstrErrorMsg.GetLength();
}
CString GetErrorMessage(DWORD dwError, DWORD dwFlags)
{
CString strError;
GetErrorMessage(dwError, strError, dwFlags);
return strError;
}
int GetAppImageListColorFlag()
{
HDC hdcScreen = ::GetDC(NULL);
int iColorBits = GetDeviceCaps(hdcScreen, BITSPIXEL) * GetDeviceCaps(hdcScreen, PLANES);
::ReleaseDC(NULL, hdcScreen);
int iIlcFlag;
if (iColorBits >= 32)
iIlcFlag = ILC_COLOR32;
else if (iColorBits >= 24)
iIlcFlag = ILC_COLOR24;
else if (iColorBits >= 16)
iIlcFlag = ILC_COLOR16;
else if (iColorBits >= 8)
iIlcFlag = ILC_COLOR8;
else if (iColorBits >= 4)
iIlcFlag = ILC_COLOR4;
else
iIlcFlag = ILC_COLOR;
return iIlcFlag;
}
CString DbgGetHexDump(const uint8* data, UINT size)
{
CString buffer;
buffer.Format(_T("Size=%u"), size);
buffer += _T(", Data=[");
UINT i = 0;
for(; i < size && i < 50; i++){
if (i > 0)
buffer += _T(" ");
TCHAR temp[3];
_stprintf(temp, _T("%02x"), data[i]);
buffer += temp;
}
buffer += (i == size) ? _T("]") : _T("..]");
return buffer;
}
void DbgSetThreadName(LPCSTR szThreadName, ...)
{
#ifdef DEBUG
#ifndef MS_VC_EXCEPTION
#define MS_VC_EXCEPTION 0x406d1388
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, must be zero
} THREADNAME_INFO;
#endif
__try
{
va_list args;
va_start(args, szThreadName);
int lenBuf = 0;
char *buffer = NULL;
int lenResult;
do // the VS debugger truncates the string to 31 characters anyway!
{
lenBuf += 128;
if (buffer != NULL)
delete [] buffer;
buffer = new char[lenBuf];
lenResult = _vsnprintf(buffer, lenBuf, szThreadName, args);
} while (lenResult == -1);
va_end(args);
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = buffer;
info.dwThreadID = (DWORD)-1;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
} __except (EXCEPTION_CONTINUE_EXECUTION) { }
delete [] buffer;
}
__except (EXCEPTION_CONTINUE_EXECUTION) {}
#endif
}
CString RemoveFileExtension(const CString& rstrFilePath)
{
int iDot = rstrFilePath.ReverseFind(_T('.'));
if (iDot == -1)
return rstrFilePath;
return rstrFilePath.Mid(0, iDot);
}
int CompareDirectories(const CString& rstrDir1, const CString& rstrDir2)
{
// use case insensitive compare as a starter
if (rstrDir1.CompareNoCase(rstrDir2)==0)
return 0;
// if one of the paths ends with a '\' the paths may still be equal from the file system's POV
CString strDir1(rstrDir1);
CString strDir2(rstrDir2);
PathRemoveBackslash(strDir1.GetBuffer()); // remove any available backslash
strDir1.ReleaseBuffer();
PathRemoveBackslash(strDir2.GetBuffer()); // remove any available backslash
strDir2.ReleaseBuffer();
return strDir1.CompareNoCase(strDir2); // compare again
}
bool IsGoodIP(uint32 nIP, bool forceCheck)
{
// always filter following IP's
// -------------------------------------------
// 0.0.0.0 invalid
// 127.0.0.0 - 127.255.255.255 Loopback
// 224.0.0.0 - 239.255.255.255 Multicast
// 240.0.0.0 - 255.255.255.255 Reserved for Future Use
// 255.255.255.255 invalid
if (nIP==0 || (uint8)nIP==127 || (uint8)nIP>=224){
#ifdef _DEBUG
if (nIP==0x0100007F && thePrefs.GetAllowLocalHostIP())
return true;
#endif
return false;
}
if (!thePrefs.FilterLANIPs() && !forceCheck/*ZZ:UploadSpeedSense*/)
return true;
// filter LAN IP's
// -------------------------------------------
// 0.* "This" Network
// 10.0.0.0 - 10.255.255.255 Class A
// 172.16.0.0 - 172.31.255.255 Class B
// 192.168.0.0 - 192.168.255.255 Class C
uint8 nFirst = (uint8)nIP;
uint8 nSecond = (uint8)(nIP >> 8);
if (nFirst==192 && nSecond==168) // check this 1st, because those LANs IPs are mostly spreaded
return false;
if (nFirst==172 && nSecond>=16 && nSecond<=31)
return false;
if (nFirst==0 || nFirst==10)
return false;
return true;
}
bool IsGoodIPPort(uint32 nIP, uint16 nPort)
{
return IsGoodIP(nIP) && nPort!=0;
}
CString GetFormatedUInt(ULONG ulVal)
{
TCHAR szVal[12];
_ultot(ulVal, szVal, 10);
static NUMBERFMT nf;
if (nf.Grouping == 0) {
nf.NumDigits = 0;
nf.LeadingZero = 0;
nf.Grouping = 3;
// we are hardcoding the following two format chars by intention because the C-RTL also has the decimal sep hardcoded to '.'
nf.lpDecimalSep = _T(".");
nf.lpThousandSep = _T(",");
nf.NegativeOrder = 0;
}
CString strVal;
const int iBuffSize = ARRSIZE(szVal)*2;
int iResult = GetNumberFormat(LOCALE_SYSTEM_DEFAULT, 0, szVal, &nf, strVal.GetBuffer(iBuffSize), iBuffSize);
strVal.ReleaseBuffer();
if (iResult == 0)
strVal = szVal;
return strVal;
}
CString GetFormatedUInt64(ULONGLONG ullVal)
{
TCHAR szVal[24];
_ui64tot(ullVal, szVal, 10);
static NUMBERFMT nf;
if (nf.Grouping == 0) {
nf.NumDigits = 0;
nf.LeadingZero = 0;
nf.Grouping = 3;
// we are hardcoding the following two format chars by intention because the C-RTL also has the decimal sep hardcoded to '.'
nf.lpDecimalSep = _T(".");
nf.lpThousandSep = _T(",");
nf.NegativeOrder = 0;
}
CString strVal;
const int iBuffSize = ARRSIZE(szVal)*2;
int iResult = GetNumberFormat(LOCALE_SYSTEM_DEFAULT, 0, szVal, &nf, strVal.GetBuffer(iBuffSize), iBuffSize);
strVal.ReleaseBuffer();
if (iResult == 0)
strVal = szVal;
return strVal;
}
void Debug(LPCTSTR pszFmtMsg, ...)
{
va_list pArgs;
va_start(pArgs, pszFmtMsg);
CString strBuff;
#ifdef _DEBUG
time_t tNow = time(NULL);
int iTimeLen = _tcsftime(strBuff.GetBuffer(40), 40, _T("%H:%M:%S "), localtime(&tNow));
strBuff.ReleaseBuffer(iTimeLen);
#endif
strBuff.AppendFormatV(pszFmtMsg, pArgs);
// get around a bug in the debug device which is not capable of dumping long strings
int i = 0;
while (i < strBuff.GetLength()){
OutputDebugString(strBuff.Mid(i, 1024));
i += 1024;
}
va_end(pArgs);
}
void DebugHexDump(const uint8* data, UINT lenData)
{
uint16 lenLine = 16;
uint32 pos = 0;
byte c = 0;
while (pos < lenData)
{
CStringA line;
CStringA single;
line.Format("%08X ", pos);
lenLine = min((lenData - pos), 16);
for (int i=0; i<lenLine; i++)
{
single.Format(" %02X", data[pos+i]);
line += single;
if (i == 7)
line += ' ';
}
line += CString(' ', 60 - line.GetLength());
for (int i=0; i<lenLine; i++)
{
c = data[pos + i];
single.Format("%c", (((c > 31) && (c < 127)) ? (_TUCHAR)c : '.'));
line += single;
}
Debug(_T("%hs\n"), line);
pos += lenLine;
}
}
void DebugHexDump(CFile& file)
{
int iSize = (int)(file.GetLength() - file.GetPosition());
if (iSize > 0)
{
uint8* data = NULL;
try{
data = new uint8[iSize];
file.Read(data, iSize);
DebugHexDump(data, iSize);
}
catch(CFileException* e){
TRACE("*** DebugHexDump(CFile&); CFileException\n");
e->Delete();
}
catch(CMemoryException* e){
TRACE("*** DebugHexDump(CFile&); CMemoryException\n");
e->Delete();
}
delete[] data;
}
}
LPCTSTR DbgGetFileNameFromID(const uchar* hash)
{
CKnownFile* reqfile = theApp.sharedfiles->GetFileByID((uchar*)hash);
if (reqfile != NULL)
return reqfile->GetFileName();
CPartFile* partfile = theApp.downloadqueue->GetFileByID((uchar*)hash);
if (partfile != NULL)
return partfile->GetFileName();
return NULL;
}
CString DbgGetFileInfo(const uchar* hash)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -