ini 文件读写

        自己写的一个 ini 文件读写的 dll,c 语言写的,适合于小型 ini 文件的操作。做了简单的测试,估计还有 bug,如果想用的话最好自己先进行测试、修改,然后再使用。以下是代码。

头文件:

#ifndef __ini_h__
#define __ini_h__

#ifdef	DLLEXPORT
#define	DLLAPI	__declspec(dllexport)
#else
#define	DLLAPI	__declspec(dllimport)
#endif

#ifdef	__cplusplus
extern "C" {
#endif

	int		DLLAPI	ini_create_file		( const char *filename );
	int		DLLAPI	ini_open_file		( const char *filename );
	int		DLLAPI	ini_add_section		( const char *sectionname );
	int		DLLAPI	ini_add_key			( const char *sectionname, const char *keyname, const char *keyvalue );
	int		DLLAPI	ini_modify_section	( const char *sectionname, const char *section_newname );
	int		DLLAPI	ini_modify_key_name	( const char *sectionname, const char *keyname, const char *key_newname );
	int		DLLAPI	ini_modify_key_value( const char *sectionname, const char *keyname, const char *key_newvalue );
	int		DLLAPI	ini_delete_section	( const char *sectionname );
	int		DLLAPI	ini_delete_key		( const char *sectionname, const char *keyname );
	int		DLLAPI	ini_get_sections	( char *sections, unsigned int maxlen );
	int		DLLAPI	ini_get_keys		( const char *sectionname, char *keys, unsigned int maxlen );
	int		DLLAPI	ini_get_key_value	( const char *sectionname, const char *keyname, char *value, unsigned int maxlen );
	int		DLLAPI	ini_save_file		();
	void	DLLAPI	ini_clear_content	();
	void	DLLAPI	ini_free_file		();

#ifdef	__cplusplus
}
#endif

#endif // __ini_h__


源文件:

#define DLLEXPORT
#include "ini.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma warning( disable:4996 )

//

//
//	Key
//
typedef struct _ini_key {
	char				*_name;
	char				*_value;
	struct _ini_key		*_next;
} INIKEY, *PINIKEY;

//
//	Section
//
typedef struct _ini_section {
	char				*_name;
	PINIKEY				_key;
	struct _ini_section	*_next;
} INISECTION, *PINISECTION;

//
//	File
//
typedef struct _ini_file {
	char				*_name;
	PINISECTION			_section;
} INIFILE, *PINIFILE;


PINIFILE	g_inifile	= NULL;

//

void ini_parse_content( const char *buf )
{
	static char section_name[256]	= { 0 };
	char		name[1024]			= { 0 };
	char		value[1024]			= { 0 };
	int			len					= 0;
	int			i					= 0;

	if( NULL == buf || 3 > strlen(buf) )
		return;

	// section
	if( '[' == *buf ) {
		strcpy_s( name, 1024, buf + 1 );
		len = strlen( name );
		for( i = 0; i < len, ']' != name[i]; i++ );
		if( i == len )
			return;
		name[i] = '\0';
		ini_add_section( name );

		memset( section_name, 0, sizeof(char) * 256 );
		strcpy_s( section_name, 256, name );
	}
	// key
	else {
		// name
		strcpy_s( name, 1024, buf );
		len = strlen( name );
		for( i = 0; i < len, '=' != name[i]; i++ );
		if( i == len )
			return;
		name[i] = '\0';

		// value
		len = strlen( buf );
		for( i = 0; i < len, '=' != buf[i]; i++ );
		if( i == len )
			return;
		strcpy_s( value, 1024, buf + i + 1 );

		ini_add_key( section_name, name, value );
	}
}

//

