⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.cpp

📁 Example of using HTC sensor api to get Light sensor value and set backlight level in modern HTC phon
💻 CPP
字号:
#include <windows.h>
#include <TlHelp32.h>
#include "Config.h"


#pragma comment(lib, "Toolhelp.lib")

Config config;

Config::Config()
{
	//this->Reinit();

	this->programDir = 0;
	this->exePath = 0;

	this->LogLevel = 1;
#ifdef NOLOG
	this->LogLevel = 0;
#endif
}

void Config::Init()
{
	if (this->programDir) delete[] this->programDir;
	if (this->exePath) delete[] this->exePath;

	TCHAR buf[MAX_PATH+1];
	GetModuleFileName(g_hInst, buf, MAX_PATH);

	this->exePath = new TCHAR[wcstrlen(buf, MAX_PATH)+1];
	wcstrcpy(this->exePath, buf);

	this->programDir = new TCHAR[wcstrlen(buf, MAX_PATH)+1];
	wcstrcpy(this->programDir, buf);
	wchar_t* ptr = wcsrchr(this->programDir, L'\\');
	if (!ptr) ptr = wcsrchr(this->programDir, L'/');
	if (!ptr) ptr = this->programDir;
	if (ptr) *ptr = L'\0';
}

Config::~Config()
{
	if (this->programDir) delete[] this->programDir;
	if (this->exePath) delete[] this->exePath;
}

TCHAR* Config::GetFilePath(const TCHAR* relFileName)
{
	if (!relFileName) throwError(L"Config::GetFilePath with null relFileName");
	if (!*relFileName) throwError(L"Config::GetFilePath with empty relFileName");

	TCHAR* s = new TCHAR[MAX_PATH+1];
	wcstrcpy(s, relFileName);	
	if (*s != L'\\' && *s != L'/')
	{
		int len = wcstrlen(this->programDir, MAX_PATH) + wcstrlen(s, 1024) + 2;
		TCHAR* ret = new TCHAR[len];
		wcstrcpy(ret, this->programDir);
		wcstrcat(ret, L"\\");
		wcstrcat(ret, s);
		delete[] s;
		s = ret;
	}
	return s;
}

FILE* Config::OpenFile(const TCHAR* FileName, const TCHAR* mode)
{
	TCHAR* realName = this->GetFilePath(FileName);
	FILE* ret = _wfopen(realName, mode);
	if (!ret) throwError(L"Config::OpenFile('%s') failed", realName);
	delete[] realName;
	return ret;
}

bool Config::GetLine(FILE* fp, TCHAR* buf, int bufsizeInChars)
{
	if (feof(fp)) return false;
	int i = 0;
	while (i<bufsizeInChars-2)
	{
		TCHAR c;
		size_t r = fread(&c, 2, 1, fp);
		if (r < 1) break;
		buf[i++] = c;
		if (c == L'\n') break;
	}
	buf[i] = 0;
	if (i < 1) return false;
	return true;
}

bool Config::GetUtf8FileLine(FILE* fp, TCHAR* buf, int bufsizeInChars)
{
	if (feof(fp)) return false;
	char* cbuf = (char*)new char[bufsizeInChars+1];
	int i = 0;
	while (i<bufsizeInChars-2)
	{
		char c;
		size_t r = fread(&c, 1, 1, fp);
		if (r < 1) break;
		cbuf[i++] = c;
		if (c == '\n') break;
	}
	cbuf[i] = 0;
	if (i < 1) 
	{
		delete[] cbuf;
		return false;
	}
	int ret = MultiByteToWideChar(CP_UTF8, 0, cbuf, -1, buf, bufsizeInChars);
	delete[] cbuf;
	return (ret != 0);
}







void throwError(const TCHAR* format, ...)
{
	TCHAR buf[MAX_BUF_SIZE+1];
	va_list argptr;
	va_start(argptr, format);
	HRESULT ecode;
	if (FAILED(ecode=StringCchVPrintf(buf, MAX_BUF_SIZE, format, argptr))) 
		StringCchPrintf(buf, MAX_BUF_SIZE, L"StringCchVPrintf error: %d\n", ecode);
	va_end(argptr);
	Exception *e = new Exception(buf);
	throw e;
}

int wcstrlen(const TCHAR* buf, int maxlen)
{
	size_t len;
	HRESULT e;
	if (FAILED(e=StringCchLength(buf, maxlen, &len)))
		throwError(L"StringCchLengh(str,%d) failed: %d", maxlen, e);
	return len;
}

