Simple File Input/Output with GIO

Introduction

This is a simple tutorial for reading and writing to text files using Gnome’s GIO library. I may write a more detailed one in the future but the reason for this one is that I created the program to be used as an example for this post so that I can convert JavaScript code so that they contain HTML entity tags.

Program

The JavaScript program is as follows:

   1 #!/usr/bin/env seed
   2 
   3 var gio = imports.gi.Gio;
   4 var glib = imports.gi.GLib;
   5 
   6 function get_line_from_file() 
   7 {
   8   var input_file = gio.file_new_for_path(Seed.argv[2]);
   9   var fstream = input_file.read();
  10   var dstream = new gio.DataInputStream.c_new(fstream);
  11   var line = dstream.read_until(“”, 0);
  12 
  13   fstream.close();
  14   return (line);
  15 }
  16 
  17 function convert_line(text_line)
  18 {
  19   var changed_line = ”";
  20 
  21   for (var x = 0; x < text_line.length; x = x +1) 
  22   {
  23     var curr_char = text_line[x];
  24     var curr_char = glib.utf8_get_char(curr_char);
  25     switch (curr_char)
  26     {
  27       // If a space then add its HTML entity code 
  28       case 32:
  29         changed_line = changed_line + ” ”;
  30         break;
  31       // If a tab then add two HTML entity codes for space 
  32       case 9:
  33         changed_line = changed_line + ”  ”;
  34         break;
  35       // If < then add its HTML entity code 
  36       case 60:
  37         changed_line = changed_line + ”<”; 
  38         break;
  39       // If > then add its HTML entity code 
  40       case 62:
  41         changed_line = changed_line + ”>”; 
  42         break;
  43       // The new line character is added only for the purpose of making
  44       // text file easier to read
  45       case 10:
  46         changed_line = changed_line + ”<br>” + String.fromCharCode(curr_char); 
  47         break;
  48       // Anything else change back to character and add 
  49       default:
  50         changed_line = changed_line + String.fromCharCode(curr_char);
  51     }
  52   }
  53   return(changed_line);
  54 }
  55 
  56 function write_to_file(text_line)
  57 {
  58   var output_file = gio.file_new_for_path(“temp.txt”);
  59   var fstream = output_file.replace();
  60   var dstream = new gio.DataOutputStream.c_new(fstream);   
  61 
  62   dstream.put_string(text_line);
  63   fstream.close();
  64 }
  65 
  66 function main()
  67 {
  68   var line = get_line_from_file();
  69   var line = convert_line(line);
  70   write_to_file(line);
  71 }
  72 
  73 main();

Explanation

The program is structured into a number of functions. A brief description of these are as follows:

get_line_from_file: This opens the file specified as an argument when running the program at the command part and reads its contents into a string.

convert_line: This takes the string read in from file and goes through each character and writes it a new string depending on what it is

write_to_file: This writes the converted string from last function and writes it to text file called temp.txt

main: This function ‘makes’ the program run by calling all the above functions. This isn’t required but I used it to make it more readable.

The relevant functions for the post is the get_line_from_file & write_to_file functions.

Before anything can be done, the GIO library needs to be imported. This is done by the following JavaScript command:

var gio = imports.gi.Gio;

Next, whether you are reading or writing to a file, you need to access the file by associating it with a GLocalFile object. This is done by:

var file = gio.file_new_for_path(“temp.txt”);

In the above JavaScript code line, the file temp.txt is associated with the GLocalFile object variable called file. The file can exist or not but for obvious reasons an error will occur if you use a non-existant file for reading.

To be able to read or write to file, the relevant file and data streams need to be created. The file stream is required to read the contents of file into a ’stream’. The data stream is required so that the contents of the file stream created earlier can be used by data types used by Seed like string, integers, etc.

To write to a file, it is the other way round where a datastream is created to be able to use Seed data types to write to the file via the file stream.

As mentioned, he code to read from file is from get_line_from_file. The JavaScript code for this is as follows:

var input_file = gio.file_new_for_path(Seed.argv[2]);

This creates a GLocalFile object variable called input_file which ‘associates’ it with the file defined at command prompt argument ie Seed.argv[2]

var fstream = input_file.read();

This opens the file for reading and in the process creates an Input file stream which can only be read. The name of this variable is fstream. The file can be opened to be read in a number of different ways but for the purpose of this post it opened as read.

var dstream = new gio.DataInputStream.c_new(fstream);

This creates a Data Input Stream called called dstream which uses the File Input Stream created earlier.

var line = dstream.read_until(“”, 0);

With the Data Input stream, the contents of the file can finally be used. In the above statement, the whole contents of the file is copied into a string variable called line. There are various other ways to read from the data stream to different data types like integers or characters.

fstream.close();

This closes the File Stream.

For writing to file, the JavaScript code is in the write_to_file function.

var output_file = gio.file_new_for_path(“temp.txt”);

This creates a GLocalFile object variable called output_file which ‘associates’ it with the file “temp.txt”

var fstream = output_file.replace();

This creates a File Output stream called fstream. The file stream can be opened in a number of files to be written to. For the program, the text file “temp.txt” is replaced with an empty text file is it exist else creates a new empty file. Other ways to opening a file for writing is creating a new file, appending to an existing one or opening it for both reading and writing. Due to this nature, if the file does not exist, an error will not occur as it will be created.

var dstream = new gio.DataOutputStream.c_new(fstream);

This creates a Data Output Stream called called dstream which uses the File Input Stream created earlier.

dstream.put_string(text_line);

They ‘puts’ a string of text into the Data Output stream which then puts it into File stream to write to file. There are different function that allows different data types like integers etc to the Data Stream.

fstream.close();

This closes the File stream.

Projects/Seed/Tutorial/Simple_file_io (last edited 2013-11-22 19:19:57 by WilliamJonMcCann)