Forum Navigation
read filtered tokens from file
Posted: 09/26/07 09:54:26Hello,
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); } }