__buffered_read.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if !__MESC__
  24. #define __READ_BUFFER_MAX 128
  25. int __read_buffer_max;
  26. #else /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */
  27. #define __READ_BUFFER_MAX 100
  28. #define __read_buffer_max 100
  29. #endif
  30. struct __read_buffer
  31. {
  32. ssize_t size;
  33. char string[__READ_BUFFER_MAX];
  34. };
  35. struct __read_buffer *__read_cache = 0;
  36. void
  37. __buffered_read_init (int filedes)
  38. {
  39. if (!__read_cache)
  40. {
  41. __read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX);
  42. #if !__MESC__
  43. __read_buffer_max = __READ_BUFFER_MAX;
  44. char *p = getenv ("MES_READ_BUFFER");
  45. if (p)
  46. {
  47. __read_buffer_max = atoi (p);
  48. if (__read_buffer_max < 0)
  49. __read_buffer_max = 0;
  50. if (__read_buffer_max > __READ_BUFFER_MAX)
  51. __read_buffer_max = __READ_BUFFER_MAX;
  52. }
  53. #endif
  54. }
  55. }
  56. size_t
  57. __buffered_read_clear (int filedes)
  58. {
  59. __buffered_read_init (filedes);
  60. size_t size = __read_cache[filedes].size;
  61. __read_cache[filedes].size = 0;
  62. return size;
  63. }
  64. ssize_t
  65. __buffered_read (int filedes, void *buffer, size_t size)
  66. {
  67. size_t todo = size;
  68. __buffered_read_init (filedes);
  69. struct __read_buffer *cache = &__read_cache[filedes];
  70. char *p = buffer;
  71. if (!cache->size && size > __read_buffer_max)
  72. return _read (filedes, buffer, size);
  73. while (cache->size > 0 && todo)
  74. {
  75. todo--;
  76. *p++ = cache->string[__read_buffer_max - cache->size--];
  77. }
  78. if (todo)
  79. {
  80. #if !__MESC__
  81. if (todo > __read_buffer_max)
  82. return size - todo + _read (filedes, p, todo);
  83. if (__mes_debug () > 4)
  84. {
  85. eputs ("__buffered_read: ");
  86. eputs (itoa (__read_buffer_max));
  87. eputs ("\n");
  88. }
  89. #endif
  90. ssize_t bytes = _read (filedes, cache->string, __read_buffer_max);
  91. if (bytes < 0)
  92. return -1;
  93. if (bytes)
  94. {
  95. cache->size = bytes;
  96. if (bytes < __read_buffer_max)
  97. memmove (cache->string + __read_buffer_max - bytes, cache->string, bytes);
  98. return size - todo + __buffered_read (filedes, p, todo);
  99. }
  100. }
  101. return size - todo;
  102. }