148 lines
2.7 KiB
C
148 lines
2.7 KiB
C
#include "window_state.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
struct state *s = NULL;
|
|
|
|
#define INTENTION_MAX_SIZE 2048
|
|
|
|
typedef struct record_t {
|
|
char *key;
|
|
U32 value;
|
|
} Record;
|
|
|
|
struct state {
|
|
Mode mode;
|
|
char intention[INTENTION_MAX_SIZE];
|
|
Record **records; // array of records
|
|
size_t size_records;
|
|
size_t capacity_records;
|
|
};
|
|
|
|
void init_window_state() {
|
|
s = malloc(sizeof(struct state));
|
|
s->mode = WINDOW;
|
|
|
|
memset(s->intention, 0, sizeof(s->intention));
|
|
|
|
s->size_records = 0;
|
|
s->capacity_records = 1000; //TODO: make dynamic
|
|
s->records = malloc(sizeof(Record *) * s->capacity_records);
|
|
}
|
|
|
|
static void add_record(char *key, U32 value) {
|
|
if (s->size_records >= s->capacity_records) {
|
|
printf("MAX capacity");
|
|
exit(-1);
|
|
}
|
|
|
|
char *cpy = strdup(key);
|
|
|
|
Record *new_record = malloc(sizeof(Record));
|
|
new_record->key = cpy;
|
|
new_record->value = value;
|
|
|
|
*(s->records + s->size_records) = new_record;
|
|
s->size_records++;
|
|
}
|
|
|
|
static void upsert_record(char *key, U32 value) {
|
|
bool found = false;
|
|
for (int i = 0; i < s->size_records; i++) {
|
|
Record *rec = *(s->records + i);
|
|
if (strcmp(rec->key, key) == 0) {
|
|
found = true;
|
|
rec->value = value;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
add_record(key, value);
|
|
}
|
|
}
|
|
|
|
static Record *get_record(char *key) {
|
|
for (int i = 0; i < s->size_records; i++) {
|
|
Record *rec = *(s->records + i);
|
|
if (strcmp(rec->key, key) == 0) {
|
|
return rec;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void print_records() {
|
|
for (int i = 0; i < s->size_records; i++) {
|
|
Record *rec = *(s->records + i);
|
|
printf("record: %s -> %d\n", rec->key, rec->value);
|
|
}
|
|
}
|
|
|
|
static void generate_key(struct tm *time, char *buffer, size_t buffer_size) {
|
|
snprintf(buffer, buffer_size, "%d", time->tm_yday);
|
|
}
|
|
|
|
void cleanup_window_state() {
|
|
if (!s) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < s->size_records; i++) {
|
|
Record *rec = *(s->records + s->size_records);
|
|
free(rec);
|
|
}
|
|
free(s->records);
|
|
|
|
free(s);
|
|
s = NULL;
|
|
}
|
|
|
|
void set_intention(char *intention) {
|
|
assert(s);
|
|
|
|
strncpy(s->intention, intention, sizeof(s->intention));
|
|
s->intention[sizeof(s->intention) - 1] = '\0';
|
|
}
|
|
|
|
char *get_intention() {
|
|
assert(s);
|
|
return s->intention;
|
|
}
|
|
|
|
size_t get_intention_size() {
|
|
assert(s);
|
|
return sizeof(s->intention);
|
|
}
|
|
|
|
Mode get_mode() {
|
|
assert(s);
|
|
return s->mode;
|
|
}
|
|
|
|
void set_mode(Mode new_mode) {
|
|
s->mode = new_mode;
|
|
}
|
|
|
|
U32 get_result(struct tm *time) {
|
|
char key[10];
|
|
generate_key(time, key, sizeof(key));
|
|
Record *rec = get_record(key);
|
|
if (!rec) {
|
|
return 0;
|
|
}
|
|
|
|
return rec->value;
|
|
}
|
|
|
|
void set_result(struct tm *time, U32 result) {
|
|
char key[10];
|
|
generate_key(time, key, sizeof(key));
|
|
upsert_record(key, result);
|
|
|
|
print_records();
|
|
}
|