QtFit  0.1
Internal library development documentation
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
FitStreamReader Class Reference

The FitStreamReader class provides a streaming parser for reading Garmin FIT files. More...

Collaboration diagram for FitStreamReader:
[legend]

Public Member Functions

 FitStreamReader ()
 Constructs a FIT stream reader with no initial data. More...
 
 FitStreamReader (const QByteArray &data)
 Constructs a FIT stream reader that reads from data. More...
 
 FitStreamReader (QIODevice *device)
 Constructs a FIT stream reader that reads from device. More...
 
 ~FitStreamReader ()
 Destroys the FIT stream reader.
 
void addData (const QByteArray &data)
 Adds more data to read. More...
 
bool atEnd () const
 Returns true if the reader has reached the end of the underlying data or device, or an error has occurred. More...
 
void clear ()
 Clears the FIT stream reader. More...
 
QIODevice * device () const
 Returns the current device associated with the reader, or nullptr if no device is assigned. More...
 
void setDevice (QIODevice *device)
 Sets the current device to device, and resets the stream to its initial state. More...
 
QVersionNumber protocolVersion () const
 Returns the protocol version read from the FIT file header, otherwise a null QVersionNumber. More...
 
QVersionNumber profileVersion () const
 Returns the profile version read from the FIT file header, otherwise a null QVersionNumber. More...
 
AbstractDataMessagereadNext ()
 Returns the next FIT data message from the underlying stream, or a null data message if none could be read. More...
 

Protected Member Functions

 FitStreamReader (FitStreamReaderPrivate *const d)
 Constructs a FIT stream reader with private implementation d. More...
 

Protected Attributes

FitStreamReaderPrivate *const d_ptr
 Internal d-pointer.
 

Detailed Description

The FitStreamReader class provides a streaming parser for reading Garmin FIT files.

Todo:
Mention that this is a lower-level class, a users should probably use some yet-to-be FitFile class, that wraps up the calls to this one.
See also
https://developer.garmin.com/fit/protocol/

Definition at line 37 of file fitstreamreader.h.

Constructor & Destructor Documentation

◆ FitStreamReader() [1/4]

FitStreamReader::FitStreamReader ( )

Constructs a FIT stream reader with no initial data.

Use addData() or setDevice() to provide data when ready.

See also
addData
setDevice

Definition at line 54 of file fitstreamreader.cpp.

54  : d_ptr(new FitStreamReaderPrivate(this))
55 {
56 
57 }
Provides private implementation for FitStreamReader.
FitStreamReaderPrivate *const d_ptr
Internal d-pointer.

◆ FitStreamReader() [2/4]

FitStreamReader::FitStreamReader ( const QByteArray &  data)
explicit

Constructs a FIT stream reader that reads from data.

data may be empty or incomplete, in which case more data can be added later via addData().

Parameters
dataFIT data to begin reading.
See also
addData

Definition at line 68 of file fitstreamreader.cpp.

68  : d_ptr(new FitStreamReaderPrivate(this))
69 {
70  Q_D(FitStreamReader);
71  d->data = data;
72  d->parseFileHeader<QByteArray>();
73 }
The FitStreamReader class provides a streaming parser for reading Garmin FIT files.

◆ FitStreamReader() [3/4]

FitStreamReader::FitStreamReader ( QIODevice *  device)
explicit

Constructs a FIT stream reader that reads from device.

device must be open for reading, but does not need to have any bytes available yet.

Note
The caller is responsible for ensuring that device remains valid for the lifetime of the constructed reader, or until clear() or setDevice() is used to clear or replace the device.
Parameters
deviceDevice to to begin reading.
See also
clear
setDevice

Definition at line 88 of file fitstreamreader.cpp.

88  : d_ptr(new FitStreamReaderPrivate(this))
89 {
90  Q_D(FitStreamReader);
91  d->device = device;
92  d->parseFileHeader<QIODevice>();
93 }
QIODevice * device() const
Returns the current device associated with the reader, or nullptr if no device is assigned.

References device().

Here is the call graph for this function:

◆ FitStreamReader() [4/4]

FitStreamReader::FitStreamReader ( FitStreamReaderPrivate *const  d)
explicitprotected

Constructs a FIT stream reader with private implementation d.

Parameters
dPointer to private implementation.

Definition at line 102 of file fitstreamreader.cpp.

102  : d_ptr(d)
103 {
104 
105 }

Member Function Documentation

◆ addData()

void FitStreamReader::addData ( const QByteArray &  data)

Adds more data to read.

Note
It is not valid to use this function if a device has been assigned, ie via either the FitStreamReader(QIODevice *device) constructor, or setDevice().
Parameters
dataAdditonal data to read.

