Compare commits

...

4 Commits

Author SHA1 Message Date
a948ca19fc new memory free strategy that relies on goto 2026-01-08 21:16:11 +01:00
ad85394014 header guard 2026-01-08 21:15:55 +01:00
6d8018f21b header guard 2026-01-08 21:15:46 +01:00
4864f01af3 added array definiitions with getter and setter for each type 2026-01-08 21:15:35 +01:00
5 changed files with 456 additions and 21 deletions

313
src/arrays.c Normal file
View File

@@ -0,0 +1,313 @@
#include "arrays.h"
#include <stdint.h>
bool bool_array_get(const bool_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void bool_array_set(bool_array *array, const bool value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length && index <= 0 && index < array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
char char_array_get(const char_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void char_array_set(char_array *array, const char value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
int8_t int8_array_get(const int8_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void int8_array_set(int8_array *array, const int8_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
int16_t int16_array_get(const int16_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void int16_array_set(int16_array *array, const int16_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
int32_t int32_array_get(const int32_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void int32_array_set(int32_array *array, const int32_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
int64_t int64_array_get(const int64_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void int64_array_set(int64_array *array, const int64_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
uint8_t uint8_array_get(const uint8_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void uint8_array_set(uint8_array *array, const uint8_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
uint16_t uint16_array_get(const uint16_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void uint16_array_set(uint16_array *array, const uint16_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
uint32_t uint32_array_get(const uint32_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void uint32_array_set(uint32_array *array, const uint32_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
uint64_t uint64_array_get(const uint64_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void uint64_array_set(uint64_array *array, const uint64_t value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
float float_array_get(const float_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void float_array_set(float_array *array, const float value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}
double double_array_get(const double_array *array, int32_t index) {
if (index >= 0 && index < array->length) {
return array->items[index];
}
return 0;
}
//@param index: set to -1 to put it at the end of the array
void double_array_set(double_array *array, const double value, int32_t index) {
if (!array) {
return;
}
if (array->capacity <= array->length) {
return;
}
if (index == -1) {
array->items[array->length - 1] = value;
return;
}
array->items[index] = value;
}

108
src/arrays.h Normal file
View File

@@ -0,0 +1,108 @@
#ifndef ARRAYS_H
#define ARRAYS_H
#include <stdbool.h>
#include <stdint.h>
// --- Boolean ---
typedef struct {
bool *items;
int32_t length;
int32_t capacity;
} bool_array;
bool bool_array_get(const bool_array *array, int32_t index);
// --- Char (Strings/Bytes) ---
typedef struct {
char *items;
int32_t length;
int32_t capacity;
} char_array;
char char_array_get(const char_array *array, int32_t index);
// --- Signed Integers ---
typedef struct {
int8_t *items;
int32_t length;
int32_t capacity;
} int8_array;
int8_t int8_array_get(const int8_array *array, int32_t index);
typedef struct {
int16_t *items;
int32_t length;
int32_t capacity;
} int16_array;
int16_t int16_array_get(const int16_array *array, int32_t index);
typedef struct {
int32_t *items;
int32_t length;
int32_t capacity;
} int32_array;
int32_t int32_array_get(const int32_array *array, int32_t index);
typedef struct {
int64_t *items;
int32_t length;
int32_t capacity;
} int64_array;
int64_t int64_array_get(const int64_array *array, int32_t index);
// --- Unsigned Integers ---
typedef struct {
uint8_t *items;
int32_t length;
int32_t capacity;
} uint8_array;
uint8_t uint8_array_get(const uint8_array *array, int32_t index);
typedef struct {
uint16_t *items;
int32_t length;
int32_t capacity;
} uint16_array;
uint16_t uint16_array_get(const uint16_array *array, int32_t index);
typedef struct {
uint32_t *items;
int32_t length;
int32_t capacity;
} uint32_array;
uint32_t uint32_array_get(const uint32_array *array, int32_t index);
typedef struct {
uint64_t *items;
int32_t length;
int32_t capacity;
} uint64_array;
uint64_t uint64_array_get(const uint64_array *array, int32_t index);
// --- Floating Point ---
typedef struct {
float *items;
int32_t length;
int32_t capacity;
} float_array;
float float_array_get(const float_array *array, int32_t index);
typedef struct {
double *items;
int32_t length;
int32_t capacity;
} double_array;
double double_array_get(const double_array *array, int32_t index);
#endif

View File

@@ -1,3 +1,6 @@
#ifndef EXIT_CODE_H
#define EXIT_CODE_H
typedef enum {
EXIT_OK = 0,
ERR_ARGUMENT = 1,
@@ -7,3 +10,5 @@ typedef enum {
ERR_MEMORY = 5,
ERR_QUERY = 6,
} exit_code;
#endif

View File

@@ -1,4 +1,5 @@
#pragma once
#ifndef LAPINFO_H
#define LAPINFO_H
#include <stdint.h>
@@ -29,3 +30,5 @@ typedef struct {
void free_telemetry_info(TelemetryInfo *t);
void destroyLapinfo(LapInfo *info);
#endif

View File

@@ -68,7 +68,6 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo
info->data_last_index++;
break;
}
// printf("%f, ", ((float *)col1_data)[row]);
} else {
printf("NULL");
}
@@ -80,44 +79,44 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo
}
int main(int argc, char **argv) {
duckdb_database db;
duckdb_connection conn;
duckdb_result lap_count_res;
LapInfo *session_data = NULL;
double *lap_timestamps = NULL;
printf("\nHello from HardCompound!\n");
if (argc < 2) {
printf("You need to specify a duckdb file path! Terminating!\n");
exit(ERR_ARGUMENT);
goto cleanup;
}
if (!is_valid_path(argv[1])) {
printf("The file you gave is not valid. Exiting!");
exit(ERR_FILE);
goto cleanup;
}
printf("First argument: %s\n", argv[1]);
duckdb_database db;
if (duckdb_open(argv[1], &db) == DuckDBError) {
printf("Error opening duckdb file, terminating!\n");
exit(ERR_DATABASE);
goto cleanup;
}
duckdb_connection conn;
if (duckdb_connect(db, &conn) == DuckDBError) {
printf("Error connecting to the duckdb database! Terminating!\n");
duckdb_close(&db);
exit(ERR_CONNECTION);
goto cleanup;
}
// get how many laps is in the session and allocate that many LapInfo-s
duckdb_result lap_count_res;
int32_t lap_count = 0;
duckdb_query(conn, "Select count(value) from Lap\n", &lap_count_res);
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res);
if (!data_chunk) {
printf("Error in retrieving laps");
duckdb_disconnect(&conn);
duckdb_close(&db);
exit(ERR_QUERY);
goto cleanup;
}
// set column
@@ -136,22 +135,22 @@ int main(int argc, char **argv) {
duckdb_destroy_result(&lap_count_res);
printf("Lap count: %d\n", lap_count);
LapInfo *session_data = calloc(lap_count, sizeof(LapInfo));
double *lap_timestamps = calloc(lap_count, sizeof(double));
session_data = calloc(lap_count, sizeof(LapInfo));
lap_timestamps = calloc(lap_count, sizeof(double));
if (!session_data) {
printf("Session data failed to allocate!");
exit(ERR_MEMORY);
goto cleanup;
}
if (!lap_timestamps) {
printf("Lap timestaps array failed to init!");
exit(ERR_MEMORY);
goto cleanup;
}
duckdb_result lap_timestamps_res;
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
exit(ERR_QUERY);
goto cleanup;
}
int current_index = 0;
@@ -185,12 +184,19 @@ int main(int argc, char **argv) {
}
printf("\n");
free(session_data);
// get the per laptime data
printf("Shutting down HardCompound!\n");
printf("Closing database and any connection.\n");
goto cleanup;
exit:
return EXIT_OK;
cleanup:
free(NULL);
free(session_data);
free(lap_timestamps);
duckdb_disconnect(&conn);
duckdb_close(&db);
return EXIT_OK;
goto exit;
}