package bigloo;

import java.io.*;

public class input_string_port extends input_port
{
  public input_string_port( final byte[]  s )
  {
    super( "[string]", s.length+1 );

    final int size= s.length;

    for ( int i= 0 ; i < size ; ++i )
      buffer[i] = s[i];
    buffer[size] = 0;
    abufsiz = bufsiz;
    eof = true;
  }

  public boolean rgc_charready()
  {
    return ((forward+1) < abufsiz);
  }

  public void reopen_input_c_string( final byte[]  s )
  {
    int len = s.length;

    if (bufsiz < (len + 1))
    {
      bufsiz = len + 1;
      buffer = new byte[len + 1];
    }

    abufsiz = len + 1;
    matchstart = 0;
    matchstop = 0;
    forward = 0;
    lastchar = (byte)'\n';
    buffer[len] = 0;
    --len;

    while (0 <= len)
    {
      buffer[len]= s[len];
      --len;
    }
  }

  Object bgl_input_port_seek( final int  pos )
    throws IOException
  {
    if (pos < bufsiz)
    {
      filepos = pos;
      matchstart = pos;
      matchstop = pos;
      forward = pos;
      return foreign.BTRUE;
    }

    return foreign.BFALSE;
  }

  public void close()
  {
    eof = true;
    //other_eof= true;
  }

  public boolean rgc_fill_buffer()
  {
    return false;
  }
}
