Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array in ini file for ini parser stream to parse #120

Closed
chienhsinlin opened this issue Oct 19, 2020 · 8 comments
Closed

array in ini file for ini parser stream to parse #120

chienhsinlin opened this issue Oct 19, 2020 · 8 comments

Comments

@chienhsinlin
Copy link

Hi!:

I have an array in a ini file like

[section1]

key1 = value1
key2 = value2
.....
lut_table =
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16
......

In the structure, it is defined as
struct {
...
int lut_table[16];
}
for(i=0; i<16; i++)
pconfig.lut_table[i] = atoi[multi_value] something?

Can I implement something in the ini_parse_stream to read values for this array? any suggestions? Or something similar like simpleIni to have a key whic has multivalues?

@benhoyt
Copy link
Owner

benhoyt commented Oct 19, 2020

Hi @chienhsinlin, there's no special provision for this in inih itself, but you can definitely parse comma-separated values in the handler yourself -- use something like strchr or strtok to find the ',' (comma) separators.

If the values for one key are spread across multiple lines, you'll also want to set INI_ALLOW_MULTILINE to 1. This works like Python's configparser multi-line values -- subsequent values have to be indented with whitespace. For example:

[section]
lut_table = 1, 2, 3, 4, 5, 6, 7, 8,
            9, 10, 11, 12, 13, 14, 15, 16

Then you'll receive two separate calls to your handler function with the same section and key name, and you'll need to save the last lut_table array index in a global or something like that.

Hope that helps!

@benhoyt benhoyt closed this as completed Oct 19, 2020
@chienhsinlin
Copy link
Author

Thanks a lot, It works now after using strtok in the handler to parse each line and tokenize strings with delimiter.

@chienhsinlin
Copy link
Author

Something like that. as you said idx is global variable.
idx = 0; // as global variable.

        tok = strtok((char *)value,",");
        pconfig.lut[idx] = atoi(tok);
        while(tok!=NULL) {
            idx++;
            tok =strtok(NULL," ,");
            if(tok!= NULL)
                pconfig.lut[idx] = atoi(tok); 
        }

@wang-mb
Copy link

wang-mb commented Jul 12, 2022

Hi @chienhsinlin, there's no special provision for this in inih itself, but you can definitely parse comma-separated values in the handler yourself -- use something like strchr or strtok to find the ',' (comma) separators.

If the values for one key are spread across multiple lines, you'll also want to set INI_ALLOW_MULTILINE to 1. This works like Python's configparser multi-line values -- subsequent values have to be indented with whitespace. For example:

[section]
lut_table = 1, 2, 3, 4, 5, 6, 7, 8,
            9, 10, 11, 12, 13, 14, 15, 16

Then you'll receive two separate calls to your handler function with the same section and key name, and you'll need to save the last lut_table array index in a global or something like that.

Hope that helps!
hi, @benhoyt,
for multiple lines like this, seems it's no longer possible to add comments after the value, and the comments either with # or ; are treated as part of value. I feel it's not right. Is this an issue? Thanks.

@benhoyt
Copy link
Owner

benhoyt commented Jul 12, 2022

Hi @silly2020, do you have INI_ALLOW_INLINE_COMMENTS turned off? See ini.h for a complete list of settings.

@wang-mb
Copy link

wang-mb commented Jul 12, 2022

Hi, @benhoyt , thanks for prompt reply.
I didn't turned off INI_ALLOW_INLINE_COMMENTS, it is 1 and INI_ALLOW_INLINE_COMMENTS is ";".
Comments start with ";" in subsequent lines are treated as part of value, comments in the line with name is OK.
Thanks again.
P.S.
I tested with dump in example as below:
ini file content:

[test]
MyName  = value part 1 ; comments OK
 value part 2 ; expect ignore this as comments

what I got with dump:

[test]
MyName = value part 1
MyName = value part 2 ; expect ignore this as comments

benhoyt added a commit that referenced this issue Jul 12, 2022
This is a bug fix that makes inih's behaviour here work as per Python's
ConfigParser, which is what inih behaviour is (loosely) based on.

Thanks @silly2020 for the report. See
#120 (comment)
benhoyt added a commit that referenced this issue Jul 12, 2022
This is a bug fix that makes inih's behaviour here work as per Python's
ConfigParser, which is what inih behaviour is (loosely) based on.

Thanks @silly2020 for the report. See
#120 (comment)
@benhoyt
Copy link
Owner

benhoyt commented Jul 12, 2022

@silly2020 Thanks for the report. This is an oversight (bug!) in inih, and differs from how Python's ConfigParser works. Does the latest master version work for you? If so, I'll release this in r56.

@wang-mb
Copy link

wang-mb commented Jul 12, 2022

@benhoyt Yes. I tested again with master version and it works for me. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants