1
0

hist.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /** @file hist.c
  2. */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <faux/faux.h>
  9. #include <faux/str.h>
  10. #include <faux/list.h>
  11. #include <faux/file.h>
  12. #include "tinyrl/hist.h"
  13. struct hist_s {
  14. faux_list_t *list;
  15. faux_list_node_t *pos;
  16. size_t stifle;
  17. char *fname;
  18. bool_t temp;
  19. };
  20. static int hist_compare(const void *first, const void *second)
  21. {
  22. const char *f = (const char *)first;
  23. const char *s = (const char *)second;
  24. return strcmp(f, s);
  25. }
  26. static int hist_kcompare(const void *key, const void *list_item)
  27. {
  28. const char *f = (const char *)key;
  29. const char *s = (const char *)list_item;
  30. return strcmp(f, s);
  31. }
  32. hist_t *hist_new(const char *hist_fname, size_t stifle)
  33. {
  34. hist_t *hist = faux_zmalloc(sizeof(hist_t));
  35. if (!hist)
  36. return NULL;
  37. // Init
  38. hist->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  39. hist_compare, hist_kcompare, (void (*)(void *))faux_str_free);
  40. hist->pos = NULL; // It means position is reset
  41. hist->stifle = stifle;
  42. if (hist_fname)
  43. hist->fname = faux_str_dup(hist_fname);
  44. hist->temp = BOOL_FALSE;
  45. return hist;
  46. }
  47. void hist_free(hist_t *hist)
  48. {
  49. if (!hist)
  50. return;
  51. faux_str_free(hist->fname);
  52. faux_list_free(hist->list);
  53. faux_free(hist);
  54. }
  55. void hist_pos_reset(hist_t *hist)
  56. {
  57. if (!hist)
  58. return;
  59. // History contain temp entry
  60. if (hist->temp) {
  61. faux_list_del(hist->list, faux_list_tail(hist->list));
  62. hist->temp = BOOL_FALSE;
  63. }
  64. hist->pos = NULL;
  65. }
  66. const char *hist_pos(hist_t *hist)
  67. {
  68. if (!hist)
  69. return NULL;
  70. if (!hist->pos)
  71. return NULL;
  72. return (const char *)faux_list_data(hist->pos);
  73. }
  74. const char *hist_pos_up(hist_t *hist)
  75. {
  76. if (!hist)
  77. return NULL;
  78. if (!hist->pos) {
  79. hist->pos = faux_list_tail(hist->list);
  80. } else {
  81. faux_list_node_t *new_pos = faux_list_prev_node(hist->pos);
  82. if (new_pos) // Don't go up over the list
  83. hist->pos = new_pos;
  84. }
  85. if (!hist->pos)
  86. return NULL;
  87. return (const char *)faux_list_data(hist->pos);
  88. }
  89. const char *hist_pos_down(hist_t *hist)
  90. {
  91. if (!hist)
  92. return NULL;
  93. if (!hist->pos)
  94. return NULL;
  95. hist->pos = faux_list_next_node(hist->pos);
  96. if (!hist->pos)
  97. return NULL;
  98. return (const char *)faux_list_data(hist->pos);
  99. }
  100. void hist_add(hist_t *hist, const char *line, bool_t temp)
  101. {
  102. if (!hist)
  103. return;
  104. hist_pos_reset(hist);
  105. if (temp) {
  106. hist->temp = BOOL_TRUE;
  107. } else {
  108. // Try to find the same string within history
  109. faux_list_kdel(hist->list, line);
  110. }
  111. // Add line to the end of list.
  112. // Use (void *)line to make compiler happy about 'const' modifier.
  113. faux_list_add(hist->list, (void *)faux_str_dup(line));
  114. // Stifle list. Note we add only one element so list length can be
  115. // (stifle + 1) but not greater so remove only one element from list.
  116. // If stifle = 0 then don't stifle at all (special case).
  117. if ((hist->stifle != 0) && (faux_list_len(hist->list) > hist->stifle))
  118. faux_list_del(hist->list, faux_list_head(hist->list));
  119. }
  120. void hist_clear(hist_t *hist)
  121. {
  122. if (!hist)
  123. return;
  124. faux_list_del_all(hist->list);
  125. hist_pos_reset(hist);
  126. }
  127. int hist_save(const hist_t *hist)
  128. {
  129. faux_file_t *f = NULL;
  130. faux_list_node_t *node = NULL;
  131. const char *line = NULL;
  132. if (!hist)
  133. return -1;
  134. if (!hist->fname)
  135. return 0;
  136. f = faux_file_open(hist->fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
  137. if (!f)
  138. return -1;
  139. node = faux_list_head(hist->list);
  140. while ((line = (const char *)faux_list_each(&node))) {
  141. faux_file_write(f, line, strlen(line));
  142. faux_file_write(f, "\n", 1);
  143. }
  144. faux_file_close(f);
  145. return 0;
  146. }
  147. int hist_restore(hist_t *hist)
  148. {
  149. faux_file_t *f = NULL;
  150. char *line = NULL;
  151. size_t count = 0;
  152. if (!hist)
  153. return -1;
  154. if (!hist->fname)
  155. return 0;
  156. // Remove old entries from list
  157. hist_clear(hist);
  158. f = faux_file_open(hist->fname, O_RDONLY, 0);
  159. if (!f)
  160. return -1;
  161. while (((hist->stifle == 0) || (count < hist->stifle)) &&
  162. (line = faux_file_getline(f))) {
  163. faux_list_add(hist->list, line);
  164. count++;
  165. }
  166. faux_file_close(f);
  167. return 0;
  168. }