IniFile Read Write

#include <boost/shared_ptr.hpp>
#include <QSettings>
class IniFile
{
public:
	explicit IniFile(const char * path):m_path(path){}
	~IniFile(){};
	QString GetPath() const {return m_path;}
	void SetPath(const char * path) {if(m_path != path) m_path = path;}
	QString GetValue(const char * key)
	{
		if(key == NULL)
		{
			return QString();
		}
		if(m_pSetting == NULL)
		{
			// create setting failed
			if(CreateSetting() != 0)
			{
				return QString();
			}
		}
		return m_pSetting->value(key).toString();
	}
	int SetValue(const char *  key, const char *  value)
	{
		if(key == NULL)
		{
			return  -1;
		}
		
		if(m_pSetting == NULL)
		{
			if(CreateSetting() != 0)
			{
				return -1;
			}
		}
		m_pSetting->setValue(key, value);
		return 0;
	}
	int Remove(const char * key)
	{
		if(key == NULL) 
		{
			return -1;
		}
		if(m_pSetting == NULL)
		{
			// create setting failed
			if(CreateSetting() != 0)
			{
				return -1;
			}
		}
		this->m_pSetting->remove(key);
		return 0;
	}
protected:
	int CreateSetting()
	{
		if(m_path == NULL)
		{
			return -1;
		}
		m_pSetting.reset(new QSettings(m_path, QSettings::IniFormat));
		return 0;
	}
private:
	const char * m_path;
	boost::shared_ptr<QSettings> m_pSetting;
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章