class SeeYouCup::Parser

Public Class Methods

new(path) click to toggle source
# File lib/see_you_cup/parser.rb, line 7
def initialize(path)
  raise FileNotFound unless File.exist?(path)

  @buffer = StringIO.new(load_file(path), 'r')
end

Public Instance Methods

parse() click to toggle source
# File lib/see_you_cup/parser.rb, line 13
def parse
  res = []
  res << read_waypoint(@buffer) until @buffer.eof?
  res
end

Private Instance Methods

load_file(path) click to toggle source
# File lib/see_you_cup/parser.rb, line 70
def load_file(path)
  str = File.readlines(path).map(&:chomp)
  # remove first line (column headers)
  str.shift
  # standardize line separator (in case \r was used before)
  str.join("\n")
end
read_quoted_field(buffer) click to toggle source
# File lib/see_you_cup/parser.rb, line 50
def read_quoted_field(buffer)
  ret = String.new
  quote_open = false
  first = false
  while (char = buffer.getc)
    raise 'quote expected' if first && char != '"'

    first = false
    break if char == ',' && !quote_open
    break if char == "\n" && !quote_open

    if char == '"'
      quote_open = !quote_open
    else
      ret << char
    end
  end
  ret
end
read_simple_field(buffer) click to toggle source
# File lib/see_you_cup/parser.rb, line 38
def read_simple_field(buffer)
  ret = String.new
  while (char = buffer.getc)
    break if char == ','

    raise 'quote not allowed' if char == '"'

    ret << char
  end
  ret
end
read_waypoint(buffer) click to toggle source
# File lib/see_you_cup/parser.rb, line 21
def read_waypoint(buffer)
  # :name, :code, :country, :lat, :lon, :elev, :style, :rwydir, :rwylen, :freq, :desc
  wp = Waypoint.new
  wp.name = read_quoted_field(buffer)
  wp.code = read_quoted_field(buffer)
  wp.country = read_simple_field(buffer)
  wp.lat = read_simple_field(buffer)
  wp.lon = read_simple_field(buffer)
  wp.elev = read_simple_field(buffer)
  wp.style = read_simple_field(buffer)
  wp.rwydir = read_simple_field(buffer)
  wp.rwylen = read_simple_field(buffer)
  wp.freq = read_simple_field(buffer)
  wp.desc = read_quoted_field(buffer)
  wp
end