Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 16, 2012 [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Greetings. Recently, I've been working on INI parser in D. The main goals were to keep code easy and well documented. Suggestions are really welcome(main reason of this thread) because it needs polishing and stuff. It provides simple interface for operating on parsed file, including useful features like section inheriting and variable lookups. Here is simple example taken from README with little modifications: import std.stdio; void main() { // Hard code the contents string c = " # defaults [def] name1:value1 name2:value2 ; override defaults [foo : def] name1=Name1 from foo. Lookup for def.name2: %name2%"; // create parser instance auto iniParser = new IniParser(); // Set ini structure details; can be ommited iniParser.commentChars = [';', '#']; iniParser.delimChars = ['=', ':']; // parse auto ini = iniParser.parse(c); // write foo.name1 value writeln(ini.getSection("foo")["name1"].value); } You can also define parsing details, like commentCharacters* and others. As for the keys, structure is used rather than associative arrays. There's also bug** that does not allow chaining with opCall which I hope will be fixed :). IniStructure (result of parsing) overloads some basic operators allowing you to looping through it and accessing data with opIndex and opCall. Feel free to share suggestions, changes, help me make it better :). Repo: https://github.com/robik/DIni * https://github.com/robik/DIni/blob/master/src/dini.d#L400 ** http://d.puremagic.com/issues/show_bug.cgi?id=7210 |
February 16, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik | On Thursday, 16 February 2012 at 20:50:23 UTC, Robik wrote:
>
> Feel free to share suggestions, changes, help me make it better :).
>
It's great, I think I could find uses for it.
One thing that confuses me about the implementation is that the IniSection has an array of key-value pairs. I think it would be more efficient, and cleaner code, to use an associative array.
|
February 16, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik Attachments:
| I wonder if there is a problem with a 'standard' ini parser, in that ini
files are not really very standard.
I have seen a lot of ini files with scope (also also use this in some of my
own apps), will I be able to parse these with your parser?
[section]
{
key = value
key2 = value
[subsection]
{
subkey = value
}
}
?
I notice your interesting delimiters too, I've never seen anything like that in an ini file before, where did you see that? What makes it standard? I might like to use something like that if I had thought it was a normal thing to use in an ini file...
On 16 February 2012 22:50, Robik <szadows@gmail.com> wrote:
> Greetings.
>
> Recently, I've been working on INI parser in D. The main goals were to keep code easy and well documented. Suggestions are really welcome(main reason of this thread) because it needs polishing and stuff.
>
> It provides simple interface for operating on parsed file, including
> useful features like section inheriting and variable lookups. Here is
> simple example taken from README with little modifications:
> import std.stdio;
> void main()
> {
> // Hard code the contents
> string c = "
> # defaults
> [def]
> name1:value1
> name2:value2
>
> ; override defaults
> [foo : def]
> name1=Name1 from foo. Lookup for def.name2: %name2%";
>
> // create parser instance
> auto iniParser = new IniParser();
>
> // Set ini structure details; can be ommited
> iniParser.commentChars = [';', '#'];
> iniParser.delimChars = ['=', ':'];
>
>
> // parse
> auto ini = iniParser.parse(c);
>
> // write foo.name1 value
> writeln(ini.getSection("foo")[**"name1"].value);
> }
>
> You can also define parsing details, like commentCharacters* and others. As for the keys, structure is used rather than associative arrays. There's also bug** that does not allow chaining with opCall which I hope will be fixed :).
>
> IniStructure (result of parsing) overloads some basic operators allowing you to looping through it and accessing data with opIndex and opCall.
>
> Feel free to share suggestions, changes, help me make it better :).
>
> Repo: https://github.com/robik/DIni
> * https://github.com/robik/DIni/**blob/master/src/dini.d#L400<https://github.com/robik/DIni/blob/master/src/dini.d#L400>
> ** http://d.puremagic.com/issues/**show_bug.cgi?id=7210<http://d.puremagic.com/issues/show_bug.cgi?id=7210>
>
|
February 16, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
At this point you may as well just use JSON.
On Feb 16, 2012, at 2:29 PM, Manu wrote:
> I wonder if there is a problem with a 'standard' ini parser, in that ini files are not really very standard.
> I have seen a lot of ini files with scope (also also use this in some of my own apps), will I be able to parse these with your parser?
>
> [section]
> {
> key = value
> key2 = value
>
> [subsection]
> {
> subkey = value
> }
> }
>
> ?
>
> I notice your interesting delimiters too, I've never seen anything like that in an ini file before, where did you see that? What makes it standard?
> I might like to use something like that if I had thought it was a normal thing to use in an ini file...
>
> On 16 February 2012 22:50, Robik <szadows@gmail.com> wrote: Greetings.
>
> Recently, I've been working on INI parser in D. The main goals were to keep code easy and well documented. Suggestions are really welcome(main reason of this thread) because it needs polishing and stuff.
>
> It provides simple interface for operating on parsed file, including useful features like section inheriting and variable lookups. Here is simple example taken from README with little modifications:
> import std.stdio;
> void main()
> {
> // Hard code the contents
> string c = "
> # defaults
> [def]
> name1:value1
> name2:value2
>
> ; override defaults
> [foo : def]
> name1=Name1 from foo. Lookup for def.name2: %name2%";
>
> // create parser instance
> auto iniParser = new IniParser();
>
> // Set ini structure details; can be ommited
> iniParser.commentChars = [';', '#'];
> iniParser.delimChars = ['=', ':'];
>
>
> // parse
> auto ini = iniParser.parse(c);
>
> // write foo.name1 value
> writeln(ini.getSection("foo")["name1"].value);
> }
>
> You can also define parsing details, like commentCharacters* and others. As for the keys, structure is used rather than associative arrays. There's also bug** that does not allow chaining with opCall which I hope will be fixed :).
>
> IniStructure (result of parsing) overloads some basic operators allowing you to looping through it and accessing data with opIndex and opCall.
>
> Feel free to share suggestions, changes, help me make it better :).
>
> Repo: https://github.com/robik/DIni
> * https://github.com/robik/DIni/blob/master/src/dini.d#L400
> ** http://d.puremagic.com/issues/show_bug.cgi?id=7210
>
|
February 16, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Am 16.02.2012, 23:34 Uhr, schrieb Sean Kelly <sean@invisibleduck.org>: > At this point you may as well just use JSON. Listen to this guy, he's right. JSON allows hierarchies and arrays, strings, numbers and booleans as values. It is clearly defined and as light-weight as an INI file (compared to XML). I stored game replays in JSON format for http://aichallenge.org/ (gzip compressed and served via HTTP). I found it very flexible for the data structures we came up with and portable since most programming languages have a standard JSON parser. |
February 17, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Attachments:
| True, but JSON didn't exist/wasn't well known when these formats were in use.
On 17 February 2012 00:34, Sean Kelly <sean@invisibleduck.org> wrote:
> At this point you may as well just use JSON.
>
> On Feb 16, 2012, at 2:29 PM, Manu wrote:
>
> > I wonder if there is a problem with a 'standard' ini parser, in that ini
> files are not really very standard.
> > I have seen a lot of ini files with scope (also also use this in some of
> my own apps), will I be able to parse these with your parser?
> >
> > [section]
> > {
> > key = value
> > key2 = value
> >
> > [subsection]
> > {
> > subkey = value
> > }
> > }
> >
> > ?
> >
> > I notice your interesting delimiters too, I've never seen anything like
> that in an ini file before, where did you see that? What makes it standard?
> > I might like to use something like that if I had thought it was a normal
> thing to use in an ini file...
> >
> > On 16 February 2012 22:50, Robik <szadows@gmail.com> wrote: Greetings.
> >
> > Recently, I've been working on INI parser in D. The main goals were to
> keep code easy and well documented. Suggestions are really welcome(main reason of this thread) because it needs polishing and stuff.
> >
> > It provides simple interface for operating on parsed file, including
> useful features like section inheriting and variable lookups. Here is simple example taken from README with little modifications:
> > import std.stdio;
> > void main()
> > {
> > // Hard code the contents
> > string c = "
> > # defaults
> > [def]
> > name1:value1
> > name2:value2
> >
> > ; override defaults
> > [foo : def]
> > name1=Name1 from foo. Lookup for def.name2: %name2%";
> >
> > // create parser instance
> > auto iniParser = new IniParser();
> >
> > // Set ini structure details; can be ommited
> > iniParser.commentChars = [';', '#'];
> > iniParser.delimChars = ['=', ':'];
> >
> >
> > // parse
> > auto ini = iniParser.parse(c);
> >
> > // write foo.name1 value
> > writeln(ini.getSection("foo")["name1"].value);
> > }
> >
> > You can also define parsing details, like commentCharacters* and others.
> As for the keys, structure is used rather than associative arrays. There's also bug** that does not allow chaining with opCall which I hope will be fixed :).
> >
> > IniStructure (result of parsing) overloads some basic operators allowing
> you to looping through it and accessing data with opIndex and opCall.
> >
> > Feel free to share suggestions, changes, help me make it better :).
> >
> > Repo: https://github.com/robik/DIni
> > * https://github.com/robik/DIni/blob/master/src/dini.d#L400
> > ** http://d.puremagic.com/issues/show_bug.cgi?id=7210
> >
>
>
|
February 17, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise Attachments:
| On 17 February 2012 01:57, Marco Leise <Marco.Leise@gmx.de> wrote:
> Am 16.02.2012, 23:34 Uhr, schrieb Sean Kelly <sean@invisibleduck.org>:
>
>
> At this point you may as well just use JSON.
>>
>
> Listen to this guy, he's right. JSON allows hierarchies and arrays, strings, numbers and booleans as values. It is clearly defined and as light-weight as an INI file (compared to XML). I stored game replays in JSON format for http://aichallenge.org/ (gzip compressed and served via HTTP). I found it very flexible for the data structures we came up with and portable since most programming languages have a standard JSON parser.
>
Sure, I would certainly use JSON now, but we're talking about reading existing data right? Or what's the point of ini at all?
|
February 17, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Thursday, 16 February 2012 at 22:29:30 UTC, Manu wrote:
> I have seen a lot of ini files with scope (also also use this in some of my own apps), will I be able to parse these with your parser?
I've never seen such a file. What software (other than yours) uses it?
|
February 17, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robik | On 2012-02-16 21:50, Robik wrote: > Greetings. > > Recently, I've been working on INI parser in D. The main goals were to > keep code easy and well documented. Suggestions are really welcome(main > reason of this thread) because it needs polishing and stuff. > > It provides simple interface for operating on parsed file, including > useful features like section inheriting and variable lookups. Here is > simple example taken from README with little modifications: > import std.stdio; > void main() > { > // Hard code the contents > string c = " > # defaults > [def] > name1:value1 > name2:value2 > > ; override defaults > [foo : def] > name1=Name1 from foo. Lookup for def.name2: %name2%"; > > // create parser instance > auto iniParser = new IniParser(); > > // Set ini structure details; can be ommited > iniParser.commentChars = [';', '#']; > iniParser.delimChars = ['=', ':']; > > > // parse > auto ini = iniParser.parse(c); > > // write foo.name1 value > writeln(ini.getSection("foo")["name1"].value); > } Why the need for ".value"? It would guess because opIndex returns IniKey which both contains the key and the value. I would sugest you add a "alias this" pointing to "value". Something like this. struct IniKey { string name; string value; alias value this; } -- /Jacob Carlborg |
February 17, 2012 Re: [RFC] Ini parser | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Thursday, 16 February 2012 at 22:29:30 UTC, Manu wrote:
> I wonder if there is a problem with a 'standard' ini parser, in that ini
> files are not really very standard.
> I have seen a lot of ini files with scope (also also use this in some of my
> own apps), will I be able to parse these with your parser?
>
> [section]
> {
> key = value
> key2 = value
>
> [subsection]
> {
> subkey = value
> }
> }
>
> ?
>
> I notice your interesting delimiters too, I've never seen anything like
> that in an ini file before, where did you see that? What makes it standard?
> I might like to use something like that if I had thought it was a normal
> thing to use in an ini file...
Also, you can "make it" simplest/standard-est as possible by disabling those features. To do that you have to set for example 'sectionInheritChar' to 0 to disable it. But to nest sections you have to use something like that:
[section]
key = value
key2 = value
[section.subsection]
subkey = value
Where dot in second section name is delimeter you've set up.
|
Copyright © 1999-2021 by the D Language Foundation