Has anyone written an INI parser in tango?
Janderson
ask at me.com
Sat Sep 13 12:37:16 PDT 2008
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
More information about the Digitalmars-d
mailing list