Class: Fit4Ruby::FitFile
- Inherits:
-
Object
- Object
- Fit4Ruby::FitFile
- Defined in:
- lib/fit4ruby/FitFile.rb
Instance Method Summary collapse
-
#initialize ⇒ FitFile
constructor
A new instance of FitFile.
- #read(file_name, filter = nil) ⇒ Object
- #write(file_name, top_level_record) ⇒ Object
Constructor Details
#initialize ⇒ FitFile
Returns a new instance of FitFile.
27 28 29 |
# File 'lib/fit4ruby/FitFile.rb', line 27 def initialize() @header = nil end |
Instance Method Details
#read(file_name, filter = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fit4ruby/FitFile.rb', line 31 def read(file_name, filter = nil) @file_name = file_name definitions = {} begin io = ::File.open(file_name, 'rb') rescue StandardError => e Log.fatal "Cannot open FIT file '#{file_name}': #{e.}" end header = FitHeader.read(io) header.check check_crc(io, header.end_pos) entity = FitFileEntity.new # This Array holds the raw data of the records that may be needed to # dump a human readable form of the FIT file. records = [] # This hash will hold a counter for each record type. The counter is # incremented each time the corresponding record type is found. record_counters = Hash.new { 0 } while io.pos < header.end_pos record = FitRecord.new(definitions) record.read(io, entity, filter, record_counters) records << record if filter end io.close header.dump if filter && filter.record_numbers.nil? dump_records(records) if filter entity.check entity.top_level_record end |
#write(file_name, top_level_record) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fit4ruby/FitFile.rb', line 66 def write(file_name, top_level_record) begin io = ::File.open(file_name, 'wb+') rescue StandardError => e Log.fatal "Cannot open FIT file '#{file_name}': #{e.}" end # Create a header object, but don't yet write it into the file. header = FitHeader.new start_pos = header.header_size # Move the pointer behind the header section. io.seek(start_pos) id_mapper = FitMessageIdMapper.new top_level_record.write(io, id_mapper) end_pos = io.pos crc = write_crc(io, start_pos, end_pos) # Complete the data of the header section and write it at the start of # the file. header.data_size = end_pos - start_pos header.crc = crc io.seek(0) header.write(io) io.close end |