INI文件操作类

         阅读(526)  2017-04-15 12:20:07


#ifndef _LEARN_OBJECT_H_
#define _LEARN_OBJECT_H_

class CIniFile
{
public:
	CIniFile()
	{
	}
	~CIniFile() { }

private:
	wchar_t		m_path[_MAX_FNAME];

public:
	void InitConfigPath(const wchar_t *filePath)
	{
		memset(m_path, 0, sizeof(m_path));
		wcscpy(m_path, filePath);
	}

	// 获取字符串, 返回字符串个数
	int GetObjValue(const wchar_t *section, const wchar_t *firstKey, wchar_t* outValue, int size)
	{
		int nCount = 0;

		if (section && firstKey)
		{
			memset(outValue, 0, sizeof(outValue));
			nCount = GetPrivateProfileString(section, firstKey, L"0", outValue, size, m_path);
		}
		
		return nCount;
	}

	// 获取int型数值, 返回值即获取值
	int GetObjValue(const wchar_t *section, const wchar_t *firstKey)
	{
		int nRet = 0;
		if (section && firstKey)
		{
			nRet = GetPrivateProfileInt(section, firstKey, 0, m_path);
		}

		return nRet;
	}

	// 获取坐标如point=14,13, 返回值高位为x轴坐标,低位为y轴坐标
	DWORD GetObjValuePoint(const wchar_t *section, const wchar_t *firstKey)
	{
		wchar_t			outValue[16] = {0};
		int				nLen = 0;
		int				nCurPos = 0;
		int				x = 0;
		int				y = 0;
		DWORD			dwRet = 0;

		GetPrivateProfileString(section, firstKey, L"0", outValue, 16, m_path);
		nLen = wcslen(outValue);

		for (int i = 0; i < nLen; i++)
		{
			if (outValue[i] == ',')
			{
				outValue[i] = '\0';
				break;
			}
			nCurPos++;
		}

		x = _ttoi(outValue);
		y = _ttoi(&outValue[++nCurPos]);

		dwRet = MAKELONG(y, x);
		return dwRet;
	
	}

	BOOL SetObjValue(const wchar_t *section, const wchar_t *firstKey, const wchar_t* inValue)
	{
		BOOL bRet = FALSE;

		if (section && firstKey)
		{
			bRet = WritePrivateProfileString(section, firstKey, inValue, m_path);
		}

		return bRet;
	}

	BOOL SetObjValue(const wchar_t *section, const wchar_t *firstKey, int nValue)
	{
		BOOL bRet = FALSE;

		if (section && firstKey && (nValue > 0))
		{
			wchar_t szValue[8] = {0};
			swprintf(szValue, L"%d", nValue);
			bRet = WritePrivateProfileString(section, firstKey, szValue, m_path);
		}

		return bRet;
	}

	BOOL WriteObjSection(const wchar_t *section, const wchar_t *string)
	{
		int bRet = FALSE;
		if (!(section && string))
		{
			return FALSE;
		}

		bRet = WritePrivateProfileSection(section, string, m_path);
		return bRet;
	}

	BOOL DeleteObjSection(const wchar_t *section)
	{
		BOOL bRet = FALSE;
		if (section)
		{
			bRet = WritePrivateProfileString(section, NULL, NULL, m_path);
		}

		return bRet;
	}
};

#endif // _LEARN_OBJECT_H_

迁移2011-05-08

文章评论

Keep it simple,stupid
文章数
284
总访问量
263210
今日访问
123
最近评论

ningto : 请到next.ningto.com里发表评论。
tujiaw : 抱歉csdn code服务关闭了,这个代码我也找不到了
于淞 : 你好,这个文章的源码能分享一下吗,songsong9181@163.com,谢谢了 上面的写错了
于淞 : 你好,这个文章的源码能分享一下吗,838106303@163.com,谢谢了 上面的链接不能用了
tujiaw : 多谢多谢
essaypinglun college-paper.org : 很好的博客,赞赞
Folly : 这个实现有点奇怪,Qt为什么没有统一的比对方法。
过多s : alert("hello, world!")
tujiaw : 还不错哦
回到顶部