hist.c 3.9 KB

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