qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

qp_stream.h (3616B)


      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 
     17 #pragma once
     18 
     19 #include <stdbool.h>
     20 #include <stdint.h>
     21 #include <stdlib.h>
     22 #include <stdio.h>
     23 
     24 #include "qp_internal.h"
     25 
     26 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     27 // Stream API
     28 
     29 typedef struct qp_stream_t qp_stream_t;
     30 
     31 #define qp_stream_get(stream_ptr) (((qp_stream_t *)(stream_ptr))->get((qp_stream_t *)(stream_ptr)))
     32 #define qp_stream_put(stream_ptr, c) (((qp_stream_t *)(stream_ptr))->put((qp_stream_t *)(stream_ptr), (c)))
     33 #define qp_stream_seek(stream_ptr, offset, origin) (((qp_stream_t *)(stream_ptr))->seek((qp_stream_t *)(stream_ptr), (offset), (origin)))
     34 #define qp_stream_tell(stream_ptr) (((qp_stream_t *)(stream_ptr))->tell((qp_stream_t *)(stream_ptr)))
     35 #define qp_stream_eof(stream_ptr) (((qp_stream_t *)(stream_ptr))->is_eof((qp_stream_t *)(stream_ptr)))
     36 #define qp_stream_setpos(stream_ptr, offset) qp_stream_seek((stream_ptr), (offset), SEEK_SET)
     37 #define qp_stream_getpos(stream_ptr) qp_stream_tell((stream_ptr))
     38 #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))
     39 #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))
     40 
     41 uint32_t qp_stream_read_impl(void *output_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream);
     42 uint32_t qp_stream_write_impl(const void *input_buf, uint32_t member_size, uint32_t num_members, qp_stream_t *stream);
     43 
     44 #define qp_stream_close(stream_ptr) (((qp_stream_t *)(stream_ptr))->close((qp_stream_t *)(stream_ptr)))
     45 
     46 #define STREAM_EOF ((int16_t)(-1))
     47 
     48 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     49 // Stream definition
     50 
     51 typedef struct qp_stream_t {
     52     int16_t (*get)(qp_stream_t *stream);
     53     bool (*put)(qp_stream_t *stream, uint8_t c);
     54     int (*seek)(qp_stream_t *stream, int32_t offset, int origin);
     55     int32_t (*tell)(qp_stream_t *stream);
     56     bool (*is_eof)(qp_stream_t *stream);
     57     void (*close)(qp_stream_t *stream);
     58 } qp_stream_t;
     59 
     60 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     61 // Memory streams
     62 
     63 typedef struct qp_memory_stream_t {
     64     qp_stream_t base;
     65     uint8_t    *buffer;
     66     int32_t     length;
     67     int32_t     position;
     68     bool        is_eof;
     69 } qp_memory_stream_t;
     70 
     71 qp_memory_stream_t qp_make_memory_stream(void *buffer, int32_t length);
     72 
     73 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     74 // FILE streams
     75 
     76 #ifdef QP_STREAM_HAS_FILE_IO
     77 
     78 typedef struct qp_file_stream_t {
     79     qp_stream_t base;
     80     FILE       *file;
     81 } qp_file_stream_t;
     82 
     83 qp_file_stream_t qp_make_file_stream(FILE *f);
     84 
     85 #endif // QP_STREAM_HAS_FILE_IO