123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- /** @file hist.c
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
- #include <errno.h>
- #include <faux/faux.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <faux/file.h>
- #include "tinyrl/hist.h"
- struct hist_s {
- faux_list_t *list;
- faux_list_node_t *pos;
- size_t stifle;
- char *fname;
- };
- static int hist_compare(const void *first, const void *second)
- {
- const char *f = (const char *)first;
- const char *s = (const char *)second;
- return strcmp(f, s);
- }
- static int hist_kcompare(const void *key, const void *list_item)
- {
- const char *f = (const char *)key;
- const char *s = (const char *)list_item;
- return strcmp(f, s);
- }
- hist_t *hist_new(size_t stifle, const char *hist_fname)
- {
- hist_t *hist = faux_zmalloc(sizeof(hist_t));
- if (!hist)
- return NULL;
- // Init
- hist->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
- hist_compare, hist_kcompare, (void (*)(void *))faux_str_free);
- hist->pos = NULL; // It means position is reset
- hist->stifle = stifle;
- if (hist_fname)
- hist->fname = faux_str_dup(hist_fname);
- return hist;
- }
- void hist_free(hist_t *hist)
- {
- if (!hist)
- return;
- faux_str_free(hist->fname);
- faux_list_free(hist->list);
- faux_free(hist);
- }
- void hist_pos_reset(hist_t *hist)
- {
- if (!hist)
- return;
- hist->pos = NULL;
- }
- const char *hist_pos(hist_t *hist)
- {
- if (!hist)
- return NULL;
- if (!hist->pos)
- return NULL;
- return (const char *)faux_list_data(hist->pos);
- }
- const char *hist_pos_up(hist_t *hist)
- {
- if (!hist)
- return NULL;
- if (!hist->pos)
- hist->pos = faux_list_tail(hist->list);
- else
- hist->pos = faux_list_prev_node(hist->pos);
- if (!hist->pos)
- return NULL;
- return (const char *)faux_list_data(hist->pos);
- }
- const char *hist_pos_down(hist_t *hist)
- {
- if (!hist)
- return NULL;
- if (!hist->pos)
- return NULL;
- hist->pos = faux_list_next_node(hist->pos);
- if (!hist->pos)
- return NULL;
- return (const char *)faux_list_data(hist->pos);
- }
- void hist_add(hist_t *hist, const char *line)
- {
- if (!hist)
- return;
- hist_pos_reset(hist);
- // Try to find the same string within history
- faux_list_kdel(hist->list, line);
- // Add line to the end of list.
- // Use (void *)line to make compiler happy about 'const' modifier.
- faux_list_add(hist->list, (void *)line);
- // Stifle list. Note we add only one element so list length can be
- // (stifle + 1) but not greater so remove only one element from list.
- // If stifle = 0 then don't stifle at all (special case).
- if ((hist->stifle != 0) && (faux_list_len(hist->list) > hist->stifle))
- faux_list_del(hist->list, faux_list_head(hist->list));
- }
- void hist_clear(hist_t *hist)
- {
- if (!hist)
- return;
- faux_list_del_all(hist->list);
- hist_pos_reset(hist);
- }
- int hist_save(const hist_t *hist)
- {
- faux_file_t *f = NULL;
- faux_list_node_t *node = NULL;
- const char *line = NULL;
- if (!hist)
- return -1;
- if (!hist->fname)
- return 0;
- f = faux_file_open(hist->fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
- if (!f)
- return -1;
- node = faux_list_head(hist->list);
- while ((line = (const char *)faux_list_each(&node))) {
- faux_file_write(f, line, strlen(line));
- faux_file_write(f, "\n", 1);
- }
- faux_file_close(f);
- return 0;
- }
- int hist_restore(hist_t *hist)
- {
- faux_file_t *f = NULL;
- char *line = NULL;
- size_t count = 0;
- if (!hist)
- return -1;
- if (!hist->fname)
- return 0;
- // Remove old entries from list
- hist_clear(hist);
- f = faux_file_open(hist->fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
- if (!f)
- return -1;
- while (((hist->stifle == 0) || (count < hist->stifle)) &&
- (line = faux_file_getline(f))) {
- faux_list_add(hist->list, line);
- count++;
- }
- faux_file_close(f);
- return 0;
- }
|