Re: [Vala] readline from a FileStream?



I have filed a bug report / feature request:

http://bugzilla.gnome.org/show_bug.cgi?id=538405

To make clear what you're currently doing I have slightly modified your
code. Your code works as long as you won't do any other operation
than assignment on 'next_line'. Perhaps valac should warn when using an
existing type name as a generic type placeholder.

PS: If you use a newer version of Vala/libgee you'll have to mark the
return type of Iterable.get() as nullable:

public G? get() { ... }


-------

// Dov Grobgeld <dov grobgeld gmail com>
// This code is in the public domain.
using GLib;

class FileIter : GLib.Object, Gee.Iterable<string> {
    private class Iterator<G> : GLib.Object, Gee.Iterator<G> {
        weak FileIter fi {set; get; }
        G next_line;

        public Iterator (FileIter file_iter) {
            this.fi = file_iter;
        }
        public bool next() {
            next_line = fi.dis.read_line (null, null) + "\n";
            return next_line!=null;
        }
        public G get() {
            return next_line;
        }
    }

    private DataInputStream dis { get; set;}

    public FileIter(DataInputStream in_stream)
    {
        this.dis = in_stream;
    }

    public static FileIter? open(string filename)
    {
        File my_file = File.new_for_path (filename);
DataInputStream in_stream = new DataInputStream (my_file.read (null));
        return new FileIter(in_stream);
    }

    GLib.Type get_element_type()
    {
        return typeof(string);
    }

    Gee.Iterator<string> iterator()
    {
        return new Iterator<string> (this);
    }
}

int main(string[] args) {
    var fi = FileIter.open(args[1]);

    foreach (string s in fi)
        print(s);

    return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]