Created
August 8, 2009 19:00
-
-
Save nickw/164472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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