context: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
  /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
  /* Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library 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
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   * (or see http://www.gnu.org/copyleft/lesser.html)
   */
  
  using System;
  
  namespace NCDK.Smiles.SMARTS.Parser
  {
      /// <summary>
      /// An implementation of interface CharStream, where the stream is assumed to
      /// contain only ASCII characters (without unicode processing).
      /// </summary>
      [Obsolete]
      public class SimpleCharStream
      {
          /// <summary>Whether parser is static.</summary>
          public const bool staticFlag = false;
          private int bufsize;
          private int available;
          private int tokenBegin;
          /// <summary>Position in buffer.</summary>
          private int bufpos = -1;
          private int[] bufline;
          private int[] bufcolumn;
  
          private int column = 0;
          private int line = 1;
  
          private bool prevCharIsCR = false;
          private bool prevCharIsLF = false;
  
          private System.IO.TextReader inputStream;
  
          private char[] buffer;
          private int maxNextCharInd = 0;
          private int inBuf = 0;
          private int tabSize = 8;
  
          protected void SetTabSize(int i) { tabSize = i; }
          protected int GetTabSize(int i) { return tabSize; }
  
          protected void ExpandBuff(bool wrapAround)
          {
              char[] newbuffer = new char[bufsize + 2048];
              int[] newbufline = new int[bufsize + 2048];
              int[] newbufcolumn = new int[bufsize + 2048];
  
              try
              {
                  if (wrapAround)
                  {
                      Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
                      Array.Copy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
                      buffer = newbuffer;
  
                      Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
                      Array.Copy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
                      bufline = newbufline;
  
                      Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
                      Array.Copy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
                      bufcolumn = newbufcolumn;
  
                      maxNextCharInd = (bufpos += (bufsize - tokenBegin));
                  }
                  else
                  {
                      Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
                      buffer = newbuffer;
  
                      Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
                      bufline = newbufline;
  
                      Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
                      bufcolumn = newbufcolumn;
  
                      maxNextCharInd = (bufpos -= tokenBegin);
                  }
              }
              catch (Exception t)
              {
                  throw new Exception(t.Message);
              }
  
              bufsize += 2048;
              available = bufsize;
              tokenBegin = 0;
          }
  
          protected void FillBuff()
          {
              if (maxNextCharInd == available)
              {
                  if (available == bufsize)
                  {
                      if (tokenBegin > 2048)
                      {
                          bufpos = maxNextCharInd = 0;
                          available = tokenBegin;
                      }
                      else if (tokenBegin < 0)
                          bufpos = maxNextCharInd = 0;
                      else
                          ExpandBuff(false);
                  }
                  else if (available > tokenBegin)
                      available = bufsize;
                  else if ((tokenBegin - available) < 2048)
                      ExpandBuff(true);
                  else
                      available = tokenBegin;
              }
  
              int i;
              try
              {
                  if ((i = inputStream.Read(buffer, maxNextCharInd, available - maxNextCharInd)) == 0)
                  {
                      inputStream.Close();
                      throw new System.IO.IOException();
                  }
                  else
                      maxNextCharInd += i;
                  return;
              }
              catch (Exception ee)
              {
                  var e = ee;
                  if (e is System.ObjectDisposedException)
                      e = new System.IO.IOException(e.Message);
                  if (e is System.IO.IOException)
                  {
                      --bufpos;
                      Backup(0);
                      if (tokenBegin == -1)
                          tokenBegin = bufpos;
                  }
                  throw e;
              }
          }
  
          /// <summary>Start.</summary>
          public char BeginToken()
          {
              tokenBegin = -1;
              char c = ReadChar();
              tokenBegin = bufpos;
  
              return c;
          }
  
          protected void UpdateLineColumn(char c)
          {
              column++;
  
              if (prevCharIsLF)
              {
                  prevCharIsLF = false;
                  line += (column = 1);
              }
              else if (prevCharIsCR)
              {
                  prevCharIsCR = false;
                  if (c == '\n')
                  {
                      prevCharIsLF = true;
                  }
                  else
                      line += (column = 1);
              }
  
              switch (c)
              {
                  case '\r':
                      prevCharIsCR = true;
                      break;
                  case '\n':
                      prevCharIsLF = true;
                      break;
                  case '\t':
                      column--;
                      column += (tabSize - (column % tabSize));
                      break;
                  default:
                      break;
              }
  
              bufline[bufpos] = line;
              bufcolumn[bufpos] = column;
          }
  
          /// <summary>Read a character.</summary>
          public char ReadChar()
          {
              if (inBuf > 0)
              {
                  --inBuf;
  
                  if (++bufpos == bufsize)
                      bufpos = 0;
  
                  return buffer[bufpos];
              }
  
              if (++bufpos >= maxNextCharInd)
                  FillBuff();
  
              char c = buffer[bufpos];
  
              UpdateLineColumn(c);
              return c;
          }
  
          [Obsolete]
          public int GetColumn()
          {
              return bufcolumn[bufpos];
          }
  
          [Obsolete]
          public int GetLine()
          {
              return bufline[bufpos];
          }
  
          /// <summary>Get token end column number.</summary>
          public int GetEndColumn()
          {
              return bufcolumn[bufpos];
          }
  
          /// <summary>Get token end line number.</summary>
          public int GetEndLine()
          {
              return bufline[bufpos];
          }
  
          /// <summary>Get token beginning column number.</summary>
          public int GetBeginColumn()
          {
              return bufcolumn[tokenBegin];
          }
  
          /// <summary>Get token beginning line number.</summary>
          public int GetBeginLine()
          {
              return bufline[tokenBegin];
          }
  
          /// <summary>Backup a number of characters.</summary>
          public void Backup(int amount)
          {
  
  
gt:             inBuf += amount;