Programming - cpueblo.com

IniFile 구현


글쓴이 : 유광희 날짜 : 2011-02-11 (금) 16:31 조회 : 17961
	namespace	IniFile
	{
		// ReadSectionValues :: Reads the values from all keys within a section of an INI file into a string list.
		void	ReadSectionValues	(const tstring sFileName,	const tstring sSectionName, vector  *lst);
		void	ReadSectionValues	(const tstring sFileName,	const tstring sSectionName, vector > *lst);

		int		ReadInteger			(const tstring sFileName,	const tstring sSection,	const tstring sName,	const int DefaultValue);
		BOOL	ReadBool			(const tstring sFileName,	const tstring sSection,	const tstring sName,	const BOOL DefaultValue);
		tstring	ReadString			(const tstring sFileName,	const tstring sSection,	const tstring sName,	const tstring sDefaultValue);

		BOOL	WriteString			(const tstring sFileName,	const tstring sSection,	const tstring sName,	const tstring Value);
		BOOL	WriteInteger		(const tstring sFileName,	const tstring sSection,	const tstring sName,	const int Value);

		int		ReadIntergerA		(const string sFileName,	const string sSection,	const string sName,	const int DefaultValue);
		BOOL	ReadBoolA			(const string sFileName,	const string sSection,	const string sName,	const BOOL DefaultValue);
		string	ReadStringA			(const string sFileName,	const string sSection,	const string sName,	const string sDefaultValue);

		BOOL	WriteIntegerA		(const string sFileName,	const string sSection,	const string sName,	const int dwValue);		
	};	};
	namespace	IniFile
	{
		//-------------------------------------------------------------------------------------------------------------
		void	ReadSectionValues(const tstring sFileName, const tstring sSectionName, vector  *lst)
		{
			const DWORD dwSize = 4096;
			TCHAR *pszSections = new TCHAR [dwSize];
			TCHAR *pszValue = new TCHAR [dwSize];
			lst->clear();

			GetPrivateProfileString(sSectionName.c_str(), NULL, _T(""), pszSections, dwSize, sFileName.c_str());

			for (TCHAR *i= pszSections; *i;i+= ::_tcslen(i)+ 1)
			{
				::GetPrivateProfileString(sSectionName.c_str(), i, _T(""), pszValue, dwSize*50, sFileName.c_str());

				tstring sValue = i + (tstring)_T("=") + (tstring)pszValue;
				lst->push_back(sValue);
			}

			delete []pszSections;
			delete []pszValue;
		}
		//-------------------------------------------------------------------------------------------------------------
		void	ReadSectionValues(const tstring sFileName, const tstring sSectionName, vector > *lst)
		{
			const DWORD dwSize = 4096;
			TCHAR *pszSections = new TCHAR [dwSize];
			TCHAR *pszValue = new TCHAR [dwSize];
			lst->clear();

			GetPrivateProfileString(sSectionName.c_str(), NULL, _T(""), pszSections, dwSize, sFileName.c_str());

			for (TCHAR *i= pszSections; *i;i+= ::_tcslen(i)+ 1)
			{
				::GetPrivateProfileString(sSectionName.c_str(), i, _T(""), pszValue, dwSize*50, sFileName.c_str());

				pair p;
				p.first = i;
				p.second = pszValue;
				lst->push_back(p);
			}

			delete []pszSections;
			delete []pszValue;
		}
		//-------------------------------------------------------------------------------------------------------------
		int		ReadInteger(const tstring sFileName, const tstring sSection, const tstring sName, const int DefaultValue)
		{
			return GetPrivateProfileInt(sSection.c_str(), sName.c_str(), DefaultValue, sFileName.c_str());
		}
		//-------------------------------------------------------------------------------------------------------------
		int		ReadIntergerA(const string sFileName, const string sSection, const string sName, const int DefaultValue)
		{
			return GetPrivateProfileIntA(sSection.c_str(), sName.c_str(), DefaultValue, sFileName.c_str());
		}
		//-------------------------------------------------------------------------------------------------------------
		BOOL	ReadBool(const tstring sFileName, const tstring sSection, const tstring sName, const BOOL DefaultValue)
		{
			tstring s = ReadStrValueFromIni(sFileName, sSection, sName, _T(""));
			if (s.empty())
				return DefaultValue;

			if (s.length() >= 4 && !_tcsnicmp(_T("TRUE"), s.c_str(), 4))
				return TRUE;

			return FALSE;
		}
		//-------------------------------------------------------------------------------------------------------------
		BOOL	ReadBoolA(const string sFileName, const string sSection, const string sName, const BOOL DefaultValue)
		{
			string s = ReadStrValueFromIniA(sFileName, sSection, sName, "");
			if (s.empty())
				return DefaultValue;

			if (s.length() >= 4 && !strnicmp("TRUE", s.c_str(), 4))
				return TRUE;

			return FALSE;
		}
		//-------------------------------------------------------------------------------------------------------------
		string	ReadStringA(const string sFileName, const string sSection, const string sName, const string sDefaultValue)
		{
			char szValue[MAX_PATH] = {0, };
			if (::GetPrivateProfileStringA(sSection.c_str(), sName.c_str(), sDefaultValue.c_str(), szValue, MAX_PATH, sFileName.c_str()) == 0)
				return "";

			return szValue;
		}
		//-------------------------------------------------------------------------------------------------------------
		tstring	ReadString(const tstring sFileName, const tstring sSection, const tstring sName, const tstring sDefaultValue)
		{
			TCHAR szValue[MAX_PATH] = {0, };
			if (::GetPrivateProfileString(sSection.c_str(), sName.c_str(), sDefaultValue.c_str(), szValue, MAX_PATH, sFileName.c_str()) == 0)
				return _T("");

			return szValue;
		}
		//-------------------------------------------------------------------------------------------------------------
		BOOL	WriteInteger(const tstring sFileName, const tstring sSection, const tstring sName, const int Value)
		{
			tstring s = str_format(_T("%d"), Value);
			return WritePrivateProfileString(sSection.c_str(), sName.c_str(), s.c_str(), sFileName.c_str());
		}
		//-------------------------------------------------------------------------------------------------------------
		BOOL	WriteIntegerA	(const string sFileName,	const string sSection,		const string sName,	const int dwValue)
		{
			string s = str_formatA("%d", dwValue);
			return WritePrivateProfileStringA(sSection.c_str(), sName.c_str(), s.c_str(), sFileName.c_str());
		}
		//-------------------------------------------------------------------------------------------------------------
		BOOL	WriteString(const tstring sFileName, const tstring sSection, const tstring sName, const tstring Value)
		{
			return WritePrivateProfileString(sSection.c_str(), sName.c_str(), Value.c_str(), sFileName.c_str());
		}
		//-------------------------------------------------------------------------------------------------------------

	}