Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

read filtered tokens from file

Moderators: larsivi kris

Posted: 09/26/07 09:54:26

Hello,

One of my projects stores string data and configs in one file. This file is usually edited by hand and can contain blank lines and comments. I need to read config and data, filtering comments. At first i tried to use LineIterator? and FilteringIterator?, but they don't share the same interface. After searching for a "right" solution for half an hour i wrote this class:

class StreamIteratorAdaptor(V) : Iterator!(V[])
{
    StreamIterator!(V) iterator;
	
    this(StreamIterator!(V) iterator)
    {
	this.iterator = iterator;
    }
	
    public bool more()
    {
	V[] token = iterator.next();
	if (token.ptr)
	{
	    iterator.push(token);
	}
	return token !is null;
    }
	
    public V[] get()
    {
	return iterator.next;
    }
	
    int opApply(int delegate (ref V[] value) dg)
    {
	return iterator.opApply(dg);
    }
}

Did i miss something? Can i do this in tango without Adaptor:

void main()
{
    auto conduit = new FileConduit("somefile");
    auto buffer = new Buffer(conduit);
    auto lines = new LineIterator!(char)(buffer);
    auto adaptor = new StreamIteratorAdaptor!(char)(lines);
    auto predicate = delegate bool (char[] line) { return line.length > 0 && line[0] != '#';};
    auto iterator = new FilteringIterator!(char[])(adaptor, predicate);

    parseConfig(iterator.get());
    parseConfig(iterator.get());
    foreach (line; iterator)
    {
        parseData(line);
    }
}
Author Message

Posted: 09/26/07 10:13:55

I think you have mixed a couple of things that were not originally intended to be mixed.

FilteringIterator is part of the collections package and thus meant to filter out hits when iterating over objects in a collection. LineIterator on the other hand is a descendant of StreamIterator, see this page.

So I would probably suggest either ignoring comment lines found by LineIterator or subclass LineIterator or StreamIterator for your more specific needs?