Files

Files

The manipulation of files is extraordinary easy using PicoLisp. One can read,write,append files in just a few lines of code.

Reading Files Using PicoLisp.

In order to read a file we use the command "in" from PicoLisp. The description of the "in" command follows from the PicoLisp Documentation Page (https://software-lab.de/doc/index.html)


(in 'any . prg) -> any






Opens any as input channel during the execution of prg. The current input channel will be saved and restored appropriately. If the argument is NIL, standard input is used. If the argument is a symbol, it is used as a file name (opened for reading and writing if the first character is "+"). If it is a positive number, it is used as the descriptor of an open file. If it is a negative number, the saved input channel such many levels above the current one is used. Otherwise (if it is a list), it is taken as a command with arguments, and a pipe is opened for input. The (system dependent) exit status code of the child process is stored in the global variable @@. See also ipidcallloadfileouterrpollpipe and ctl.



The following function is used to read a file and show its contents on the computer screen:

#read a file
(de readFile(filename) (in filename (until (eof) (println (line T)))))

Appending Files Using PicoLisp.

Appending data into a file means that we add new data at the end of the file. In order to append we need to add a "+" plus sign in front of the filename. The following function will append data into filename. Both filename and data are given as arguments to the function appendFile:

#appends data to filename
(de appendFile (filename data)(out (pack "+" filename) (println data)))

Writing Files Using Picolisp.

Writing data into a file means that we overwrite the file so as the old data saved before will be deleted for ever. We use "out" command to write data into a file. If the file begins with a plus sign "+" then the old data will be preserved and the file will be opened in appending mode:

#writes data to filename
(de writeFile (filename data) (out filename (println data)))

The description of the "out" command follows from the PicoLisp Documentation Page (https://software-lab.de/doc/index.html)








(out 'any . prg) -> any
Opens any as output channel during the execution of prg. The current output channel will be saved and restored appropriately. If the argument is NIL, standard output is used. If the argument is a symbol, it is used as a file name (opened in "append" mode if the first character is "+"). If it is a positive number, it is used as the descriptor of an open file. If it is a negative number, the saved output channel such many levels above the current one is used. Otherwise (if it is a list), it is taken as a command with arguments, and a pipe is opened for output. The (system dependent) exit status code of the child process is stored in the global variable @@. In all cases, flush is called when prg is done. See also opidcallinerrctlpipepollclose and load.



Printing Data To Printer Using PicoLisp.

Output to printer means that we send data to the connected printer. Printing is done using "(out '(lpr) (prinl data))" where "lpr" is the printer.

#prints a data at printer
(de printText(data)(out '(lpr) (prinl data)))

Printing File To Printer Using PicoLisp

The whole file filename is printed using the following function:

#prints filename at printer
(de printFile(filename)(out '(lpr) (readFile filename)))

Open, Close, With File Descriptor.

We can open a file using the "open" PicoLisp command. It returns a file descriptor to be saved into a variable and used later for outputting data to this file. Read the following example:

: (setq fout (open "qq1.txt"))                             
-> 4
: (out fout (print "hello world and my friends!"))         
-> "hello world and my friends!"
: (out fout (print "hello world and my friends!"))
-> "hello world and my friends!"
: (out fout (print "hello world and my friends!"))
-> "hello world and my friends!"
: (out fout (print "hello world and my friends!"))
-> "hello world and my friends!"
: (close fout)

Commands of PicoLisp Used in Page.

The description of the commands used in the functions of this page are at  the PicoLisp Documentation Page (https://software-lab.de/doc/index.html) and a copy of them follows in order to easy your reading.

(open 'any ['flg]) -> cnt | NIL
Opens the file with the name any in read/write mode (or read-only if flg is non-NIL), and returns a file descriptor cnt (or NIL on error). A leading "@" character in any is substituted with the PicoLisp Home Directory, as it was remembered during interpreter startup. If flgis NIL and the file does not exist, it is created. The file descriptor can be used in subsequent calls to in and out. See also close and poll.

: (open "x")
-> 3


(close 'cnt) -> cnt | NIL
Closes a file descriptor cnt, and returns it when successful. Should not be called inside an out body for that descriptor. See also openpolllisten and connect.

: (close 2)                            # Close standard error
-> 2



(until 'any . prg) -> any
Conditional loop: While the condition any evaluates to NILprg is repeatedly executed. If prg is never executed, NIL is returned. Otherwise the result of prg is returned. See also whileforloopand do.




(eof ['flg]) -> flg
Returns the end-of-file status of the current input channel. If flgis non-NIL, the channel's status is forced to end-of-file, so that the next call to eof will return T, and calls to charpeeklinefromtillread or skip will return NIL. Note that eof cannot be used with the binary rd function. See also eol.




(println 'any ..) -> any
Prints all any arguments to the current output channel, followed by a newline. If there is more than one argument, a space is printed between successive arguments. See also printprintsp.




(line 'flg ['cnt ..]) -> lst|sym
Reads a line of characters from the current input channel. End of line is recognized as linefeed (hex "0A"), carriage return (hex "0D"), or the combination of both. (Note that a single carriage return may not work on network connections, because the character look-ahead to distinguish from return+linefeed can block the connection.) If flg is NIL, a list of single-character transient symbols is returned. When cnt arguments are given, subsequent characters of the input line are grouped into sublists, to allow parsing of fixed field length records. If flg is non-NIL, strings are returned instead of single-character lists. NIL is returned upon end of file. See also charreadtill and eof.





(pack 'any ..) -> sym
Returns a transient symbol whose name is concatenated from all arguments any. A NIL arguments contributes nothing to the result string, a number is converted to a digit string, a symbol supplies the characters of its name, and for a list its elements are taken. See also text and glue.




(read ['sym1 ['sym2]]) -> any
Reads one item from the current input channel. NIL is returned upon end of file. When called without arguments, an arbitrary Lisp expression is read. Otherwise, a token (a number, an internal symbol, a transient symbol (for punctuation), or a list of symbols (for a string)) is read. In that case, sym1 specifies which set of characters to accept for continuous symbol names (in addition to the standard alphanumerical characters), and sym2 an optional comment character. See also anystrlineskip and eof.