int DLLAPI ini_create_file( const char *filename )
{
	if( NULL == filename )
		return 0;

	if( NULL != g_inifile )
		ini_free_file();

	// create a new "inifile" and set its name
	g_inifile = (PINIFILE)malloc( sizeof(INIFILE) );
	g_inifile->_name = (char*)malloc( sizeof(char) * (strlen(filename) + 1) );
	strcpy( g_inifile->_name, filename );
	g_inifile->_section = NULL;

	return 1;
}

int DLLAPI ini_open_file( const char *filename )
{
	FILE		*fp			= NULL;
	PINISECTION	section		= NULL;
	PINIKEY		key			= NULL;
	char		buf[1024]	= { 0 };

	if( NULL == filename )
		return 0;

	if( NULL != g_inifile ) {
		ini_save_file();
		ini_free_file();
	}

	fp = fopen( filename, "rt" );
	if( NULL == fp )
		return 0;

	// create a new "inifile" and set its name
	g_inifile = (PINIFILE)malloc( sizeof(INIFILE) );
	g_inifile->_name = (char*)malloc( sizeof(char) * (strlen(filename) + 1) );
	strcpy( g_inifile->_name, filename );
	g_inifile->_section = NULL;

	while( fgets(buf, 1024, fp) ) {
		ini_parse_content( buf );
		memset( buf, 0, sizeof(char) * 1024 );
	}

	fclose( fp );
	return 1;
}

int DLLAPI ini_add_section( const char *sectionname )
{
	PINISECTION	section		= NULL;
	PINISECTION	sectionnew	= NULL;

	if( NULL == g_inifile || NULL == sectionname )
		return 0;

	// create a new section
	sectionnew = (PINISECTION)malloc( sizeof(INISECTION) );
	sectionnew->_name = (char*)malloc( sizeof(char) * (strlen(sectionname) + 1) );
	strcpy( sectionnew->_name, sectionname );
	sectionnew->_key = NULL;
	sectionnew->_next = NULL;

	// the first section ?
	if( NULL == g_inifile->_section )
		g_inifile->_section = sectionnew;
	else {
		section = g_inifile->_section;
		while( NULL != section->_next ) {
			if( 0 == strcmp(section->_name, sectionname) )
				break;
			section = section->_next;
		}

		// if the section has been existed, free the new section and return zero
		if( NULL != section->_next || 0 == strcmp(section->_name, sectionname) ) {
			free( sectionnew->_name );
			free( sectionnew );
			return 0;
		}

		// add the new section to the end
		section->_next = sectionnew;
	}

	return 1;
}

int DLLAPI ini_add_key( const char *sectionname, const char *keyname, const char *keyvalue )
{
	PINIKEY		key		= NULL;
	PINIKEY		keynew	= NULL;
	PINISECTION	section	= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keyname )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	// create a new key
	keynew = (PINIKEY)malloc( sizeof(INIKEY) );
	keynew->_name = (char*)malloc( sizeof(char) * (strlen(keyname) + 1) );
	strcpy( keynew->_name, keyname );
	if( NULL == keyvalue )
		keynew->_value = NULL;
	else {
		keynew->_value = (char*)malloc( sizeof(char) * (strlen(keyvalue) + 1) );
		strcpy( keynew->_value, keyvalue );
	}
	keynew->_next = NULL;

	// the first key ?
	if( NULL == section->_key )
		section->_key = keynew;
	else {
		key = section->_key;
		while( NULL != key->_next ) {
			if( 0 == strcmp(key->_name, keyname) )
				break;
			key = key->_next;
		}

		// the key has been existed ?
		if( NULL != key->_next || 0 == strcmp(key->_name, keyname) ) {
			free( keynew->_name );
			free( keynew->_value );
			free( keynew );
			return 0;
		}

		// add the key to the end
		key->_next = keynew;
	}

	return 1;
}

int DLLAPI ini_modify_section( const char *sectionname, const char *section_newname )
{
	PINISECTION section = NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == section_newname )
		return 0;

	if( 0 == strcmp(sectionname, section_newname) )
		return 1;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;
	
	// modify the section's name
	free( section->_name );
	section->_name = (char*)malloc( sizeof(char) * (strlen(section_newname) + 1) );
	strcpy( section->_name, section_newname );
	return 1;
}

