Sha256: 28239877619a42b2ca01f980f8105d11ff737a65b3bcb3b3ef2927c51fc4dd17

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

require 'stringio'

class SGF::Stream
  attr_reader :stream

  def initialize sgf, error_checker
    sgf = sgf.read if sgf.instance_of?(File)
    sgf = File.read(sgf) if File.exist?(sgf)
    error_checker.check_for_errors_before_parsing sgf
    @stream = StringIO.new clean(sgf), 'r'
  end

  def eof?
    stream.eof?
  end

  def next_character
    !stream.eof? && stream.sysread(1)
  end

  def read_token format
    property = ""
    while char = next_character and format.still_inside? char, property, self
      property << char
    end
    format.transform property
  end

  def peek_skipping_whitespace
    while char = next_character
      next if char[/\s/]
      break
    end
    rewind if char
    char
  end

  private

  def rewind
    stream.pos -= 1
  end

  def clean sgf
    sgf.gsub("\\\\n\\\\r", '')
    .gsub("\\\\r\\\\n", '')
    .gsub("\\\\r", '')
    .gsub("\\\\n", '')
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
SgfParser-3.0.1 lib/sgf/stream.rb
SgfParser-3.0.0 lib/sgf/stream.rb