#ifndef __NMEA_H__
#define __NMEA_H__

#include <stdio.h>


#define nmea_sprintf(buf, format, ...)   {      \
        sprintf (buf, format, ## __VA_ARGS__);  \
        char *tmp = strdup (buf);               \
        nmea_append_checksum (buf, tmp);        \
        free (tmp);                             \
    }

#define nmea_snprintf(buf, size, format, ...)   {       \
        snprintf (buf, size, format, ## __VA_ARGS__);   \
        char *tmp = strdup (buf);                       \
        nmea_append_checksum (buf, tmp);                \
        free (tmp);                                     \
    }


#ifdef __cplusplus
extern "C" {
#endif

/* Extracts the payload string between the $ and *
   Returns strlen on succes; 0 otherwise
*/
int
nmea_payload (const char *buf, char *str);

/* Computes the NMEA checksum ignoring leading $ and trailing *
   Returns checksum on success; -1 on error
*/
int
nmea_compute_checksum (const char *buf);

/* Validates NMEA message checksum
   Returns 1 if valid, 0 if invalid
*/
int
nmea_validate_checksum(const char *buf);

/* Copies src to dest with appended NMEA checksum.  The strings may overlap.
   Returns strlen on success; 0 otherwise
 */
int
nmea_append_checksum (char *dest, const char *src);

/* Extracts the string argument associated with field n.
   Correctly handles empty NMEA fields.
   Returns 1 success; 0 otherwise
*/
int
nmea_arg (const char *str, int n, char *out);

/* Extracts character argument for field n.
   Returns 1 success; 0 otherwise
*/
int
nmea_argc (const char *buf, int n, char *c);

/* Extracts integer argument for field n.
   Returns 1 success; 0 otherwise
*/
int
nmea_argi (const char *buf, int n, int *i);

/* Extracts double argument for field n.
   Returns 1 success; 0 otherwise
*/
int
nmea_argf (const char *buf, int n, double *d);

#ifdef __cplusplus
}
#endif

#endif //__NMEA_H__
