hist.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_UNIQUE,
  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_up(hist_t *hist)
  55. {
  56. if (!hist)
  57. return NULL;
  58. if (!hist->pos)
  59. hist->pos = faux_list_tail(hist->list);
  60. else
  61. hist->pos = faux_list_prev_node(hist->pos);
  62. if (!hist->pos)
  63. return NULL;
  64. return (const char *)faux_list_data(hist->pos);
  65. }
  66. const char *hist_pos_down(hist_t *hist)
  67. {
  68. if (!hist)
  69. return NULL;
  70. if (!hist->pos)
  71. return NULL;
  72. hist->pos = faux_list_next_node(hist->pos);
  73. if (!hist->pos)
  74. return NULL;
  75. return (const char *)faux_list_data(hist->pos);
  76. }
  77. void hist_add(hist_t *hist, const char *line)
  78. {
  79. if (!hist)
  80. return;
  81. hist_pos_reset(hist);
  82. // Try to find the same string within history
  83. faux_list_kdel(hist->list, line);
  84. // Add line to the end of list.
  85. // Use (void *)line to make compiler happy about 'const' modifier.
  86. faux_list_add(hist->list, (void *)line);
  87. // Stifle list. Note we add only one element so list length can be
  88. // (stifle + 1) but not greater so remove only one element from list.
  89. // If stifle = 0 then don't stifle at all (special case).
  90. if ((hist->stifle != 0) && (faux_list_len(hist->list) > hist->stifle))
  91. faux_list_del(hist->list, faux_list_head(hist->list));
  92. }
  93. void hist_clear(hist_t *hist)
  94. {
  95. if (!hist)
  96. return;
  97. hist_pos_reset(hist);
  98. faux_list_del_all(hist->list);
  99. }
  100. /*
  101. int hist_save(const hist_t *hist, const char *fname)
  102. {
  103. hist_entry_t *entry;
  104. hist_iterator_t iter;
  105. FILE *f;
  106. if (!fname) {
  107. errno = EINVAL;
  108. return -1;
  109. }
  110. if (!(f = fopen(fname, "w")))
  111. return -1;
  112. for (entry = hist_getfirst(hist, &iter);
  113. entry; entry = hist_getnext(&iter)) {
  114. if (fprintf(f, "%s\n", hist_entry__get_line(entry)) < 0)
  115. return -1;
  116. }
  117. fclose(f);
  118. return 0;
  119. }
  120. int hist_restore(hist_t *hist, const char *fname)
  121. {
  122. FILE *f;
  123. char *p;
  124. int part_len = 300;
  125. char *buf;
  126. int buf_len = part_len;
  127. int res = 0;
  128. if (!fname) {
  129. errno = EINVAL;
  130. return -1;
  131. }
  132. if (!(f = fopen(fname, "r")))
  133. return 0; // Can't find history file
  134. buf = malloc(buf_len);
  135. p = buf;
  136. while (fgets(p, buf_len - (p - buf), f)) {
  137. char *ptmp = NULL;
  138. char *el = strchr(buf, '\n');
  139. if (el) { // The whole line was readed
  140. *el = '\0';
  141. hist_add(hist, buf);
  142. p = buf;
  143. continue;
  144. }
  145. buf_len += part_len;
  146. ptmp = realloc(buf, buf_len);
  147. if (!ptmp) {
  148. res = -1;
  149. goto end;
  150. }
  151. buf = ptmp;
  152. p = buf + buf_len - part_len - 1;
  153. }
  154. end:
  155. free(buf);
  156. fclose(f);
  157. return res;
  158. }
  159. */