int DLLAPI ini_modify_key_name( const char *sectionname, const char *keyname, const char *key_newname )
{
	PINISECTION	section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keyname || NULL == key_newname )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	// find the key
	key = section->_key;
	while( NULL != key ) {
		if( 0 == strcmp(key->_name, keyname) )
			break;
		key = key->_next;
	}

	if( NULL == key )
		return 0;

	// modify the key's name
	free( key->_name );
	key->_name = (char*)malloc( sizeof(char) * (strlen(key_newname) + 1) );
	strcpy( key->_name, key_newname );

	return 1;
}

int DLLAPI ini_modify_key_value( const char *sectionname, const char *keyname, const char *key_newvalue )
{
	PINISECTION section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keyname )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	// find the key
	key = section->_key;
	while( NULL != key ) {
		if( 0 == strcmp(key->_name, keyname) )
			break;
		key = key->_next;
	}

	if( NULL == key )
		return 0;

	// if the key's value equals to key_newvalue, return
	if( 0 == strcmp(key->_value, key_newvalue) )
		return 1;

	// modify the key's value
	free( key->_value );
	key->_value = NULL;
	if( NULL != key_newvalue ) {
		key->_value = (char*)malloc( sizeof(char) * (strlen(key_newvalue) + 1) );
		strcpy( key->_value, key_newvalue );
	}

	return 1;
}

int DLLAPI ini_delete_section( const char *sectionname )
{
	PINISECTION	section_cur	= NULL;
	PINISECTION	section_prv	= NULL;
	PINIKEY		key			= NULL;

	if( NULL == g_inifile || NULL == sectionname )
		return 0;

	// find the section to be deleted
	section_cur = g_inifile->_section;
	while( NULL != section_cur && 0 != strcmp(section_cur->_name, sectionname) ) {
		section_prv = section_cur;
		section_cur = section_cur->_next;
	}

	if( NULL == section_cur )
		return 0;

	// delete the section from the link
	if( NULL == section_prv )
		g_inifile->_section = section_cur->_next;
	else
		section_prv->_next = section_cur->_next;

	// free the section's keys
	while( NULL != section_cur->_key ) {
		key = section_cur->_key->_next;
		free( section_cur->_key->_name );
		free( section_cur->_key->_value );
		free( section_cur->_key );
		section_cur->_key = key;
	}

	// free the section's name
	free( section_cur->_name );

	// free the section
	free( section_cur );

	return 1;
}

int DLLAPI ini_delete_key( const char *sectionname, const char *keyname )
{
	PINISECTION	section	= NULL;
	PINIKEY		key_cur	= NULL;
	PINIKEY		key_prv	= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keyname )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		section = g_inifile->_section;
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	// find the key to be deleted
	key_cur = section->_key;
	while( NULL != key_cur && 0 != strcmp(key_cur->_name, keyname) ) {
		key_prv = key_cur;
		key_cur = key_cur->_next;
	}

	if( NULL == key_cur )
		return 0;

	// delete the key from the link
	if( NULL == key_prv )
		section->_key = key_cur->_next;
	else
		key_prv->_next = key_cur->_next;

	// free the key
	free( key_cur->_name );
	free( key_cur->_value );
	free( key_cur );

	return 1;
}

int DLLAPI ini_get_sections( char *sections, unsigned int maxlen )
{
	int			rst		= 0;
	PINISECTION	section = NULL;

	if( NULL == g_inifile || NULL == g_inifile->_section || NULL == sections || 0 == maxlen )
		return 0;

	// the first section's name
	memset( sections, 0, sizeof(char) * maxlen );
	strcpy_s( sections, maxlen, g_inifile->_section->_name );
	rst++;

	section = g_inifile->_section->_next;
	while( NULL != section ) {
		strcat_s( sections, maxlen, "|" );
		strcat_s( sections, maxlen, section->_name );
		section = section->_next;
		rst++;
	}

	return rst;
}

