You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.8 KiB
C

5 years ago
#ifndef _Z_FUNCTIONS_H
#define _Z_FUNCTIONS_H
#include "zdefines.h"
#include <Psapi.h>
#include <tchar.h>
#define _USE_QT_F_INLINE
#ifdef _USE_QT_F_INLINE
#include <QtCore\qstring.h>
#include <QtCore\qfile.h>
#include <QtCore\qdir.h>
#include <QtCore\qmap.h>
#endif
namespace ZKF
{
inline void truncatePathA(char* szExePath)
{
if( NULL == szExePath )
{
return ;
}
int i = strlen(szExePath);
while( i > 0 )
{
--i;
if( '\\' == szExePath[i] || '/' == szExePath[i] )
{
++i;
szExePath[i] = '\0';
break;
}
}
if( i == 0 )
{
szExePath[i] = '\\';
}
}
inline int processOpenedNumW(const WCHAR* processName)
{
int nTimes = 0;
DWORD pProcessIds[1024*64];
DWORD cbNeeded = 0;
if (!EnumProcesses(pProcessIds, sizeof(pProcessIds), &cbNeeded))
{
return nTimes;
}
DWORD nProcesses = cbNeeded / sizeof(DWORD);
for (DWORD i = 0; i < nProcesses; ++i)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pProcessIds[i]);
if (NULL == hProcess)
{
continue;
}
WCHAR procName[MAX_PATH];
if (0 != GetModuleFileNameExW(hProcess, NULL, procName, MAX_PATH))
{
if (0 == wcscmp(procName, processName))
{
++nTimes;
}
}
CloseHandle(hProcess);
}
return nTimes;
}
inline int splitSize(int nTotal, int nBlock, int dx, int* pVector, int nNum) {
if (NULL == pVector || 0 == nNum) {
return 0;
}
int count = nBlock;
if (dx > 0) {
count = dx;
}
else {
count += dx;
}
int i = 0;
while (count < nTotal && i < nNum - 1) {
pVector[i++] = count;
count += nBlock;
}
pVector[i++] = nTotal;
return i;
}
inline int squI(int i) {
return i*i;
}
inline bool compareBit(int a, int b, int idx)
{
if (idx >= 32 || idx < 0)
{
return false;
}
int nMast = 1 << idx;
return ((a&nMast) == (b&nMast));
}
inline bool equalBit(int v, int bit, int idx)
{
return ((v&(1 << idx)) == (bit << idx));
}
inline void setBits(int& value, int bitV, int bitIdx)
{
if (bitV)
{
value = value | (1 << bitIdx);
}
else
{
value = value&(~(1 << bitIdx));
}
}
#ifdef _USE_QT_F_INLINE
inline bool qtDllExit(const QString& szPath, const QString& szName) {
QString name(szPath + szName);
if (!name.endsWith(".dll", Qt::CaseInsensitive)) {
name.append(".dll");
}
return QFile::exists(name);
}
inline int qtAllFilesOfFolder(QDir* pDir, QMap<QString, QStringList>& mapFiles) {
if (NULL != pDir && pDir->exists()){
QString szNowPath = pDir->path() + "/";
QStringList files = pDir->entryList(QDir::Files, QDir::Name);
if (!files.isEmpty()) {
mapFiles.insert(szNowPath, files);
}
QStringList subDirs = pDir->entryList(QDir::Dirs | QDir::NoDot | QDir::NoDotDot, QDir::Name);
for (QStringList::Iterator it = subDirs.begin(); it != subDirs.end(); ++it){
QString szDir = pDir->path() + "/" + *it + "/";
QDir* pSubDir = new QDir(szDir);
if (NULL == pSubDir){
continue;
}
qtAllFilesOfFolder(pSubDir, mapFiles);
delete pSubDir;
}
}
return mapFiles.size();
}
inline void qtCopyFile(const char* szUtf8Name, const char* szUtf8New){
QString fileName = QString::fromUtf8(szUtf8Name);
QString newName = QString::fromUtf8(szUtf8New);
newName += fileName.right(fileName.size() - fileName.lastIndexOf('/'));
QFile::copy(fileName, newName);
}
inline bool qtByteArrayEqual(const QByteArray& b1, const QByteArray& b2){
if (b1.size() != b2.size()){
return false;
}
return (0 == memcmp(b1.constData(), b2.constData(), b1.size()));
}
inline bool qtHasParent(const QString& name, QObject* pObj){
if (NULL == pObj) {
return false;
}
pObj = pObj->parent();
while (NULL != pObj){
if (name == pObj->objectName()) {
return true;
}
pObj = pObj->parent();
}
return false;
}
#endif
}
#endif