73 lines
1.7 KiB
C
Raw Normal View History

2025-12-05 17:08:50 +01:00
#ifndef TIKKER_DB_H
#define TIKKER_DB_H
#include <string.h>
#include <stdlib.h>
static inline char *tikker_csv_get_field(char *result, int index) {
char *end = NULL;
int current_index = 0;
while ((end = strstr((char *)result, ";")) != NULL) {
if (index == current_index) {
result[end - (char *)result] = 0;
return result;
}
result = end + 1;
current_index++;
}
if (end) *end = 0;
return result;
}
typedef struct {
char **fields;
int field_count;
} tikker_csv_row_t;
typedef struct {
char *data;
char *current;
} tikker_csv_iter_t;
static inline tikker_csv_iter_t tikker_csv_iter_init(char *csv) {
tikker_csv_iter_t iter;
iter.data = csv;
iter.current = csv;
return iter;
}
static inline int tikker_csv_is_metadata(const char *line) {
return strstr(line, "(text)") != NULL ||
strstr(line, "(integer)") != NULL ||
strstr(line, "(real)") != NULL ||
strstr(line, "(blob)") != NULL;
}
static inline int tikker_csv_iter_next(tikker_csv_iter_t *iter, char **field1, char **field2) {
while (iter->current && *iter->current) {
char *line = iter->current;
char *next = strchr(line, '\n');
if (next) *next = '\0';
iter->current = next ? next + 1 : NULL;
if (tikker_csv_is_metadata(line)) continue;
*field1 = line;
char *sep = strchr(line, ';');
if (sep) {
*sep = '\0';
*field2 = sep + 1;
char *end = strchr(*field2, ';');
if (end) *end = '\0';
} else {
*field2 = NULL;
}
return 1;
}
return 0;
}
#endif