1. 程式人生 > >C語言讀取配置檔案以及128位元組對齊.bin配置檔案

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 ;
}