📄 config.cpp
字号:
return false;
}
delete[] Decoded;
return true;
}
#endif // CONFIG_STREAM_METHODS
// private
bool TFileConfig::ParseString(const string& Config)
{
string Line, Comments, Name, Value;
#if 0
map<string,TFileConfigSection>::iterator si = Sections.end();
map<string,TFileConfigValue>::iterator vi;
#else
std::pair<map<string,TFileConfigSection>::iterator,bool> si_insert;
std::pair<map<string,TFileConfigValue>::iterator,bool> vi_insert;
#endif
int pos, lstart, lend;
lstart = 0;
while(lstart != -1) {
if((lend = Config.find('\n', lstart + 1)) == -1)
Line = Config.substr(lstart);
else
Line = Config.substr(lstart,lend - lstart);
lstart = lend;
Line = string_trim(Line);
// save comments and file layout
if(Line.empty() || Line[0] == '#') {
Comments += Line + "\r\n";
continue;
}
// new section?
if(Line[0] == '[') {
if((pos = Line.find(']')) == -1) {
// some fucked up line
Comments += Line + "\r\n";
continue;
}
Name = Line.substr(1,pos-1);
#if 0
si = Sections.insert(Sections.end(),map<string,TFileConfigSection>::value_type(Name,TFileConfigSection(Name,Sections.size())));
// add comments
(*si).second.Descr = Comments;
#else
si_insert = Sections.insert(map<string,TFileConfigSection>::value_type(Name,TFileConfigSection(Name,Sections.size())));
// insert may fail and si_insert.first will point to the section already in Sections.
if(si_insert.second) {
// insert succeeded, add comments
(*(si_insert.first)).second.Descr = Comments;
}
#endif
Comments = "";
continue;
}
if(Sections.empty()) {
// a value before the first section, make it a comment
Comments += Line + "\r\n";
continue;
}
// get name/value pair
if((pos = Line.find('=')) == -1) {
// some fucked up line
Comments += Line + "\r\n";
continue;
}
Name = string_trim(Line.substr(0,pos));
Value = string_trim(Line.substr(pos+1));
#if 0
// add to value to current section
vi = (*si).second.Values.insert((*si).second.Values.end(),map<string,TFileConfigValue>::value_type(Name,TFileConfigValue(Name,Value,(*si).second.Values.size())));
// add comments
(*vi).second.Descr = Comments;
#else
// add to value to current section or update already present one
vi_insert = (*(si_insert.first)).second.Values.insert(map<string,TFileConfigValue>::value_type(Name,TFileConfigValue(Name,Value,(*(si_insert.first)).second.Values.size())));
if(vi_insert.second) {
// insert succeeded, add comments
(*(vi_insert.first)).second.Descr = Comments;
}
#endif
Comments = "";
}
return true;
}
string TFileConfig::SectionFromKey(const char* Key)
{
const char *p = strchr(Key,'/');
if(p)
return string().assign(Key,p-Key);
else
return "";
}
string TFileConfig::NameFromKey(const char* Key)
{
const char *p = strchr(Key,'/');
return p ? p+1 : Key;
}
// registry functions
// reads string or dword (which gets converted to string)
// Key is of the form "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\foo"
// returns NULL on fail, caller frees returned string
char* ReadRegKey(const char* Key)
{
char* tmp = strdup(Key);
char *p2 = strrchr(tmp,'\\');
if(p2 == NULL) {
free(tmp);
return NULL;
}
*p2 = 0; ++p2;
char *p1 = strchr(tmp,'\\');
if(p1 == NULL) {
free(tmp);
return NULL;
}
*p1 = 0; ++p1;
HKEY hKey;
if(stricmp(tmp,"HKCR") == 0)
hKey = HKEY_CLASSES_ROOT;
else if(stricmp(tmp,"HKCU") == 0)
hKey = HKEY_CURRENT_USER;
else if(stricmp(tmp,"HKLM") == 0)
hKey = HKEY_LOCAL_MACHINE;
else if(stricmp(tmp,"HKU") == 0)
hKey = HKEY_USERS;
else {
free(tmp);
return NULL;
}
if(RegOpenKeyEx(hKey,p1,0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS) {
free(tmp);
return NULL;
}
DWORD KeyType, Size;
if(RegQueryValueEx(hKey,p2,NULL,&KeyType,NULL,&Size) == ERROR_SUCCESS) {
char *Result = NULL;
if(KeyType == REG_SZ || KeyType == REG_EXPAND_SZ) {
// string value
Result = (char*)malloc(Size);
RegQueryValueEx(hKey,p2,NULL,NULL,Result,&Size);
} else if(KeyType == REG_DWORD || KeyType == REG_DWORD_LITTLE_ENDIAN) {
// DWORD value
DWORD Value;
Size = sizeof(DWORD);
RegQueryValueEx(hKey,p2,NULL,NULL,(unsigned char*)&Value,&Size);
Result = (char*)malloc(33);
itoa(Value,Result,10);
}
RegCloseKey(hKey);
free(tmp);
return Result;
}
RegCloseKey(hKey);
free(tmp);
return NULL;
}
// writes string
// Key is of the form "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\foo"
// if Value is NULL value is removed
bool WriteRegKey(const char* Key, const char* Value)
{
DWORD WasCreated;
char* tmp = strdup(Key);
char *p2 = strrchr(tmp,'\\');
if(p2 == NULL) {
free(tmp);
return false;
}
*p2 = 0; ++p2;
char *p1 = strchr(tmp,'\\');
if(p1 == NULL) {
free(tmp);
return false;
}
*p1 = 0; ++p1;
HKEY hKey;
if(stricmp(tmp,"HKCR") == 0)
hKey = HKEY_CLASSES_ROOT;
else if(stricmp(tmp,"HKCU") == 0)
hKey = HKEY_CURRENT_USER;
else if(stricmp(tmp,"HKLM") == 0)
hKey = HKEY_LOCAL_MACHINE;
else if(stricmp(tmp,"HKU") == 0)
hKey = HKEY_USERS;
else {
free(tmp);
return false;
}
// open or create key
if(RegCreateKeyEx(hKey,p1,0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,&WasCreated) != ERROR_SUCCESS) {
free(tmp);
return false;
}
if(Value) {
// set value
if(RegSetValueEx(hKey,p2,0,REG_SZ,Value,strlen(Value)+1) == ERROR_SUCCESS) {
RegCloseKey(hKey);
free(tmp);
return true;
}
} else {
// remove value
if(RegDeleteValue(hKey,p2) == ERROR_SUCCESS) {
RegCloseKey(hKey);
free(tmp);
return true;
}
}
RegCloseKey(hKey);
free(tmp);
return false;
}
// writes dword
// Key is of the form "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\foo"
// use WriteRegKey(NULL) to remove key
bool WriteRegKeyDWORD(const char* Key, DWORD Value)
{
DWORD WasCreated;
char* tmp = strdup(Key);
char *p2 = strrchr(tmp,'\\');
if(p2 == NULL) {
free(tmp);
return false;
}
*p2 = 0; ++p2;
char *p1 = strchr(tmp,'\\');
if(p1 == NULL) {
free(tmp);
return false;
}
*p1 = 0; ++p1;
HKEY hKey;
if(stricmp(tmp,"HKCR") == 0)
hKey = HKEY_CLASSES_ROOT;
else if(stricmp(tmp,"HKCU") == 0)
hKey = HKEY_CURRENT_USER;
else if(stricmp(tmp,"HKLM") == 0)
hKey = HKEY_LOCAL_MACHINE;
else if(stricmp(tmp,"HKU") == 0)
hKey = HKEY_USERS;
else {
free(tmp);
return false;
}
// open or create key
if(RegCreateKeyEx(hKey,p1,0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,&WasCreated) != ERROR_SUCCESS) {
free(tmp);
return false;
}
// set value
if(RegSetValueEx(hKey,p2,0,REG_DWORD,(unsigned char*)&Value,sizeof(DWORD)) == ERROR_SUCCESS) {
RegCloseKey(hKey);
free(tmp);
return true;
}
RegCloseKey(hKey);
free(tmp);
return false;
}
// returns the installed physical ram in MB or 0 on failure
int GetPhysicalMem()
{
MEMORYSTATUS MemStat;
MemStat.dwLength = sizeof(MemStat);
::GlobalMemoryStatus(&MemStat);
return (MemStat.dwTotalPhys / 1024 / 1024);
}
// return the cpu speed in MHz or 0 on filure
int GetCPUSpeed()
{
char *SpeedStr = ReadRegKey("HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz");
if(!SpeedStr)
return 0;
int Speed = atoi(SpeedStr);
free (SpeedStr);
return Speed;
}
// returns true if OS is NT/2k/XP
bool RunningWindowsNT()
{
OSVERSIONINFO osvi;
memset (&osvi, 0, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(!GetVersionEx(&osvi))
return false;
return (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
} // namespace KCeasyEngine
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -