1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#ifndef BUFFER_H
#define BUFFER_H
#include <stdint.h>
#include <stdio.h>
/********************************************************
Audio Tools, a module and set of tools for manipulating audio data
Copyright (C) 2007-2015 Brian Langenberger
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************/
/*bs_buffer can be thought of as a FIFO queue of byte data
buf_putc and other data writers append to "data"
starting from "window_end",
increasing the size of "data" and "data_size" as necessary to fit
buf_getc and other data readers pull from the beginning of "data"
starting from "window_start" to "window_end"
"rewindable" indicates whether "window_start" can go backwards
to point at previously read data
if false, data writers may slide the window down and reuse the buffer
if true, data writers may only append new data to the buffer*/
struct bs_buffer {
uint8_t* data;
unsigned data_size;
unsigned window_start;
unsigned window_end;
};
typedef unsigned buf_size_t;
typedef unsigned buf_pos_t;
/*returns a new bs_buffer struct which can be appended to and read from
it must be closed with buf_close() when no longer needed*/
struct bs_buffer*
buf_new(void);
/*deallocates buffer struct*/
void
buf_close(struct bs_buffer *stream);
/*returns the amount of data in the buffer in bytes*/
static inline unsigned
buf_window_size(const struct bs_buffer *stream)
{
return stream->window_end - stream->window_start;
}
/*returns the amount of data that can be added to the buffer
without resizing, in bytes*/
static inline unsigned
buf_unused_size(const struct bs_buffer *stream)
{
return stream->data_size - stream->window_end;
}
/*returns the starting position of the buffer window
pointing to the first byte that may be read*/
static inline uint8_t*
buf_window_start(const struct bs_buffer *stream)
{
return stream->data + stream->window_start;
}
/*returns the ending position of the buffer window
pointing to the first byte that may be written*/
static inline uint8_t*
buf_window_end(const struct bs_buffer *stream)
{
return stream->data + stream->window_end;
}
/*resize buffer to fit at least "additional_bytes", if necessary
this may alter where window_start and window_end point to*/
void
buf_resize(struct bs_buffer *stream, unsigned additional_bytes);
/*clears out the buffer for possible reuse*/
static inline void
buf_reset(struct bs_buffer *stream)
{
stream->window_start = stream->window_end = 0;
}
/*** stdio-like functions for bs_buffer ***/
/*analagous to fgetc
returns byte at beginning of buffer
returns EOF if no bytes remain in buffer*/
static inline int
buf_getc(struct bs_buffer *stream)
{
if (stream->window_start < stream->window_end)
return stream->data[stream->window_start++];
else
return EOF;
}
/*analagous to fputc
places byte "i" at end of buffer*/
static inline int
buf_putc(int i, struct bs_buffer *stream)
{
if (stream->window_end == stream->data_size) {
buf_resize(stream, 1);
}
stream->data[stream->window_end++] = (uint8_t)i;
return i;
}
/*analagous to fread
reads "data_size" bytes from "stream" to "data"
starting at the beginning of stream
returns the amount of bytes actually read
(which may be less than the amount requested)*/
unsigned
buf_read(struct bs_buffer *stream, uint8_t *data, unsigned data_size);
/*analagous to buf_read except that data is ignored rather than returned
returns the amount of bytes actually skipped*/
unsigned
buf_skip(struct bs_buffer *stream, unsigned data_size);
/*analgous to fwrite
appends "data_size" bytes from "data" to stream starting at "window_end"*/
void
buf_write(struct bs_buffer *stream, const uint8_t *data, unsigned data_size);
/*appends unconsumed data in "source" to "target"*/
static inline void
buf_extend(const struct bs_buffer *source, struct bs_buffer* target)
{
buf_write(target, buf_window_start(source), buf_window_size(source));
}
#endif
|