PatchworkOS
Loading...
Searching...
No Matches
history.c
Go to the documentation of this file.
1#include "history.h"
2
3#include <stdlib.h>
4#include <string.h>
5
6void history_init(history_t* history)
7{
8 history->count = 0;
9 history->index = 0;
10}
11
13{
14 for (uint64_t i = 0; i < history->count; i++)
15 {
16 free(history->entries[i]);
17 }
18}
19
20void history_push(history_t* history, const char* entry)
21{
22 if (history->count != 0 && strcmp(entry, history->entries[history->count - 1]) == 0)
23 {
24 history->index = history->count;
25 return;
26 }
27
28 uint64_t entryLen = strlen(entry);
29 if (entryLen == 0)
30 {
31 history->index = history->count;
32 return;
33 }
34
35 if (history->count == HISTORY_MAX_ENTRY)
36 {
37 free(history->entries[0]);
38 memmove(history->entries, history->entries + 1, sizeof(char*) * (HISTORY_MAX_ENTRY - 1));
39 history->count--;
40 }
41
42 history->entries[history->count] = malloc(entryLen + 1);
43 strcpy(history->entries[history->count], entry);
44 history->count++;
45 history->index = history->count;
46}
47
48const char* history_next(history_t* history)
49{
50 if (history->count == 0 || history->index >= history->count - 1)
51 {
52 history->index = history->count;
53 return NULL;
54 }
55
56 history->index++;
57 return history->entries[history->index];
58}
59
60const char* history_previous(history_t* history)
61{
62 if (history->count == 0 || history->index == 0)
63 {
64 return NULL;
65 }
66
67 history->index--;
68 return history->entries[history->index];
69}
#define NULL
Pointer error value.
Definition NULL.h:23
const char * history_previous(history_t *history)
Definition history.c:60
void history_deinit(history_t *history)
Definition history.c:12
void history_push(history_t *history, const char *entry)
Definition history.c:20
void history_init(history_t *history)
Definition history.c:6
const char * history_next(history_t *history)
Definition history.c:48
#define HISTORY_MAX_ENTRY
Definition history.h:6
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC void * memmove(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
_PUBLIC size_t strlen(const char *s)
Definition strlen.c:3
_PUBLIC char * strcpy(char *_RESTRICT s1, const char *_RESTRICT s2)
Definition strcpy.c:3
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
char * entries[HISTORY_MAX_ENTRY]
Definition history.h:10
uint64_t index
Definition history.h:12
uint64_t count
Definition history.h:11