hist.c 3.6 KB

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