C语言读取配置文件以及128字节对齐.bin配置文件

配置文件:

#begin build properties
factory=四川九州电子科技股份有限公司
oui=03
product_type=71
stbid=0371419916190000129
hw_version=00000473
sw_version=2016-10-11 17:27
swdate=20161010
soc=Hi3796MV100
product_type_eoc=72
product_configure=00
currentWifiVersion=201603163
pendingWifiVersion=201603163
def_factory_search_freq=235000000
def_factory_search_sym=6875000
def_factory_search_mod=QAM64
stb_upgrade_server_ip=STBLoader.fjgdwl.com
swdate=20161019
mac=011212121201
cm_mac=011212121202
wifi_mac=012121212120
stb_factorytest_need=no

C语言读取接口(此处文件名为properties, 文件路径如下):

#define PROPERTIES_FILE  "/dev/block/platform/30020000.rksdmmc/by-name/properties"
#define PROPERTIES_FILE_LENGTH 1024*128



void prop_set_properties(char *key_name, char *line, size_t len)//获取硬件版本号 匹配成功不需要重复执行
{
	char *substr = NULL;
	char value[256];
	size_t tlen = 0;
	int ret = 0;
	
	LOGD("kongrun get_properties_info_line1: %s", line);
	
	tlen = strlen(key_name);
	
	substr = strstr(line, key_name);//在line中寻找相应的字符串
	
	LOGD("get_properties_info_key_name: %s  ,line: %s", key_name, line);
	
	if( substr == NULL ) // 有的话就一定匹配到 没有的话就匹配不到
	{
		LOGD("cannot_get_properties_info_key_name");
		return ;
	}
	
	LOGD("find_properties_info_key_name000: %s", key_name);
	
	if(line[tlen] == '=')
	{
		
		LOGD("find_properties_info_key_value");
		strncpy(value, &line[tlen+1], len-tlen+1);
		tlen = strlen(value);
		*(value+tlen-1) = '\0';
		
		LOGD("get string %s length is %d ,value  string %s length %d\n", line, len,key_name,tlen);
		LOGD("get value is %s tlen is %d\n", value, tlen);
		LOGD("get_properties_info_value: %s", value);
		ret = property_set(key_name,value);//此处做相应处理
		if ( 0 != ret )
		{
			LOGE("prop_set_hdver failed!\n");
		}
	}
	
	return;
}

void get_properties_info(void)
{
	FILE *fp = NULL;
    char *line = NULL;
    size_t len = 0, tlen = 0;
    ssize_t read = 0;
    
    fp = fopen(PROPERTIES_FILE, "r");
    if (fp == NULL) {
		LOGD("properties file can not be opened");
		return;
	}

    while ((read = getline(&line, &len, fp)) != -1) 
    {
		
		LOGD("get_properties_info_line0: %s , len : %d, read : %d", line , len, read);
        //substr = strstr(line, key_name);
        if( line[0] == '#' )
        {
            continue;
        }
		
        else
        {
                        
			prop_set_properties("ro.di.product_type", line, read);//机顶盒型号
			prop_set_properties(VENDER, line, read);//制造厂商
			prop_set_properties("ro.di.oui", line, read);//制造厂商ID
			prop_set_properties(SN, line, read);//机顶序列号			
			prop_set_properties(HD_VER, line, read);//硬件版本号			
			prop_set_properties(SW_VER, line, read);//软件版本号
			prop_set_properties(DATE, line, read);//软件发布时间
        }
    }

    free(line);
    fclose(fp);
	
	return;
}

读取128字节对齐的配置文件:


读取接口代码:

void prop_get_properties_info(void)
{
	
	FILE *fp = NULL;
    size_t read = 0;
	char buf[256] = {0};
	char key_name[32] = {0};
	char value[32] = {0};
	int offset = 0;
	int ret = 0;

    fp = fopen(PROPERTIES_FILE, "r");//打开文件
    if (fp == NULL) {
		LOGD("properties file can not be opened");
		return;
	}
	
	fseek(fp,offset,SEEK_SET);
	
	while(1)
	{
		memset(buf,0,128*2);
		memset(key_name,0,32);
		memset(value,0,32);
		
		read = fread(buf, 1, 128, fp);

		if(buf[0] == 0 || buf[0] == NULL)
		{
			break;
		}

		else
		{
			strncpy(value,buf+32,32);//拿到value
			
			if( 0 == strncmp(buf,"product_type",sizeof("product_type")) )//机顶盒型号
			{
				ret = property_set("ro.di.product_type",value);
				if ( 0 != ret )
				{
					LOGE("prop_set_product_type failed!\n");
				}
			}
			if( 0 == strncmp(buf,"factory",sizeof("factory")) )//制造厂商
			{
				ret = property_set(VENDER,value);
				if ( 0 != ret )
				{
					LOGE("prop_set_factory failed!\n");
				}
			}
			if( 0 == strncmp(buf,"oui",sizeof("oui")) )//制造厂商ID
			{
				ret = property_set("ro.di.oui",value);
				if ( 0 != ret )
				{
					LOGE("prop_set_oui failed!\n");
				}
			}
			if( 0 == strncmp(buf,"stbid",sizeof("stbid")) )//机顶序列号
			{
				ret = property_set(SN,value);
				if ( 0 != ret )
				{
					LOGE("prop_set_stbid failed!\n");
				}
			}
			if( 0 == strncmp(buf,"hw_version",sizeof("hw_version")) )//硬件版本号
			{
				ret = property_set(HD_VER,value);
				if ( 0 != ret )
				{
					LOGE("prop_set_hw_version failed!\n");
				}
			}
			if( 0 == strncmp(buf,"sw_version",sizeof("sw_version")) )//软件版本号
			{
				ret = property_set(SW_VER,value);
				if ( 0 != ret )
				{
					LOGE("prop_set_sw_version failed!\n");
				}
			}
			if( 0 == strncmp(buf,"swdate",sizeof("swdate")) )//软件发布时间
			{
				ret = property_set(DATE,value);
				if ( 0 != ret )
				{
					LOGE("prop_set_swdate failed!\n");
				}
			}
			
		}
				
		offset += read;
		fseek(fp,offset,SEEK_SET);	
	}
	fclose(fp);

	return ;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值