#include "stdafx.h" #include "Configuration.h" #include #include #include static bool TryParseHHMM(const std::string& text, int& outMinutes) { int hour = 0; int minute = 0; if (sscanf_s(text.c_str(), "%d:%d", &hour, &minute) != 2) return false; if (hour < 0 || hour >= 24) return false; if (minute < 0 || minute >= 60) return false; outMinutes = hour * 60 + minute; return true; } BOOL CConfiguration::getProductionShiftStartMinutes(int& dayStartMinutes, int& nightStartMinutes) { struct CachedShift { BOOL ok = FALSE; int day = 0; int night = 0; bool inited = false; }; static std::mutex s_mtx; static std::unordered_map s_cache; const std::string filePath((LPCSTR)(LPCTSTR)m_strFilepath); { std::lock_guard g(s_mtx); auto it = s_cache.find(filePath); if (it != s_cache.end() && it->second.inited) { dayStartMinutes = it->second.day; nightStartMinutes = it->second.night; return it->second.ok; } } char buf[64] = {}; GetPrivateProfileStringA("Production", "DayShiftStart", "08:00", buf, (DWORD)sizeof(buf), m_strFilepath); std::string dayStr(buf); GetPrivateProfileStringA("Production", "NightShiftStart", "", buf, (DWORD)sizeof(buf), m_strFilepath); std::string nightStr(buf); const int kDefaultDay = 8 * 60; const int kDefaultNight = 20 * 60; bool okDay = TryParseHHMM(dayStr, dayStartMinutes); bool okNight = false; if (!nightStr.empty()) okNight = TryParseHHMM(nightStr, nightStartMinutes); if (!okDay) dayStartMinutes = kDefaultDay; if (!okNight) nightStartMinutes = (dayStartMinutes + 12 * 60) % (24 * 60); if (dayStartMinutes == nightStartMinutes) { dayStartMinutes = kDefaultDay; nightStartMinutes = kDefaultNight; { std::lock_guard g(s_mtx); s_cache[filePath] = CachedShift{ FALSE, dayStartMinutes, nightStartMinutes, true }; } return FALSE; } const BOOL ok = (okDay && (nightStr.empty() ? TRUE : okNight)) ? TRUE : FALSE; { std::lock_guard g(s_mtx); s_cache[filePath] = CachedShift{ ok, dayStartMinutes, nightStartMinutes, true }; } return ok; }