[HGE]-源码分析-8 ini
来源:互联网 发布:长沙知豆电动汽车租赁 编辑:程序博客网 时间:2024/05/01 23:55
ini,配置文件相关的函数,配置文件是个好东西,特别对于大型的项目,或者资源文件特别多的,或者是服务器端这种重启一下要很久的程序,只要有reload函数,动态的在程序里载入配置,对于写在配置里的东西就可以直接改变了。
因此,推荐一些常变的东西写在配置或者脚本里,会方便特别多,甚至可以直接交给策划写,想怎么变都随他,再也不用担心一个东西返工好多次。
ini里的函数都很简单,所有的都是成对出现的(一个set,一个get),写入和读取都有。
void CALL HGE_Impl::Ini_SetInt(const char *section, const char *name, int value){char buf[256];if(szIniFile[0]) {sprintf(buf,"%d",value);WritePrivateProfileString(section, name, buf, szIniFile);}}int CALL HGE_Impl::Ini_GetInt(const char *section, const char *name, int def_val){char buf[256];if(szIniFile[0]) {if(GetPrivateProfileString(section, name, "", buf, sizeof(buf), szIniFile)){ return atoi(buf); }else { return def_val; }}return def_val;}
这些函数要说的没什么,全部都是依靠两个函数实现的,WritePrivateProfileString和GetPrivateProfileString
想了一下最直观的方法就是放一个例子在这里。
用这两个函数的配置文件是这样的格式:
[NETWORK]
ServerIP=100.100.100.53
其中[]里的代表section
SeverIP代表name
ip地址代表要写入或者读取的buf
要独取上面的ini只要把ini放在程序同一个目录,然后调用GetPrivateProfileString("NETWORK","ServerIP","", buf,sizeof(ip), "Server.ini")
读取出来的是string,需要其他类型转换就可以。例如这里掉用atoi转成int
其他的几个都类似
void CALL HGE_Impl::Ini_SetFloat(const char *section, const char *name, float value){char buf[256];if(szIniFile[0]) {sprintf(buf,"%f",value);WritePrivateProfileString(section, name, buf, szIniFile);}}float CALL HGE_Impl::Ini_GetFloat(const char *section, const char *name, float def_val){char buf[256];if(szIniFile[0]) {if(GetPrivateProfileString(section, name, "", buf, sizeof(buf), szIniFile)){ return (float)atof(buf); }else { return def_val; }}return def_val;}void CALL HGE_Impl::Ini_SetString(const char *section, const char *name, const char *value){if(szIniFile[0]) WritePrivateProfileString(section, name, value, szIniFile);}char* CALL HGE_Impl::Ini_GetString(const char *section, const char *name, const char *def_val){if(szIniFile[0]) GetPrivateProfileString(section, name, def_val, szIniString, sizeof(szIniString), szIniFile);else strcpy(szIniString, def_val);return szIniString;}
配置文件这块还有一部分文件管理的,不过不属于读写这一块。
- [HGE]-源码分析-8 ini
- [HGE]-源码分析-10 resource
- [HGE]-源码分析-11 sound
- [HGE]-源码分析-1 引擎源码构成
- [HGE]-源码分析-2 一个小demo
- [HGE]-源码分析-3 Core部分
- [HGE]-源码分析-9 input,power,random
- [HGE]-源码分析-12 system (a)
- [HGE]-源码分析-13system (b)
- [HGE]-源码分析-14 system (c)
- [HGE]-源码分析-15 最后的timer
- [HGE]-源码分析-5 core里的graphics(b)
- [HGE]-源码分析-6 core里的graphics(c)
- [HGE]-源码分析-7 core里的graphics(d)
- [HGE]-源码分析-4 core里的demo和graphics(a)
- 写在学习HGE源码之前
- hge源码注释 1 hge引擎的启动(1)
- hge源码注释 2 hge引擎的启动(2)
- 实现文件拖放的一种简洁方法
- ubuntu系统下adb连接小米2[无需装驱动]
- asp
- LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
- 心有所思
- [HGE]-源码分析-8 ini
- USACO American Heritage 解题报告
- 左连接、右连接、交叉连接、全外连接
- 华东交通大学2013年ACM“双基”程序设计竞赛 解题报告
- 软件高手是怎么样炼成的?
- 用java源代码学数据结构<三>:ArrayList 详解
- 函数
- boost::asio发送与传输相关的几个函数,备忘
- Leetcode Pascal's Triangle II