Module: NBTFile

Defined in:
lib/nbtfile.rb

Defined Under Namespace

Modules: CommonMethods, ReadMethods, Tokens, WriteMethods Classes: BaseToken, CompoundReaderState, CompoundWriterState, EncodingError, EndReaderState, EndWriterState, ListReaderState, ListWriterState, Reader, TopReaderState, TopWriterState, Writer

Constant Summary collapse

TOKEN_CLASSES_BY_INDEX =
[]
TOKEN_INDICES_BY_CLASS =
{}

Class Method Summary collapse

Class Method Details

.emit(io) ⇒ Object



532
533
534
535
536
537
538
539
# File 'lib/nbtfile.rb', line 532

def self.emit(io)
  writer = Writer.new(io)
  begin
    yield writer
  ensure
    writer.finish
  end
end

.load(io) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/nbtfile.rb', line 541

def self.load(io)
  root = {}
  stack = [root]

  self.tokenize(io) do |token|
    case token
    when Tokens::TAG_Compound
      value = {}
    when Tokens::TAG_List
      value = []
    when Tokens::TAG_End
      stack.pop
      next
    else
      value = token.value
    end

    stack.last[token.name] = value

    case token
    when Tokens::TAG_Compound, Tokens::TAG_List
      stack.push value
    end
  end

  pair = root.first
  {'name' => pair.first, 'body' => pair.last}
end

.tokenize(io) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/nbtfile.rb', line 516

def self.tokenize(io)
  case io
  when String
    io = StringIO.new(io, "rb")
  end
  reader = Reader.new(io)

  if block_given?
    reader.each_token { |token| yield token }
  else
    tokens = []
    reader.each_token { |token| tokens << token }
    tokens
  end
end