package SPVM::IO::File {
  use SPVM::FileHandle;
  
  native sub O_RDONLY : int();
  native sub O_WRONLY : int();
  native sub O_RDWR : int();
  native sub O_APPEND : int();
  native sub O_CREAT : int();
  native sub O_TRUNC : int();
  native sub SEEK_SET : int();
  native sub SEEK_CUR : int();
  native sub SEEK_END : int();
  native sub EOF : int();
  native sub STDIN : int();
  native sub STDOUT : int();
  
  has fh : ro SPVM::FileHandle;
  
  native sub open : SPVM::IO::File ($file_name : string, $mode : string);
  native sub close : int ($self : self);
  native sub putc : int ($self : self, $c : byte);
  native sub read : int ($self : self, $buf : byte[]);
  native sub write : int ($self: self, $buf : string);
  native sub seek : int ($self : self, $offset : long, $whence : int);
  
  native sub readline : string ($self : self);

  sub slurp : string ($self : self, $file : string) {
    
    my $buffer_length = 4096;
    my $buffer = new byte[$buffer_length];
    my $string = "";
    while (1) {
      my $read_length = $self->read($buffer);
      
      if ($read_length < $buffer_length) {
        $string .= sliceb((byte[])$buffer, 0, $read_length);
        last;
      }
      else {
        $string .= $buffer;
      }
    }
    return $string;
  }
}