TCHAR* wcstrcpy(TCHAR* dest, const TCHAR* src)
{
	HRESULT e;
	if (FAILED(e=StringCchCopy(dest, MAX_BUF_SIZE, src)))
		throwError(L"StringCchCopy() failed: %d", e);
	return dest;
}

TCHAR* wcstrcat(TCHAR* dest, const TCHAR* src)
{
	HRESULT e;
	if (FAILED(e=StringCchCat(dest, MAX_BUF_SIZE, src)))
		throwError(L"StringCchCat() failed: %d", e);
	return dest;
}

TCHAR* wcstrdup(const TCHAR* src)
{
	TCHAR* ret = new TCHAR[wcstrlen(src)+1];
	wcstrcpy(ret, src);
	return ret;
}

TCHAR* wcsprintf(TCHAR* dest, const TCHAR* format, ...)
{
	va_list argptr;
	va_start(argptr, format);
	HRESULT ecode;
	if (FAILED(ecode=StringCchVPrintf(dest, MAX_BUF_SIZE, format, argptr))) 
	{
		va_end(argptr);
		throwError(L"StringCcgVPrintf(wcsprintf) failed: %d",  ecode);
	}
	va_end(argptr);
	return dest;
}

TCHAR* wctrim(TCHAR* str)
{
	if (!str) throwError(L"wctrim call with null pointer");

	TCHAR* ptr = str + wcstrlen(str);
	while (ptr && ptr > str && is_spacer(*ptr)) { *ptr=0; ptr--; }
	ptr = str;
	while (ptr && *ptr && is_spacer(*ptr)) ptr++;
	if (ptr != str) 
	{
		int i;
		for (i=0; ptr[i]; i++) str[i] = ptr[i];
		str[i] = 0;
	}
	return str;
}

bool wcstrbegins(const TCHAR* str, const TCHAR* begin)
{
	if (!str || !begin) throwError(L"wcstrbegins call with null pointer");

	for (int i=0; begin[i]; i++)
	{
		if (!str[i]) return false;
		if (str[i] != begin[i]) return false;
	}
	return true;
}

TCHAR* wcstrtok(TCHAR* str, const TCHAR* delimChars)
{
	if (!str || !delimChars || !*delimChars) return str;

	int i = 0;
	bool flag = false;
	while (str[i])
	{
		bool delim = (wcschr(delimChars, str[i]) != 0);
		if (delim) { str[i] = 0; flag = true; }
		if (flag && !delim) break;
		i++;
	}
	if (flag && str[i]) return str+i;
	return 0;
}

int wctoi(const TCHAR* str)
{
	if (!str || !*str) return 0;
	return _wtoi(str);
}

float wctof(const TCHAR* str)
{
	if (!str || !*str) return 0;
	float ret = 0;
	if (swscanf(str, L"%f", &ret) == 1)
		return ret;
	else
		return 0;
}

static TCHAR getProcNameBuf[MAX_PATH+1];
const TCHAR* GetProccessName(DWORD procId)
{
	BOOL lRes;
	PROCESSENTRY32 procEntry;

	getProcNameBuf[0] = 0;
	HANDLE lProcSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);
	if(lProcSnapshot!=INVALID_HANDLE_VALUE)
	{
		memset(&procEntry,0,sizeof(PROCESSENTRY32));
		procEntry.dwSize=sizeof(PROCESSENTRY32);
		lRes=Process32First(lProcSnapshot,&procEntry);
		while (lRes)
		{
			if (procEntry.th32ProcessID == procId)
			{
				wcstrcpy(getProcNameBuf, procEntry.szExeFile);
				break;
			}
			lRes = Process32Next(lProcSnapshot, &procEntry);
		}
		CloseToolhelp32Snapshot(lProcSnapshot);
	}
	else
	{
		_LOG(L"CreateToolhelp32Snapshot error: %d\n", GetLastError());
	}  
	if (getProcNameBuf[0]) return getProcNameBuf;
	return 0;
}

const TCHAR* GetForegroundWindowPath()
{
	HWND hWnd = GetForegroundWindow();
	DWORD procId = 0;
	GetWindowThreadProcessId(hWnd, &procId);
	if (procId) return GetProccessName(procId);
	return 0;
}

