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 >
Text File  |  1995-02-14  |  12KB  |  322 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -------------------------------------------------------------------
  9. class FILE is
  10.    -- Buffered file.  Uses the C standard "FILE".  To avoid buffering
  11.    -- penalties, read or write entire files using an FSTR.  For systems
  12.    -- which do funny things for linefeeds and cntl-z, this is a text
  13.    -- file, not a binary finle, for which you should use BFILE.
  14.  
  15.    private attr fp:EXT_OB;       -- The FILE pointer private attr
  16.  
  17.    stdin_macro:EXT_OB is raise "FILE::stdin_macro undefined"; end;
  18.    stdout_macro:EXT_OB is raise "FILE::stdout_macro undefined"; end;
  19.    stderr_macro:EXT_OB is raise "FILE::stderr_macro undefined"; end;
  20.  
  21.    stdin:SAME is
  22.       -- A file object for stdin.
  23.       r::=new; r.fp:=stdin_macro; return r end;
  24.  
  25.    stdout:SAME is
  26.       -- A file object for stdout.
  27.       r::=new; r.fp:=stdout_macro; return r end;
  28.  
  29.    stderr:SAME is
  30.       -- A file object for stderr.
  31.       r::=new; r.fp:=stderr_macro; return r end;
  32.  
  33.    open_for_write(nm:STR):SAME is
  34.       -- Create a new file with name `nm' and default permissions.
  35.       -- File is truncated and opened for writing. 
  36.       r::=new; r.fp:=C_FILE::fopen(nm,"w"); return r end;
  37.  
  38.    open_for_read(nm:STR):SAME is
  39.       -- A new object representing the file named `nm' accessible
  40.       -- for reading. 
  41.       r::=new; r.fp:=C_FILE::fopen(nm,"r"); return r end;
  42.  
  43.    open_for_append(nm:STR):SAME is
  44.       -- A new object representing the file named `nm' accessible
  45.       -- for appending.  File is created if not existing.
  46.       r::=new; r.fp:=C_FILE::fopen(nm,"a"); return r end;
  47.  
  48.    open_for_update_truncating(nm:STR):SAME is
  49.       -- A new object representing the file named `nm' accessible
  50.       -- for reading and writing.  File is truncated if existing.
  51.       r::=new; r.fp:=C_FILE::fopen(nm,"w+"); return r end;
  52.  
  53.    open_for_update(nm:STR):SAME is
  54.       -- A new object representing the file named `nm' accessible
  55.       -- for reading and writing.
  56.       r::=new; r.fp:=C_FILE::fopen(nm,"r+"); return r end;
  57.  
  58.    open_for_update_appending(nm:STR):SAME is
  59.       -- A new object representing the file named `nm' accessible
  60.       -- for reading and appending.  File is created if not existing.
  61.       r::=new; r.fp:=C_FILE::fopen(nm,"a+"); return r end;
  62.  
  63.    eof:BOOL pre ~void(fp) is
  64.       -- true if EOF has been encountered.  Cleared by "clear".
  65.       return C_FILE::feof(fp) end;
  66.  
  67.    error:BOOL is
  68.       -- true is an error has occurred with this file.  Cleared by "clear".
  69.       return void(fp) or C_FILE::ferror(fp) end;
  70.  
  71.    clear pre ~void(fp) is
  72.       -- resets end_seen and error status
  73.       C_FILE::clearerr(fp) end;
  74.    
  75.    size:INT pre ~void(fp) is
  76.       -- The size of self in characters. -1 for error.
  77.       current::=current_loc;
  78.       seek_from_end(0);
  79.       r::=current_loc;
  80.       seek_from_front(current);
  81.       return r end;
  82.  
  83.    seek_from_front(off:INT) pre ~void(fp) is
  84.       -- Attempt to move to character `off' in self.
  85.       dummy::=C_FILE::fseek(fp,off,0) end;
  86.    
  87.    seek_from_current(off:INT) pre ~void(fp) is
  88.       -- Attempt to move `off' characters forward.
  89.       dummy::=C_FILE::fseek(fp,off,1) end;
  90.  
  91.    seek_from_end(off:INT) pre ~void(fp) is
  92.       -- Attempt to move `off' characters back from the end of self.
  93.       dummy::=C_FILE::fseek(fp,off,2) end;
  94.    
  95.    current_loc:INT pre ~void(fp) is
  96.       -- The current location in the file.
  97.       return C_FILE::ftell(fp) end;
  98.    
  99.    fstr:FSTR pre ~void(fp) is
  100.       -- A string buffer containing the contents of self.
  101.       --
  102.       -- Due to conversions on some systems, the number of
  103.       -- characters read may be less than the total number
  104.       -- of characters in the file.
  105.       fsize::=size;
  106.       if fsize = -1 then return void; end;
  107.       r::=#FSTR(fsize);
  108.       seek_from_front(0);
  109.       bsize::=C_FILE::fread(r,1,fsize,fp);
  110.       r.loc:=bsize;
  111.       return r end;
  112.  
  113.    fstr(start,msize:INT):FSTR pre ~void(fp) and start<size is
  114.       -- A string buffer of size at most `msize' containing successive
  115.       -- characters from self beginning at `start'.
  116.       --
  117.       -- The number of characters read may be less than 'msize'
  118.       -- if end-of-file was encountered or some characters have
  119.       -- been discarded due to system-specific conversions.
  120.       r::=#FSTR(msize);
  121.       seek_from_front(start);
  122.       bsize::=C_FILE::fread(r,1,msize,fp);
  123.       r.loc:=bsize;
  124.       return r end;
  125.  
  126.    get_char:CHAR pre ~void(fp) is
  127.       return C_FILE::fgetc(fp); end;
  128.  
  129.    get_str:STR is
  130.       -- A string containing the characters up to the next newline.
  131.       return get_line.str;
  132.    end;
  133.  
  134.    get_line:FSTR is
  135.       -- A string buffer containing the characters up to the next newline.
  136.       res::=#FSTR;
  137.       loop 
  138.           c::=get_char;
  139.       if eof then return res end;
  140.       res:=res.push(c);
  141.       until!(c='\n');
  142.       end;
  143.       return res;
  144.    end;
  145.  
  146.    get_up_to(sc:CHAR):STR is
  147.        -- A string buffer containing the characters up to the next c.
  148.        res::=#FSTR;
  149.        loop
  150.            c::=get_char;
  151.          res:=res.push(c);
  152.          until!(c=sc);
  153.        end;
  154.        return res.str;
  155.     end;
  156.  
  157.    plus(s:STR) pre ~void(fp) is
  158.       -- Write the string `s' at the current location in self.
  159.       -- Now MACROized to eliminate continual ".fstr"ing which
  160.       -- creates garbage on each output.
  161.       fs::=C_FILE::fwrite(s.fstr,s.length,1,fp) end;
  162.  
  163.    plus(s:STR):SAME pre ~void(fp) is plus(s); return self; end;
  164.  
  165.    plus(f:FSTR) pre ~void(fp) is
  166.       -- Write the string buffer `s' at the current location in self.
  167.       fs::=C_FILE::fwrite(f,f.size,1,fp) end;
  168.  
  169.    plus(f:FSTR):SAME pre ~void(fp) is plus(f); return self; end;
  170.  
  171.    plus(c:CHAR) pre ~void(fp) is 
  172.       -- Write the character `c' at the current location.
  173.       C_FILE::fputc(c, fp) end;
  174.  
  175.    plus(c:CHAR):SAME pre ~void(fp) is plus(c); return self; end;
  176.  
  177.    plus(i:INT) pre ~void(fp) is
  178.       -- Write the INT `i' as an ascii decimal at the current location.
  179.       plus(i.str) end;
  180.  
  181.    plus(i:INT):SAME pre ~void(fp) is plus(i); return self; end;
  182.  
  183.    flush pre ~void(fp) is
  184.       -- Forces write of any buffered data.
  185.       C_FILE::fflush(fp) end;
  186.  
  187.    buffer_size(sz:INT) pre ~void(fp) is
  188.       -- Set buffer size to `sz' bytes.  Must be called after
  189.       -- a file is opened but before reading or writing.
  190.    -- C_FILE::setbuffer(fp,#FSTR(sz),sz) end;                                   -- NLP
  191.       C_FILE::setbuffer(fp,sz) end;                                             -- NLP
  192.  
  193.    close pre ~void(fp) is
  194.       -- Close the file corresponding to self.
  195.       C_FILE::fclose(fp) end;
  196.    
  197.    delete(nm:STR) is
  198.       -- Delete a file.
  199.       C_FILE::unlink(nm) end;
  200.  
  201.    temp_file:SAME is
  202.       -- Open a temporary file for writing.  It will be deleted
  203.       -- automatically when the process terminates.
  204.       r::=new; r.fp:=C_FILE::tmpfile; return r end;
  205.  
  206.    temp_filename(prefix:STR):STR is
  207.       -- Generate a unique name somewhere appropriate.
  208.       e::=C_FILE::tempnam(void,prefix);
  209.       if void(e) then raise "Couldn't get temporary filename"; end;
  210.       return STR::create_from_c_string(e);
  211.    end;
  212.  
  213.    resolve_path(fn:STR):STR is
  214.       -- Path with ".", "..", and any symbolic links resolved.
  215.       -- NOT IMPLEMENTED CORRECTLY.
  216.       return fn;
  217.    end;
  218.  
  219.    current_directory:STR is
  220.       -- Absolute path of the current directory.
  221.       buf::=#FSTR(1024); -- Buffer to hold C string
  222.       e::=C_FILE::getwd(buf);
  223.       if void(e) then raise "Couldn't get current directory path"; end;
  224.       return STR::create_from_c_string(e);
  225.    end;
  226.  
  227.    create_directory(nm:STR) is
  228.       -- Make a directory with the path `nm'.
  229. --    C_FILE::mkdir(nm,0o777);                                                  -- NLP
  230.       C_FILE::mkdirxx(nm);                                                      -- NLP
  231.    end;
  232.  
  233. end; -- class FILE
  234.  
  235. class BFILE is
  236.    -- A binary file.  For many systems this should behave the
  237.    -- same as FILE.  This class is untested and not used in the
  238.    -- compiler.
  239.  
  240.    include FILE;
  241.  
  242.    open_for_write(nm:STR):SAME is
  243.       -- Create a new file with name `nm' and default permissions.
  244.       -- File is truncated and opened for writing.
  245.       r::=new; r.fp:=C_FILE::fopen(nm,"wb"); return r end;
  246.  
  247.    open_for_read(nm:STR):SAME is
  248.       -- A new object representing the file named `nm' accessible
  249.       -- for reading.
  250.       r::=new; r.fp:=C_FILE::fopen(nm,"rb"); return r end;
  251.  
  252.    open_for_append(nm:STR):SAME is
  253.       -- A new object representing the file named `nm' accessible
  254.       -- for appending.  File is created if not existing.
  255.       r::=new; r.fp:=C_FILE::fopen(nm,"ab"); return r end;
  256.  
  257.    open_for_update_truncating(nm:STR):SAME is
  258.       -- A new object representing the file named `nm' accessible
  259.       -- for reading and writing.  File is truncated if existing.
  260.       r::=new; r.fp:=C_FILE::fopen(nm,"w+b"); return r end;
  261.  
  262.    open_for_update(nm:STR):SAME is
  263.       -- A new object representing the file named `nm' accessible
  264.       -- for reading and writing.
  265.       r::=new; r.fp:=C_FILE::fopen(nm,"r+b"); return r end;
  266.  
  267.    open_for_update_appending(nm:STR):SAME is
  268.       -- A new object representing the file named `nm' accessible
  269.       -- for reading and appending.  File is created if not existing.
  270.       r::=new; r.fp:=C_FILE::fopen(nm,"a+b"); return r end;
  271.  
  272.    fstr:FSTR pre ~void(fp) is
  273.       -- A string buffer containing the contents of self.
  274.       sz::=size; 
  275.       if sz= -1 then return void; end;
  276.       r::=#FSTR(sz);
  277.       seek_from_front(0);
  278.       fs::=C_FILE::fread(r,sz,1,fp);
  279.       r.loc:=sz;
  280.       return r end;
  281.    
  282.    fstr(st,sz:INT):FSTR pre ~void(fp) is
  283.       -- A string buffer of size at most `sz' containing successive 
  284.       -- characters from self beginning at `st'.
  285.       fsz::=size;
  286.       asz::=(fsz-st).min(sz);
  287.       r::=#FSTR(asz);
  288.       seek_from_front(0);
  289.       fs::=C_FILE::fread(r,asz,1,fp);
  290.       r.loc:=asz;
  291.       return r end;
  292.  
  293. end;
  294.    
  295. -------------------------------------------------------------------   
  296. external class C_FILE is
  297.  
  298.     fopen(nm:STR,tp:STR):EXT_OB;
  299.     feof(fp:EXT_OB):BOOL;
  300.     ferror(fp:EXT_OB):BOOL;
  301.     clearerr(fp:EXT_OB);
  302.     fclose(fp:EXT_OB);
  303.     fseek(fp:EXT_OB,off:INT,loc:INT):INT;
  304.     ftell(fp:EXT_OB):INT;
  305.     fread(fs:FSTR,sz,items:INT,fp:EXT_OB):INT;
  306.     fwrite(fs:FSTR,sz,items:INT,fp:EXT_OB):INT;
  307.     fputc(c:CHAR,fp:EXT_OB);
  308.     fgetc(fp:EXT_OB):CHAR;
  309.     unlink(s:STR);
  310.     tmpfile:EXT_OB;
  311.     tempnam(dir:EXT_OB,pfx:STR):EXT_OB;
  312.     fflush(fp:EXT_OB);
  313.  -- setbuffer(fp:EXT_OB,buf:FSTR,sz:INT);                                       -- NLP
  314.     setbuffer(fp:EXT_OB,sz:INT);                                                -- NLP
  315.     getwd(buf:FSTR):EXT_OB;
  316.  -- mkdir(nm:STR,md:INT);                                                       -- NLP
  317.     mkdirxx(nm:STR);                                                            -- NLP
  318.    
  319. end; -- external class C_FILE
  320.    
  321. -------------------------------------------------------------------
  322.