123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- module Sitef
-
-
-
-
-
-
-
-
-
-
- def self.parse_file(file)
- lineno = 0
- results = []
- errors = []
-
- record = {}
- key = nil
- value = nil
- until file.eof?
- lineno += 1
- line = file.gets(256)
-
- break if line.nil?
- next if line.empty?
-
- if line[0,2] == '--'
-
- if key
- record[key.downcase] = value.chomp
- key = nil
- end
- results << record
- record = {}
- elsif line[0] == '#'
-
- elsif line[0] == ':'
-
- key, value = line[1,line.size].split(':', 2)
- record[key.downcase] = value.chomp
- key = value = nil
- elsif line[0, 2] == '..'
-
- record[key.downcase] = value.chomp
- key = value = nil
- elsif (line[0] == '.') && key.nil?
-
-
- key = line[1, line.size]
- value = ""
- elsif key
-
- value << line
- else
-
- errors << "#{lineno}: Don't know how to process line"
- end
- end
-
- if key
- record[key.downcase] = value.chomp
- end
-
- results << record unless record.empty?
- return results, errors
- end
-
- def self.load_filename(filename)
- results , errors, warnings = nil, nil, nil;
- file = File.open(filename, 'rt')
- return nil, ["Could not open #{filename}"] unless file
- begin
- results, errors = parse_file(file)
- ensure
- file.close
- end
- return results, errors
- end
-
- def self.save_field(file, key, value)
- sval = value.to_s
- if sval["\n"]
- file.puts(".#{key}\n")
- file.puts(sval)
- file.puts("\n..\n")
- else
- file.puts(":#{key}:#{sval}\n")
- end
- end
-
- def self.save_record(file, record)
- record.each do | key, value |
- save_field(file, key, value)
- end
- end
- def self.save_file(file, records)
- records.each do | record |
- save_record(file, record)
- file.puts("--\n")
- end
- end
-
- def self.save_filename(filename, records)
- results , errors = nil, nil
- file = File.open(filename, 'wt')
- return false, ["Could not open #{filename}"] unless file
- begin
- save_file(file, records)
- ensure
- file.close
- end
- return true, []
- end
-
- end
|