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

C语言读取配置文件以及128字节对齐.bin配置文件配置文件 begin build properties factory 四川九州电子科技股份有限公司 oui 03 product type 71 stbid 00000129 hw version 00000473 sw version 2016 10 11 17 27

大家好,我是讯享网,很高兴认识大家。

配置文件:

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

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

讯享网#define PROPERTIES_FILE "/dev/block/platform/.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 ; }


小讯
上一篇 2025-01-14 09:11
下一篇 2025-03-14 17:25

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/80388.html