hist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "tinyrl/hist.h"
  12. struct hist_s {
  13. faux_list_t *list;
  14. faux_list_node_t *pos;
  15. size_t stifle;
  16. };
  17. static int hist_compare(const void *first, const void *second)
  18. {
  19. const char *f = (const char *)first;
  20. const char *s = (const char *)second;
  21. return strcmp(f, s);
  22. }
  23. static int hist_kcompare(const void *key, const void *list_item)
  24. {
  25. const char *f = (const char *)key;
  26. const char *s = (const char *)list_item;
  27. return strcmp(f, s);
  28. }
  29. hist_t *hist_new(size_t stifle)
  30. {
  31. hist_t *hist = faux_zmalloc(sizeof(hist_t));
  32. if (!hist)
  33. return NULL;
  34. // Init
  35. hist->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  36. hist_compare, hist_kcompare, (void (*)(void *))faux_str_free);
  37. hist->pos = NULL; // It means position is reset
  38. hist->stifle = stifle;
  39. return hist;
  40. }
  41. void hist_free(hist_t *hist)
  42. {
  43. if (!hist)
  44. return;
  45. faux_list_free(hist->list);
  46. faux_free(hist);
  47. }
  48. void hist_pos_reset(hist_t *hist)
  49. {
  50. if (!hist)
  51. return;
  52. hist->pos = NULL;
  53. }
  54. const char *hist_pos(hist_t *hist)
  55. {
  56. if (!hist)
  57. return NULL;
  58. if (!hist->pos)
  59. return NULL;
  60. return (const char *)faux_list_data(hist->pos);
  61. }
  62. const char *hist_pos_up(hist_t *hist)
  63. {
  64. if (!hist)
  65. return NULL;
  66. if (!hist->pos)
  67. hist->pos = faux_list_tail(hist->list);
  68. else
  69. hist->pos = faux_list_prev_node(hist->pos);
  70. if (!hist->pos)
  71. return NULL;
  72. return (const char *)faux_list_data(hist->pos);
  73. }
  74. const char *hist_pos_down(hist_t *hist)
  75. {
  76. if (!hist)
  77. return NULL;
  78. if (!hist->pos)
  79. return NULL;
  80. hist->pos = faux_list_next_node(hist->pos);
  81. if (!hist->pos)
  82. return NULL;
  83. return (const char *)faux_list_data(hist->pos);
  84. }
  85. void hist_add(hist_t *hist, const char *line)
  86. {
  87. if (!hist)
  88. return;
  89. hist_pos_reset(hist);
  90. // Try to find the same string within history
  91. faux_list_kdel(hist->list, line);
  92. // Add line to the end of list.
  93. // Use (void *)line to make compiler happy about 'const' modifier.
  94. faux_list_add(hist->list, (void *)line);
  95. // Stifle list. Note we add only one element so list length can be
  96. // (stifle + 1) but not greater so remove only one element from list.
  97. // If stifle = 0 then don't stifle at all (special case).
  98. if ((hist->stifle != 0) && (faux_list_len(hist->list) > hist->stifle))
  99. faux_list_del(hist->list, faux_list_head(hist->list));
  100. }
  101. void hist_clear(hist_t *hist)
  102. {
  103. if (!hist)
  104. return;
  105. faux_list_del_all(hist->list);
  106. hist_pos_reset(hist);
  107. }
  108. /*
  109. int hist_save(const hist_t *hist, const char *fname)
  110. {
  111. hist_entry_t *entry;
  112. hist_iterator_t iter;
  113. FILE *f;
  114. if (!fname) {
  115. errno = EINVAL;
  116. return -1;
  117. }
  118. if (!(f = fopen(fname, "w")))
  119. return -1;
  120. for (entry = hist_getfirst(hist, &iter);
  121. entry; entry = hist_getnext(&iter)) {
  122. if (fprintf(f, "%s\n", hist_entry__get_line(entry)) < 0)
  123. return -1;
  124. }
  125. fclose(f);
  126. return 0;
  127. }
  128. int hist_restore(hist_t *hist, const char *fname)
  129. {
  130. FILE *f;
  131. char *p;
  132. int part_len = 300;
  133. char *buf;
  134. int buf_len = part_len;
  135. int res = 0;
  136. if (!fname) {
  137. errno = EINVAL;
  138. return -1;
  139. }
  140. if (!(f = fopen(fname, "r")))
  141. return 0; // Can't find history file
  142. buf = malloc(buf_len);
  143. p = buf;
  144. while (fgets(p, buf_len - (p - buf), f)) {
  145. char *ptmp = NULL;
  146. char *el = strchr(buf, '\n');
  147. if (el) { // The whole line was readed
  148. *el = '\0';
  149. hist_add(hist, buf);
  150. p = buf;
  151. continue;
  152. }
  153. buf_len += part_len;
  154. ptmp = realloc(buf, buf_len);
  155. if (!ptmp) {
  156. res = -1;
  157. goto end;
  158. }
  159. buf = ptmp;
  160. p = buf + buf_len - part_len - 1;
  161. }
  162. end:
  163. free(buf);
  164. fclose(f);
  165. return res;
  166. }
  167. */