stream.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Generic streams.
  19. */
  20. #ifndef KERN_STREAM_H
  21. #define KERN_STREAM_H 1
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <kern/init.h>
  25. #include <kern/macros.h>
  26. struct stream;
  27. struct stream_ops
  28. {
  29. void (*write) (struct stream *, const void *, uint32_t);
  30. int32_t (*read) (struct stream *, void *, uint32_t);
  31. void (*lock) (struct stream *);
  32. void (*unlock) (struct stream *);
  33. };
  34. struct stream
  35. {
  36. const struct stream_ops *ops;
  37. };
  38. struct string_stream
  39. {
  40. struct stream base;
  41. char *ptr;
  42. size_t size;
  43. size_t cur;
  44. };
  45. static inline void
  46. stream_init (struct stream *strm, const struct stream_ops *ops)
  47. {
  48. strm->ops = ops;
  49. }
  50. // Lock/unlock a stream.
  51. static inline void
  52. stream_lock (struct stream *strm)
  53. {
  54. if (strm->ops->lock)
  55. strm->ops->lock (strm);
  56. }
  57. static inline void
  58. stream_unlock (struct stream *strm)
  59. {
  60. if (strm->ops->unlock)
  61. strm->ops->unlock (strm);
  62. }
  63. // Read/Write to a stream.
  64. static inline void
  65. stream_write_unlocked (struct stream *strm, const void *data, uint32_t bytes)
  66. {
  67. _Auto write_fn = strm->ops->write;
  68. if (write_fn)
  69. write_fn (strm, data, bytes);
  70. }
  71. static inline int32_t
  72. stream_read_unlocked (struct stream *strm, void *out, uint32_t bytes)
  73. {
  74. _Auto read_fn = strm->ops->read;
  75. return (read_fn ? read_fn (strm, out, bytes) : -1);
  76. }
  77. static inline void
  78. stream_write (struct stream *strm, const void *data, uint32_t bytes)
  79. {
  80. stream_lock (strm);
  81. stream_write_unlocked (strm, data, bytes);
  82. stream_unlock (strm);
  83. }
  84. static inline int32_t
  85. stream_read (struct stream *strm, void *out, uint32_t bytes)
  86. {
  87. stream_lock (strm);
  88. int32_t ret = stream_read_unlocked (strm, out, bytes);
  89. stream_unlock (strm);
  90. return (ret);
  91. }
  92. static inline void
  93. stream_putc (struct stream *strm, int ch)
  94. {
  95. char c = (char) ch;
  96. stream_write (strm, &c, 1);
  97. }
  98. static inline int
  99. stream_getc (struct stream *strm)
  100. {
  101. char c;
  102. return (stream_read (strm, &c, 1) == 1 ? (int)c : -1);
  103. }
  104. static inline void
  105. stream_puts (struct stream *strm, const char *s)
  106. {
  107. return (stream_write (strm, s, strlen (s)));
  108. }
  109. // String streams.
  110. void string_stream_init (struct string_stream *strm, char *ptr, size_t size);
  111. // Standard console stream.
  112. extern struct stream* console_stream;
  113. // Init operation for streams. Sets up the standard streams.
  114. INIT_OP_DECLARE (stream_setup);
  115. #endif