05-21-2006, 01:00 PM
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
No support is given. If you are the owner of the thread, please contact administration.
I created this small thingie for GS his game (check his blog)
Its a small code which can be used for settings or so, just use INI.read("filename.ini","key") to get an value and use INI.write("filename.ini","key","value") to write something. Note that everything is returned as an variable. For the other functions, read the comments or scroll down.
Code:
module INI
#------------------------------------------------------------------------------
# Converts to hash but still has the comments
def self.decode(file)
hash = {}
lines = File.open(file).readlines
for i in lines
split = i.chomp.split(/=/)
hash[split[0]] = split[1]
end
return hash
end
#------------------------------------------------------------------------------
# Checks if key is in the ini
def self.has_key?(file, key)
keys = self.decode(file).keys
return keys.include?(key)
end
#------------------------------------------------------------------------------
# Read an key, always returns a string
def self.read(file, key)
return unless self.has_key?(file, key)
hash = self.decode(file)
return hash[key].split(/\#/)[0]
end
#------------------------------------------------------------------------------
# Returns hash without comment
def self.hash(file)
ohash = self.decode(file)
hash = {}
ohash.each_key {|key| hash[key] = self.read(file,key)}
return hash
end
#------------------------------------------------------------------------------
# Returns key comment
def self.comment(file, key)
return unless self.has_key?(file, key)
hash = self.decode(file)
return hash[key].split(/\#/)[1]
end
#------------------------------------------------------------------------------
# Writes value to key
def self.write(file, key, value)
return unless self.has_key?(file, key)
hash = self.decode(file)
comment = self.comment(file, key)
if comment
hash[key] = "#{value} \# #{comment}"
else
hash[key] = value
end
string = ""
hash.each {|key, value| string += "#{key}=#{value}\n"}
pie = File.open(file,"w")
pie.write(string)
pie.close
end
#------------------------------------------------------------------------------
# Builds an ini file. Replaces if file exists
def self.build(file, contents)
if contents.is_a? Hash
string = ""
contents.each {|key, value| string += "#{key}=#{value}\n"}
elsif contents.is_a? String
string = contents
else
return
end
pie = File.new(file, "w")
pie.write(string)
pie.close
end
end
And for checking if a value is true i built a very small add-on for the string class(thanks slipknot for faster method):
Code:
class String
def true?
return self == "true"
end
end
Code:
if INI.read("Blah.ini","poo").true?
INI.write("Blah.ini","poo",false)
end
If you want to build an ini, use build as showed below.
Code:
# String
INI.build("string.ini","pie=good # yes it is\nself=best # yeah")
# Hash
hash = {}
hash["pie"] = "delicious"
hash["self"] = "great"
INI.build("hash.ini", hash)