Code tweaks and cleanup.

This commit is contained in:
spx 2011-12-09 03:28:19 +00:00
parent 8e8b7d1268
commit 2835739b12
11 changed files with 164 additions and 86 deletions

View File

@ -119,9 +119,9 @@ void CConfigParser::SetBuiltInVariables(CRainmeter* pRainmeter, CMeterWindow* me
SetBuiltInVariable(L"CRLF", L"\n");
static const std::wstring CURRENTSECTION = L"CURRENTSECTION";
const std::wstring CURRENTSECTION = StrToLower(L"CURRENTSECTION");
SetBuiltInVariable(CURRENTSECTION, L"");
m_CurrentSection = &((*m_BuiltInVariables.find(StrToLower(CURRENTSECTION))).second); // shortcut
m_CurrentSection = &((*m_BuiltInVariables.find(CURRENTSECTION)).second); // shortcut
}
/*
@ -151,7 +151,17 @@ void CConfigParser::SetVariable(std::unordered_map<std::wstring, std::wstring>&
{
// LogWithArgs(LOG_DEBUG, L"Variable: %s=%s (size=%i)", strVariable.c_str(), strValue.c_str(), (int)variables.size());
variables[StrToLower(strVariable)] = strValue;
const std::wstring strTmp = StrToLower(strVariable);
variables[strTmp] = strValue;
}
void CConfigParser::SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const WCHAR* strVariable, const WCHAR* strValue)
{
// LogWithArgs(LOG_DEBUG, L"Variable: %s=%s (size=%i)", strVariable.c_str(), strValue.c_str(), (int)variables.size());
const std::wstring strTmp = StrToLower(strVariable);
variables[strTmp] = strValue;
}
/**
@ -163,7 +173,7 @@ void CConfigParser::SetVariable(std::unordered_map<std::wstring, std::wstring>&
*/
bool CConfigParser::GetVariable(const std::wstring& strVariable, std::wstring& strValue)
{
std::wstring strTmp = StrToLower(strVariable);
const std::wstring strTmp = StrToLower(strVariable);
// #1: Built-in variables
std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_BuiltInVariables.find(strTmp);
@ -645,14 +655,14 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
{
if (result.find(L'#') != std::wstring::npos)
{
m_CurrentSection->assign(strSection); // Set temporarily
SetCurrentSection(strSection); // Set temporarily
if (ReplaceVariables(result))
{
m_LastReplaced = true;
}
m_CurrentSection->clear(); // Reset
ClearCurrentSection(); // Reset
}
else
{
@ -1118,7 +1128,7 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile, LPCTSTR config, int
// Get all the sections (i.e. different meters)
std::list<std::wstring> sections;
std::unordered_set<std::wstring> unique;
std::wstring section, sectionKey; // buffer
std::wstring key, value; // buffer
DWORD itemsSize = MAX_LINE_LENGTH;
WCHAR* items = new WCHAR[itemsSize];
@ -1156,17 +1166,17 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile, LPCTSTR config, int
{
if (*pos)
{
section = pos;
StrToLowerC(sectionKey.assign(section));
if (unique.insert(sectionKey).second)
value = pos; // section name
StrToLowerC(key.assign(value));
if (unique.insert(key).second)
{
if (m_FoundSections.insert(sectionKey).second)
if (m_FoundSections.insert(key).second)
{
m_Sections.push_back(section);
m_Sections.push_back(value);
}
sections.push_back(section);
sections.push_back(value);
}
pos += section.size() + 1;
pos += value.size() + 1;
}
else // Empty string
{
@ -1191,7 +1201,6 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile, LPCTSTR config, int
}
// Read the keys and values
std::wstring key, value; // buffer
std::list<std::wstring>::const_iterator iter = sections.begin();
for ( ; iter != sections.end(); ++iter)
{
@ -1242,22 +1251,26 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile, LPCTSTR config, int
++sep;
}
value.assign(sep, clen);
if (wcsncmp(key.c_str(), L"@include", 8) == 0)
{
ReadVariables();
ReplaceVariables(value);
if (!CSystem::IsAbsolutePath(value))
if (clen > 0)
{
// It's a relative path so add the current path as a prefix
value.insert(0, CRainmeter::ExtractPath(iniFile));
value.assign(sep, clen);
ReadVariables();
ReplaceVariables(value);
if (!CSystem::IsAbsolutePath(value))
{
// It's a relative path so add the current path as a prefix
value.insert(0, CRainmeter::ExtractPath(iniFile));
}
ReadIniFile(value, config, depth + 1);
}
ReadIniFile(value, config, depth + 1);
}
else
{
if (!isMetadata) // Uncache Metadata's key-value pair in the skin
{
value.assign(sep, clen);
SetValue((*iter), key, value);
if (isVariables)
@ -1318,7 +1331,7 @@ void CConfigParser::DeleteValue(const std::wstring& strSection, const std::wstri
strTmp += L'~';
strTmp += strKey;
std::unordered_map<std::wstring, std::wstring>::iterator iter = m_Values.find(StrToLowerC(strTmp));
std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_Values.find(StrToLowerC(strTmp));
if (iter != m_Values.end())
{
m_Values.erase(iter);

View File

@ -43,9 +43,13 @@ public:
void Initialize(LPCTSTR filename, CRainmeter* pRainmeter, CMeterWindow* meterWindow = NULL, LPCTSTR config = NULL);
void AddMeasure(CMeasure* pMeasure);
bool GetVariable(const std::wstring& strVariable, std::wstring& strValue);
void SetVariable(const std::wstring& strVariable, const std::wstring& strValue) { SetVariable(m_Variables, strVariable, strValue); }
void SetBuiltInVariable(const std::wstring& strVariable, const std::wstring& strValue) { SetVariable(m_BuiltInVariables, strVariable, strValue); }
bool GetVariable(const std::wstring& strVariable, std::wstring& strValue);
void SetBuiltInVariable(const WCHAR* strVariable, const WCHAR* strValue) { SetVariable(m_BuiltInVariables, strVariable, strValue); }
void SetCurrentSection(const std::wstring& strSection) { m_CurrentSection->assign(strSection); }
void ClearCurrentSection() { m_CurrentSection->clear(); }
const std::wstring& GetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strDefault);
void SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue);
@ -106,6 +110,7 @@ private:
void SetAutoSelectedMonitorVariables(CMeterWindow* meterWindow);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const std::wstring& strVariable, const std::wstring& strValue);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const WCHAR* strVariable, const WCHAR* strValue);
static void SetMultiMonitorVariables(bool reset);
static void SetMonitorVariable(const std::wstring& strVariable, const std::wstring& strValue) { SetVariable(c_MonitorVariables, strVariable, strValue); }

View File

@ -198,16 +198,15 @@ void CMeasure::ReadConfig(CConfigParser& parser, const WCHAR* section)
*/
bool CMeasure::MakePlainSubstitute(std::wstring& str, size_t index)
{
size_t start = 0;
size_t pos = std::wstring::npos;
size_t start = 0, pos;
do
{
pos = str.find(m_Substitute[index].first, start);
pos = str.find(m_Substitute[index], start);
if (pos != std::wstring::npos)
{
str.replace(pos, m_Substitute[index].first.length(), m_Substitute[index].second);
start = pos + m_Substitute[index].second.length();
str.replace(pos, m_Substitute[index].length(), m_Substitute[index + 1]);
start = pos + m_Substitute[index + 1].length();
}
}
while (pos != std::wstring::npos);
@ -230,16 +229,16 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
{
str = buffer;
for (size_t i = 0, isize = m_Substitute.size(); i < isize; ++i)
for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2)
{
if (!m_Substitute[i].first.empty())
if (!m_Substitute[i].empty())
{
MakePlainSubstitute(str, i);
}
else if (str.empty())
{
// Empty result and empty substitute -> use second
str = m_Substitute[i].second;
str = m_Substitute[i + 1];
}
}
}
@ -248,7 +247,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
std::string utf8str = ConvertToUTF8(buffer);
int* ovector = new int[OVECCOUNT];
for (size_t i = 0, isize = m_Substitute.size() ; i < isize ; ++i)
for (size_t i = 0, isize = m_Substitute.size() ; i < isize ; i += 2)
{
pcre* re;
const char* error;
@ -258,7 +257,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
int offset = 0;
re = pcre_compile(
ConvertToUTF8(m_Substitute[i].first.c_str()).c_str(), // the pattern
ConvertToUTF8(m_Substitute[i].c_str()).c_str(), // the pattern
flags, // default options
&error, // for error message
&erroffset, // for error offset
@ -289,25 +288,30 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
}
else
{
std::string result = ConvertToUTF8(m_Substitute[i].second.c_str());
std::string result = ConvertToUTF8(m_Substitute[i + 1].c_str());
if (rc > 1)
{
for (int j = rc - 1 ; j >= 0 ; --j)
{
size_t new_start = ovector[2*j];
size_t in_length = ovector[2*j+1] - ovector[2*j];
size_t new_start = ovector[2 * j];
size_t in_length = ovector[2 * j + 1] - ovector[2 * j];
char tmpName[64];
_snprintf_s(tmpName, _TRUNCATE, "\\%i", j);
size_t cut_length = strlen(tmpName);
size_t pos = result.find(tmpName);
while (pos != std::string::npos)
size_t start = 0, pos;
do
{
result.replace(pos, cut_length, utf8str, new_start, in_length);
pos = result.find(tmpName, pos + in_length);
pos = result.find(tmpName, start, cut_length);
if (pos != std::string::npos)
{
result.replace(pos, cut_length, utf8str, new_start, in_length);
start = pos + in_length;
}
}
while (pos != std::string::npos);
}
}
@ -356,7 +360,8 @@ bool CMeasure::ParseSubstitute(std::wstring buffer)
if (wcscmp(word1.c_str(), word2.c_str()) != 0)
{
m_Substitute.push_back(std::pair<std::wstring, std::wstring>(word1, word2));
m_Substitute.push_back(word1);
m_Substitute.push_back(word2);
}
std::wstring sep2 = ExtractWord(buffer);

View File

@ -105,7 +105,7 @@ protected:
const std::wstring m_Name; // Name of this Measure
const std::string m_AsciiName; // Name of this Measure in ANSI
std::vector< std::pair<std::wstring, std::wstring> > m_Substitute; // Vec of substitute strings
std::vector<std::wstring> m_Substitute; // Vec of substitute strings
bool m_RegExpSubstitute;
std::vector<double> m_MedianMaxValues; // The values for the median filtering

View File

@ -27,7 +27,14 @@
#define SystemProcessorPerformanceInformation 8
//#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER Reserved1[2];
ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION, *PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
#define Li2Double(x) ((double)((x).QuadPart))
#define Ft2Double(x) ((double)((x).dwHighDateTime) * 4.294967296E9 + (double)((x).dwLowDateTime))
@ -72,7 +79,7 @@ CMeasureCPU::CMeasureCPU(CMeterWindow* meterWindow, const WCHAR* name) : CMeasur
}
if (c_NumOfProcessors == 0)
{
SYSTEM_INFO systemInfo = {0};
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
c_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
}

View File

@ -21,14 +21,6 @@
#include "Measure.h"
typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER Reserved1[2];
ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION, *PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
class CMeasureCPU : public CMeasure
@ -44,7 +36,6 @@ protected:
private:
void CalcUsage(double idleTime, double systemTime);
void CalcAverageUsage(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* systemPerfInfo);
bool m_FirstTime;

View File

@ -169,30 +169,35 @@ void CMeasureCalc::FormulaReplace()
//To implement random numbers the word "Random" in the string
//formula is being replaced by the random number value
m_Formula = m_FormulaHolder;
std::wstring::size_type loc = 0;
size_t start = 0, pos;
while ((loc = m_Formula.find_first_of(L"Rr", loc)) != std::wstring::npos)
do
{
if (_wcsnicmp(L"Random", m_Formula.c_str() + loc, 6) == 0 &&
(loc == 0 || IsDelimiter(*(m_Formula.c_str() + loc - 1))) &&
(loc == (m_Formula.length() - 6) || IsDelimiter(*(m_Formula.c_str() + loc + 6))))
pos = m_Formula.find_first_of(L"Rr", start);
if (pos != std::wstring::npos)
{
int range = (m_HighBound - m_LowBound) + 1;
srand((unsigned) rand());
int randNumber = m_LowBound + (int)(range * rand()/(RAND_MAX + 1.0));
if (_wcsnicmp(L"Random", m_Formula.c_str() + pos, 6) == 0 &&
(pos == 0 || IsDelimiter(*(m_Formula.c_str() + pos - 1))) &&
(pos == (m_Formula.length() - 6) || IsDelimiter(*(m_Formula.c_str() + pos + 6))))
{
int range = (m_HighBound - m_LowBound) + 1;
srand((unsigned) rand());
int randNumber = m_LowBound + (int)(range * rand() / (RAND_MAX + 1.0));
WCHAR buffer[32];
_itow_s(randNumber, buffer, 10);
size_t len = wcslen(buffer);
WCHAR buffer[32];
_itow_s(randNumber, buffer, 10);
size_t len = wcslen(buffer);
m_Formula.replace(loc, 6, buffer, len);
loc += len;
}
else
{
++loc;
m_Formula.replace(pos, 6, buffer, len);
start = pos + len;
}
else
{
start = pos + 1;
}
}
}
while (pos != std::wstring::npos);
}
/*

View File

@ -568,6 +568,7 @@ bool CMeter::Update()
void CMeter::SetAllMeasures(CMeasure* measure)
{
m_AllMeasures.clear();
m_AllMeasures.reserve(2);
m_AllMeasures.push_back(m_Measure);
m_AllMeasures.push_back(measure);
}
@ -580,6 +581,7 @@ void CMeter::SetAllMeasures(CMeasure* measure)
void CMeter::SetAllMeasures(const std::vector<CMeasure*>& measures)
{
m_AllMeasures.clear();
m_AllMeasures.reserve(1 + measures.size());
m_AllMeasures.push_back(m_Measure);
std::vector<CMeasure*>::const_iterator i = measures.begin();
@ -635,15 +637,14 @@ bool CMeter::ReplaceMeasures(const std::vector<std::wstring>& stringValues, std:
{
_snwprintf_s(buffer, _TRUNCATE, L"%%%i", (int)i);
size_t start = 0;
size_t pos = std::wstring::npos;
size_t len = wcslen(buffer);
size_t start = 0, pos;
do
{
pos = str.find(buffer, start);
pos = str.find(buffer, start, len);
if (pos != std::wstring::npos)
{
str.replace(pos, wcslen(buffer), stringValues[i - 1]);
str.replace(pos, len, stringValues[i - 1]);
start = pos + stringValues[i - 1].length();
replaced = true;
}

View File

@ -316,10 +316,17 @@ std::vector<std::wstring> CRainmeter::ParseString(LPCTSTR str)
arg.erase(0, pos + 1);
// Strip quotes
while ((pos = newStr.find(L'"')) != std::wstring::npos)
size_t start = 0;
do
{
newStr.erase(pos, 1);
pos = newStr.find(L'"', start);
if (pos != std::wstring::npos)
{
newStr.erase(pos, 1);
start = pos;
}
}
while (pos != std::wstring::npos);
result.push_back(newStr);
}
@ -332,10 +339,17 @@ std::vector<std::wstring> CRainmeter::ParseString(LPCTSTR str)
if (!arg.empty())
{
// Strip quotes
while ((pos = arg.find(L'"')) != std::wstring::npos)
size_t start = 0;
do
{
arg.erase(pos, 1);
pos = arg.find(L'"', start);
if (pos != std::wstring::npos)
{
arg.erase(pos, 1);
start = pos;
}
}
while (pos != std::wstring::npos);
result.push_back(arg);
}
@ -1557,7 +1571,7 @@ int CRainmeter::ScanForConfigsRecursive(const std::wstring& path, std::wstring b
CONFIGMENU menuItem;
menuItem.name = filename;
menuItem.index = m_ConfigStrings.size();
menu.push_back(menuItem);
menu.push_back(std::move(menuItem));
config.iniFiles.push_back(filename);
++index;
@ -1570,7 +1584,7 @@ int CRainmeter::ScanForConfigsRecursive(const std::wstring& path, std::wstring b
if (!config.iniFiles.empty())
{
m_ConfigStrings.push_back(config);
m_ConfigStrings.push_back(std::move(config));
}
}
@ -1587,7 +1601,7 @@ int CRainmeter::ScanForConfigsRecursive(const std::wstring& path, std::wstring b
CONFIGMENU menuItem;
menuItem.name = (*iter);
menuItem.index = -1;
menu.push_back(menuItem);
menu.push_back(std::move(menuItem));
if (!DontRecurse)
{

View File

@ -57,6 +57,26 @@ public:
std::vector<std::wstring> iniFiles;
UINT commandBase;
int active;
CONFIG() {}
~CONFIG() {}
CONFIG(CONFIG&& r) :
config(std::move(r.config)),
iniFiles(std::move(r.iniFiles)),
commandBase(r.commandBase),
active(r.active)
{
}
CONFIG& operator=(CONFIG&& r)
{
config = std::move(r.config);
iniFiles = std::move(r.iniFiles);
commandBase = r.commandBase;
active = r.active;
return *this;
}
};
struct CONFIGMENU
@ -64,6 +84,24 @@ public:
std::wstring name;
size_t index;
std::vector<CONFIGMENU> children;
CONFIGMENU() {}
~CONFIGMENU() {}
CONFIGMENU(CONFIGMENU&& r) :
name(std::move(r.name)),
index(r.index),
children(std::move(r.children))
{
}
CONFIGMENU& operator=(CONFIGMENU&& r)
{
name = std::move(r.name);
index = r.index;
children = std::move(r.children);
return *this;
}
};
struct LOG_INFO

View File

@ -25,10 +25,9 @@ static int Measure_GetOption(lua_State* L)
{
if (strTmp.find(L'#') != std::wstring::npos)
{
static const std::wstring CURRENTSECTION = L"CURRENTSECTION";
parser.SetBuiltInVariable(CURRENTSECTION, self->GetOriginalName()); // Set temporarily
parser.SetCurrentSection(self->GetOriginalName()); // Set temporarily
parser.ReplaceVariables(strTmp);
parser.SetBuiltInVariable(CURRENTSECTION, L""); // Reset
parser.ClearCurrentSection(); // Reset
}
parser.ReplaceMeasures(strTmp);
}