genie-logo

File and console I/O

Page updated October 27, 2009
There is not yet an official logo for Genie/Vala, above image just a placeholder.

Introduction

Genie gives you access to all the low-level C functions, everything in the libc library. Genie accesses the libglib library which is a wrapper around the lower-level libc functions.

To find out what Glib (libglib) has to offer, look here:

http://references.valadoc.org/glib-2.0.html

Glib is described in greater depth here:

http://library.gnome.org/devel/glib/stable/

The latter URL is intended for C programmers, however it is usually not to difficult to figure out how it all applies to Genie.

Digging down into the above sites, we can find information about classes and functions related to file and console I/O. What follows is my basic intro...

File I/O

One place to look are the FileUtils group of functions, described here:
http://references.valadoc.org/glib-2.0/GLib.FileUtils.html
http://library.gnome.org/devel/glib/stable/glib-File-Utilities.html

Class FileUtils has the following static methods (functions) (the term "static" is explained further down this page):

get_contents, set_contents, test, open_tmp, read_link, mkstemp, rename, unlink, chmod, symlink

This how to read the contents of an entire file:
init
s:string
len:ulong
FileUtils.get_contents("/etc/rc.d/PUPSTATE",out s,out len)
Note the out qualifier on two of the passed parameters -- this is explained in my Genie functions page.

FileStream stream I/O

All file and console I/O can be handled by the FileStream class. Console I/O is covered further down, but firstly looking at file I/O...

The most appropriate place to look for documentation is here:
http://references.valadoc.org/glib-2.0/GLib.FileStream.html
/usr/share/vala/vapi/glib-2.0.vapi

Here is a program to read a file one line at a time:
init
var f = FileStream.open("/etc/rc.d/PUPSTATE","r")
var a = new array of char[64]
while f.gets(a) != null
for s in a do print "%c",s //or whatever
There is a technical detail about classes and objects that I should mention here. FileStream is a class, that is, it is not instantiated. If instantiated, it becomes an object, that is, something that actually exists in the memory. Normally, an object must exist to be able to execute its methods, however there are such things as static methods...

Static methods
If you are not familiar with classes and objects then the above paragraph may be bordering on gibberish! Never mind, I'll reword it.
A class is really only a definition, and it has to be created in memory to be usable -- which we then call an object, and the process instantiation.
Once the object exists, there are then various methods (functions) available to act on it.
However, a class may have some functions that can execute without needing an instantiation. The above URL has this information about FileStream:
Static methods: open, fdopen
Methods: printf, putc, puts, getc, gets, eof, scanf, seek, tell, rewind
The static methods operate directly on the class.

What the first line of the above program does is call the open() function of the FileStream class, and that actually creates an object (of the FileStream class), which I have assigned to f.

Once we have an object, we can then call any of its methods:

printf, putc, puts, getc, gets, eof, scanf, seek, tell, rewind

I have called the gets() function, which reads a line (until a carriage-return character) in a while-loop until all of the file has been read.

There is one awkward thing about gets() -- unlike the plain C version, this one returns the read line as an array of characters, which is a bit difficult to do anything with. Usually it would be more useful as a string. The next section introduces console I/O and the example code shows how the array of chars returned by gets() can be converted to a string...

Console I/O

The FileStream class is introduced above. Console I/O is really a specific case of this generic mechanism.

FileStream class is already instantiated as objects stdin and stdout, and gets() is a method of FileStream class, so we can call it like this:
init
var a = new array of char[64]
stdin.gets(a)
a[a.length - 1] = 0
s:string = (string)a
print(s)
What the stdin.gets(a) does is read one line from the keyboard and store as an array of characters.

What I really want though is a string (thanks to Thomas on the vala-list who advised me how to do this conversion). Strings in Genie have to be zero-terminated, so  a[a.length - 1] = 0 takes care of that. Then s:string = (string)a castes the array of char a into a string s.

Casting
If we just had s:string = a, the compiler would object, as they are two different data types. However, what is an array of characters anyway? -- with a zero as the last value, it is really just like a null-terminated string. So in this case we are saying to the compiler, "it's ok, treat this like a string".
Casting can also do limited conversions, like caste a integer to a long or to a float.





(c) Copyright 2008,2009 Barry Kauler puppylinux.com, all reproduction rights reserved.