#include "StdAfx.h" #include "TypeConvert.h" #include "FilePath.h" #include "initools.h" #include "UIHistory.h" #pragma region UIHistory bool UIHistory::hasCatchFile(LPCTSTR pszFileNameNoPath) { if (!pszFileNameNoPath) return false; CString str = FilePath::getProgramDataFolder() + _T("\\") + pszFileNameNoPath; return _taccess(str, 0x0) == 0x0; } CString UIHistory::getCacheFilePath(LPCTSTR pszFileNameNoPath, bool bCreate) { if (!pszFileNameNoPath) return _T(""); CString str = FilePath::getProgramDataFolder() + _T("\\") + pszFileNameNoPath;; // 如果不存在,则创建 if (!hasCatchFile(pszFileNameNoPath) && bCreate) { std::fstream fs; fs.open(str, std::ios::out); if (!fs.good()) return _T(""); fs.close(); } return str; } bool UIHistory::writeCache(LPCTSTR pszFileNameNoPath, LPCTSTR cmd, LPCTSTR format, ...) { if (!cmd || !format || !pszFileNameNoPath) return false; CString sFormat(format), sWildCard(_T("")), strItem(_T("")); sFormat.Replace(_T(" "), _T("")); int idx(sFormat.Find(_T('%'))), prec(3), i(0); std::map mapV; va_list vlist; va_start(vlist, format); while (idx != -1) { sWildCard = sFormat.Mid(idx, 2); idx = sFormat.Find(_T('%'), idx + 2); if (sWildCard.CompareNoCase(_T("%d")) == 0) // 整数 strItem = TypeConvert::i2s(va_arg(vlist, int)); else if (sWildCard.CompareNoCase(_T("%f")) == 0) // 浮点 strItem = TypeConvert::d2s(va_arg(vlist, double), &prec); else if (sWildCard.CompareNoCase(_T("%s")) == 0) // 字符串 strItem = va_arg(vlist, TCHAR*); else continue; mapV.insert(std::make_pair(TypeConvert::i2s(i), strItem)); ++i; } va_end(vlist); return IniTools::setSectionContent(getCacheFilePath(pszFileNameNoPath, true), cmd, mapV); } bool UIHistory::readCache(LPCTSTR pszFileNameNoPath, LPCTSTR cmd, LPCTSTR format, ...) { if (!cmd || !format ||!pszFileNameNoPath) return false; if (!hasCatchFile(pszFileNameNoPath)) return false; std::map mapV; if (!IniTools::getSectionContent(getCacheFilePath(pszFileNameNoPath, false), cmd, mapV)) return false; std::map::const_iterator it(mapV.begin()); CString sFormat(format), sWildCard(_T("")); sFormat.Replace(_T(" "), _T("")); std::wstring wformat(sFormat); int idx(sFormat.Find(_T('%'))), count((int)std::count(wformat.begin(), wformat.end(), _T('%'))); if (count != mapV.size()) return false; int i(0); va_list vlist; va_start(vlist, format); while (idx != -1) { it = mapV.find(TypeConvert::i2s(i)); sWildCard = sFormat.Mid(idx, 2); idx = sFormat.Find(_T('%'), idx + 2); if (sWildCard.CompareNoCase(_T("%d")) == 0) // 整数 *va_arg(vlist, int*) = TypeConvert::s2i(it->second); else if (sWildCard.CompareNoCase(_T("%f")) == 0) // 浮点 *va_arg(vlist, double*) = TypeConvert::s2d(it->second); else if (sWildCard.CompareNoCase(_T("%s")) == 0) // 字符串 *va_arg(vlist, CString*) = it->second; else continue; ++i; } va_end(vlist); return true; } #pragma endregion