int DLLAPI ini_get_keys( const char *sectionname, char *keys, unsigned int maxlen )
{
	int			rst		= 0;
	PINISECTION	section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keys || 0 == maxlen )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	if( NULL == section->_key )
		return 0;

	// the first key's name
	strcpy_s( keys, maxlen, section->_key->_name );
	rst++;

	key = section->_key->_next;
	while( NULL != key ) {
		strcat_s( keys, maxlen, "|" );
		strcat_s( keys, maxlen, key->_name );
		key = key->_next;
		rst++;
	}

	return rst;
}

int DLLAPI ini_get_key_value( const char *sectionname, const char *keyname, char *value, unsigned int maxlen )
{
	PINISECTION	section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile || NULL == sectionname || NULL == keyname || NULL == value || 0 == maxlen )
		return 0;

	// find the section
	section = g_inifile->_section;
	while( NULL != section ) {
		if( 0 == strcmp(section->_name, sectionname) )
			break;
		section = section->_next;
	}

	if( NULL == section )
		return 0;

	// find the key
	key = section->_key;
	while( NULL != key ) {
		if( 0 == strcmp(key->_name, keyname) )
			break;
		key = key->_next;
	}

	if( NULL == key )
		return 0;

	// get the key's value
	if( NULL == key->_value )
		strcpy( value, "" );
	else
		strcpy_s( value, maxlen, key->_value );

	return 1;
}

int DLLAPI ini_save_file()
{
	FILE		*fp		= NULL;
	PINISECTION	section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile )
		return 0;

	fp = fopen( g_inifile->_name, "w+t" );
	if( NULL == fp )
		return 0;

	section = g_inifile->_section;
	while( NULL != section ) {
		fwrite( "[", sizeof(char), strlen("["), fp );
		fwrite( section->_name, sizeof(char), strlen(section->_name), fp );
		fwrite( "]\r\n", sizeof(char), strlen("]\r\n"), fp );
		fflush( fp );

		key = section->_key;
		while( NULL != key ) {
			fwrite( key->_name, sizeof(char), strlen(key->_name), fp );
			fwrite( "=", sizeof(char), strlen("="), fp );
			if( NULL != key->_value )
				fwrite( key->_value, sizeof(char), strlen(key->_value), fp );
			fwrite( "\r\n", sizeof(char), strlen("\r\n"), fp );
			fflush( fp );
			key = key->_next;
		}

		fwrite( "\r\n", sizeof(char), strlen("\r\n"), fp );
		fflush( fp );
		section = section->_next;
	}

	fclose( fp );
	return 1;
}

void DLLAPI ini_clear_content()
{
	PINISECTION	section	= NULL;
	PINIKEY		key		= NULL;

	if( NULL == g_inifile )
		return;

	section = g_inifile->_section;
	while( NULL != section ) {
		// get the next section
		section = section->_next;

		// free the section's name
		free( g_inifile->_section->_name );

		// free the section's keys
		key = g_inifile->_section->_key;
		while( NULL != key ) {
			key = key->_next;
			free( g_inifile->_section->_key->_name );
			free( g_inifile->_section->_key->_value );
			free( g_inifile->_section->_key );
			g_inifile->_section->_key = key;
		}

		// free the section
		free( g_inifile->_section );
		g_inifile->_section = section;
	}

	g_inifile->_section = NULL;
}

void DLLAPI ini_free_file()
{
	if( NULL == g_inifile )
		return;

	if( NULL != g_inifile->_section )
		ini_clear_content();

	// free the file's name
	free( g_inifile->_name );

	// free the file
	free( g_inifile );
	g_inifile = NULL;
}


<完>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值