0

我需要为分析项目解析 gedcom 5.5 文件。我发现的第一个 ruby​​ 解析器会导致堆栈级别太深的错误,所以我试图找到替代方案。我找到了这个项目:https ://github.com/jslade/gedcom-ruby

包括一些样本,但我也没有让它们工作。

这是解析器本身:https ://github.com/jslade/gedcom-ruby/blob/master/lib/gedcom.rb

如果我尝试这样的示例:

ruby ./samples/count.rb ./samples/royal.ged

我收到以下错误:

D:/rails_projects/gedom_test/lib/gedcom.rb:185:in `readchar': end of file reached (EOFError)

为了更好地理解,我在每种方法中都写了一个“gets”,这是异常引发之前的输出:

Parsing './samples/royal.ged'...
INIT
BEFORE
CHECK_PROC_OR_BLOCK
BEFORE
CHECK_PROC_OR_BLOCK
PARSE
PARSE_FILE
PARSE_IO
DETECT_RS

导致问题的确切线路是

while ch = io.readchar

在 detect_rs 方法中:

# valid gedcom may use either of \r or \r\n as the record separator.
# just in case, also detects simple \n as the separator as well
# detects the rs for this string by scanning ahead to the first occurence
# of either \r or \n, and checking the character after it
def detect_rs io
puts "DETECT_RS"
  rs = "\x0d"
  mark = io.pos
  begin
    while ch = io.readchar
      case ch
      when 0x0d
        ch2 = io.readchar
        if ch2 == 0x0a
          rs = "\x0d\x0a"
        end
        break
      when 0x0a
        rs = "\x0a"
        break
      end
    end
  ensure
    io.pos = mark
  end
  rs
end

我希望有人可以帮助我。

4

1 回答 1

1

readcharRuby类的方法在遇到文件末尾时 IO会引发一个。http://www.ruby-doc.org/core-2.1.1/IO.html#method-i-readcharEOFError

宝石已经很多年gedcom-ruby没有被碰过,但是它的一个叉子花了几年时间来解决这个问题。

基本上它会改变:

while ch = io.readchar

while !io.eof && ch = io.readchar

您可以在这里获得 gem 的分支:https ://github.com/trentlarson/gedcom-ruby

于 2014-05-04T10:07:13.067 回答