added path checking for input file and exit codes
This commit is contained in:
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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "exit_code.h"
|
||||||
#include "lapinfo.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) {
|
void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo *info) {
|
||||||
duckdb_result result;
|
duckdb_result result;
|
||||||
|
|
||||||
@@ -60,20 +84,27 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("You need to specify a duckdb file path! Terminating!\n");
|
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]);
|
printf("First argument: %s\n", argv[1]);
|
||||||
|
|
||||||
duckdb_database db;
|
duckdb_database db;
|
||||||
if (duckdb_open(argv[1], &db) == DuckDBError) {
|
if (duckdb_open(argv[1], &db) == DuckDBError) {
|
||||||
printf("Error opening duckdb file, terminating!\n");
|
printf("Error opening duckdb file, terminating!\n");
|
||||||
exit(2);
|
exit(ERR_DATABASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
duckdb_connection conn;
|
duckdb_connection conn;
|
||||||
if (duckdb_connect(db, &conn) == DuckDBError) {
|
if (duckdb_connect(db, &conn) == DuckDBError) {
|
||||||
printf("Error connecting to the duckdb database! Terminating!\n");
|
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
|
// 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);
|
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res);
|
||||||
if (!data_chunk) {
|
if (!data_chunk) {
|
||||||
printf("Error in retrieving laps");
|
printf("Error in retrieving laps");
|
||||||
exit(4);
|
duckdb_disconnect(&conn);
|
||||||
|
duckdb_close(&db);
|
||||||
|
exit(ERR_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set column
|
// set column
|
||||||
@@ -103,14 +136,12 @@ int main(int argc, char **argv) {
|
|||||||
duckdb_destroy_result(&lap_count_res);
|
duckdb_destroy_result(&lap_count_res);
|
||||||
|
|
||||||
printf("Lap count: %d\n", lap_count);
|
printf("Lap count: %d\n", lap_count);
|
||||||
LapInfo *session_data = malloc(sizeof(LapInfo) * lap_count);
|
LapInfo *session_data = calloc(lap_count, sizeof(LapInfo));
|
||||||
|
double *lap_timestamps = calloc(lap_count, sizeof(double));
|
||||||
double lap_timestamps[lap_count];
|
|
||||||
memset(lap_timestamps, 0, sizeof(lap_timestamps));
|
|
||||||
|
|
||||||
duckdb_result lap_timestamps_res;
|
duckdb_result lap_timestamps_res;
|
||||||
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
|
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
|
||||||
exit(5);
|
exit(ERR_QUERY);
|
||||||
}
|
}
|
||||||
|
|
||||||
int current_index = 0;
|
int current_index = 0;
|
||||||
@@ -151,5 +182,5 @@ int main(int argc, char **argv) {
|
|||||||
duckdb_disconnect(&conn);
|
duckdb_disconnect(&conn);
|
||||||
duckdb_close(&db);
|
duckdb_close(&db);
|
||||||
|
|
||||||
return 0;
|
return EXIT_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user