hist.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. hist_t *hist_new(size_t stifle)
  18. {
  19. hist_t *hist = faux_zmalloc(sizeof(hist_t));
  20. if (!hist)
  21. return NULL;
  22. // Init
  23. hist->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  24. NULL, NULL, (void (*)(void *))faux_str_free);
  25. hist->pos = NULL; // It means position is reset
  26. hist->stifle = stifle;
  27. return hist;
  28. }
  29. void hist_free(hist_t *hist)
  30. {
  31. if (!hist)
  32. return;
  33. faux_list_free(hist->list);
  34. faux_free(hist);
  35. }
  36. void hist_pos_reset(hist_t *hist)
  37. {
  38. if (!hist)
  39. return;
  40. hist->pos = NULL;
  41. }
  42. const char *hist_pos_up(hist_t *hist)
  43. {
  44. if (!hist)
  45. return NULL;
  46. if (!hist->pos)
  47. hist->pos = faux_list_tail(hist->list);
  48. else
  49. hist->pos = faux_list_prev_node(hist->pos);
  50. if (!hist->pos)
  51. return NULL;
  52. return (const char *)faux_list_data(hist->pos);
  53. }
  54. const char *hist_pos_down(hist_t *hist)
  55. {
  56. if (!hist)
  57. return NULL;
  58. if (!hist->pos)
  59. return NULL;
  60. hist->pos = faux_list_next_node(hist->pos);
  61. if (!hist->pos)
  62. return NULL;
  63. return (const char *)faux_list_data(hist->pos);
  64. }
  65. void hist_add(hist_t *hist, const char *line)
  66. {
  67. }
  68. void hist_clear(hist_t *hist)
  69. {
  70. if (!hist)
  71. return;
  72. hist_pos_reset(hist);
  73. faux_list_del_all(hist->list);
  74. }
  75. /*
  76. int hist_save(const hist_t *hist, const char *fname)
  77. {
  78. hist_entry_t *entry;
  79. hist_iterator_t iter;
  80. FILE *f;
  81. if (!fname) {
  82. errno = EINVAL;
  83. return -1;
  84. }
  85. if (!(f = fopen(fname, "w")))
  86. return -1;
  87. for (entry = hist_getfirst(hist, &iter);
  88. entry; entry = hist_getnext(&iter)) {
  89. if (fprintf(f, "%s\n", hist_entry__get_line(entry)) < 0)
  90. return -1;
  91. }
  92. fclose(f);
  93. return 0;
  94. }
  95. int hist_restore(hist_t *hist, const char *fname)
  96. {
  97. FILE *f;
  98. char *p;
  99. int part_len = 300;
  100. char *buf;
  101. int buf_len = part_len;
  102. int res = 0;
  103. if (!fname) {
  104. errno = EINVAL;
  105. return -1;
  106. }
  107. if (!(f = fopen(fname, "r")))
  108. return 0; // Can't find history file
  109. buf = malloc(buf_len);
  110. p = buf;
  111. while (fgets(p, buf_len - (p - buf), f)) {
  112. char *ptmp = NULL;
  113. char *el = strchr(buf, '\n');
  114. if (el) { // The whole line was readed
  115. *el = '\0';
  116. hist_add(hist, buf);
  117. p = buf;
  118. continue;
  119. }
  120. buf_len += part_len;
  121. ptmp = realloc(buf, buf_len);
  122. if (!ptmp) {
  123. res = -1;
  124. goto end;
  125. }
  126. buf = ptmp;
  127. p = buf + buf_len - part_len - 1;
  128. }
  129. end:
  130. free(buf);
  131. fclose(f);
  132. return res;
  133. }
  134. */