Thursday, May 24, 2012

File operations in Fuzuli

Fuzuli, our new programming language and interpreter, was first introduced in Fuzuli: A new general purpose interpreter and language has several input and output functions completely derived from standard C and C++.

Since our language is in its first stages and input and output functions have high priority, Fuzuli has adequate number of functions for those jobs.

First of all, the io package must be included before any function call. This package is stored in /usr/lib/fuzuli/nfl by default. Nfl packages (Nafile) have a similar file extension with the name Fuzuli. It means "useless" in English.

In Fuzuli, a package can be included using the require function:


(require "/usr/lib/fuzuli/nfl/io.nfl")


Fuzuli has fopen, fwrite, fread and fclose functions for opening, writing, reading and closing of files,respectively. The code shown below shows how to open file for writing.

(let f (fopen "/tmp/example.txt" "w"))
(let liste (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14))
(fwrite f liste)
(fclose f)


In the example above, we are opening the file example.txt for writing and writing elements of a list in it. This list contains integer numbers from 1 to 14. fwrite takes the reference of file and object to be written as parameters. A single element can be written in same manner. Finaly we are closing the file using 'fclose'. The example shown below shows how to read from values from the same file:

(let dd (fopen "/tmp/example.txt"  "r"))
(let a 0)
(let mysum 0)
(for (let i 0) (< i 5) (inc i)
                (let mysum (+ mysum (fread dd a)))
)


In the example above, dd is a reference to the example.txt. Note that, fopen shares the same file mode types with C++. So "r" means, the file will be opened in "read" mode. Variables "a" and "mysum" are set to zero before the loop. In loop, we are reading the next object using "fread" from the file reference "dd". mysum holds the sum of first 5 integers. The result is 15. Question: How fread knows the data type? The answer is easy. "a" is set using (let a 0) so a is an integer. If "a" is defined using (let a 0.1) then "a" is a double and fread will read a double from the file. In previous example, 5 objects were read from the file. Sometimes, it is impossible to know the number of records of objects that contained by the file. feof function can be used in a loop as in Fuzuli's parents.

(def a INTEGER)
(let dd (fopen "/tmp/kek.txt"  "r"))
(while (= (feof dd) 0)
        (block
                (print (fread dd a) " ")
        )
)
(fclose dd)


In the example above, the value of "a" is not set but it is defined as integer. We are reading from the file in a while loop and the stop condition is reaching the end of file. As we mentioned above, fread reads integers because of the type of variable "a". Fuzuli has several input and output functions beyond the file operations. The function fflush is used for writing bytes stored in the buffer.

(let f (fopen "/tmp/example.txt" "w"))
(let liste (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14))
(fwrite f liste)
(fflush f)
(fclose f)


In the example above, fflush sends content of the file buffer to the opened file after each fwrite operation. getpdw function is to get the working directory:

(let mydir (getpwd))
(print mydir "\n")


With chdir, current path can be changed:

(chdir "/tmp")


Function dir returns a list of directories and files of a given directory:

(let mydir (dir "."))
(foreach direntry in mydir
        (block
                (print direntry "\n")
        )
)


In the example above, dir takes a parameter of "." so content of the current directory is shown. Function unlink and rename deletes and renames files, respectively:

(let mydir (dir "."))
(unlink "/tmp/example.txt")
(rename "1.txt" "2.txt")


In the example above, example.txt was deleted and name of 1.txt is set to 2.txt.

Fuzuli currently has those io functions in io.nfl:
  • fopen
  • fclose
  • feof
  • fwrite
  • fread
  • fflush
  • chdir
  • getpwd
  • dir
  • unlink
  • rename
  • tmpfile
  • tmpnam
  • datetime
  • asctime
  • sleep
  • getenv
  • rnd
  • print_r
  • popen
  • pclose

There are several input and output functions in Fuzuli. Please have a look at the Fuzuli Documentation in Fuzuli Source Code page for further information. We will explain the language and its libraries with more examples in next posts.

1 comment:

Thanks