配置文件的一些接口API

#include <stdio.h>
#include <malloc.h>
#include <string.h>


#define alloca _alloca

#define MAX_CHAR_PER_LINE 4096

char *Trim(char *str)
{
	int i,j;

	if(str==NULL)
		return NULL;

	i = strlen(str);
	for(j=0; j<i; j++)
	{
		if(*(str+j)!=' ')
			break;
	}
	do
	{
		i--;
		if(*(str+i)==' ' || *(str+i)=='\n')
			*(str+i) = 0;
		else
			break;
	}while(i);
	return str+j;
}

int SpiltString(char *str, char *delimiter, char *values[], int size)
{
	char *p_temp = NULL;
	int res = 0;

	if(str==NULL || delimiter==NULL || values==NULL || size<=0)
	{
		return 0;
	}

	p_temp = strtok(str, delimiter);
	//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
	if(!p_temp)
	{
		return 0;
	}
	p_temp = Trim(p_temp);
	values[res++] = p_temp;

	while( (p_temp=strtok(NULL, delimiter))!=NULL )
	{
		p_temp = Trim(p_temp);
		values[res++] = p_temp;
		if(res>=size)
			break;
	}

	return res;
}

int ReadConfig(char *cfgFile, char *key, char *value, int size)
{
	FILE *fptr;
	char str_line[MAX_CHAR_PER_LINE];
	char *p_temp = NULL;
	int res = 0;

	if((NULL == key) || (NULL == value))
	{
		return -1;
	}

	if ( ( fptr = fopen(cfgFile, "r" ) ) == NULL ) 
	{
		//cprintf( "\ropen %s error!\n", cfgFile );
		return -1;
	}

	while(fgets(str_line, MAX_CHAR_PER_LINE, fptr) != NULL)
	{
		if(str_line[0]=='#')
			continue;
		p_temp = strtok(str_line, "="); // 分割字符串
		//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
		p_temp = Trim(p_temp);
		if(!p_temp || strcmp(key, p_temp)!=0)
			continue;

		p_temp = strtok(NULL, "=");
		//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
		p_temp = Trim(p_temp);
		if(strlen(p_temp) > size || strlen(p_temp)==0)
			break;

		strcpy(value, p_temp);
		res = 1;
		break;
	}

	fclose( fptr );

	if(!res)
	{
		//printf("input error or cannot find the config.\n");
		return -1;
	}
	return 0;
}

int WriteConfig(char *cfgFile, char *key, char *value)
{
	FILE *fptr;
	char str_line[MAX_CHAR_PER_LINE];
	char *new_cfg;
	char *p_temp = NULL;
	int isSet = 0;
	int cfg_len;

	if((NULL == key) || (NULL == value))
	{
		return -1;
	}

	if ( ( fptr = fopen(cfgFile, "r" ) ) == NULL ) 
	{
		if( (fptr = fopen(cfgFile, "w+") ) == NULL )
		{
			//cprintf( "\ropen %s error!\n", cfgFile );
			return -1;
		}
	}

	fseek(fptr,0,SEEK_END);
	cfg_len = ftell(fptr);
	cfg_len += strlen(key);
	cfg_len += strlen(value);
	cfg_len += 10;
	new_cfg = (char*)alloca(cfg_len);
	//char new_cfg[cfg_len];
	new_cfg[0] = 0;

	fseek(fptr,0,SEEK_SET); 

	while(fgets(str_line, MAX_CHAR_PER_LINE, fptr) != NULL)
	{
		if(!isSet)
		{
			if(str_line[0]=='#')
			{
				strcat(new_cfg, str_line);
				continue;
			}
			p_temp = strtok(str_line, "=");
			//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
			if(!p_temp)
			{
				strcat(new_cfg, str_line);
				continue;
			}
			p_temp = Trim(p_temp);
			if(strcmp(key, p_temp)==0)
			{
				strcat(new_cfg, p_temp);
				strcat(new_cfg, "=");
				strcat(new_cfg, value);
				strcat(new_cfg, "\n");
				isSet = 1;
				continue;
			}
			strcat(new_cfg, p_temp);
			p_temp = strtok(NULL, "=");
			//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
			if(p_temp)
			{
				p_temp = Trim(p_temp);
				strcat(new_cfg, "=");
				strcat(new_cfg, p_temp);
			}
			strcat(new_cfg, "\n");
		}
		else
			strcat(new_cfg, str_line);
	}

	if(!isSet)
	{
		strcat(new_cfg, key);
		strcat(new_cfg, "=");
		strcat(new_cfg, value);
		strcat(new_cfg, "\n");
	}

	fclose( fptr );

	remove(cfgFile);

	fptr = fopen(cfgFile,"w+");
	if(fptr == NULL)
	{
		//printf("OPEN CONFIG FALID\n");
		return -1;
	}
	fputs(new_cfg,fptr);
	fclose(fptr);

	return 0;
}

int DeleteConfig(char *cfgFile, char *key)
{
	FILE *fptr;
	char str_line[MAX_CHAR_PER_LINE];
	char *new_cfg;
	char *p_temp = NULL;
	int isSet = 0;
	int cfg_len;

	if((NULL == key))
	{
		return -1;
	}

	if ( ( fptr = fopen(cfgFile, "r" ) ) == NULL ) 
	{
		//cprintf( "\ropen %s error!\n", cfgFile );
		return -1;
	}

	fseek(fptr,0,SEEK_END);
	cfg_len = ftell(fptr);
	new_cfg = (char*)alloca(cfg_len);
	//char new_cfg[cfg_len];
	new_cfg[0] = 0;

	fseek(fptr,0,SEEK_SET); 

	while(fgets(str_line, MAX_CHAR_PER_LINE, fptr) != NULL)
	{
		if(!isSet)
		{
			if(str_line[0]=='#')
			{
				strcat(new_cfg, str_line);
				continue;
			}
			p_temp = strtok(str_line, "=");
			//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
			if(!p_temp)
			{
				strcat(new_cfg, str_line);
				continue;
			}
			p_temp = Trim(p_temp);
			if(strcmp(key, p_temp)==0)
			{
				isSet = 1;
				continue;
			}
			strcat(new_cfg, p_temp);
			p_temp = strtok(NULL, "=");
			//p_temp = (unsigned long)p_temp & 0x7fffffffffff;
			if(p_temp)
			{
				p_temp = Trim(p_temp);
				strcat(new_cfg, "=");
				strcat(new_cfg, p_temp);
			}
			strcat(new_cfg, "\n");
		}
		else
			strcat(new_cfg, str_line);
	}

	fclose( fptr );

	remove(cfgFile);

	fptr = fopen(cfgFile,"w+");
	if(fptr == NULL)
	{
		//printf("OPEN CONFIG FALID\n");
		return -1;
	}
	fputs(new_cfg,fptr);
	fclose(fptr);

	return 0;
}



配置文件调用:

#define CONFIG_FILE_PATH "config.txt"


char cfgStr[50];

if(ReadConfig(CONFIG_FILE_PATH,"ISUP.DPCActive",cfgStr,50)==0)

DPCActive = atoi(cfgStr);


猜你喜欢

转载自blog.csdn.net/liulong1010/article/details/42965135