7 #include "system/error.h"
19 History *create_history(size_t capacity)
26 History *history = PUSH_LT(
28 malloc(sizeof(History)),
30 if (history == NULL) {
31 throw_error(ERROR_TYPE_LIBC);
36 history->capacity = capacity;
39 history->buffer = PUSH_LT(lt, calloc(capacity, sizeof(char*)), free);
40 if (history->buffer == NULL) {
41 throw_error(ERROR_TYPE_LIBC);
48 void destroy_history(History *history)
52 for (size_t i = 0; i < history->capacity; ++i) {
53 if (history->buffer[i] != NULL) {
54 free(history->buffer[i]);
58 RETURN_LT0(history->lt);
61 int history_push(History *history, const char *command)
66 const size_t next_cursor = (history->cursor + 1) % history->capacity;
68 if (history->buffer[history->cursor] != NULL) {
69 free(history->buffer[history->cursor]);
72 history->buffer[history->cursor] = string_duplicate(command, NULL);
74 if (history->buffer[history->cursor] == NULL) {
78 history->cursor = next_cursor;
83 const char *history_get(History *history, size_t i)
86 return history->buffer[(history->cursor + i) % history->capacity];