Skip to content

Instantly share code, notes, and snippets.

@nickw
Created August 8, 2009 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickw/164472 to your computer and use it in GitHub Desktop.
Save nickw/164472 to your computer and use it in GitHub Desktop.
# USAGE:
# parser = Util::PlistParser.new("#{ENV['HOME']}/.cappconfig")
# config_hash = parser.parse!
module Util
class PlistParser
attr_accessor :file
DOCTYPE = /280NPLIST;1.0;D;/
END_OF_FILE = "E;"
VALUE_LENGTH = /([0-9]\;)|([0-9][0-9]\;)/
def initialize(file)
@file = file
end
def parse!
contents = File.open(@file) {|f| f.read}
# remove the doctype
contents.gsub!(DOCTYPE, "")
# remove the end of file identifier
contents.gsub!(END_OF_FILE, "")
# remove the value length identifiers
contents.gsub!(VALUE_LENGTH, "")
# split the remaining string by end of key values
pairs = contents.split("K;")
# pull the kv pairs into a ruby hash
hash = {}
pairs.each do |p|
kv = p.split("S;")
hash[kv[0]] = kv[1]
end
return hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment