///////////////////////////////////////////////////////////////////////////////
//                            Lisaac OS Library                              //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name    := STD_FILE;

  - copyright   := "2003-2005 Jérome Boutet, 2003-2007 Benoit Sonntag";
  
  - comment := "File management";
    
Section Inherit
  
  + parent_abstract_file:Expanded ABSTRACT_FILE;
  
Section Private  
  
  + stream:POINTER; // Unix file pointer (FILE *).

Section Public
    
  //
  // Physical implementation.
  //
  
  - physical_open:BOOLEAN <-
  ( + path_pointer:NATIVE_ARRAY[CHARACTER];    
    path_pointer := path.to_external;
    stream := `fopen((char*)@path_pointer,"r+b")`:(POINTER); 
    stream != NULL
  ); 

  - physical_open_read_only:BOOLEAN <-
  ( + path_pointer:NATIVE_ARRAY[CHARACTER];    
    path_pointer := path.to_external;
    stream := `fopen((char*)@path_pointer,"rb")`:(POINTER); 
    stream != NULL
  ); 
    
  - basic_seek pos:INTEGER :INTEGER <-
  // return size read or 0 if end of input (-1 on error => exception ?)
  ( + str:POINTER;
    ? {is_open};
    ? {stream != NULL};
    str := stream;    
    `fseek((FILE*)(@str),@pos,SEEK_SET)`:(INTEGER)
  );
  
  - basic_read buf:NATIVE_ARRAY[UINTEGER_8] size s:INTEGER :INTEGER <-
  // return size read or 0 if end of input (-1 on error => exception ?)
  ( + str:POINTER;
    ? {is_open};
    str := stream;    
    `fread((void *)(@buf),(size_t)(1), (size_t)(@s),(FILE*)(@str))`:(INTEGER)
  );
  
  - basic_write buf:NATIVE_ARRAY[UINTEGER_8] size s:INTEGER :INTEGER <-
  // return size read or 0 if end of input (-1 on error => exception ?)
  ( + str:POINTER;
    ? {is_open};
    str := stream;      
    `fwrite((void *)(@buf),(size_t)(1), (size_t)(@s),(FILE*)(@str))`:(INTEGER)
  );
  
  - basic_close:BOOLEAN <-
  ( + str:POINTER;
    str := stream;
    stream := NULL;
    `fclose((FILE*)(@str))`:(INTEGER) = 0
  );

  
