diff --git a/igc.c b/igc.c index 56b9177..9edb2ab 100644 --- a/igc.c +++ b/igc.c @@ -14,6 +14,7 @@ #include #include #include "igc.h" +#include "linkage.h" struct IGC_DataPoint* parse_datapoint(char* line) { @@ -107,13 +108,11 @@ struct IGC_Header* parse_header_record(struct IGC_Header* hdr, char* line) return hdr; } -void parse_igc_file(FILE* fp) +void parse_igc_file(FILE* fp, struct IGC_Header* hdr, struct dp_node* list) { size_t len = 0; char* line = NULL; - struct IGC_Header* hdr = (struct IGC_Header*)calloc(1, sizeof(struct IGC_Header)); - while (getline(&line, &len, fp) != -1) { switch(line[0]) @@ -126,9 +125,9 @@ void parse_igc_file(FILE* fp) break; case DATAPOINT: struct IGC_DataPoint* dp = parse_datapoint(line); - // show_datapoint(dp); + //show_datapoint(dp); // Doubly linked list of points (so theyre joined in chronological order; opens possibilities for analysis later) - // append_datapoint(dp); + append_datapoint(list, dp); break; case HEADER: hdr = parse_header_record(hdr, line); @@ -143,27 +142,3 @@ void parse_igc_file(FILE* fp) } show_header_info(hdr); } - -int main(int argc, char* argv[]) -{ - if (argc < 2) - { - printf("Usage: %s \n", argv[0]); - return -EINVAL; - } - - char filename[NAME_MAX] = {0}; - strncpy(filename, argv[1], NAME_MAX-1); - - FILE* fp = fopen(filename, "r"); - - if (!fp) - { - printf("Couldn't open file %s\n", filename); - return -EINVAL; - } - - parse_igc_file(fp); - - return EXIT_SUCCESS; -} diff --git a/igc.h b/igc.h index 9122ac2..6e63aa3 100644 --- a/igc.h +++ b/igc.h @@ -13,6 +13,8 @@ #define NAME_MAX 256 #define TMP_SIZE 8 +#include "linkage.h" + enum IGC_RecordType { MANUFACTURER = 'A', @@ -81,7 +83,7 @@ struct IGC_DataPoint int gps_alt; }; -void parse_igc_file(FILE* fp); +void parse_igc_file(FILE* fp, struct IGC_Header* hdr, struct dp_node* list); struct IGC_DataPoint* parse_datapoint(char* line); void show_datapoint(struct IGC_DataPoint* dp); void show_header_info(struct IGC_Header* hdr); diff --git a/linkage.c b/linkage.c new file mode 100644 index 0000000..80b2fa1 --- /dev/null +++ b/linkage.c @@ -0,0 +1,69 @@ +/* + * IGC parser - data linking module (doubly linked datapoint list) + * @brief parse IGC file, get all data points, compute average horizontal/vertical speed, print to STDOUT + * + * As per standard: https://xp-soaring.github.io/igc_file_format/igc_format_2008.html + * https://xp-soaring.github.io/igc_file_format/igc_fr_specification_with_al8_2023-2-1_0.pdf + * + * @author xamidev + * @license GNU GPL v3 + */ + +#include +#include +#include "igc.h" +#include "linkage.h" + +struct dp_node* create_datapoint_list() +{ + struct dp_node* head = (struct dp_node*)calloc(1, sizeof(struct dp_node)); + + head->data = NULL; + head->prev = NULL; + head->next = NULL; + return head; +} + +void append_datapoint(struct dp_node* list, struct IGC_DataPoint* dp) +{ + struct dp_node* new = (struct dp_node*)calloc(1, sizeof(struct dp_node)); + + if (list->next == NULL) + { + list->next = new; + + new->prev = list; + new->next = NULL; + new->data = dp; + return; + } + + while (list->next != NULL) + { + list = list->next; + } + + list->next = new; + new->prev = list; + new->next = NULL; + new->data = dp; + return; +} + +void print_datapoint_list(struct dp_node* list) +{ + if (list->data == NULL) + { + // We're at head! + list = list->next; + } + + while (list->next != NULL) + { + show_datapoint(list->data); + printf(" <-> "); + list = list->next; + } + puts("NULL"); + return; +} diff --git a/linkage.h b/linkage.h new file mode 100644 index 0000000..b7a6a7d --- /dev/null +++ b/linkage.h @@ -0,0 +1,29 @@ +/* + * IGC parser - data linking module (doubly linked datapoint list) + * @brief parse IGC file, get all data points, compute average horizontal/vertical speed, print to STDOUT + * + * As per standard: https://xp-soaring.github.io/igc_file_format/igc_format_2008.html + * https://xp-soaring.github.io/igc_file_format/igc_fr_specification_with_al8_2023-2-1_0.pdf + * + * @author xamidev + * @license GNU GPL v3 + */ + +#ifndef LINKAGE_H +#define LINKAGE_H + +#include "igc.h" + +struct dp_node +{ + struct IGC_DataPoint* data; + + struct dp_node* prev; + struct dp_node* next; +}; + +struct dp_node* create_datapoint_list(); +void append_datapoint(struct dp_node* list, struct IGC_DataPoint* dp); +void print_datapoint_list(struct dp_node* list); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..a162cea --- /dev/null +++ b/main.c @@ -0,0 +1,45 @@ +/* + * IGC parser + * @brief parse IGC file, get all data points, compute average horizontal/vertical speed, print to STDOUT + * + * As per standard: https://xp-soaring.github.io/igc_file_format/igc_format_2008.html + * https://xp-soaring.github.io/igc_file_format/igc_fr_specification_with_al8_2023-2-1_0.pdf + * + * @author xamidev + * @license GNU GPL v3 + */ + +#include +#include +#include +#include +#include "igc.h" +#include "linkage.h" + +int main(int argc, char* argv[]) +{ + if (argc < 2) + { + printf("Usage: %s \n", argv[0]); + return -EINVAL; + } + + char filename[NAME_MAX] = {0}; + strncpy(filename, argv[1], NAME_MAX-1); + + FILE* fp = fopen(filename, "r"); + + if (!fp) + { + printf("Couldn't open file %s\n", filename); + return -EINVAL; + } + + struct dp_node* head = create_datapoint_list(); + struct IGC_Header* hdr = (struct IGC_Header*)calloc(1, sizeof(struct IGC_Header)); + parse_igc_file(fp, hdr, head); + + print_datapoint_list(head); + + return EXIT_SUCCESS; +} diff --git a/makefile b/makefile index 3611072..25d7b31 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ all: - gcc -Wall -Wextra igc.c + gcc -g -Wall -Wextra linkage.c igc.c main.c clean: rm a.out