home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
sa104os2.zip
/
SATHR104.ZIP
/
SATHER
/
LIBRARY
/
FILE.SA
< prev
next >
Wrap
Text File
|
1995-02-14
|
12KB
|
322 lines
-- Copyright (C) International Computer Science Institute, 1994. COPYRIGHT --
-- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
-- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in --
-- the file "Doc/License" of the Sather distribution. The license is also --
-- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA. --
--------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
-------------------------------------------------------------------
class FILE is
-- Buffered file. Uses the C standard "FILE". To avoid buffering
-- penalties, read or write entire files using an FSTR. For systems
-- which do funny things for linefeeds and cntl-z, this is a text
-- file, not a binary finle, for which you should use BFILE.
private attr fp:EXT_OB; -- The FILE pointer private attr
stdin_macro:EXT_OB is raise "FILE::stdin_macro undefined"; end;
stdout_macro:EXT_OB is raise "FILE::stdout_macro undefined"; end;
stderr_macro:EXT_OB is raise "FILE::stderr_macro undefined"; end;
stdin:SAME is
-- A file object for stdin.
r::=new; r.fp:=stdin_macro; return r end;
stdout:SAME is
-- A file object for stdout.
r::=new; r.fp:=stdout_macro; return r end;
stderr:SAME is
-- A file object for stderr.
r::=new; r.fp:=stderr_macro; return r end;
open_for_write(nm:STR):SAME is
-- Create a new file with name `nm' and default permissions.
-- File is truncated and opened for writing.
r::=new; r.fp:=C_FILE::fopen(nm,"w"); return r end;
open_for_read(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading.
r::=new; r.fp:=C_FILE::fopen(nm,"r"); return r end;
open_for_append(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for appending. File is created if not existing.
r::=new; r.fp:=C_FILE::fopen(nm,"a"); return r end;
open_for_update_truncating(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and writing. File is truncated if existing.
r::=new; r.fp:=C_FILE::fopen(nm,"w+"); return r end;
open_for_update(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and writing.
r::=new; r.fp:=C_FILE::fopen(nm,"r+"); return r end;
open_for_update_appending(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and appending. File is created if not existing.
r::=new; r.fp:=C_FILE::fopen(nm,"a+"); return r end;
eof:BOOL pre ~void(fp) is
-- true if EOF has been encountered. Cleared by "clear".
return C_FILE::feof(fp) end;
error:BOOL is
-- true is an error has occurred with this file. Cleared by "clear".
return void(fp) or C_FILE::ferror(fp) end;
clear pre ~void(fp) is
-- resets end_seen and error status
C_FILE::clearerr(fp) end;
size:INT pre ~void(fp) is
-- The size of self in characters. -1 for error.
current::=current_loc;
seek_from_end(0);
r::=current_loc;
seek_from_front(current);
return r end;
seek_from_front(off:INT) pre ~void(fp) is
-- Attempt to move to character `off' in self.
dummy::=C_FILE::fseek(fp,off,0) end;
seek_from_current(off:INT) pre ~void(fp) is
-- Attempt to move `off' characters forward.
dummy::=C_FILE::fseek(fp,off,1) end;
seek_from_end(off:INT) pre ~void(fp) is
-- Attempt to move `off' characters back from the end of self.
dummy::=C_FILE::fseek(fp,off,2) end;
current_loc:INT pre ~void(fp) is
-- The current location in the file.
return C_FILE::ftell(fp) end;
fstr:FSTR pre ~void(fp) is
-- A string buffer containing the contents of self.
--
-- Due to conversions on some systems, the number of
-- characters read may be less than the total number
-- of characters in the file.
fsize::=size;
if fsize = -1 then return void; end;
r::=#FSTR(fsize);
seek_from_front(0);
bsize::=C_FILE::fread(r,1,fsize,fp);
r.loc:=bsize;
return r end;
fstr(start,msize:INT):FSTR pre ~void(fp) and start<size is
-- A string buffer of size at most `msize' containing successive
-- characters from self beginning at `start'.
--
-- The number of characters read may be less than 'msize'
-- if end-of-file was encountered or some characters have
-- been discarded due to system-specific conversions.
r::=#FSTR(msize);
seek_from_front(start);
bsize::=C_FILE::fread(r,1,msize,fp);
r.loc:=bsize;
return r end;
get_char:CHAR pre ~void(fp) is
return C_FILE::fgetc(fp); end;
get_str:STR is
-- A string containing the characters up to the next newline.
return get_line.str;
end;
get_line:FSTR is
-- A string buffer containing the characters up to the next newline.
res::=#FSTR;
loop
c::=get_char;
if eof then return res end;
res:=res.push(c);
until!(c='\n');
end;
return res;
end;
get_up_to(sc:CHAR):STR is
-- A string buffer containing the characters up to the next c.
res::=#FSTR;
loop
c::=get_char;
res:=res.push(c);
until!(c=sc);
end;
return res.str;
end;
plus(s:STR) pre ~void(fp) is
-- Write the string `s' at the current location in self.
-- Now MACROized to eliminate continual ".fstr"ing which
-- creates garbage on each output.
fs::=C_FILE::fwrite(s.fstr,s.length,1,fp) end;
plus(s:STR):SAME pre ~void(fp) is plus(s); return self; end;
plus(f:FSTR) pre ~void(fp) is
-- Write the string buffer `s' at the current location in self.
fs::=C_FILE::fwrite(f,f.size,1,fp) end;
plus(f:FSTR):SAME pre ~void(fp) is plus(f); return self; end;
plus(c:CHAR) pre ~void(fp) is
-- Write the character `c' at the current location.
C_FILE::fputc(c, fp) end;
plus(c:CHAR):SAME pre ~void(fp) is plus(c); return self; end;
plus(i:INT) pre ~void(fp) is
-- Write the INT `i' as an ascii decimal at the current location.
plus(i.str) end;
plus(i:INT):SAME pre ~void(fp) is plus(i); return self; end;
flush pre ~void(fp) is
-- Forces write of any buffered data.
C_FILE::fflush(fp) end;
buffer_size(sz:INT) pre ~void(fp) is
-- Set buffer size to `sz' bytes. Must be called after
-- a file is opened but before reading or writing.
-- C_FILE::setbuffer(fp,#FSTR(sz),sz) end; -- NLP
C_FILE::setbuffer(fp,sz) end; -- NLP
close pre ~void(fp) is
-- Close the file corresponding to self.
C_FILE::fclose(fp) end;
delete(nm:STR) is
-- Delete a file.
C_FILE::unlink(nm) end;
temp_file:SAME is
-- Open a temporary file for writing. It will be deleted
-- automatically when the process terminates.
r::=new; r.fp:=C_FILE::tmpfile; return r end;
temp_filename(prefix:STR):STR is
-- Generate a unique name somewhere appropriate.
e::=C_FILE::tempnam(void,prefix);
if void(e) then raise "Couldn't get temporary filename"; end;
return STR::create_from_c_string(e);
end;
resolve_path(fn:STR):STR is
-- Path with ".", "..", and any symbolic links resolved.
-- NOT IMPLEMENTED CORRECTLY.
return fn;
end;
current_directory:STR is
-- Absolute path of the current directory.
buf::=#FSTR(1024); -- Buffer to hold C string
e::=C_FILE::getwd(buf);
if void(e) then raise "Couldn't get current directory path"; end;
return STR::create_from_c_string(e);
end;
create_directory(nm:STR) is
-- Make a directory with the path `nm'.
-- C_FILE::mkdir(nm,0o777); -- NLP
C_FILE::mkdirxx(nm); -- NLP
end;
end; -- class FILE
class BFILE is
-- A binary file. For many systems this should behave the
-- same as FILE. This class is untested and not used in the
-- compiler.
include FILE;
open_for_write(nm:STR):SAME is
-- Create a new file with name `nm' and default permissions.
-- File is truncated and opened for writing.
r::=new; r.fp:=C_FILE::fopen(nm,"wb"); return r end;
open_for_read(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading.
r::=new; r.fp:=C_FILE::fopen(nm,"rb"); return r end;
open_for_append(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for appending. File is created if not existing.
r::=new; r.fp:=C_FILE::fopen(nm,"ab"); return r end;
open_for_update_truncating(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and writing. File is truncated if existing.
r::=new; r.fp:=C_FILE::fopen(nm,"w+b"); return r end;
open_for_update(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and writing.
r::=new; r.fp:=C_FILE::fopen(nm,"r+b"); return r end;
open_for_update_appending(nm:STR):SAME is
-- A new object representing the file named `nm' accessible
-- for reading and appending. File is created if not existing.
r::=new; r.fp:=C_FILE::fopen(nm,"a+b"); return r end;
fstr:FSTR pre ~void(fp) is
-- A string buffer containing the contents of self.
sz::=size;
if sz= -1 then return void; end;
r::=#FSTR(sz);
seek_from_front(0);
fs::=C_FILE::fread(r,sz,1,fp);
r.loc:=sz;
return r end;
fstr(st,sz:INT):FSTR pre ~void(fp) is
-- A string buffer of size at most `sz' containing successive
-- characters from self beginning at `st'.
fsz::=size;
asz::=(fsz-st).min(sz);
r::=#FSTR(asz);
seek_from_front(0);
fs::=C_FILE::fread(r,asz,1,fp);
r.loc:=asz;
return r end;
end;
-------------------------------------------------------------------
external class C_FILE is
fopen(nm:STR,tp:STR):EXT_OB;
feof(fp:EXT_OB):BOOL;
ferror(fp:EXT_OB):BOOL;
clearerr(fp:EXT_OB);
fclose(fp:EXT_OB);
fseek(fp:EXT_OB,off:INT,loc:INT):INT;
ftell(fp:EXT_OB):INT;
fread(fs:FSTR,sz,items:INT,fp:EXT_OB):INT;
fwrite(fs:FSTR,sz,items:INT,fp:EXT_OB):INT;
fputc(c:CHAR,fp:EXT_OB);
fgetc(fp:EXT_OB):CHAR;
unlink(s:STR);
tmpfile:EXT_OB;
tempnam(dir:EXT_OB,pfx:STR):EXT_OB;
fflush(fp:EXT_OB);
-- setbuffer(fp:EXT_OB,buf:FSTR,sz:INT); -- NLP
setbuffer(fp:EXT_OB,sz:INT); -- NLP
getwd(buf:FSTR):EXT_OB;
-- mkdir(nm:STR,md:INT); -- NLP
mkdirxx(nm:STR); -- NLP
end; -- external class C_FILE
-------------------------------------------------------------------