Has anyone written an INI parser in tango?

Walter Bright newshound1 at digitalmars.com
Fri Sep 12 21:13:40 PDT 2008


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;
}



More information about the Digitalmars-d mailing list