Module: ICU::Util::File

Defined in:
lib/icu_tournament/util.rb

Class Method Summary collapse

Class Method Details

.load_ini(name) ⇒ Object

Load an INI file and convert to a hash.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/icu_tournament/util.rb', line 38

def self.load_ini(name)
  text = self.read_utf8(name)
  data = Hash.new
  header = nil
  text.split(/\n/).each do |line|
    if line.match(/^\s*\[([^\]]+)\]\s*$/)
      header = $1.strip
      header = nil if header == ""
    elsif header && line.match(/^([^=]+)=(.*)$/)
      key = $1.strip
      val = $2.strip
      unless key == ""
        data[header] ||= Hash.new
        data[header][key] = val
      end
    end
  end
  data
end

.read_utf8(name) ⇒ Object

Read UTF data from a file.



28
29
30
31
32
33
34
35
# File 'lib/icu_tournament/util.rb', line 28

def self.read_utf8(name)
  ::File.open(name, "r:ASCII-8BIT") do |f|
    data = f.read
    bom = "\xEF\xBB\xBF".force_encoding("ASCII-8BIT")
    data.sub!(/^#{bom}/, "")         # get rid of a UTF-8 BOM
    ICU::Util::String.to_utf8(data)  # defined in icu_name
  end
end