You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.4 KiB

  1. /* Copyright 2021 Nick Brassel (@tzarc)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "qp_internal.h"
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. // Stream API
  24. typedef struct qp_stream_t qp_stream_t;
  25. #define qp_stream_get(stream_ptr) (((qp_stream_t *)(stream_ptr))->get((qp_stream_t *)(stream_ptr)))
  26. #define qp_stream_put(stream_ptr, c) (((qp_stream_t *)(stream_ptr))->put((qp_stream_t *)(stream_ptr), (c)))
  27. #define qp_stream_seek(stream_ptr, offset, origin) (((qp_stream_t *)(stream_ptr))->seek((qp_stream_t *)(stream_ptr), (offset), (origin)))
  28. #define qp_stream_tell(stream_ptr) (((qp_stream_t *)(stream_ptr))->tell((qp_stream_t *)(stream_ptr)))
  29. #define qp_stream_eof(stream_ptr) (((qp_stream_t *)(stream_ptr))->is_eof((qp_stream_t *)(stream_ptr)))
  30. #define qp_stream_setpos(stream_ptr, offset) qp_stream_seek((stream_ptr), (offset), SEEK_SET)
  31. #define qp_stream_getpos(stream_ptr) qp_stream_tell((stream_ptr))
  32. #define qp_stream_read(output_buf, member_size, num_members, stream_ptr) qp_stream_read_impl((output_buf), (member_size), (num_members), (qp_stream_t *)(stream_ptr))
  33. #define qp_stream_write(input_buf, member_size, num_members, stream_ptr) qp_stream_write_impl((input_buf), (member_size), (num_members), (qp_stream_t *)(stream_ptr))
  34. uint32_t qp_stream_read_impl(void *output_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream);
  35. uint32_t qp_stream_write_impl(const void *input_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream);
  36. #define STREAM_EOF ((int16_t)(-1))
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. // Stream definition
  39. struct qp_stream_t {
  40. int16_t (*get)(qp_stream_t *stream);
  41. bool (*put)(qp_stream_t *stream, uint8_t c);
  42. int (*seek)(qp_stream_t *stream, int32_t offset, int origin);
  43. int32_t (*tell)(qp_stream_t *stream);
  44. bool (*is_eof)(qp_stream_t *stream);
  45. };
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. // Memory streams
  48. typedef struct qp_memory_stream_t {
  49. qp_stream_t base;
  50. uint8_t * buffer;
  51. int32_t length;
  52. int32_t position;
  53. bool is_eof;
  54. } qp_memory_stream_t;
  55. qp_memory_stream_t qp_make_memory_stream(void *buffer, int32_t length);
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // FILE streams
  58. #ifdef QP_STREAM_HAS_FILE_IO
  59. typedef struct qp_file_stream_t {
  60. qp_stream_t base;
  61. FILE * file;
  62. } qp_file_stream_t;
  63. qp_file_stream_t qo_make_file_stream(FILE *f);
  64. #endif // QP_STREAM_HAS_FILE_IO