Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ini module
#1
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.


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
So if you want to make "poo" false if variable "poo" stored in INI "Blah.ini" (for example) is "true" you use this code:
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)
The other methods are used because they are needed in nearly all other methods, check comments to use them.
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Keyboard Input Module Near_Fantastica 0 2,357 07-12-2005, 01:00 PM
Last Post: Near_Fantastica



Users browsing this thread: