class Rozi::WaypointFile
A thin layer above File
that handles reading and writing of waypoints to files
Public Instance Methods
each_waypoint() { |read_waypoint| ... }
click to toggle source
Reads and yields all waypoints
# File lib/rozi/waypoints.rb, line 189 def each_waypoint return to_enum(:each_waypoint) unless block_given? @file.rewind loop { yield read_waypoint } rescue EOFError return nil end
read_properties()
click to toggle source
Reads the waypoint file properties
@raise [RuntimeError] If the file position isn’t 0 @return [WaypointFileProperties]
# File lib/rozi/waypoints.rb, line 205 def read_properties if @file.pos != 0 raise "File position must be 0 to read properties" end text = "" 4.times { text << @file.readline } parse_waypoint_file_properties text end
read_waypoint()
click to toggle source
Reads the next waypoint
@raise [EOFError] When EOF is reached @return [Waypoint]
# File lib/rozi/waypoints.rb, line 223 def read_waypoint if @file.pos == 0 read_properties end parse_waypoint @file.readline end
write(waypoints)
click to toggle source
Writes waypoints to the file
@param [Enumerator<Waypoint>] waypoints @return [nil]
# File lib/rozi/waypoints.rb, line 141 def write(waypoints) waypoints.each { |wpt| write_waypoint wpt } nil end
write_properties(properties)
click to toggle source
Writes waypoint file properties to the file
The file must be empty when this method is called!
@raise [RuntimeError] if the file isn’t empty @param [WaypointFileProperties] properties @return [nil]
# File lib/rozi/waypoints.rb, line 158 def write_properties(properties) if @file.size > 0 raise "Can't write file properties, file is not empty" end @file.write serialize_waypoint_file_properties(properties) @file.write "\n" nil end
write_waypoint(waypoint)
click to toggle source
Writes a waypoint to the file
@param [Waypoint] waypoint @return [nil]
# File lib/rozi/waypoints.rb, line 175 def write_waypoint(waypoint) ensure_file_properties @file.write serialize_waypoint(waypoint) @file.write "\n" nil end
Private Instance Methods
ensure_file_properties()
click to toggle source
Ensures that waypoint file properties has been written to the file
# File lib/rozi/waypoints.rb, line 305 def ensure_file_properties return if @properties_written @properties_written = true if @file.size == 0 write_properties WaypointFileProperties.new end end
parse_waypoint(text)
click to toggle source
@endgroup
# File lib/rozi/waypoints.rb, line 235 def parse_waypoint(text) map = { 0 => {symbol: :number, cast: method(:Integer)}, 1 => {symbol: :name, cast: method(:String)}, 2 => {symbol: :latitude, cast: method(:Float)}, 3 => {symbol: :longitude, cast: method(:Float)}, 4 => {symbol: :date, cast: method(:Float)}, 5 => {symbol: :symbol, cast: method(:Integer)}, 7 => {symbol: :display_format, cast: method(:Integer)}, 8 => {symbol: :fg_color, cast: method(:Integer)}, 9 => {symbol: :bg_color, cast: method(:Integer)}, 10 => {symbol: :description, cast: method(:String)}, 11 => {symbol: :pointer_direction, cast: method(:Integer)}, 14 => {symbol: :altitude, cast: method(:Integer)}, 15 => {symbol: :font_size, cast: method(:Integer)}, 16 => {symbol: :font_style, cast: method(:Integer)}, 17 => {symbol: :symbol_size, cast: method(:Integer)}, } text = text.strip fields = text.split(",").map { |x| x.strip } waypoint = Waypoint.new map.each_pair { |index, data| value = fields[index] next if value.empty? value = data[:cast].call(value) if value.is_a? String value = unescape_text(value) end waypoint.set(data[:symbol], value) } waypoint end
parse_waypoint_file_properties(text)
click to toggle source
# File lib/rozi/waypoints.rb, line 276 def parse_waypoint_file_properties(text) lines = text.lines version = lines[0].strip[-3..-1] datum = lines[1].strip WaypointFileProperties.new(datum, version) end
serialize_waypoint(waypoint)
click to toggle source
# File lib/rozi/waypoints.rb, line 285 def serialize_waypoint(waypoint) array = waypoint.to_a array.map! { |item| item.is_a?(String) ? escape_text(item) : item } array.map! { |item| item.nil? ? "" : item } array.map! { |item| item.is_a?(Float) ? item.round(6) : item } "%d,%s,%f,%f,%s,%d,1,%d,%d,%d,%s,%d,,,%d,%d,%d,%d" % array end
serialize_waypoint_file_properties(properties)
click to toggle source
# File lib/rozi/waypoints.rb, line 294 def serialize_waypoint_file_properties(properties) <<-TEXT.gsub(/^[ ]{8}/, "") OziExplorer Waypoint File Version #{properties.version} #{properties.datum} Reserved 2 TEXT end