#include "stdafx.h" #include "ConfigVision.h" #include #include // °üº¬CStringÍ·Îļþ #include #include #include #include #include #include "FileRecipe.h" #pragma comment(lib,"Dbghelp.lib") CConfigVision::CConfigVision(){ /* code */ } CConfigVision::~CConfigVision(){ /* code */ } void CConfigVision::SetWorkDir(const char *pszPath, const char *pszName) { /* code */ std::string filePath = pszPath; std::string strName = pszName; init(filePath, pszName); } void CConfigVision::init(std::string filePath, std::string strName) { /* code */ std::string fileName = filePath + "\\" + strName; if (!CFileRecipe::fileIsExist(fileName)) { MakeSureDirectoryPathExists(fileName.c_str()); } std::string str = fileName + "\\Config.ini"; m_strFileName = CA2T(str.c_str()); CStdioFile file; if (!file.Open(m_strFileName, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate)) return; file.Close(); } bool CConfigVision::WriteText(const char *pszSection, const char *pszName, const char *pszValue) { /* code */ CString strSection = CString(pszSection); CString strName = CString(pszName); CString strValue = CString(pszValue); BOOL isRes = WritePrivateProfileString(strSection, strName, strValue, m_strFileName); if (!isRes) return false; return true; } std::string CConfigVision::ReadText(const char *pszSection, const char *pszName, const char *pszDefault) { /* code */ std::string strRes = pszDefault; CString strValue; CString strSection = CString(pszSection); CString strName = CString(pszName); CString strDefault = CString(pszName); GetPrivateProfileString(strSection, strName, strDefault, strValue.GetBuffer(MAX_PATH), MAX_PATH, m_strFileName); strRes = CT2A(strValue); return strRes; } bool CConfigVision::WriteInt(const char *pszSection, const char *pszName, int intValue) { /* code */ CString strSection = CString(pszSection); CString strName = CString(pszName); CString strValue; strValue.Format(_T("%d"), intValue); BOOL isRes = WritePrivateProfileString(strSection, strName, strValue, m_strFileName); if (!isRes) return false; return true; } int CConfigVision::ReadInt(const char *pszSection, const char *pszName, int defaultValue) { /* code */ int intRes = defaultValue; CString strSection = CString(pszSection); CString strName = CString(pszName); intRes = GetPrivateProfileInt(strSection, strName, defaultValue, m_strFileName); return intRes; } bool CConfigVision::WriteBool(const char *pszSection, const char *pszName, int bValue) { /* code */ int iValue = 0; if (bValue) iValue = 1; return WriteInt(pszSection, pszName, iValue); } bool CConfigVision::ReadBool(const char *pszSection, const char *pszName, bool deaultBool) { /* code */ int defaultInt = 0; if (deaultBool) defaultInt = 1; int nRet = ReadInt(pszSection, pszName, defaultInt); bool res = (1 == nRet); return res; } bool CConfigVision::WriteFloat(const char *pszSection, const char *pszName, double fValue) { /* code */ CString strSection = CString(pszSection); CString strName = CString(pszName); CString strValue; strValue.Format(_T("%.6f"), fValue); BOOL isRes = WritePrivateProfileString(strSection, strName, strValue, m_strFileName); if (!isRes) return false; return true; } double CConfigVision::ReadFloat(const char *pszSection, const char *pszName, double defaultValue) { /* code */ CString strSection = CString(pszSection); CString strName = CString(pszName); CString strDefault; strDefault.Format(_T("%.6f"), defaultValue); CString strValue; GetPrivateProfileString(strSection, strName, strDefault, strValue.GetBuffer(MAX_PATH), MAX_PATH, m_strFileName); std::string strRes = CT2A(strValue); double fValue = atof(strRes.c_str()); return fValue; }