Ruby Tips / tag map file parser in Ruby
Powered by dKingyo 規格準拠 | 図解 辞典 | C言語 | シューティング | TOEIC 突破
http://d.hatena.ne.jp/studiokingyo/20070322#p1
にて提案するファイル形式のパーサーを
http://d.hatena.ne.jp/studiokingyo/20070322#p2
にてC++版を公開していましたが、Rubyに移植してみました。
ライセンスはNYSLです。
いつかsetup.rbと一緒に配布する予定です。
続きを読む
#!/usr/local/bin/ruby def tagmapfile_parse(filename,tag) file = File.open(filename).read result = Hash.new state = 0 first = String.new second = String.new str = file for i in 0..str.length-1 do case (state) when 0 first = String.new second = String.new if tag[0] == str[i] then state = 1; #puts state end when 1 if tag[2] == str[i] then state = 2; #puts state else first = first + sprintf("%c",str[i]); end when 2 if tag[0] == str[i] && tag[1] == str[i + 1] then if str.length - i > first.length then vl = String.new for j in 0..first.length-1 do vl = vl + sprintf("%c",str[i+j+2]); end #puts vl if vl != first then second = second + sprintf("%c",str[i]); next end end state = 3 i = i+1 #puts state next end second = second + sprintf("%c",str[i]); next when 3 if tag[2] == str[i] then state = 0; #puts state if true == result.member?(first) then print "TagMapFile#parse repetition of key\n" break; end result.store(first,second) # print "OK" end else print "TagMapFile#parse logic error\n" break; end #end of case end #end of for return result end filename = $*[0] h = tagmapfile_parse(filename,"</>") h.each{|k,v| print "key :" + k + " / value : " + v } exit