|
1 |
| -# ------------------------------------------------------------------------- |
2 |
| -# gedcom.rb -- core module definition of GEDCOM-Ruby interface |
3 |
| -# Copyright (C) 2003 Jamis Buck (jgb3@email.byu.edu) |
4 |
| -# ------------------------------------------------------------------------- |
5 |
| -# This library is free software; you can redistribute it and/or |
6 |
| -# modify it under the terms of the GNU Lesser General Public |
7 |
| -# License as published by the Free Software Foundation; either |
8 |
| -# version 2.1 of the License, or (at your option) any later version. |
9 |
| -# |
10 |
| -# This library is distributed in the hope that it will be useful, |
11 |
| -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
| -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
| -# Lesser General Public License for more details. |
14 |
| -# |
15 |
| -# You should have received a copy of the GNU Lesser General Public |
16 |
| -# License along with this library; if not, write to the Free Software |
17 |
| -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
| -# ------------------------------------------------------------------------- |
19 |
| - |
20 |
| -#require '_gedcom' |
21 |
| -require 'gedcom_date' |
22 |
| - |
23 |
| -module GEDCOM |
24 |
| - |
25 |
| - # Possibly a better way to do this? |
26 |
| - VERSION = "0.0.1" |
27 |
| - |
28 |
| - class Parser |
29 |
| - def defaultHandler( data, cookie, parm ) |
30 |
| - end |
31 |
| - |
32 |
| - def initialize( cookie = nil ) |
33 |
| - @cookie = cookie |
34 |
| - @pre_handler = Hash.new( [ method( "defaultHandler" ), nil ] ) |
35 |
| - @post_handler = Hash.new( [ method( "defaultHandler" ), nil ] ) |
36 |
| - end |
37 |
| - |
38 |
| - def setPreHandler( context, func, parm = nil ) |
39 |
| - @pre_handler[ context ] = [ func, parm ] |
40 |
| - end |
41 |
| - |
42 |
| - def setPostHandler( context, func, parm = nil ) |
43 |
| - @post_handler[ context ] = [ func, parm ] |
44 |
| - end |
45 |
| - |
46 |
| - def callPreHandler( context, data, cookie ) |
47 |
| - func, parm = @pre_handler[ context ] |
48 |
| - func.call( data, cookie, parm ) |
49 |
| - end |
50 |
| - |
51 |
| - def callPostHandler( context, data, cookie ) |
52 |
| - func, parm = @post_handler[ context ] |
53 |
| - func.call( data, cookie, parm ) |
54 |
| - end |
55 |
| - |
56 |
| - # The parser is based on a stack. Every time a new level is found that is |
57 |
| - # greater than the level of the previously seen item, it is pushed onto the |
58 |
| - # stack. If the next item seen is of a lower level than previously seen |
59 |
| - # items, those previously seen items are popped off the stack and their post |
60 |
| - # handlers are called. |
61 |
| - |
62 |
| - def parse( file ) |
63 |
| - ctxStack = [] |
64 |
| - dataStack = [] |
65 |
| - curlvl = -1 |
66 |
| - File.open( file, "r" ) do |f| |
67 |
| - f.each_line do |line| |
68 |
| - level, tag, rest = line.chop.split( ' ', 3 ) |
69 |
| - while level.to_i <= curlvl |
70 |
| - callPostHandler( ctxStack, dataStack.last, @cookie ) |
71 |
| - ctxStack.pop |
72 |
| - dataStack.pop |
73 |
| - curlvl -= 1 |
74 |
| - end |
75 |
| - |
76 |
| - tag, rest = rest, tag if tag =~ /@.*@/ |
77 |
| - |
78 |
| - ctxStack.push tag |
79 |
| - dataStack.push rest |
80 |
| - curlvl = level.to_i |
81 |
| - |
82 |
| - callPreHandler( ctxStack, dataStack.last, @cookie ) |
83 |
| - end |
84 |
| - end |
85 |
| - end |
86 |
| - end |
87 |
| - |
88 |
| - class DatePart |
89 |
| - def <=>( dp ) |
90 |
| - return -1 if has_year? and !dp.has_year? |
91 |
| - return 1 if !has_year? and dp.has_year? |
92 |
| - |
93 |
| - if has_year? and dp.has_year? |
94 |
| - rc = ( year <=> dp.year ) |
95 |
| - return rc unless rc == 0 |
96 |
| - end |
97 |
| - |
98 |
| - return -1 if dp.has_month? and !dp.has_month? |
99 |
| - return 1 if !dp.has_month? and dp.has_month? |
100 |
| - |
101 |
| - if has_month? and dp.has_month? |
102 |
| - rc = ( month <=> dp.month ) |
103 |
| - return rc unless rc == 0 |
104 |
| - end |
105 |
| - |
106 |
| - return -1 if dp.has_day? and !dp.has_day? |
107 |
| - return 1 if !dp.has_day? and dp.has_day? |
108 |
| - |
109 |
| - if has_day? and dp.has_day? |
110 |
| - rc = ( day <=> dp.day ) |
111 |
| - return rc unless rc == 0 |
112 |
| - end |
113 |
| - |
114 |
| - return 0 |
115 |
| - end |
116 |
| - end |
117 |
| - |
118 |
| - class Date |
119 |
| - def Date.safe_new( parm ) |
120 |
| - Date.new( parm ) { |errmsg| } |
121 |
| - end |
122 |
| - |
123 |
| - def <=>( d ) |
124 |
| - if is_date? and d.is_date? |
125 |
| - rc = ( first <=> d.first ) |
126 |
| - return rc unless rc == 0 |
127 |
| - |
128 |
| - if is_range? and d.is_range? |
129 |
| - return ( last <=> d.last ) |
130 |
| - elsif is_range? |
131 |
| - return 1 |
132 |
| - elsif d.is_range? |
133 |
| - return -1 |
134 |
| - end |
135 |
| - |
136 |
| - return 0 |
137 |
| - elsif is_date? |
138 |
| - return -1 |
139 |
| - elsif d.is_date? |
140 |
| - return 1 |
141 |
| - end |
142 |
| - |
143 |
| - return format <=> d.format |
144 |
| - end |
145 |
| - end |
146 |
| -end |
147 |
| -
|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# gedcom.rb -- core module definition of GEDCOM-Ruby interface |
| 3 | +# Copyright (C) 2003 Jamis Buck (jgb3@email.byu.edu) |
| 4 | +# ------------------------------------------------------------------------- |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | +# ------------------------------------------------------------------------- |
| 19 | + |
| 20 | +#require '_gedcom' |
| 21 | +require 'gedcom_date' |
| 22 | + |
| 23 | +module GEDCOM |
| 24 | + |
| 25 | + # Possibly a better way to do this? |
| 26 | + VERSION = "0.0.1" |
| 27 | + |
| 28 | + class Parser |
| 29 | + def defaultHandler( data, cookie, parm ) |
| 30 | + end |
| 31 | + |
| 32 | + def initialize( cookie = nil ) |
| 33 | + @cookie = cookie |
| 34 | + @pre_handler = Hash.new( [ method( "defaultHandler" ), nil ] ) |
| 35 | + @post_handler = Hash.new( [ method( "defaultHandler" ), nil ] ) |
| 36 | + end |
| 37 | + |
| 38 | + def setPreHandler( context, func, parm = nil ) |
| 39 | + @pre_handler[ context ] = [ func, parm ] |
| 40 | + end |
| 41 | + |
| 42 | + def setPostHandler( context, func, parm = nil ) |
| 43 | + @post_handler[ context ] = [ func, parm ] |
| 44 | + end |
| 45 | + |
| 46 | + def callPreHandler( context, data, cookie ) |
| 47 | + func, parm = @pre_handler[ context ] |
| 48 | + func.call( data, cookie, parm ) |
| 49 | + end |
| 50 | + |
| 51 | + def callPostHandler( context, data, cookie ) |
| 52 | + func, parm = @post_handler[ context ] |
| 53 | + func.call( data, cookie, parm ) |
| 54 | + end |
| 55 | + |
| 56 | + # The parser is based on a stack. Every time a new level is found that is |
| 57 | + # greater than the level of the previously seen item, it is pushed onto the |
| 58 | + # stack. If the next item seen is of a lower level than previously seen |
| 59 | + # items, those previously seen items are popped off the stack and their post |
| 60 | + # handlers are called. |
| 61 | + |
| 62 | + def parse( file ) |
| 63 | + ctxStack = [] |
| 64 | + dataStack = [] |
| 65 | + curlvl = -1 |
| 66 | + File.open( file, "r" ) do |f| |
| 67 | + f.each_line do |line| |
| 68 | + level, tag, rest = line.chop.split( ' ', 3 ) |
| 69 | + while level.to_i <= curlvl |
| 70 | + callPostHandler( ctxStack, dataStack.last, @cookie ) |
| 71 | + ctxStack.pop |
| 72 | + dataStack.pop |
| 73 | + curlvl -= 1 |
| 74 | + end |
| 75 | + |
| 76 | + tag, rest = rest, tag if tag =~ /@.*@/ |
| 77 | + |
| 78 | + ctxStack.push tag |
| 79 | + dataStack.push rest |
| 80 | + curlvl = level.to_i |
| 81 | + |
| 82 | + callPreHandler( ctxStack, dataStack.last, @cookie ) |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + class DatePart |
| 89 | + def <=>( dp ) |
| 90 | + return -1 if has_year? and !dp.has_year? |
| 91 | + return 1 if !has_year? and dp.has_year? |
| 92 | + |
| 93 | + if has_year? and dp.has_year? |
| 94 | + rc = ( year <=> dp.year ) |
| 95 | + return rc unless rc == 0 |
| 96 | + end |
| 97 | + |
| 98 | + return -1 if dp.has_month? and !dp.has_month? |
| 99 | + return 1 if !dp.has_month? and dp.has_month? |
| 100 | + |
| 101 | + if has_month? and dp.has_month? |
| 102 | + rc = ( month <=> dp.month ) |
| 103 | + return rc unless rc == 0 |
| 104 | + end |
| 105 | + |
| 106 | + return -1 if dp.has_day? and !dp.has_day? |
| 107 | + return 1 if !dp.has_day? and dp.has_day? |
| 108 | + |
| 109 | + if has_day? and dp.has_day? |
| 110 | + rc = ( day <=> dp.day ) |
| 111 | + return rc unless rc == 0 |
| 112 | + end |
| 113 | + |
| 114 | + return 0 |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + class Date |
| 119 | + def Date.safe_new( parm ) |
| 120 | + Date.new( parm ) { |errmsg| } |
| 121 | + end |
| 122 | + |
| 123 | + def <=>( d ) |
| 124 | + if is_date? and d.is_date? |
| 125 | + rc = ( first <=> d.first ) |
| 126 | + return rc unless rc == 0 |
| 127 | + |
| 128 | + if is_range? and d.is_range? |
| 129 | + return ( last <=> d.last ) |
| 130 | + elsif is_range? |
| 131 | + return 1 |
| 132 | + elsif d.is_range? |
| 133 | + return -1 |
| 134 | + end |
| 135 | + |
| 136 | + return 0 |
| 137 | + elsif is_date? |
| 138 | + return -1 |
| 139 | + elsif d.is_date? |
| 140 | + return 1 |
| 141 | + end |
| 142 | + |
| 143 | + return format <=> d.format |
| 144 | + end |
| 145 | + end |
| 146 | +end |
| 147 | + |
0 commit comments