#!/usr/local/bin/ruby
#
# parse filename
#
# ^field: value
#
# ^field:
# [\t ]value...
# fields[fieldname] = fieldvalue
# Open file for reading
# infield=false
# curfield=""
# curvalue=""
# for each line ll in file do
# switch ll:
# /^[^\t ]/ : if infield? close and add field to fields
# begin new field
# /^[\t ]/ : append to curvalue
# eof : if infield? close and add field to fields
# done
# done
#
if ARGV[0] == nil then
print "Usage: parse filename\n"
exit 1
end
def parse_form( formdata, fieldnames )
fields = {}
infield=false
curfield=nil
curvalue=nil
formdata.each do |line|
line.chomp!
case line
when /^[\t ]*#/
# ignore comment lines
when /^[^\t ]/
# begin field
if infield then
fields[curfield] = curvalue
fieldnames << curfield
end
infield=true
curfield = line[/^[^:]*/].strip
curvalue = line.sub(/^[^:]*:[\t ]*/, "")
when /^[\t ]/
# continue field
if infield then
curvalue = "#{curvalue}\n#{line}"
else
# unknown
end
else
# unknown
end
end
if infield then
fields[curfield] = curvalue
fieldnames << curfield
end
return fields
end
a_fieldnames = []
a_file = File.new(ARGV[0])
a_fields = parse_form(a_file, a_fieldnames)
a_file.close
b_fieldnames = []
b_file = File.new(ARGV[1])
b_fields = parse_form(b_file, b_fieldnames)
b_file.close
in_both = a_fieldnames & b_fieldnames
deleted = a_fieldnames - b_fieldnames
added = b_fieldnames - a_fieldnames
changed = []
for f in in_both
if a_fields[f] != b_fields[f] then
changed << f
end
end
same = in_both - changed
printf "Unchanged fields: %s\n", same.join(", ")
printf "Deleted fields: %s\n", deleted.join(", ")
printf "Added fields: %s\n", added.join(", ")
printf "Changed fields: %s\n", changed.join(", ")