⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inifile.h

📁 一个自己写的游戏引擎,用DirectX 写成
💻 H
字号:
//--------------------------------------------------
//  Desc: INI config file operation
//  Date: 2006.7.24 /update
//  Author: artsylee
//
//  Copyright (C) 2006 artsylee
//
//  扩展: 1, 项的后面,值的前面或后面可以有空格或TAB(2006_10_29)
//        2, 无法正常读取常用数据类型时有默认值返回.(2006_10_29)
//		  3, 读取字符串时提供一种不用释放的方式.(2006_10_29)
//		  4, 获取索引的连续项.(2006_11_08)
//		  5, 读取连续项数据(INT & STRING).(2006_11_08)
//		  6, 读写unsigned int(DWORD)类型(2006_11_08)
//		  7, 获取索引数量(2006_11_20)
//		  8, 根据索引号获取索引(2006_11_20)
//		  9, 条件编译可以不带LOG.h(2006-12-10)
//		 10, (私有, 可减少重复代码)获取下一个索引的位置, 直至文件尾(2007_3_10) 
//		 11, 现在写数据是写在现有项的后面, 原来的是写在现有项的前面(2007_3_10)
//		 12, 使用const(2007_3_26)
//		 13, 读写Float类型的Vec2D和Vec3D(2007_3_26)
//		 14, 每个读取操作之后会记录是成功读取还是返回默认值(2007_6_9)
//--------------------------------------------------

#ifndef _INIFILE_
#define _INIFILE_

struct Vec2D
{
	float x;
	float y;
#ifdef __cplusplus
	Vec2D(){};
	Vec2D(float fx, float fy)
	{
		x = fx;
		y = fy;
	}
#endif
};

struct Vec3D
{
	float x;
	float y;
	float z;
#ifdef __cplusplus
	Vec3D(){};
	Vec3D(float fx, float fy, float fz)
	{
		x = fx;
		y = fy;
		z = fz;
	}
#endif
};

enum INIRETURN
{
	RET_FAILED = 0,
	RET_SUCCEED = 1,
};

class ASE_DLL CIniFile
{
public:
	CIniFile(void);
	CIniFile(const char *pFileName);
	~CIniFile(void);

	bool Open(const char *pFileName);
	bool Save(const char *pFileName=NULL);
	// 2006_11_20-------------
	int		GetIndexNum(void) const;
	char*	GetIndex(const int &indexnum, char *buf) const;
	// -----------------------

	bool	ReadBOOL(const char *indexname, const char *dataname, const bool bValue = false);
	int		ReadInt(const char *indexname, const char *dataname, const int value = 0);
	unsigned ReadDWORD(const char *indexname, const char *dataname, const unsigned value = 0);
	float	ReadFloat(const char *indexname, const char *dataname, const float fvalue = 0.0f);
	char	*ReadString(const char *indexname, const char *dataname);
	char    *ReadString(const char *indexname, const char *dataname, char *buf, const char *szValue = "");
	int		ReadBinary(const char *indexname, const char *dataname, unsigned char *pData, 
				   const unsigned nBytes);
	POINT	ReadPoint(const char *indexname, const char *dataname);
	RECT	ReadRect(const char *indexname, const char *dataname);
	Vec2D	ReadVec2D(const char *indexname, const char *dataname, 
					  const Vec2D &vec = Vec2D(0.0f, 0.0f));
	Vec3D	ReadVec3D(const char *indexname, const char *dataname,
					  const Vec3D &vec = Vec3D(0.0f, 0.0f, 0.0f));

	bool WriteBOOL(const char *indexname, const char *dataname, const bool bValue);
	bool WriteInt(const char *indexname, const char *dataname, const int value);
	bool WriteDWORD(const char *indexname, const char *dataname, const unsigned value);
	bool WriteFloat(const char *indexname, const char *dataname, const float fvalue);
	bool WriteString(const char *indexname, const char *dataname, const char *szValue);
	bool WriteBinary(const char *indexname, const char *dataname, const unsigned char *pData, 
		             const unsigned nBytes);
	bool WritePoint(const char *indexname, const char *dataname, const POINT &pt);
	bool WriteRect(const char *indexname, const char *dataname, const RECT &rc);
	bool WriteVec2D(const char *indexname, const char *dataname, const Vec2D &vec);
	bool WriteVec3D(const char *indexname, const char *dataname, const Vec3D &vec);

	// 2006_11_08-------------
	int		GetContinueData(const char *indexname) const;							//获取一个索引连续的数据项
	int		ReadInt(const char *indexname, const int linenum, const int value = 0);
	int		ReadString(const char *indexname, const int linenum, char *buf);		//返回数据长度
	char	*ReadDataName(const char *indexname, const int linenum) const;			//需要释放
	// -----------------------

	// 是否成功读取(2007_6_9)
	BOOL	IsReadSucceed(void)	{	return m_Return;	}

private:	
	char *GetString(const int &position) const;
	char *GetString(int *position) const;
	// 2006_10_29-------------
	char *CutStringFront(char *pString) const;
	char *CutStringBack(char *pString) const;
	// -----------------------
	int FindIndex(const char *indexname) const;
	int FindDataName(const char *indexname, const char *dataname) const;
	void CreateIndex(void);

	int NextLinePos(const int &oldposition) const;
	// 2007_03_10-------------
	int NextIndexPos(const int &pos) const;
	// -----------------------

	bool AddIndex(const char *indexname);
	bool AddData(const int &pos, const char *dataname, const char *szValue);
	bool ModifyData(const int &pos, const char *dataname, const char *szValue);
	
	char			*m_pData;
	int 			m_nDataLen;
	char            m_FileName[MAX_PATH];
	int             *m_Index;
	int             m_IndexNum;

	INIRETURN		m_Return;
};

#endif // _INIFILE 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -