next up previous contents index
Next: Communication with Subprocesses Up: Library Utilities Previous: String Manipulation   Contents   Index

Script Writing Utilities

Prolog, (or XSB) can be useful for writing scripts in a UNIX system. Prolog's simple syntax and declarative semantics make it especially suitable for scripts that involve text processing. Wherever noted, some of these functions are currently working under Unix only.

date(?Date)
scrptutl

Unifies Date to the current date, returned as a Prolog term, suitable for term comparison. Currently this only works under Unix, is slow, and should be rewritten in C using time() and localtime().

Example:


                > date 
                Thu Feb 20 08:46:08 EST 1997
                > xsb -i
               XSB Version 1.7
               [sequential, single word, optimal mode]
               | ?- [scrptutl].
               [scrptutl loaded]

               yes
               | ?- date(D).
               D = date(1997,1,20,8,47,41)

               yes

file_time(+FileName, -time(Time1,Time2))
file_io

Returns file's modification time. Because XSB steals 5 bits from each word, time must be returned as two words: Time1, representing the most significant digits, and Time2, representing the less significant digits.

file_size(+FileName, -Size)
file_io

Returns file size.

directory(+Path,?Directory)
directry

Unifies Directory with a list of files in the directory specified by path. Information about the files is similar to that obtained by ls -l, but transformed for ease of processing. This currently works for Unix only, is slow, and should be reimplemented in C using opendir() and readdir().

expand_filename(+FileName,-ExpandedName)
machine

Expands the file name passed as the first argument and binds the variable in the second argument to the expanded name. This includes expanding Unix tildas, prepending the current directory, etc. In addition, the expanded file name is ``rectified'' so that multiple repeated slashes are replaced with a single slash, the intervening ``./'' are removed, and ``../'' are applied so that the preceding item in the path name is deleted. For instance, if the current directory is /home, then abc//cde/..///ff/./b will be converted into /home/abc/ff/b.

Under NT and Windows, this predicates does rectification (using backslashes when appropriate), but it does not expand the tildas.

tilde_expand_filename(+FileName,-ExpandedName)
machine
Like expand_filename/2, but only expands tildas and does rectification. This does not prepend the current working directory to relative file names.

is_absolute_filename(+FileName)
machine
Succeeds, if file name is absolute; fails otherwise. This predicate works also under NT and Windows, i.e., it recognizes drive letters, etc.

parse_filename(+FileName,-Dir,-Base,-Extension)
machine
This predicate parses file names by separating the directory part, the base name part, and file extension. If file extension is found, it is removed from the base name. Also, directory names are rectified and if a directory name starts with a tilde (in Unix), then it is expanded. Directory names always end with a slash or a backslash, as appropriate for the OS at hand.

For instance, $\sim$john///doe/dir1//../foo.bar will be parsed into: /home/john/doe/, foo, and bar (where we assume that /home/john is what $\sim$john expands into).

file_to_list(IOport, List)
scrptutl
s Read lines from an open I/O port. Return a list of terms, one per each line read. Each such term is a list of tokens on the corresponding line. Tokens are lists of characters separated by a space symbol (space, newline, return, tabs, formfeed). For instance, if IOport 10 is bound to a file

ads sdfdsfd ee
112 444
4555
then

| ?- file_to_list(10, L).  
L = [[ads,sdfdsfd,ee],[112,444],[4555]]
yes
Note: file_to_list/2 does not close the I/O port, so it is an application program responsibility.


next up previous contents index
Next: Communication with Subprocesses Up: Library Utilities Previous: String Manipulation   Contents   Index
Baoqiu Cui
2000-04-23