September 12, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
2008/9/12 Bill Baxter <wbaxter@gmail.com>: >> >> 4) It's simple enough to be edited from a command-line editor. > > It's not hierarchical. I've always thought that was a benefit rather than a hinderence. It's also untyped and supports comments. JSON certainly isn't a good format to expose end users to. INIs are also very amenable to having other software tweak settings, whereas more complex formats can't always claim that (you ever tried to write code that updates a bind configuration file?). > > But you're right, if you never have any plans to go beyond very simple key=value uses, then it's fine. But if that's all you need then maybe you don't even need ini. Just a file with plain old > ---- > foo = bar > x = y > ---- > will do and you can just read the lines with a simple regex. > > That said I don't know the full spec of the ini format. So maybe it has some tricks I'm not aware of. > > --bb > | ||||
September 12, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > On Fri, Sep 12, 2008 at 3:43 PM, Chris R. Miller <lordsauronthegreat@gmail.com> wrote: >> Bill Baxter wrote: >>> On Fri, Sep 12, 2008 at 9:38 AM, Rayne <DiscipleRayne@gmail.com> wrote: >>>> Just wondering if anyone had because it doesnt seen that the creators of Tango have, and their crappy properties stuff doesnt appeal to me in the least bit. >>> Is there some reason you are really set on using a crappy format like ini? >> What's so bad about ini? >> >> 1) It's simple. Even people with little to no computer savvy can look at an ini file and deduce how to make things work. >> >> 2) It's easy to parse. >> >> 3) It's not platform-dependent. Using the line iterator in Tango you >> don't even have to be cognizant of the differences between /r/n and /n. >> Just go! >> >> 4) It's simple enough to be edited from a command-line editor. > > It's not hierarchical. You could emulate that with headers like: [hier1.hier11] foo = bar [hier1.hier12] bar = foo I think I did that with a Java project a (long) while back. That was using Java's java.util.Properties though... I forget if that was even partly ini-compliant. > But you're right, if you never have any plans to go beyond very simple key=value uses, then it's fine. But if that's all you need then maybe you don't even need ini. Just a file with plain old > ---- > foo = bar > x = y > ---- > will do and you can just read the lines with a simple regex. Heh, I never got as far as a regex. My code does exactly what you describe (only it adds the construct of lines beginning with # for comments). I only thought it would be useful as a starting point to add the ability to parse ini headers into. > That said I don't know the full spec of the ini format. So maybe it has some tricks I'm not aware of. I think the attractiveness of ini is that there aren't any special tricks to have to be aware of. It's the ultimate WYSIWYG in configuration storage. | |||
September 13, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | On Fri, 12 Sep 2008 17:17:55 +0900, Bill Baxter wrote:
> But you're right, if you never have any plans to go beyond very simple
> key=value uses, then it's fine. But if that's all you need then maybe
> you don't even need ini. Just a file with plain old ----
> foo = bar
> x = y
> ----
Ummm, isn't that what an ini file is? I assume it is not a .d file, as that would need compiled and thus defeat the purpose of using ini.
| |||
September 13, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Sat, Sep 13, 2008 at 11:46 AM, Jesse Phillips <jessekphillips@gmail.com> wrote:
> On Fri, 12 Sep 2008 17:17:55 +0900, Bill Baxter wrote:
>
>> But you're right, if you never have any plans to go beyond very simple
>> key=value uses, then it's fine. But if that's all you need then maybe
>> you don't even need ini. Just a file with plain old ----
>> foo = bar
>> x = y
>> ----
>
> Ummm, isn't that what an ini file is? I assume it is not a .d file, as that would need compiled and thus defeat the purpose of using ini.
There are also the [section] thingys in an ini file.
But anyway, I've always associated ini files with Windows-specific cruftery. I guess from Windows' win.ini, system.ini etc. Registry .reg files and ".inf" files are also just ini files, basically.
But you've convinced me. I was wrong. INI is a super format with lightweight syntax that fits the bill perfectly if all you need is to save and load a few variables. Especially if having a human-editable format is important.
I've gained new respect for the humble INI.
Thanks,
--bb
| |||
September 13, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Rayne | Rayne wrote: > Just wondering if anyone had because it doesnt seen that the creators > of Tango have, and their crappy properties stuff doesnt appeal to me > in the least bit. Here's a simple one I wrote a while back. A description of the file format is here: http://openrj.sourceforge.net/ // Written in the D programming language // placed into Public Domain // Written by Walter Bright module std.openrj; import std.string; alias char[][] [char[]] [] openrj_t; class OpenrjException : Exception { uint linnum; this(uint linnum, char[] msg) { this.linnum = linnum; super(std.string.format("OpenrjException line %s: %s", linnum, msg)); } } openrj_t parse(char[] db) { openrj_t rj; char[][] lines; char[][] [char[]] record; lines = std.string.splitlines(db); for (uint linnum = 0; linnum < lines.length; linnum++) { char[] line = lines[linnum]; // Splice lines ending with backslash while (line.length && line[length - 1] == '\\') { if (++linnum == lines.length) throw new OpenrjException(linnum, "no line after \\ line"); line = line[0 .. length - 1] ~ lines[linnum]; } if (line[0 .. 2] == "%%") { // Comment lines separate records if (record) rj ~= record; record = null; line = null; continue; } int colon = std.string.find(line, ':'); if (colon == -1) throw new OpenrjException(linnum, "'key : value' expected"); char[] key = std.string.strip(line[0 .. colon]); char[] value = std.string.strip(line[colon + 1 .. length]); char[][] fields = record[key]; fields ~= value; record[key] = fields; } if (record) rj ~= record; return rj; } | |||
September 13, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright Wrote:
> Rayne wrote:
> > Just wondering if anyone had because it doesnt seen that the creators of Tango have, and their crappy properties stuff doesnt appeal to me in the least bit.
>
> Here's a simple one I wrote a while back. A description of the file format is here: http://openrj.sourceforge.net/
>
> // Written in the D programming language
> // placed into Public Domain
> // Written by Walter Bright
>
> module std.openrj;
>
> import std.string;
>
> alias char[][] [char[]] [] openrj_t;
>
> class OpenrjException : Exception
> {
> uint linnum;
>
> this(uint linnum, char[] msg)
> {
> this.linnum = linnum;
> super(std.string.format("OpenrjException line %s: %s", linnum, msg));
> }
> }
>
> openrj_t parse(char[] db)
> {
> openrj_t rj;
> char[][] lines;
> char[][] [char[]] record;
>
> lines = std.string.splitlines(db);
>
> for (uint linnum = 0; linnum < lines.length; linnum++)
> {
> char[] line = lines[linnum];
>
> // Splice lines ending with backslash
> while (line.length && line[length - 1] == '\\')
> {
> if (++linnum == lines.length)
> throw new OpenrjException(linnum, "no line after \\ line");
> line = line[0 .. length - 1] ~ lines[linnum];
> }
>
> if (line[0 .. 2] == "%%")
> {
> // Comment lines separate records
> if (record)
> rj ~= record;
> record = null;
> line = null;
> continue;
> }
>
> int colon = std.string.find(line, ':');
> if (colon == -1)
> throw new OpenrjException(linnum, "'key : value' expected");
>
> char[] key = std.string.strip(line[0 .. colon]);
> char[] value = std.string.strip(line[colon + 1 .. length]);
>
> char[][] fields = record[key];
> fields ~= value;
> record[key] = fields;
> }
> if (record)
> rj ~= record;
> return rj;
> }
Thanks for the effort, however as it turns out I was being an idiot, I figured out how the properties work in Tango, its basicly an INI file. At least it works almost the same.
| |||
September 13, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Rayne | Rayne wrote: > Walter Bright Wrote: > >> Rayne wrote: >>> Just wondering if anyone had because it doesnt seen that the creators >>> of Tango have, and their crappy properties stuff doesnt appeal to me >>> in the least bit. >> Here's a simple one I wrote a while back. A description of the file format is here: http://openrj.sourceforge.net/ >> >> // Written in the D programming language >> // placed into Public Domain >> // Written by Walter Bright >> >> module std.openrj; >> >> import std.string; >> >> alias char[][] [char[]] [] openrj_t; >> >> class OpenrjException : Exception >> { >> uint linnum; >> >> this(uint linnum, char[] msg) >> { >> this.linnum = linnum; >> super(std.string.format("OpenrjException line %s: %s", linnum, msg)); >> } >> } >> >> openrj_t parse(char[] db) >> { >> openrj_t rj; >> char[][] lines; >> char[][] [char[]] record; >> >> lines = std.string.splitlines(db); >> >> for (uint linnum = 0; linnum < lines.length; linnum++) >> { >> char[] line = lines[linnum]; >> >> // Splice lines ending with backslash >> while (line.length && line[length - 1] == '\\') >> { >> if (++linnum == lines.length) >> throw new OpenrjException(linnum, "no line after \\ line"); >> line = line[0 .. length - 1] ~ lines[linnum]; >> } >> >> if (line[0 .. 2] == "%%") >> { >> // Comment lines separate records >> if (record) >> rj ~= record; >> record = null; >> line = null; >> continue; >> } >> >> int colon = std.string.find(line, ':'); >> if (colon == -1) >> throw new OpenrjException(linnum, "'key : value' expected"); >> >> char[] key = std.string.strip(line[0 .. colon]); >> char[] value = std.string.strip(line[colon + 1 .. length]); >> >> char[][] fields = record[key]; >> fields ~= value; >> record[key] = fields; >> } >> if (record) >> rj ~= record; >> return rj; >> } > > Thanks for the effort, however as it turns out I was being an idiot, I figured out how the properties work in Tango, its basicly an INI file. At least it works almost the same. I know this has been solved but I'd like to point out that windows offers GetPrivateProfileString. Its not portable and not that efficient (although that would only be a concern if your reading 10000's of entries). http://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx -Koel | |||
October 17, 2008 Re: Has anyone written an INI parser in tango? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Rayne | "Rayne" <DiscipleRayne@gmail.com> wrote in message news:gacdld$19l0$1@digitalmars.com... > Just wondering if anyone had because it doesnt seen that the > creators of Tango have, and their crappy properties stuff doesnt > appeal to me in the least bit. Are you looking for an INI parser in the first place, or are you seeing whether anybody's written a Tango one before you convert an existing Phobos one to use Tango? Since a few alternatives to INI have been mentioned.... While I was a PhD student, I created Configur8, which is a configuration file format inspired by INI, but with a slightly different syntax and the ability to import and override stuff from another Configur8 file. The only problem is that I never got round to writing a D implementation, but I should be able to dig it up and convert my C++ implementation to D without too much work. Since I don't use Tango, somebody else would have to do the final step of the conversion process. Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply