The following predicates are relevant to stream I/O.
Character, byte and line counts are maintained per stream. All
streams connected to the terminal, however, share the same set of
counts. For example, writing to user_output
will advance the
counts for user_input
, if both are connected to the
terminal. Bidirectional streams use the same counters for input
and output.
Wherever a stream argument appears as input (+Stream), a stream alias can be used instead.
absolute_file_name(
+FileSpec,
-AbsFileName)
absolute_file_name(
+FileSpec,
-AbsFileName,
+Options)
If FileSpec is user
, then AbsFileName is
unified with user
; this “file name” stands for the
standard input or output stream, depending on context. Otherwise,
unifies AbsFileName with the first absolute file name that
corresponds to the relative file specification FileSpec and
that satisfies the access modes given by Options. Options
is a list of zero or more of the following, the default being the
empty list:
ignore_underscores(
+Boolean)
true
or false
.
If true
, when constructing an absolute
file name that matches the given access modes, two names are tried:
First the absolute file name derived directly from FileSpec, and
then the file name obtained by first deleting all underscores from
FileSpec.
If false (default), suppresses any deletion of underscores.
extensions(
+Ext)
'.pl'
) that should be tried
when constructing the absolute file name. The extensions are tried in
the order they appear in the list. Default value is Ext =
[”], i.e. only the given FileSpec is tried, no extension is
added. To specify extensions('')
or extensions([]
) is
equal to not giving any extensions option at all.
file_type(
+Type)
extensions(Ext)
will be more portable between operating systems.
This extension mechanism has no effect if FileSpec contains a file
extension. Type must be one of the following atoms:
text
source
object
ql
saved_state
foreign_file
foreign_resource
directory
absolute_file_name/3
access directories
without raising an exception.
access(
+Mode)
read
write
append
exist
none
file_errors(
+Val)
fileerrors(
+Val)
fileerrors
Prolog flag:
error
access
option.
fail
solutions(
+Val)
first
all
file_errors(fail)
.
relative_to(
+FileOrDirectory)
''
, file names will be
treated as relative to the current working directory. If a regular,
existing file is given, file names will be treated as relative to the
directory containing FileOrDirectory. Otherwise, file names will
be treated as relative to FileOrDirectory.
If absolute_file_name/3
is called from a goal in a file
being loaded, the default is the directory containing that file.
Otherwise, the default is the current working directory.
The functionality of absolute_file_name/3
is most easily described as a four phase process, in which each phase
gets an infile from the preceding phase, and constructs one or more
outfiles to be consumed by the succeeding phases. The phases are:
Each of the three first phases modifies the infile and produces variants
that will be fed into the succeeding phases. The functionality of all
phases but the first are decided with the option list. The last
phase checks if the generated file exists, and if not asks for a new
variant from the preceding phases. If the file exists, but doesn't obey
the access mode option, a permission exception is raised. If the file
obeys the access mode option, absolute_file_name/3
commits to
that solution, subject to the solutions
option, and unifies
AbsFileName with the file name. For a thorough description, see
below.
Note that the relative file specification FileSpec may also be of the form Path(FileSpec), in which case the absolute file name of the file FileSpec in one of the directories designated by Path is returned (see the description of each phase below).
relative_to
option. There can be
more than one solution, in which case the outfile becomes the solutions
in the order they are generated. If the succeeding phase fails, and
there are no more solutions, an existence exception is raised.
ignore_underscores
option.
extensions
and file_type
options.
access
option.
Comments:
absolute_file_name/3
succeeds, and the file access option was one
of {read, write, append}
, it is guaranteed that the file can be
opened with open/[3,4]
. If the access option was exist
, the file
does exist, but might be both read and write protected.
file_type(directory)
is not given,
the file access option is other than none
, and
a specified file refers to a directory, then
absolute_file_name/3
signals a permission error.
absolute_file_name/[2,3]
is sensitive to the fileerrors
Prolog flag, which determines whether the predicate should fail or
raise permission errors when encountering files with the wrong
permission. Failing has the effect that the search space always is
exhausted.
open/[3,4]
see/1
tell/1
consult/1
reconsult/1
compile/1
fcompile/1
load/1
ensure_loaded/1
use_module/[1,2,3]
load_files/[1,2]
load_foreign_files/2
load_foreign_resource/1
unload_foreign_resource/1
save_modules/2
save_predicates/2
save_files/2
restore/1
save_program/[1,2]
To check whether the file my_text exists in the home directory, with one of the extensions `.text' or `.txt', and is both writable and readable:
| ?- absolute_file_name('~/my_text', File, [extensions(['.text','.txt']), access([read,write])]).
To check if the Prolog file same_functor exists in some library, and also check if it exists under the name samefunctor:
| ?- absolute_file_name(library(same_functor), File, [file_type(source), access(exist), ignore_underscores(true)]).
file_search_path(
+Path,
-Expansion)
hookuser:file_search_path(
+Path,
-Expansion)
The predicate is undefined at startup, but behaves as if it were a
dynamic, multifile predicate with the following
clauses. See State Info for more info on the Prolog flag
host_type
. The environment variables SP_APP_DIR and
SP_RT_DIR expand respectively to the absolute path of the
directory that contains the executable and the directory that contains
the SICStus run-time.
file_search_path(library, Path) :- library_directory(Path). file_search_path(system, Platform) :- prolog_flag(host_type, Platform). file_search_path(application, '$SP_APP_DIR'). file_search_path(runtime, '$SP_RT_DIR').
library_directory(
-Directory)
hookuser:library_directory(
-Directory)
library(
Name)
is used. The predicate is
undefined at startup, but behaves as if it were a dynamic,
multifile predicate with a single clause defining the
location of the Prolog library. The initial value is the same as the
value of the environment variable SP_LIBRARY_DIR. The
predicate may succeed nondeterminately in this search for a
library directory.
open(
+FileName,
+Mode,
-Stream)
ISOopen(
+FileName,
+Mode,
-Stream,
+Options)
ISOfopen
) and the resulting stream is unified with
Stream. Mode is one of:
read
write
append
If FileName is an integer, it is assumed to be a file descriptor
passed to Prolog from C. The file descriptor is connected to a Prolog
stream (invoking the POSIX function fdopen
) which is
unified with Stream.
Options is a list of zero or more of:
type(
+T)
text
or binary
stream. Default is text
.
reposition(
+Boolean)
true
), or not (false
). The latter is the default.
alias(
+A)
eof_action(
+Action)
end_of_file
), and a
further attempt to input is made. Action can have the following
values:
error
eof_code
end_of_file
) is returned
again.
reset
wcx(
Wcx)
close(
+X)
ISOclose(
+X,
+Options)
ISOsee/1
or
tell/1
, the corresponding stream is closed. Options
is a list possibly containing the following element:
force(
Boolean)
true
), or not
(false
). The latter is the default. Currently this option has no
effect.
current_input(
?Stream)
ISOSP_curin
.
current_output(
?Stream)
ISOSP_curout
.
current_stream(
?FileName,
?Mode,
?Stream)
set_input(
+Stream)
ISOset_output(
+Stream)
ISOflush_output
ISOflush_output(
+Stream)
ISOopen_null_stream(
-Stream)
character_count(
+Stream,
?N)
set_stream_position/2
.
byte_count(
+Stream,
?N)
character_count/2
in the presence of wide characters. The count
is reset by set_stream_position/2
.
line_count(
+Stream,
?N)
set_stream_position/2
.
line_position(
+Stream,
?N)
set_stream_position/2
.
stream_position(
+Stream,
?Position)
stream_position_data/3
.
stream_position_data(
?Field,
+Pos,
?Data)
line_count
, character_count
, line_position
,
byte_count
.
stream_property(
?Stream,
?Property))
ISOProperty can be one of the following:
file_name(
?F)
mode(
?M)
input
output
alias(
?A)
position(
?P)
stream_position(Stream, P)
.
end_of_stream(
?E)
not
; otherwise, (all
characters read) but no end of stream indicator (-1 or
end_of_file
) was reported yet, then E is unified with
at
; otherwise, E is unified with past
.
eof_action(
?A)
eof_action
option of open/4
.
type(
?T)
wcx(
?Wcx)
set_stream_position(
+Stream,
+Position)
ISOreposition(true)
was supplied at the successful opening of the stream, then
set_stream_position/2
is guaranteed to be successful.
seek(
+Stream,
+Offset,
+Method,
-NewLocation)
bof
current
eof
This operation is only available for Prolog streams connected to
“seekable devices” (disk files, usually) and is an interface to the
stdio
functions fseek
and ftell
. After applying
this operation, the character count, line count and line position
aspects of the stream position of Stream will be undefined.
at_end_of_stream
ISOat_end_of_stream(
+Stream)
ISO-1
) of the stream
have been read. These predicates peek ahead for next input
character if there is no character available on the buffer of
Stream. Unless the stream is to be treated as connected to
the terminal (see SP_force_interactive()
, Initializing the Prolog Engine), a stream remains at end of stream after
`EOF' has been read, and any further attempt to read from the
stream will raise an existence error (see Exception).
at_end_of_line
at_end_of_line(
+Stream)
fileerrors
obsolescentnofileerrors/0
.
nofileerrors
obsolescent