# [PATCH] parse_ini_string()

- Thread: https://externals.io/message/41687
- Original archive: https://news-web.php.net/php.internals/41687
- Started by Olivier Grange-Labat on 2008-11-05T16:30:21+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #41687 — Olivier Grange-Labat — 2008-11-05T16:30:21+00:00

URL: https://externals.io/message/41687#41687

Hello,

Here's a patch again PHP_5_3 to add a `parse_ini_string()` function.

It just works as `parse_ini_file()`, except it accepts a string instead of 
a filename, obviously.

We've been using for months a simple PHP function to do that, and while 
I had to modify it to accept constants (as `parse_ini_file()` does), I 
thought it was time to use the core parsers instead of reinventing the 
wheel.

I have the same patch available for 5.2, if anyone is interested.

Thank you for all the time and effort you put into PHP !

Olivier

---

## #41690 — johannes@php.net — 2008-11-05T18:12:06+00:00

URL: https://externals.io/message/41687#41690 · In reply to: #41687

Hi,


> Hello,
> 
> Here's a patch again PHP_5_3 to add a `parse_ini_string()` function.

In general I think it's good to add that function, a few comments below.
>  
> +static
> +ZEND_BEGIN_ARG_INFO_EX(arginfo_parse_ini_string, 0, 0, 1)
> +    ZEND_ARG_INFO(0, str)

If you come up with something might a bit more verbose (same below in
the prototype)

[..]

> +/* {{{ proto array parse_ini_string(string str [, bool
> process_sections [, int scanner_mode]])
> +   Parse configuration string */
> +PHP_FUNCTION(parse_ini_string)
> +{
> +    char *string = NULL, *str = NULL;
> +    int str_len = 0;

please use tabs for indention, not spaces

> +    zend_bool process_sections = 0;
> +    long scanner_mode = ZEND_INI_SCANNER_NORMAL;
> +    zend_ini_parser_cb_t ini_parser_cb;
> +
> +    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl",
> &str, &str_len, &process_sections, &scanner_mode) == FAILURE) {
> +        RETURN_FALSE;
> +    }
> +
> +    /* Set callback function */
> +    if (process_sections) {
> +        BG(active_ini_file_section) = NULL;
> +        ini_parser_cb = (zend_ini_parser_cb_t)
> php_ini_parser_cb_with_sections;
> +    } else {
> +        ini_parser_cb = (zend_ini_parser_cb_t)
> php_simple_ini_parser_cb;
> +    }
> +
> +    /* Setup string */
> +    string = (char *) emalloc(str_len + 1);
> +    strcpy(string, str);
> +    *(string + str_len + 1) = '\0';

Is that copy really needed? Where is the copy free'd?
If doing the copy please use strlcpy instead of strcpy. About strlcpy
see[1].

johannes

[1] http://www.gratisoft.us/todd/papers/strlcpy.html

---

## #41697 — Arnaud Le Blanc — 2008-11-05T21:41:13+00:00

URL: https://externals.io/message/41687#41697 · In reply to: #41690

On Wednesday 05 November 2008 17:30:21 Olivier Grange-Labat wrote:
> Hello,
> 
> Here's a patch again PHP_5_3 to add a `parse_ini_string()` function.
> 
> It just works as `parse_ini_file()`, except it accepts a string instead of 
> a filename, obviously.
> 
> We've been using for months a simple PHP function to do that, and while 
> I had to modify it to accept constants (as `parse_ini_file()` does), I 
> thought it was time to use the core parsers instead of reinventing the 
> wheel.
> 
> I have the same patch available for 5.2, if anyone is interested.
> 
> Thank you for all the time and effort you put into PHP !
> 
> Olivier
> 

Hi,

Thanks, committed to 5.3 and HEAD :)

Regards,

Arnaud
