class PDF::Reader::Explore

###############################################################################

def self.file (name)

###############################################################################
def self.file (name)
  PDF::Reader.new.parse(File.open(name), self)
end

def cd (path)

###############################################################################
def cd (path)
  path = path.to_s
  if path[0,1] == "/"
    @pwd = path
  else
    @pwd = Pathname.new(@pwd + '/' + path).cleanpath.to_s
  end
end

def document (root)

###############################################################################
def document (root)
  @root = root
  self
end

def initialize (receiver, xref)

###############################################################################
def initialize (receiver, xref)
  @xref = xref
  @pwd  = '/'
end

def ls (entry = nil)

###############################################################################
def ls (entry = nil)
  parts = @pwd.split('/')
  obj   = @root
  parts.shift if parts[0] == ""
  parts.push(entry) if entry
  parts.each do |p|
    case obj
    when Hash
      unless obj.has_key?(p)
        puts "invalid path at #{p}"
        return
      end
      obj = obj[p]
    when Array
      obj = obj[p.to_i]
    end
    obj = @xref.object(obj)
  end
  output_parent(obj)
  "#{@pwd}: #{obj.class}"
end

def output_child (obj)

###############################################################################
def output_child (obj)
  print ": #{obj.class}"
  case obj
  when Float
    print ": #{obj}"
  when String
    print ": #{obj[0, 20].sub(/\n/, ' ')}"
  end
end

def output_parent (obj)

###############################################################################
def output_parent (obj)
  case obj
  when Hash
    obj.each do |k,v| 
      print "#{k}"; output_child(v); print "\n"
      Explore::const_set(k, k) if !Explore.const_defined?(k)
    end
  when Array
    obj.each_with_index {|o, i| print "#{i}: "; output_child(o); print "\n"}
  else
    output_child(obj)
    print "\n"
  end
end

def pwd

###############################################################################
def pwd
  @pwd
end