parse_ini_file() 函数

PHP parse_ini_file() 函数

定义和用法

parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置。

语法

parse_ini_file(file,process_sections)
参数描述
file必需。规定要检查的 ini 文件。
process_sections可选。如果设置为 true,则返回一个多维数组,包括了配置文件中每一节的名称和设置。默认是 false。

说明

ini 文件的结构和 php.ini 的相似。

常量也可以在 ini 文件中被解析,因此如果在运行 parse_ini_file() 之前定义了常量作为 ini 的值,将会被集成到结果中去。只有 ini 的值会被求值。

由数字组成的键名和小节名会被 PHP 当作整数来处理,因此以 0 开头的数字会被当作八进制而以 0x 开头的会被当作十六进制。

提示和注释

注释:本函数可以用来读取你自己的应用程序的配置文件。本函数与 php.ini 文件没有关系,该文件在运行脚本时就已经处理过了。

注释:如果 ini 文件中的值包含任何非字母数字的字符,需要将其括在双引号中(")。

注释:有些保留字不能作为 ini 文件中的键名,包括:null,yes,no,true 和 false。值为 null,no 和 false 等效于 "",值为 yes 和 true 等效于 "1"。字符 {}|"~![()" 也不能用在键名的任何地方,而且这些字符在选项值中有着特殊的意义。

注释:自 PHP 5.0 版本开始,该函数也处理选项值内的新行。

例子

例子 1

"test.ini" 的内容:

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"

PHP 代码:

<?php
print_r(parse_ini_file("test.ini"));
?>

输出:

Array
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => http://www.w3school.com.cn
)

例子 2

"test.ini" 的内容:

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"

PHP 代码(process_sections 设置为 true):

<?php
print_r(parse_ini_file("test.ini",true));
?>

输出:

Array
(
[names] => Array
  (
  [me] => Robert
  [you] => Peter
  )
[urls] => Array
  (
  [first] => http://www.example.com
  [second] => http://www.w3school.com.cn
  )
)
static uint32_t hi_upg_save_partition_info(const char *label, struct hi_upg_ini_file_st **new_ini_file, struct hi_upg_ini_section_st **last_section, struct hi_upg_ini_item_st **last_item, struct fdt_resource *fres) // { int32_t ret; char offset[HI_UPG_INI_VALUE_LEN]; char size[HI_UPG_INI_VALUE_LEN]; uint32_t type = 0; uint32_t bootflag = 0; ret = sprintf_s(offset, HI_UPG_INI_VALUE_LEN, "%x", fres->start); if (ret < 0) return HI_RET_FAIL; pr_err("label == %s\n",label); // 打印 ret = sprintf_s(size, HI_UPG_INI_VALUE_LEN, "%x", (fres->end - fres->start + 1)); if (ret < 0) return HI_RET_FAIL; hi_upg_get_partition_type(label, &type, &bootflag); if (bootflag == HI_UPG_BOOTFLAGB) { // emei走这里 ret = hi_upg_parse_item(last_section, last_item, HI_UPG_INI_TYPE_KEY_SLAVE_OFFSET, offset, sizeof(offset)); printf("ubootb1_ret == %u\n", ret); if (ret < 0) return HI_RET_FAIL; ret = hi_upg_parse_item(last_section, last_item, HI_UPG_INI_TYPE_KEY_SLAVE_LEN, size, sizeof(size)); printf("ubootb2_ret == %u\n", ret); if (ret < 0) return HI_RET_FAIL; } else { // luofu走这里 A ret = hi_upg_parse_section(new_ini_file, last_section, type); printf("uboota1_ret == %u\n", ret); if (ret < 0) return HI_RET_FAIL; ret = hi_upg_parse_item(last_section, last_item, HI_UPG_INI_TYPE_KEY_MAIN_OFFSET, offset, sizeof(offset)); printf("uboota2_ret == %u\n", ret); if (ret < 0) return HI_RET_FAIL; ret = hi_upg_parse_item(last_section, last_item, HI_UPG_INI_TYPE_KEY_MAIN_LEN, size, sizeof(size)); printf("uboota3_ret == %u\n", ret); if (ret < 0) return HI_RET_FAIL; } return HI_RET_SUCC; } static void hi_upg_get_partition_type(const char *label, uint32_t *type, uint32_t *bootflag) // { printf("label111 == %s\n", label); if (strstr(label, "uboot")) { *type = HI_UPG_TYPE_IMAGE_BOOT; // type 20001 if (strstr(label, "uboota")) { // label为uboot时,进入ubootb printf("label111 == uboota\n"); *bootflag = HI_UPG_BOOTFLAGA; } else { printf("label111 == ubootb\n"); *bootflag = HI_UPG_BOOTFLAGB; } } else if (strstr(label, "kernel")) { printf("label111 == kernel\n"); *type = HI_UPG_TYPE_IMAGE_KERNEL; if (strstr(label, "kernela\n")) { printf("label111 == kernela\n"); *bootflag = HI_UPG_BOOTFLAGA; } else { *bootflag = HI_UPG_BOOTFLAGB; } } else if (strstr(label, "rootfs")) { *type = HI_UPG_TYPE_IMAGE_ROOTFS; if (strstr(label, "rootfsa")) { *bootflag = HI_UPG_BOOTFLAGA; } else { *bootflag = HI_UPG_BOOTFLAGB; } } else if (strstr(label, "fwk")) { *type = HI_UPG_TYPE_IMAGE_FWK; if (strstr(label, "fwka")) { *bootflag = HI_UPG_BOOTFLAGA; } else { *bootflag = HI_UPG_BOOTFLAGB; } } return; } static int32_t hi_upg_parse_section(struct hi_upg_ini_file_st **new_ini_file, struct hi_upg_ini_section_st **last_section, uint32_t type) { struct hi_upg_ini_section_st *new_section = NULL; new_section = (struct hi_upg_ini_section_st *)malloc(sizeof(*new_section)); printf("new_section->type111 == %u\n", new_section->type); // ????2415790532和2407400696是什么意思 ???? if (new_section == NULL){ printf("new_section == NULL\n"); return HI_RET_FAIL; } (void)memset_s(new_section, sizeof(*new_section), 0, sizeof(*new_section)); // 清零操作 printf("type555 == %u\n", type); // 关键 此处打印值4294967295 new_section->type = type; // 赋值 if (*last_section != NULL){ printf("new_section->type333 == %u\n", new_section->type); (*last_section)->next = new_section; } else{ printf("new_section->type222 == %u\n", new_section->type); (*new_ini_file)->section_list = new_section; } printf("type666 == %u\n", type); printf("new_section->type444 == %u\n", new_section->type); *last_section = new_section; (*new_ini_file)->section_num = 0; return HI_RET_SUCC; } 问:为什么bootflage == HI_UPG_BOOTFLAGB时,不让函数hi_upg_parse_section处理,只有当bootflag == HI_UPG_BOOTFLAGA,才会让函数hi_upg_parse_section去处理,在bootflage == HI_UPG_BOOTFLAGB时,也处理一次,会怎样?
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值