Skip to content

Commit

Permalink
Added custom parse_ini_file function to deal with '#' comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bergware committed Oct 25, 2016
1 parent b3236e4 commit ca7c521
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/dynamix/include/Helpers.php
Expand Up @@ -239,4 +239,8 @@ function transpose_user_path($path) {
}
return $path;
}
// custom parse_ini_file function to deal with '#' comments
function my_parse_ini_file($file) {
return parse_ini_string(str_replace('#',';',file_get_contents($file)));
}
?>

5 comments on commit ca7c521

@Squidly271
Copy link
Contributor

@Squidly271 Squidly271 commented on ca7c521 Oct 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what happens if one of the entries happens to contain a # symbol?

Better to use

function my_parse_ini_file($file,$mode=false,$scanner_mode=INI_SCANNER_NORMAL) {
return parse_ini_string(preg_replace('/^#.*\n/m', "", @file_get_contents($file)),$mode,$scanner_mode);
}

This only removes lines that start with a #

@eschultz
Copy link
Contributor

@eschultz eschultz commented on ca7c521 Oct 30, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Squidly271
Copy link
Contributor

@Squidly271 Squidly271 commented on ca7c521 Oct 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why the preg_replace is better than the str_replace (And I tested that it'll work with comment lines anywhere within a ini file. (The code as posted in this commit shows str_replace whereas bleeding edge plugin is using preg_replace)

@bergware
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was initial code, later changed to preg_replace just to avoid what you describe above.

The my_ functions in Helpers replace the '#' for ';' but do not remove the comment line, which IMHO is neater.

@Squidly271
Copy link
Contributor

@Squidly271 Squidly271 commented on ca7c521 Oct 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My version is actually better though

On an ini file like this:

this is a comment

test="testLine"

comment2="blah"

line2="line2"

Your version will parse it as

Array
(
[test] => testLine
[#comment2] => blah
[line2] => line2
)

since it only changes the first occurrence of the # to a ;

While mine will parse it correctly as

Array
(
[test] => testLine
[line2] => line2
)
Since it discards any line that begins with #

Not a real issue with unRaid, but if you're going to do it, you might as well do it right. All you need to do is add the m modifier to process the ini as separate lines and you can keep the comments if you choose

Please sign in to comment.