n.c.
[chise/ruby.git] / chise / idsdb.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "chise/char"
4
5 module CHISE
6   class IDS_DB
7     include Singleton
8
9     def initialize
10       @config = Config.instance
11       @path = @config.ids_dir.path
12       @dbs = {}
13     end
14     attr_reader :path
15
16     def get_ccs(ccs)
17       @dbs[ccs] = IDS_CCS_DB.new(self, ccs) if @dbs[ccs].nil?
18       @dbs[ccs]
19     end
20
21     def each_ccs
22       @path.each_entry {|f|
23         next unless /\AIDS-(.+)\.txt\Z/ =~ f
24         yield($1)
25       }
26     end
27   end
28
29   class IDS_CCS_DB
30     def initialize(idsdb, ccs)
31       @idsdb, @ccs = idsdb, ccs
32       @path = @idsdb.path+("IDS-"+ccs+".txt")
33     end
34
35     def each_line
36       @path.open {|f|
37         f.each {|line|
38           next if /\A;/ =~ line # skip comment
39           code, picture, ids = line.split
40           raise if code.nil?
41           ids = "" if ids.nil?
42           yield(code, ids)
43         }
44       }
45     end
46
47     def each_entry
48       each_line {|code, ids|
49         er = "&"+code+";"
50         begin
51           char = Character.get(er)
52         rescue
53           #qp er
54         end
55         next if char.nil?
56         yield(char, ids)
57       }
58     end
59
60   end
61 end