From 02251ed412c86fbd07e70d9fdeb824a8d0b9dd57 Mon Sep 17 00:00:00 2001 From: spx Date: Wed, 3 Aug 2011 19:44:40 +0000 Subject: [PATCH] Additional change for r860. Improved writing the network statistics to Rainmeter.stats. --- Library/MeasureNet.cpp | 48 +++++++++++++++++++++++++++++++++++------- Library/MeasureNet.h | 6 ++++-- Library/Rainmeter.cpp | 16 ++------------ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/Library/MeasureNet.cpp b/Library/MeasureNet.cpp index 8a89cc2d..9459c000 100644 --- a/Library/MeasureNet.cpp +++ b/Library/MeasureNet.cpp @@ -581,13 +581,19 @@ void CMeasureNet::ResetStats() ** Reads statistics. ** */ -void CMeasureNet::ReadStats(const WCHAR* iniFile) +void CMeasureNet::ReadStats(const WCHAR* iniFile, std::wstring& statsDate) { WCHAR buffer[64]; CConfigParser parser; parser.Initialize(iniFile, NULL, NULL, L"Statistics"); + std::wstring date = parser.ReadString(L"Statistics", L"Since", L"", false); + if (!date.empty()) + { + statsDate = date; + } + int count = parser.ReadInt(L"Statistics", L"NetStatsCount", 0); c_StatValues.clear(); @@ -620,16 +626,25 @@ void CMeasureNet::ReadStats(const WCHAR* iniFile) ** Writes statistics. ** */ -void CMeasureNet::WriteStats(const WCHAR* iniFile) +void CMeasureNet::WriteStats(const WCHAR* iniFile, const WCHAR* statsDate) { WCHAR buffer[32]; WCHAR buffer2[64]; size_t statsSize = c_StatValues.size() / 2; - _snwprintf_s(buffer, _TRUNCATE, L"%i", (int)statsSize); - WritePrivateProfileString(L"Statistics", L"NetStatsCount", buffer, iniFile); + // Reserve sufficient buffer for statistics + std::wstring data; + data.reserve((64 * 2) + 128 * statsSize); + // Add date + AppendStatsValue(data, L"Since", statsDate); + + // Add stats count + _snwprintf_s(buffer, _TRUNCATE, L"%i", (int)statsSize); + AppendStatsValue(data, L"NetStatsCount", buffer); + + // Add stats for (size_t i = 0; i < statsSize; ++i) { ULARGE_INTEGER value; @@ -638,22 +653,39 @@ void CMeasureNet::WriteStats(const WCHAR* iniFile) _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInHigh%i", (int)i + 1); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart); - WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); + AppendStatsValue(data, buffer2, buffer); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInLow%i", (int)i + 1); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); - WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); + AppendStatsValue(data, buffer2, buffer); value.QuadPart = c_StatValues[i * 2 + 1]; _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutHigh%i", (int)i + 1); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart); - WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); + AppendStatsValue(data, buffer2, buffer); _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutLow%i", (int)i + 1); _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); - WritePrivateProfileString(L"Statistics", buffer2, buffer, iniFile); + AppendStatsValue(data, buffer2, buffer); } + + // Write statistics + WritePrivateProfileSection(L"Statistics", data.c_str(), iniFile); +} + +/* +** AppendStatsValue +** +** Appends "key=value\0" to given string. +** +*/ +void CMeasureNet::AppendStatsValue(std::wstring& data, const WCHAR* key, const WCHAR* value) +{ + data += key; + data += L'='; + data += value; + data += L'\0'; } /* diff --git a/Library/MeasureNet.h b/Library/MeasureNet.h index 9886bef2..af745443 100644 --- a/Library/MeasureNet.h +++ b/Library/MeasureNet.h @@ -45,8 +45,8 @@ public: static void UpdateStats(); static void ResetStats(); - static void ReadStats(const WCHAR* iniFile); - static void WriteStats(const WCHAR* iniFile); + static void ReadStats(const WCHAR* iniFile, std::wstring& statsDate); + static void WriteStats(const WCHAR* iniFile, const WCHAR* statsDate); static void InitializeNewApi(); static void FinalizeNewApi(); @@ -56,6 +56,8 @@ protected: ULONG64 GetNetOctets(NET net); ULONG64 GetNetStatsValue(NET net); + static void AppendStatsValue(std::wstring& data, const WCHAR* key, const WCHAR* value); + double m_CurrentTraffic; double m_TrafficValue; UINT m_Interface; diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index ba083950..f930705d 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -3750,17 +3750,8 @@ void CRainmeter::ReadStats() delete [] tmpSz; } - WCHAR* tmpSz = new WCHAR[MAX_LINE_LENGTH]; - - if (GetPrivateProfileString(L"Statistics", L"Since", L"", tmpSz, MAX_LINE_LENGTH, m_StatsFile.c_str()) > 0) - { - m_StatsDate = tmpSz; - } - - delete [] tmpSz; - // Only Net measure has stats at the moment - CMeasureNet::ReadStats(m_StatsFile.c_str()); + CMeasureNet::ReadStats(m_StatsFile.c_str(), m_StatsDate); } /* @@ -3779,11 +3770,8 @@ void CRainmeter::WriteStats(bool bForce) { lastWrite = ticks; - // Write the date for statistics - WritePrivateProfileString(L"Statistics", L"Since", m_StatsDate.c_str(), m_StatsFile.c_str()); - // Only Net measure has stats at the moment - CMeasureNet::WriteStats(m_StatsFile.c_str()); + CMeasureNet::WriteStats(m_StatsFile.c_str(), m_StatsDate.c_str()); WritePrivateProfileString(NULL, NULL, NULL, m_StatsFile.c_str()); }