From 3bebd7002abd139bc618831980491f9930f89fe6 Mon Sep 17 00:00:00 2001 From: spx Date: Fri, 9 Dec 2011 19:49:06 +0000 Subject: [PATCH] Minor tweaks. --- Library/ConfigParser.cpp | 6 ++-- Library/Litestep.cpp | 4 +-- Library/Measure.cpp | 5 ++-- Library/MeasureNet.cpp | 65 ++++++++++++++++++++-------------------- Library/MeasureNet.h | 4 +-- Library/MeterString.cpp | 18 +++++++---- Library/Rainmeter.cpp | 18 +++++------ Library/System.cpp | 46 +++++++++++++++++----------- Library/System.h | 4 +-- Library/TintedImage.cpp | 4 +-- 10 files changed, 94 insertions(+), 80 deletions(-) diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index 8a16e013..37df624c 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -1308,7 +1308,7 @@ void CConfigParser::SetValue(const std::wstring& strSection, const std::wstring& std::wstring strTmp; strTmp.reserve(strSection.size() + 1 + strKey.size()); - strTmp += strSection; + strTmp = strSection; strTmp += L'~'; strTmp += strKey; @@ -1327,7 +1327,7 @@ void CConfigParser::DeleteValue(const std::wstring& strSection, const std::wstri { std::wstring strTmp; strTmp.reserve(strSection.size() + 1 + strKey.size()); - strTmp += strSection; + strTmp = strSection; strTmp += L'~'; strTmp += strKey; @@ -1351,7 +1351,7 @@ const std::wstring& CConfigParser::GetValue(const std::wstring& strSection, cons { std::wstring strTmp; strTmp.reserve(strSection.size() + 1 + strKey.size()); - strTmp += strSection; + strTmp = strSection; strTmp += L'~'; strTmp += strKey; diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index 1f6ede67..36f663f0 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -306,12 +306,12 @@ BOOL LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage) { // Add timestamp WCHAR buffer[128]; - _snwprintf_s(buffer, _TRUNCATE, L"%02llu:%02llu:%02llu.%03llu", elapsed / (1000 * 60 * 60), (elapsed / (1000 * 60)) % 60, (elapsed / 1000) % 60, elapsed % 1000); + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"%02llu:%02llu:%02llu.%03llu", elapsed / (1000 * 60 * 60), (elapsed / (1000 * 60)) % 60, (elapsed / 1000) % 60, elapsed % 1000); Rainmeter->AddAboutLogInfo(nLevel, buffer, pszMessage); std::wstring message(1, L'('); - message += buffer; + message.append(buffer, len); message += L") "; message += pszMessage; diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 3d8929ce..e8c3a73b 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -247,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 += 2) + for (size_t i = 0, isize = m_Substitute.size(); i < isize ; i += 2) { pcre* re; const char* error; @@ -298,9 +298,8 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) 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 cut_length = _snprintf_s(tmpName, _TRUNCATE, "\\%i", j);; size_t start = 0, pos; do { diff --git a/Library/MeasureNet.cpp b/Library/MeasureNet.cpp index 41fd6d67..5955e6d8 100644 --- a/Library/MeasureNet.cpp +++ b/Library/MeasureNet.cpp @@ -624,16 +624,31 @@ void CMeasureNet::ReadStats(const WCHAR* iniFile, std::wstring& statsDate) } } +/* +** AppendStatsValue +** +** Appends "key=value\0" to given string. +** +*/ +inline void AppendStatsValue(std::wstring& data, const WCHAR* key, size_t key_len, const WCHAR* value, size_t value_len) +{ + data.append(key, key_len); + data += L'='; + data.append(value, value_len); + data += L'\0'; +} + /* ** WriteStats ** ** Writes statistics. ** */ -void CMeasureNet::WriteStats(const WCHAR* iniFile, const WCHAR* statsDate) +void CMeasureNet::WriteStats(const WCHAR* iniFile, const std::wstring& statsDate) { - WCHAR buffer[32]; - WCHAR buffer2[64]; + WCHAR buffer[64]; + WCHAR buffer2[32]; + size_t len, len2; size_t statsSize = c_StatValues.size() / 2; @@ -642,11 +657,11 @@ void CMeasureNet::WriteStats(const WCHAR* iniFile, const WCHAR* statsDate) data.reserve((64 * 2) + 128 * statsSize); // Add date - AppendStatsValue(data, L"Since", statsDate); + AppendStatsValue(data, L"Since", 5, statsDate.c_str(), statsDate.size()); // Add stats count - _itow_s((int)statsSize, buffer, 10); - AppendStatsValue(data, L"NetStatsCount", buffer); + len = _snwprintf_s(buffer, _TRUNCATE, L"%i", (int)statsSize); + AppendStatsValue(data, L"NetStatsCount", 13, buffer, len); // Add stats for (size_t i = 0; i < statsSize; ++i) @@ -655,43 +670,29 @@ void CMeasureNet::WriteStats(const WCHAR* iniFile, const WCHAR* statsDate) value.QuadPart = c_StatValues[i * 2]; - _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInHigh%i", (int)i + 1); - _snwprintf_s(buffer, _TRUNCATE, L"%u", value.HighPart); - AppendStatsValue(data, buffer2, buffer); + len = _snwprintf_s(buffer, _TRUNCATE, L"NetStatsInHigh%i", (int)i + 1); + len2 = _snwprintf_s(buffer2, _TRUNCATE, L"%u", value.HighPart); + AppendStatsValue(data, buffer, len, buffer2, len2); - _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsInLow%i", (int)i + 1); - _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); - AppendStatsValue(data, buffer2, buffer); + len = _snwprintf_s(buffer, _TRUNCATE, L"NetStatsInLow%i", (int)i + 1); + len2 = _snwprintf_s(buffer2, _TRUNCATE, L"%u", value.LowPart); + AppendStatsValue(data, buffer, len, buffer2, len2); 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); - AppendStatsValue(data, buffer2, buffer); + len = _snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutHigh%i", (int)i + 1); + len2 = _snwprintf_s(buffer2, _TRUNCATE, L"%u", value.HighPart); + AppendStatsValue(data, buffer, len, buffer2, len2); - _snwprintf_s(buffer2, _TRUNCATE, L"NetStatsOutLow%i", (int)i + 1); - _snwprintf_s(buffer, _TRUNCATE, L"%u", value.LowPart); - AppendStatsValue(data, buffer2, buffer); + len = _snwprintf_s(buffer, _TRUNCATE, L"NetStatsOutLow%i", (int)i + 1); + len2 = _snwprintf_s(buffer2, _TRUNCATE, L"%u", value.LowPart); + AppendStatsValue(data, buffer, len, buffer2, len2); } // 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'; -} - /* ** InitializeNewApi ** diff --git a/Library/MeasureNet.h b/Library/MeasureNet.h index 74909799..0c7eceb2 100644 --- a/Library/MeasureNet.h +++ b/Library/MeasureNet.h @@ -46,7 +46,7 @@ public: static void UpdateStats(); static void ResetStats(); static void ReadStats(const WCHAR* iniFile, std::wstring& statsDate); - static void WriteStats(const WCHAR* iniFile, const WCHAR* statsDate); + static void WriteStats(const WCHAR* iniFile, const std::wstring& statsDate); static void InitializeNewApi(); static void FinalizeNewApi(); @@ -56,8 +56,6 @@ 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/MeterString.cpp b/Library/MeterString.cpp index 1cc6cd2e..56f5df83 100644 --- a/Library/MeterString.cpp +++ b/Library/MeterString.cpp @@ -723,8 +723,9 @@ void CMeterString::FreeFontCache(PrivateFontCollection* collection) if (collection) { WCHAR buffer[32]; - _snwprintf_s(buffer, _TRUNCATE, L"<%p>", collection); - prefix = buffer; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"<%p>", collection); + + prefix.assign(buffer, len); StringToLower(prefix); } @@ -774,8 +775,12 @@ void CMeterString::FreeFontCache(PrivateFontCollection* collection) std::wstring CMeterString::FontFaceToString(const std::wstring& fontFace, PrivateFontCollection* collection) { WCHAR buffer[32]; - _snwprintf_s(buffer, _TRUNCATE, L"<%p>", collection); - std::wstring strTmp = buffer + fontFace; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"<%p>", collection); + + std::wstring strTmp; + strTmp.reserve(len + fontFace.size()); + strTmp.assign(buffer, len); + strTmp += fontFace; StringToLower(strTmp); return strTmp; } @@ -789,8 +794,9 @@ std::wstring CMeterString::FontFaceToString(const std::wstring& fontFace, Privat std::wstring CMeterString::FontPropertiesToString(REAL size, FontStyle style) { WCHAR buffer[64]; - _snwprintf_s(buffer, _TRUNCATE, L"%.1f-%i", size, (int)style); - return buffer; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"%.1f-%i", size, (int)style); + + return std::wstring(buffer, len); } /* diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 37b43063..87b220a5 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -2478,8 +2478,8 @@ void CRainmeter::UpdateDesktopWorkArea(bool reset) if (i != 0) { WCHAR buffer[64]; - _snwprintf_s(buffer, _TRUNCATE, L"@%i", i); - format += buffer; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"@%i", i); + format.append(buffer, len); } format += L": L=%i, T=%i, R=%i, B=%i (W=%i, H=%i)"; if (!result) @@ -2550,7 +2550,7 @@ void CRainmeter::WriteStats(bool bForce) // Only Net measure has stats at the moment const WCHAR* statsFile = m_StatsFile.c_str(); - CMeasureNet::WriteStats(statsFile, m_StatsDate.c_str()); + CMeasureNet::WriteStats(statsFile, m_StatsDate); WritePrivateProfileString(NULL, NULL, NULL, statsFile); } @@ -2987,13 +2987,13 @@ void CRainmeter::CreateMonitorMenu(HMENU monitorMenu, CMeterWindow* meterWindow) for (int i = 0, isize = (int)monitors.size(); i < isize; ++i) { WCHAR buffer[64]; - _snwprintf_s(buffer, _TRUNCATE, L"@%i: ", i + 1); - std::wstring item = buffer; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L"@%i: ", i + 1); - size_t len = wcslen(monitors[i].monitorName); - if (len > 32) + std::wstring item(buffer, len); + + if (monitors[i].monitorName.size() > 32) { - item.append(monitors[i].monitorName, 32); + item.append(monitors[i].monitorName, 0, 32); item += L"..."; } else @@ -3212,7 +3212,7 @@ void CRainmeter::ExpandEnvironmentVariables(std::wstring& strPath) } if (ret <= bufSize) // Fits in the buffer { - strPath = buffer; + strPath.assign(buffer, ret - 1); break; } diff --git a/Library/System.cpp b/Library/System.cpp index 160b69c8..491e4008 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -191,7 +191,7 @@ BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonit { for (size_t i = 0, isize = m->monitors.size(); i < isize; ++i) { - if (m->monitors[i].handle == NULL && _wcsnicmp(info.szDevice, m->monitors[i].deviceName, 32) == 0) + if (m->monitors[i].handle == NULL && _wcsicmp(info.szDevice, m->monitors[i].deviceName.c_str()) == 0) { m->monitors[i].handle = hMonitor; m->monitors[i].screen = *lprcMonitor; @@ -202,14 +202,14 @@ BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonit } else // use only EnumDisplayMonitors { - MONITOR_INFO monitor = {0}; + MONITOR_INFO monitor; monitor.active = true; monitor.handle = hMonitor; monitor.screen = *lprcMonitor; monitor.work = info.rcWork; - wcsncpy_s(monitor.deviceName, info.szDevice, _TRUNCATE); // E.g. "\\.\DISPLAY1" + monitor.deviceName = info.szDevice; // E.g. "\\.\DISPLAY1" // Get the monitor name (E.g. "Generic Non-PnP Monitor") DISPLAY_DEVICE ddm = {sizeof(DISPLAY_DEVICE)}; @@ -218,7 +218,7 @@ BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonit { if (ddm.StateFlags & DISPLAY_DEVICE_ACTIVE && ddm.StateFlags & DISPLAY_DEVICE_ATTACHED) { - wcsncpy_s(monitor.monitorName, ddm.DeviceString, _TRUNCATE); + monitor.monitorName.assign(ddm.DeviceString, wcsnlen(ddm.DeviceString, _countof(ddm.DeviceString))); break; } } @@ -287,9 +287,14 @@ void CSystem::SetMultiMonitorInfo() { std::wstring msg; + std::wstring deviceName(dd.DeviceName, wcsnlen(dd.DeviceName, _countof(dd.DeviceName))); + std::wstring deviceString; + if (logging) { - Log(LOG_DEBUG, dd.DeviceName); + deviceString.assign(dd.DeviceString, wcsnlen(dd.DeviceString, _countof(dd.DeviceString))); + + Log(LOG_DEBUG, deviceName.c_str()); if (dd.StateFlags & DISPLAY_DEVICE_ACTIVE) { @@ -334,20 +339,20 @@ void CSystem::SetMultiMonitorInfo() MONITOR_INFO monitor = {0}; monitor.handle = NULL; - wcsncpy_s(monitor.deviceName, dd.DeviceName, _TRUNCATE); // E.g. "\\.\DISPLAY1" + monitor.deviceName = deviceName; // E.g. "\\.\DISPLAY1" // Get the monitor name (E.g. "Generic Non-PnP Monitor") DISPLAY_DEVICE ddm = {sizeof(DISPLAY_DEVICE)}; DWORD dwMon = 0; - while (EnumDisplayDevices(dd.DeviceName, dwMon++, &ddm, 0)) + while (EnumDisplayDevices(deviceName.c_str(), dwMon++, &ddm, 0)) { if (ddm.StateFlags & DISPLAY_DEVICE_ACTIVE && ddm.StateFlags & DISPLAY_DEVICE_ATTACHED) { - wcsncpy_s(monitor.monitorName, ddm.DeviceString, _TRUNCATE); + monitor.monitorName.assign(ddm.DeviceString, wcsnlen(ddm.DeviceString, _countof(ddm.DeviceString))); if (logging) { - LogWithArgs(LOG_DEBUG, L" Name : %s", ddm.DeviceString); + LogWithArgs(LOG_DEBUG, L" Name : %s", monitor.monitorName.c_str()); } break; } @@ -355,7 +360,7 @@ void CSystem::SetMultiMonitorInfo() if (logging) { - LogWithArgs(LOG_DEBUG, L" Adapter : %s", dd.DeviceString); + LogWithArgs(LOG_DEBUG, L" Adapter : %s", deviceString.c_str()); LogWithArgs(LOG_DEBUG, L" Flags : %s(0x%08X)", msg.c_str(), dd.StateFlags); } @@ -366,7 +371,7 @@ void CSystem::SetMultiMonitorInfo() DEVMODE dm = {0}; dm.dmSize = sizeof(DEVMODE); - if (EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm)) + if (EnumDisplaySettings(deviceName.c_str(), ENUM_CURRENT_SETTINGS, &dm)) { POINT pos = {dm.dmPosition.x, dm.dmPosition.y}; monitor.handle = MonitorFromPoint(pos, MONITOR_DEFAULTTONULL); @@ -417,7 +422,7 @@ void CSystem::SetMultiMonitorInfo() { if (logging) { - LogWithArgs(LOG_DEBUG, L" Adapter : %s", dd.DeviceString); + LogWithArgs(LOG_DEBUG, L" Adapter : %s", deviceString.c_str()); LogWithArgs(LOG_DEBUG, L" Flags : %s(0x%08X)", msg.c_str(), dd.StateFlags); } } @@ -448,16 +453,21 @@ void CSystem::SetMultiMonitorInfo() Log(LOG_WARNING, L"Failed to enumerate monitors. Using dummy monitor info."); c_Monitors.useEnumDisplayMonitors = false; - MONITOR_INFO monitor = {0}; - wcsncpy_s(monitor.deviceName, L"DUMMY", _TRUNCATE); + MONITOR_INFO monitor; + monitor.active = true; + POINT pos = {0, 0}; monitor.handle = MonitorFromPoint(pos, MONITOR_DEFAULTTOPRIMARY); monitor.screen.left = 0; monitor.screen.top = 0; monitor.screen.right = GetSystemMetrics(SM_CXSCREEN); monitor.screen.bottom = GetSystemMetrics(SM_CYSCREEN); - SystemParametersInfo(SPI_GETWORKAREA, 0, &(monitor.work), 0); - monitor.active = true; + if (SystemParametersInfo(SPI_GETWORKAREA, 0, &(monitor.work), 0) == 0) // failed + { + monitor.work = monitor.screen; + } + + monitor.deviceName = L"DUMMY"; monitors.push_back(monitor); @@ -498,14 +508,14 @@ void CSystem::SetMultiMonitorInfo() { if (monitors[i].active) { - LogWithArgs(LOG_DEBUG, L"@%i: %s (active), MonitorName: %s", (int)i + 1, monitors[i].deviceName, monitors[i].monitorName); + LogWithArgs(LOG_DEBUG, L"@%i: %s (active), MonitorName: %s", (int)i + 1, monitors[i].deviceName.c_str(), monitors[i].monitorName.c_str()); LogWithArgs(LOG_DEBUG, L" L=%i, T=%i, R=%i, B=%i (W=%i, H=%i)", monitors[i].screen.left, monitors[i].screen.top, monitors[i].screen.right, monitors[i].screen.bottom, monitors[i].screen.right - monitors[i].screen.left, monitors[i].screen.bottom - monitors[i].screen.top); } else { - LogWithArgs(LOG_DEBUG, L"@%i: %s (inactive), MonitorName: %s", (int)i + 1, monitors[i].deviceName, monitors[i].monitorName); + LogWithArgs(LOG_DEBUG, L"@%i: %s (inactive), MonitorName: %s", (int)i + 1, monitors[i].deviceName.c_str(), monitors[i].monitorName.c_str()); } } Log(LOG_DEBUG, L"------------------------------"); diff --git a/Library/System.h b/Library/System.h index ad6fafec..a83de674 100644 --- a/Library/System.h +++ b/Library/System.h @@ -36,8 +36,8 @@ struct MONITOR_INFO HMONITOR handle; RECT screen; RECT work; - WCHAR deviceName[32]; //Device name (E.g. "\\.\DISPLAY1") - WCHAR monitorName[128]; //Monitor name (E.g. "Generic Non-PnP Monitor") + std::wstring deviceName; //Device name (E.g. "\\.\DISPLAY1") + std::wstring monitorName; //Monitor name (E.g. "Generic Non-PnP Monitor") }; struct MULTIMONITOR_INFO diff --git a/Library/TintedImage.cpp b/Library/TintedImage.cpp index d6778099..9e4e59a0 100644 --- a/Library/TintedImage.cpp +++ b/Library/TintedImage.cpp @@ -44,8 +44,8 @@ public: } std::transform(key.begin(), key.end(), key.begin(), ::towlower); - _snwprintf_s(buffer, _TRUNCATE, L":%llx:%x", time, size); - key += buffer; + size_t len = _snwprintf_s(buffer, _TRUNCATE, L":%llx:%x", time, size); + key.append(buffer, len); return key; }