Compare commits
2 Commits
615d8c7f06
...
25a9dfb349
| Author | SHA1 | Date | |
|---|---|---|---|
| 25a9dfb349 | |||
| b380f203e3 |
9
Makefile
9
Makefile
@@ -5,6 +5,7 @@ VENDORDIR = vendor
|
||||
|
||||
target ?= linux
|
||||
profile ?= debug
|
||||
valgrind ?= false
|
||||
|
||||
ifeq ($(target), windows)
|
||||
CC = x86_64-w64-mingw32-gcc
|
||||
@@ -56,7 +57,13 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.c
|
||||
clean:
|
||||
rm -rf $(BUILDDIR)
|
||||
|
||||
ifeq ($(valgrind), true)
|
||||
RUN_CMD = valgrind --leak-check=full ./$(BUILDDIR)/$(APPNAME)$(EXT)
|
||||
else
|
||||
RUN_CMD = ./$(BUILDDIR)/$(APPNAME)$(EXT)
|
||||
endif
|
||||
|
||||
run: all
|
||||
valgrind --leak-check=full ./$(BUILDDIR)/$(APPNAME)$(EXT) ./data/Circuit.duckdb
|
||||
$(RUN_CMD) ./data/Circuit.duckdb
|
||||
|
||||
.PHONY: all clean run
|
||||
|
||||
9
src/exit_code.h
Normal file
9
src/exit_code.h
Normal file
@@ -0,0 +1,9 @@
|
||||
typedef enum {
|
||||
EXIT_OK = 0,
|
||||
ERR_ARGUMENT = 1,
|
||||
ERR_FILE = 2,
|
||||
ERR_DATABASE = 3,
|
||||
ERR_CONNECTION = 4,
|
||||
ERR_MEMORY = 5,
|
||||
ERR_QUERY = 6,
|
||||
} exit_code;
|
||||
51
src/main.c
51
src/main.c
@@ -3,9 +3,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "exit_code.h"
|
||||
#include "lapinfo.h"
|
||||
|
||||
bool is_valid_path(const char *path) {
|
||||
if (path == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *dot = strrchr(path, '.'); // find the last dot which is for the extension
|
||||
if (!dot || strcmp(dot, ".duckdb") != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat path_stat;
|
||||
if (stat(path, &path_stat) != 0) {
|
||||
return false; // path does not exist
|
||||
}
|
||||
|
||||
if (!S_ISREG(path_stat.st_mode)) {
|
||||
return false; // checks if it is a regular file
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo *info) {
|
||||
duckdb_result result;
|
||||
|
||||
@@ -60,20 +84,27 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (argc < 2) {
|
||||
printf("You need to specify a duckdb file path! Terminating!\n");
|
||||
exit(1);
|
||||
exit(ERR_ARGUMENT);
|
||||
}
|
||||
|
||||
if (!is_valid_path(argv[1])) {
|
||||
printf("The file you gave is not valid. Exiting!");
|
||||
exit(ERR_FILE);
|
||||
}
|
||||
|
||||
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(2);
|
||||
exit(ERR_DATABASE);
|
||||
}
|
||||
|
||||
duckdb_connection conn;
|
||||
if (duckdb_connect(db, &conn) == DuckDBError) {
|
||||
printf("Error connecting to the duckdb database! Terminating!\n");
|
||||
exit(3);
|
||||
duckdb_close(&db);
|
||||
exit(ERR_CONNECTION);
|
||||
}
|
||||
|
||||
// get how many laps is in the session and allocate that many LapInfo-s
|
||||
@@ -84,7 +115,9 @@ int main(int argc, char **argv) {
|
||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res);
|
||||
if (!data_chunk) {
|
||||
printf("Error in retrieving laps");
|
||||
exit(4);
|
||||
duckdb_disconnect(&conn);
|
||||
duckdb_close(&db);
|
||||
exit(ERR_QUERY);
|
||||
}
|
||||
|
||||
// set column
|
||||
@@ -103,14 +136,12 @@ int main(int argc, char **argv) {
|
||||
duckdb_destroy_result(&lap_count_res);
|
||||
|
||||
printf("Lap count: %d\n", lap_count);
|
||||
LapInfo *session_data = malloc(sizeof(LapInfo) * lap_count);
|
||||
|
||||
double lap_timestamps[lap_count];
|
||||
memset(lap_timestamps, 0, sizeof(lap_timestamps));
|
||||
LapInfo *session_data = calloc(lap_count, sizeof(LapInfo));
|
||||
double *lap_timestamps = calloc(lap_count, sizeof(double));
|
||||
|
||||
duckdb_result lap_timestamps_res;
|
||||
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
|
||||
exit(5);
|
||||
exit(ERR_QUERY);
|
||||
}
|
||||
|
||||
int current_index = 0;
|
||||
@@ -151,5 +182,5 @@ int main(int argc, char **argv) {
|
||||
duckdb_disconnect(&conn);
|
||||
duckdb_close(&db);
|
||||
|
||||
return 0;
|
||||
return EXIT_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user