Dynamic buffer with API like standard C files. Support standard C89 (ANSI C).
#include <stdio.h>
#include "iobuffer.h"
int main(void) {
BUFFER* bd; int ch;
bd = bopen("Hello, ", 7, "a+");
if (!bd) return 1;
bputs("Alex", bd);
brewind(bd);
while ((ch = bgetc(bd)) != EOB)
putchar(ch);
putchar('\n');
bclose(bd);
return 0;
}
- Macro constants
- Types
- Buffer access
- Operations on buffer
- Buffer positioning
- Direct input/output
- Unformatted input/output
- Formatted
input/output - Error handling
- View extension
Integer constant expression of type int
and negative value.
Argument to bseek
indicating seeking from beginning of the buffer.
Argument to bseek
indicating seeking from the current buffer position.
Argument to bseek
indicating seeking from end of the buffer.
Object type, capable of holding all information needed to control a buffer.
Non-array complete object type, capable of uniquely specifying a position in buffer.
Opens a buffer from data
/size
and returns a pointer to the buffer. mode
is used to determine the buffer access mode.
Return value: If successful, returns a pointer to the new buffer. On error, returns a null pointer.
Mode string | Meaning | Explanation | Using data |
---|---|---|---|
"r" |
read | open a buffer for reading | copy content from data , read with start |
"w" |
write | create a buffer for writing | ignore, start with empty |
"a" |
append | append to a buffer | copy content from data , start to end |
"r+" |
read extended | open a buffer for read/write | copy content from data , read with start |
"w+" |
write extended | create a buffer for read/write | ignore, start with empty |
"a+" |
append extended | open a buffer for read/write | copy content from data , start to end |
[ EXTENSION ] Open a buffer over data
/size
and returns a pointer to the buffer. mode
is used to determine the buffer access mode.
Using data
as external storage with capacity equal size
. If data
is NULL
, allocate memory with size size
.
Mode strings see bopen
,
no copying content in "r"
, "a"
, "r+"
and "a+"
modes.
Return value: If successful, returns a pointer to the new buffer. On error, returns a null pointer.
Closes the given buffer.
Return value: none
[ EXTENSION ] Erase count
bytes starting from the current position.
Return value: none
[ EXTENSION ] Deleting all data in buffer.
Return value: none
Obtains the buffer position indicator for the buffer
and stores them in the object pointed to by pos
. The value stored is only meaningful as the input to bsetpos
.
Return value: 0
upon success, nonzero value otherwise.
Sets the buffer position indicator for the buffer
according to the value pointed to by pos
.
Return value: 0
upon success, nonzero value otherwise.
Returns the buffer position indicator for the buffer
.
Return value: Buffer position indicator on success or -1L
if failure occurs.
Sets the buffer position indicator for the buffer
to the value pointed to by offset
.
Return value: 0
upon success, nonzero value otherwise.
Moves the buffer position indicator to the beginning of the given buffer.
Return value: none
Reads up to count
objects into the array data
from the given input buffer buffer
and storing the results, in the order obtained, into the successive positions of buffer, which is reinterpreted as an array of unsigned char
. The buffer position indicator is advanced by the number of characters read.
Return value: Number of objects read successfully, which may be less than count
if an error or end-of-buffer condition occurs.
Writes count
of objects from the given array data
to the output buffer buffer
. The objects are written as if by reinterpreting each object as an array of unsigned char
to write those unsigned char
s into buffer, in order. The buffer position indicator is advanced by the number of characters written.
Return value: The number of objects written successfully, which may be less than count
if an error occurs.
Reads the next character from the given input buffer.
Return value: On success, returns the obtained character as an unsigned char
converted to an int
. On failure, returns EOB
.
[ EXTENSION ] Peek the next character from the given input buffer.
Return value: On success, returns the obtained character as an unsigned char
converted to an int
. On failure, returns EOB
.
Reads at most count - 1
characters from the given buffer and stores them in the character array pointed to by str
. Parsing stops if a newline character is found (in which case str
will contain that newline character) or if end-of-buffer occurs. If bytes are read and no errors occur, writes a null character at the position immediately after the last character written to str
.
Return value: str
on success, null pointer on failure.
Writes a byte byte
to the given output buffer buffer
. Internally, the byte is converted to unsigned char
just before being written.
Return value: On success, returns the written character. On failure, returns EOB
.
Writes every character from the null-terminated string string
to the output buffer buffer
, as if by repeatedly executing bputc
. The terminating null character from string
is not written.
Return value: On success, returns a non-negative value. On failure, returns EOB
.
If byte
does not equal EOB
, pushes the byte byte
(reinterpreted as unsigned char
) into the buffer buffer
in such a manner that subsequent read operation from buffer will retrieve that byte.
Return value: On success byte
is returned. On failure EOB
is returned and the given buffer remains unchanged.
Note
Why doesn't exist bscanf
/vbscanf
? Standard scanf
return count success parsed values.
Not exist common way get read it characters after scanf
. I don't want to completely create bscanf
/vbscanf
.
Warning
In bprintf
/vbprintf
use funtion snprintf
. This function "offical" available in C99, but most compilators
provide his in C89.
Loads the data from the given locations, converts them to character string equivalents and writes the results to buffer buffer
.
Return value: The number of characters written if successful or negative value if an error occurred.
Loads the data from the locations, defined by list
, converts them to character string equivalents and writes the results to buffer buffer
.
Return value: The number of characters written if successful or negative value if an error occurred.
Checks if the end of the given buffer has been reached.
Return value: Nonzero value if the end of the buffer has been reached, otherwise 0
.
Complete object type with fields base
, head
, stop
with type const void*
.
Create new view object from buffer buffer
.
Return value: New view object.
Macro-constant string literal of format specifier for BUFVIEW
.
Macro-function for create arguments to printf
's functions. Valid value of from
: base
, head
.
Macro-function get count bytes in view between parameter from
and stop
.