Building on Frederik's answer, I built myself an iteratable interface to
line reading which looks just like python. This is already pretty. :-)

I don't understand how the following works though:

            next_line = fi.dis.read_line (null, null) + "\n";
            return next_line!=null;

I would rather write it:

            next_line = fi.dis.read_line(null,null);
            if (next_line != null)
                next_line = next_line + "\n"

but that gives me an error of:

            iter.vala:20.29-20.44: error: Arithmetic operation not supported
for types `string' and `string'
                next_line = next_line + "\n";

why? What's the difference?

Regads,
Dov

// Dov Grobgeld <[EMAIL PROTECTED]>
// This code is in the public domain.
using GLib;

class FileIter : GLib.Object, Gee.Iterable<string> {
    private class Iterator<string> : GLib.Object, Gee.Iterator<string> {
        weak FileIter fi {set; get; }
        string 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 string 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;
}


2008/6/9 Dov Grobgeld <[EMAIL PROTECTED]>:

> Ok. Thanks. I see that it is possible, though the syntax is not what I
> would call pretty...
>
> ...which made me thinking, would it be possible to write something that use
> the iterator interface for reading lines from a file? I.e. something like:
>
> File f = File.open("path");
>
> foreach (string s in f.readlines()) {
>     print s;
> }
>
> What exactly is an iterator? An interface? Where is it defined?
>
> Regards,
> Dov
>
> 2008/6/8 Frederik <[EMAIL PROTECTED]>:
>
> Dov Grobgeld schrieb:
>>
>>> In my efforts to of making sure that I can do simple constructs with the
>>> language I tried to read a file line by line.
>>>
>>
>> Use GIO instead, it's part of the GLib namespace.
>>
>> http://library.gnome.org/devel/gio/stable/
>>
>> File.read () will give you a FileInputStream which you can decorate as
>> a DataInputStream, then you can read line by line.
>>
>> ------
>>
>> File my_file = File.new_for_path ("myfile.data");
>>
>> DataInputStream in_stream;
>>
>> try {
>>        in_stream = new DataInputStream (my_file.read (null));
>>        string line;
>>        while (null != (line = in_stream.read_line (null, null))) {
>>                print (line);
>>        }
>> } catch (Error e) {
>>        critical (e.message);
>> } finally {
>>        in_stream.close (null);
>> }
>>
>> ------
>>
>> You will have to compile with "--pkg gio-2.0". Currently you will get
>> some warnings about nullable parameters, since they aren't marked yet in
>> the vapi bindings, which you can safely ignore.
>>
>>
>>
>> Frederik
>>
>> _______________________________________________
>> Vala-list mailing list
>> [email protected]
>> http://mail.gnome.org/mailman/listinfo/vala-list
>>
>
>
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to