Definition at line 124 of file fitstreamreader.cpp.

125 {
126  Q_D(FitStreamReader);
127  if (d->device != nullptr) {
128  qWarning("FitStreamReader: addData() with device()");
129  Q_ASSERT(d->device != nullptr);
130  return;
131  }
132  d->data += data;
133 }

◆ atEnd()

bool FitStreamReader::atEnd ( ) const

Returns true if the reader has reached the end of the underlying data or device, or an error has occurred.

Todo:
Implement this method.
Returns
true if the reader has reached the end of the underlying data or device.
Todo:
Implement this method.

Definition at line 143 of file fitstreamreader.cpp.

144 {
145  return false; /// \todo Implement this method.
146 }

◆ clear()

void FitStreamReader::clear ( )

Clears the FIT stream reader.

This returns the reader to a state equivalent to having just been default-constructed.

See also
FitStreamReader()

Definition at line 155 of file fitstreamreader.cpp.

156 {
157  Q_D(FitStreamReader);
158  d->data.clear();
159  d->dataOffset = 0;
160  d->device = nullptr;
161 // d->headerSize = 0;
162 // d->expectedChecksum = 0;
163  d->expectedDataSize = 0;
164  d->protocolVersion = QVersionNumber();
165  d->profileVersion = QVersionNumber();
166  d->dataDefinitions.clear();
167  d->recordSizes.clear();
168 }

Referenced by setDevice().

Here is the caller graph for this function:

◆ device()

QIODevice * FitStreamReader::device ( ) const

Returns the current device associated with the reader, or nullptr if no device is assigned.

Returns
the current device associated with the reader, or nullptr if no device is assigned.
See also
setDevice

Definition at line 177 of file fitstreamreader.cpp.

178 {
179  Q_D(const FitStreamReader);
180  return d->device;
181 }

Referenced by FitStreamReader(), and setDevice().

Here is the caller graph for this function:

◆ profileVersion()

QVersionNumber FitStreamReader::profileVersion ( ) const

Returns the profile version read from the FIT file header, otherwise a null QVersionNumber.

FIT profile versions have only two components - major and minor. Both have a maximum value of 15, as the underlying format uses 4-bits for each component.

Returns
the profile version read from the FIT file header, otherwise a null QVersionNumber.

Definition at line 211 of file fitstreamreader.cpp.

212 {
213  Q_D(const FitStreamReader);
214  return d->profileVersion;
215 }

◆ protocolVersion()

QVersionNumber FitStreamReader::protocolVersion ( ) const

Returns the protocol version read from the FIT file header, otherwise a null QVersionNumber.

FIT protocol versions have only two components - major and minor. The minor component is limited to the range 0 to 100, whereas the major component may be (theoreticlly) 0 to 655.

Returns
the protocol version read from the FIT file header, otherwise a null QVersionNumber.

Definition at line 225 of file fitstreamreader.cpp.

226 {
227  Q_D(const FitStreamReader);
228  return d->protocolVersion;
229 }

◆ readNext()

AbstractDataMessage * FitStreamReader::readNext ( )

Returns the next FIT data message from the underlying stream, or a null data message if none could be read.

If FIT header has not been read yet, it will be read first. If any FIT definition messages are found, they will be parsed, and kept in a dictionary, to used for interpretation of subsequent data messages. Reading will continue until the next FIT data message is found, or no more bytes are available for reading.

Todo:
Document how the caller should distinguish errors, from more-data-needed.
Returns
the next FIT data message, or a null data message.
See also
addData

Definition at line 246 of file fitstreamreader.cpp.

247 {
248  Q_D(FitStreamReader);
249  return (d->device == nullptr)
250  ? d->readNextDataMessage<QByteArray>() : d->readNextDataMessage<QIODevice>();
251 }

◆ setDevice()

void FitStreamReader::setDevice ( QIODevice *  device)

Sets the current device to device, and resets the stream to its initial state.

device must be open for reading, but does not need to have any bytes available yet.

Note
The caller is responsible for ensuring that device remains valid for the lifetime of the constructed reader, or until clear() or setDevice() is used to clear or replace the device.
Parameters
deviceIO device to read from.
See also
device

Definition at line 195 of file fitstreamreader.cpp.

196 {
197  Q_D(FitStreamReader);
198  if (device) clear();
199  d->device = device;
200  if (device) d->parseFileHeader<QIODevice>();
201 }
void clear()
Clears the FIT stream reader.

References clear(), and device().

Here is the call graph for this function:

The documentation for this class was generated from the following files: