Skip to content

Instantly share code, notes, and snippets.

@takuya
Last active August 29, 2015 14:07
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 takuya/722b35ad0fa17fb38cf3 to your computer and use it in GitHub Desktop.
Save takuya/722b35ad0fa17fb38cf3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding:utf-8
require 'stringio'
require 'pp'
class Ini
@@original_string
def initialize io
@@ini = {}
@@comments = {}
comment = ""
original_string = StringIO.new
until io.eof? do
line = io.readline
original_string.puts(line.strip)
next if line =~ /^\s*$/ # 空行無視
if line =~ /^\s*;/ # コメント行
comment << line
next
end
if line =~ %r"\[([^\]]*)\]\s*$"
line.strip!
ret = line.scan(%r"\[([^\]]*)\]")
section_name = ret.first.first
@@ini[section_name] = {} unless @@ini[section_name]
@@comments[section_name] = {} unless @@comments[section_name]
@@comments[section_name][";text"] = comment
comment = ""
next
end
entry = line.strip!.split(%r"=").map{|e| e.strip}
if entry.size > 1
@@ini[section_name][entry[0]] = entry[1]
@@comments[section_name][entry[0]] = comment
comment = ""
else
@@ini[section_name][";arrays"] = [] unless @@ini[section_name][";arrays"]
@@ini[section_name][";arrays"] << entry[0]
@@comments[section_name][";arrays"] = [] unless @@comments[section_name][";arrays"]
@@comments[section_name][";arrays"] << comment
comment = ""
end
end
@@comments[";eof"] = comment if comment != ""
end
def save arg
arg = open(arg, "w") if arg.class == String
self.sections.each{|name|
comments = @@comments.clone
arg.puts comments[name][";text"] if comments[name]
arg.puts "[#{name}]"
self[name].each{|v|
if v.class == String
arg.puts comments[name][";arrays"].shift if comments[name][";arrays"]
arg.puts v.strip
next
elsif v.class == Array
arg.puts comments[name][v.first] if comments[name][v.first]
v = v.join("=")
arg.puts v.strip
end
}
}
arg.puts @@comments[";eof"] if @@comments[";eof"]
end
def Ini.open filename ; Ini.load filename ; end
def Ini.load filename
io = File.open(filename, "r")
Ini.new io
end
def to_ini_string
sio = StringIO.new
self.save sio
sio.rewind
sio.read
end
def Ini.parse_ini str
sio = StringIO.new str
Ini.new sio
end
def sections ;@@ini.keys;end
def [] key
val = @@ini[key]
val = @@ini[key][";arrays"] if val.has_key?(";arrays")
return val
end
def []=(key,val)
self.add_section key unless self.has_section? key
if @@ini[key].has_key? ";arrays"
@@ini[key][";arrays"] = [val]
else
@@ini[key]=val
end
return
end
def add_section(name)
@@ini[name] = {}
@@comments[name] = {}
end
def has_section? name
@@ini.has_key? name
end
end
#=begin
if __FILE__ == $0
ini_str = <<EOS
;this is a sample ini
[main]
key=12345
[list]
;this ini file can write arrays
Bob
John
[sub]
;sample
usa = United States of America
;nation
uk = United Kingdom
;bye
EOS
ini = Ini.parse_ini ini_str
ini["list"] << "takuya"
ini["main"]["key"] = "0123"
ini["portal"] = { "name"=>"shrine"}
sio = StringIO.new
ini.save sio
sio.rewind
puts sio.read
end
#=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment