【代码刷屏】Win32/64 INI 配置读取

没发现比较合意的现成实现,自己山寨了一个,Windows Only,代码刷屏,非礼勿视。

头文件

#ifndef CONFIG_H
#define CONFIG_H
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
typedef struct config_binding_a{
	char*  section;
	char*  key;
	void** val;
	void*  def;	
	int    type;
}CONFIG_BINDING_A, *PCONFIG_BINDING_A;

typedef struct config_binding_w{
	wchar_t* section;
	wchar_t* key;
	void**	 val;
	void*	 def;
	int      type;
}CONFIG_BINDING_W, *PCONFIG_BINDING_W;

extern BOOL config_load_a(HMODULE module,CONFIG_BINDING_A* binding);
extern BOOL config_load_w(HMODULE module,CONFIG_BINDING_W* binding);

#define CONFIG_TYPE_INT 1
#define CONFIG_TYPE_STRING 0
#ifdef UNICODE
#define CONFIG_BINDING  CONFIG_BINDING_W
#define CONFIG_BINDINGS CONFIG_BINDINGS_W
#define config_load config_load_w
#else
#define CONFIG_BINDING  CONFIG_BINDING_A
#define CONFIG_BINDINGS CONFIG_BINDINGS_A
#define config_load config_load_a
#endif
#ifdef __cplusplus
}
#endif //__cplusplus
#endif

实现代码

#include <config.h>
BOOL config_load_a(HMODULE module,CONFIG_BINDING_A* binding){
	char path[MAX_PATH];
	GetModuleFileNameA(module,path,MAX_PATH);
	for(int i=strlen(path)-1;i>=0;i--){
		if(path[i]=='\\'){
			path[i]='\0';
			break;
		}
	}
	strcat(path,"\\config.ini");
	PCONFIG_BINDING_A b;
	int i=0;
	char buffer[32767];
	int l;
	while((b=binding+(i++))->section){
		switch(b->type){
			case CONFIG_TYPE_STRING:
				l = GetPrivateProfileStringA(b->section,b->key,(char*)b->def,buffer,32767,path);
				if(buffer[0]==0&&b->def==NULL){
					*(b->val)=NULL;
				}else{
					*(b->val)=malloc(sizeof(char)*strlen(buffer));
					strcpy((char*)*(b->val),buffer);
				}
				break;
			case CONFIG_TYPE_INT:
				*((int*)(b->val)) = GetPrivateProfileIntA(b->section,b->key,(int)b->def,path);
				break;
		}
	}
	return TRUE;
}
BOOL config_load_w(HMODULE module,CONFIG_BINDING_W* binding){
	wchar_t path[MAX_PATH];
	GetModuleFileNameW(module,path,MAX_PATH);
	for(int i=wcslen(path)-1;i>=0;i--){
		if(path[i]==L'\\'){
			path[i]=L'\0';
			break;
		}
	}
	wcscat(path,L"\\config.ini");
	PCONFIG_BINDING_W b;
	int i=0;
	wchar_t buffer[32767];
	int l;
	while((b=binding+(i++))->section){
		switch(b->type){
			case CONFIG_TYPE_STRING:
				l = GetPrivateProfileStringW(b->section,b->key,(wchar_t*)b->def,buffer,32767,path);
				if(buffer[0]==0&&b->def==NULL){
					*(b->val)=NULL;
				}else{
					*(b->val)=malloc(sizeof(wchar_t)*wcslen(buffer));
					wcscpy((wchar_t*)*(b->val),buffer);
				}
				break;
			case CONFIG_TYPE_INT:
				*((int*)b->val) = GetPrivateProfileIntW(b->section,b->key,(int)b->def,path);
				break;
		}
	}
	return TRUE;
}

示例代码

#include "config.h"
char* db_host, *db_user, *db_pass, *db_name;
int db_port;
CONFIG_BINDING_A bindings[] = {
	{"db","host",(void**)&db_host},
	{"db","user",(void**)&db_user},
	{"db","pass",(void**)&db_pass},
	{"db","name",(void**)&db_name},
	{"db","port",(void**)&db_port,(void*)3306,CONFIG_TYPE_INT},
	NULL,
}
int _tmain(int argc, _TCHAR* argv[])
{
	config_load_a(GetModuleHandle(NULL),bindings);
	printf("mysql://%s:%d/%s;user=%s;pass=%s\n",db_host,db_port,db_name,db_user,db_pass);
}
>_<|| 我手贱!!这个有问题!绝对有问题!!一般般啦,真的很一般般。还不错哦~小表扬一下!GJ!乃就是新世界的神様了,快去拯救世界吧! (No Ratings Yet)
Loading...

2 人次吐槽

  1. Run说道:
    骑着 Firefox 3.5.4 Firefox 3.5.4 和 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4

  2. is36说道:
    骑着 Firefox 3.5.7 Firefox 3.5.7 和 Windows XP Windows XP
    Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7

    阅 2

春菜 对话 相声
双击调戏
双击调戏