dp doubly linked list
This commit is contained in:
33
igc.c
33
igc.c
@@ -14,6 +14,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "igc.h"
|
#include "igc.h"
|
||||||
|
#include "linkage.h"
|
||||||
|
|
||||||
struct IGC_DataPoint* parse_datapoint(char* line)
|
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;
|
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;
|
size_t len = 0;
|
||||||
char* line = NULL;
|
char* line = NULL;
|
||||||
|
|
||||||
struct IGC_Header* hdr = (struct IGC_Header*)calloc(1, sizeof(struct IGC_Header));
|
|
||||||
|
|
||||||
while (getline(&line, &len, fp) != -1)
|
while (getline(&line, &len, fp) != -1)
|
||||||
{
|
{
|
||||||
switch(line[0])
|
switch(line[0])
|
||||||
@@ -126,9 +125,9 @@ void parse_igc_file(FILE* fp)
|
|||||||
break;
|
break;
|
||||||
case DATAPOINT:
|
case DATAPOINT:
|
||||||
struct IGC_DataPoint* dp = parse_datapoint(line);
|
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)
|
// Doubly linked list of points (so theyre joined in chronological order; opens possibilities for analysis later)
|
||||||
// append_datapoint(dp);
|
append_datapoint(list, dp);
|
||||||
break;
|
break;
|
||||||
case HEADER:
|
case HEADER:
|
||||||
hdr = parse_header_record(hdr, line);
|
hdr = parse_header_record(hdr, line);
|
||||||
@@ -143,27 +142,3 @@ void parse_igc_file(FILE* fp)
|
|||||||
}
|
}
|
||||||
show_header_info(hdr);
|
show_header_info(hdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
if (argc < 2)
|
|
||||||
{
|
|
||||||
printf("Usage: %s <file>\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;
|
|
||||||
}
|
|
||||||
|
|||||||
4
igc.h
4
igc.h
@@ -13,6 +13,8 @@
|
|||||||
#define NAME_MAX 256
|
#define NAME_MAX 256
|
||||||
#define TMP_SIZE 8
|
#define TMP_SIZE 8
|
||||||
|
|
||||||
|
#include "linkage.h"
|
||||||
|
|
||||||
enum IGC_RecordType
|
enum IGC_RecordType
|
||||||
{
|
{
|
||||||
MANUFACTURER = 'A',
|
MANUFACTURER = 'A',
|
||||||
@@ -81,7 +83,7 @@ struct IGC_DataPoint
|
|||||||
int gps_alt;
|
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);
|
struct IGC_DataPoint* parse_datapoint(char* line);
|
||||||
void show_datapoint(struct IGC_DataPoint* dp);
|
void show_datapoint(struct IGC_DataPoint* dp);
|
||||||
void show_header_info(struct IGC_Header* hdr);
|
void show_header_info(struct IGC_Header* hdr);
|
||||||
|
|||||||
69
linkage.c
Normal file
69
linkage.c
Normal file
@@ -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 <xamidev@riseup.net>
|
||||||
|
* @license GNU GPL v3
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
29
linkage.h
Normal file
29
linkage.h
Normal file
@@ -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 <xamidev@riseup.net>
|
||||||
|
* @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
|
||||||
45
main.c
Normal file
45
main.c
Normal file
@@ -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 <xamidev@riseup.net>
|
||||||
|
* @license GNU GPL v3
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "igc.h"
|
||||||
|
#include "linkage.h"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
printf("Usage: %s <file>\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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user