bool RegReadString(HKEY rootKey, LPCTSTR subKey, LPCTSTR valueName, TCHAR** value, const TCHAR* defValue)
{
	HKEY hKey = 0;
	if (!subKey) hKey = rootKey; 
	else if (S_OK != RegOpenKeyEx(rootKey, subKey, 0, KEY_QUERY_VALUE, &hKey)) return false;

	*value = NULL;
	DWORD valueType = REG_SZ;
	DWORD valueSize = MAX_BUF_SIZE;
	TCHAR* buf = new TCHAR[MAX_BUF_SIZE];
	bool ret = true;
	if (ERROR_SUCCESS != RegQueryValueEx(hKey, valueName, 0, &valueType, (LPBYTE)buf, &valueSize)) ret = false;
	if (valueType != REG_SZ) ret = false;
	if (ret) *value = buf; else delete[] buf;
	if (!ret && defValue) RegSetValueEx(hKey, valueName, 0, REG_SZ, (BYTE*)(defValue), wcstrlen(defValue, MAX_BUF_SIZE));
	if (hKey != rootKey) RegCloseKey(hKey);
	if (!ret) *value = (TCHAR*)defValue;
	return ret;
}

bool RegWriteString(HKEY rootKey, LPCTSTR subKey, LPCTSTR valueName, const TCHAR* value)
{
	HKEY hKey = 0;
	if (!subKey) hKey = rootKey; 
	else if (S_OK != RegOpenKeyEx(rootKey, subKey, 0, KEY_QUERY_VALUE, &hKey)) return false;

	DWORD valueType = REG_SZ;
	DWORD valueSize = MAX_BUF_SIZE;
	bool ret = true;
	ret = (ERROR_SUCCESS == RegSetValueEx(hKey, valueName, 0, REG_SZ, (BYTE*)(value), wcstrlen(value, MAX_BUF_SIZE)*sizeof(TCHAR)));
	if (hKey != rootKey) RegCloseKey(hKey);
	return ret;
}


void WriteFileWChar(HANDLE hFile, TCHAR* buf)
{
	char cbuf[1025];
	size_t w = 0;
	StringCchLength(buf, 1025, &w);
	w = WideCharToMultiByte(CP_ACP, 0, buf, w, cbuf, sizeof(cbuf), NULL, NULL);
	WriteFile(hFile, cbuf, w, (LPDWORD)&w, NULL);
}

TCHAR* hLogFileName = 0;
static HANDLE hLogFile = 0;
void LogEventFile(LPCTSTR str, ...)
{
	if (str == NULL) return;
	if (config.LogLevel < 1) return;

	//if (csLog) EnterCriticalSection(csLog);
	if (!hLogFileName)
	{
		TCHAR buf[MAX_PATH+1];
		TCHAR* env_ProgramDir = (TCHAR*)config.GetProgramDir();
		if (env_ProgramDir && *env_ProgramDir)
			wcscpy(buf, env_ProgramDir);
		else
			wcscpy(buf, L"");
		wcscat(buf, L"\\");
		wcscat(buf, LOGNAME);
		wcscat(buf, L".log");
		size_t len = 0;
		StringCchLength(buf, MAX_PATH, &len);
		hLogFileName = new TCHAR[len+1];
		wcscpy(hLogFileName, buf);
		hLogFile = CreateFile(hLogFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
	}
	if (!hLogFile && hLogFileName)
	{
		hLogFile = CreateFile(hLogFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
		SetFilePointer(hLogFile, 0, NULL, FILE_END);
	}

	if (hLogFile)
	{
		TCHAR buf[1025];
		SYSTEMTIME stime;

		GetLocalTime(&stime);
		wsprintf(buf, L"%02d-%02d-%02d %02d:%02d:%02d ",
			stime.wYear, stime.wMonth, stime.wDay, stime.wHour, stime.wMinute, stime.wSecond);
		WriteFileWChar(hLogFile, buf);

		va_list argptr;
		va_start(argptr, str);
		HRESULT ecode;
		if (FAILED(ecode=StringCchVPrintf(buf, 1100, str, argptr))) 
			StringCchPrintf(buf, 1100, L"StringCchVPrintf error: %d\n", ecode);
		va_end(argptr);
		WriteFileWChar(hLogFile, buf);

#ifdef LOG_EXCLUSIVE
		FlushFileBuffers(hLogFile);
#else
		CloseHandle(hLogFile);
		hLogFile = 0;
#endif
	}
	//if (csLog) LeaveCriticalSection(csLog);

}

void LogEventDebug(LPCTSTR str, ...)
{
	if (str == NULL) return;
	TCHAR buf[1100];
	va_list argptr;
	va_start(argptr, str);
	HRESULT ecode;
	if (FAILED(ecode=StringCchVPrintf(buf, 1100, str, argptr))) 
		StringCchPrintf(buf, 1100, L"StringCchVPrintf error: %d\n", ecode);
	va_end(argptr);
	OutputDebugString(buf);
}


void LogEventNull(LPCTSTR str, ...)
{
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -