mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-21 17:42:26 -04:00
Main: include/kernaux/io.h: Stores improved
This commit is contained in:
parent
d7820119f0
commit
f1d3d730b2
3 changed files with 82 additions and 31 deletions
|
@ -5,20 +5,28 @@
|
|||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
static const char *const hello = "Hello, World!";
|
||||
|
||||
static char buffer[BUFFER_SIZE];
|
||||
|
||||
void example_main()
|
||||
{
|
||||
struct KernAux_MemStore mem_store =
|
||||
KernAux_MemStore_create(buffer, BUFFER_SIZE);
|
||||
{
|
||||
struct KernAux_MemStore mem_store =
|
||||
KernAux_MemStore_create(buffer, BUFFER_SIZE);
|
||||
|
||||
assert(KernAux_Store_put_char(&mem_store.store, 'H' ) == 'H' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, 'e' ) == 'e' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, 'l' ) == 'l' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, 'l' ) == 'l' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, 'o' ) == 'o' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, '!' ) == '!' );
|
||||
assert(KernAux_Store_put_char(&mem_store.store, '\0') == '\0');
|
||||
assert(KernAux_Store_puts(&mem_store.store, hello));
|
||||
assert(strncmp(buffer, hello, strlen(hello)) == 0);
|
||||
}
|
||||
|
||||
assert(strncmp(buffer, "Hello!", BUFFER_SIZE) == 0);
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
{
|
||||
struct KernAux_MemStore mem_store;
|
||||
KernAux_MemStore_init(&mem_store, buffer, 5);
|
||||
|
||||
assert(!KernAux_Store_puts(&mem_store.store, hello));
|
||||
assert(strncmp(buffer, hello, 5) == 0);
|
||||
assert(strncmp(buffer, hello, strlen(hello)) != 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,24 +5,27 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define KERNAUX_EOF (-1)
|
||||
|
||||
typedef int (*KernAux_Store_GetChar)(void *store);
|
||||
typedef int (*KernAux_Store_PutChar)(void *store, int c);
|
||||
typedef size_t (*KernAux_Store_Read)(void *store, void *buffer, size_t size);
|
||||
typedef size_t (*KernAux_Store_Write)(void *store, const void *buffer, size_t size);
|
||||
|
||||
/*****************
|
||||
* KernAux_Store *
|
||||
*****************/
|
||||
|
||||
typedef struct KernAux_Store {
|
||||
KernAux_Store_GetChar get_char;
|
||||
KernAux_Store_PutChar put_char;
|
||||
KernAux_Store_Read read;
|
||||
KernAux_Store_Write write;
|
||||
} *KernAux_Store;
|
||||
|
||||
int KernAux_Store_put_char(KernAux_Store store, int c);
|
||||
size_t KernAux_Store_write(KernAux_Store store, const void *buffer, size_t size);
|
||||
int KernAux_Store_putc(KernAux_Store store, int c);
|
||||
bool KernAux_Store_puts(KernAux_Store store, const char *s);
|
||||
|
||||
/********************
|
||||
* KernAux_MemStore *
|
||||
|
|
72
src/io.c
72
src/io.c
|
@ -5,27 +5,50 @@
|
|||
#include <kernaux/assert.h>
|
||||
#include <kernaux/io.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/*****************
|
||||
* KernAux_Store *
|
||||
*****************/
|
||||
|
||||
int KernAux_Store_put_char(KernAux_Store store, int c)
|
||||
size_t KernAux_Store_write(
|
||||
const KernAux_Store store,
|
||||
const void *const buffer,
|
||||
const size_t size
|
||||
) {
|
||||
KERNAUX_ASSERT(store);
|
||||
KERNAUX_ASSERT(store->write);
|
||||
|
||||
return store->write(store, buffer, size);
|
||||
}
|
||||
|
||||
int KernAux_Store_putc(const KernAux_Store store, int c)
|
||||
{
|
||||
KERNAUX_ASSERT(store);
|
||||
KERNAUX_ASSERT(store->put_char);
|
||||
|
||||
return store->put_char(store, c);
|
||||
char buffer[1] = { c };
|
||||
const size_t result = KernAux_Store_write(store, buffer, 1);
|
||||
return result == 0 ? KERNAUX_EOF : buffer[0];
|
||||
}
|
||||
|
||||
bool KernAux_Store_puts(const KernAux_Store store, const char *s)
|
||||
{
|
||||
KERNAUX_ASSERT(store);
|
||||
if (!s) return 0;
|
||||
|
||||
const size_t size = strlen(s);
|
||||
return KernAux_Store_write(store, s, size) >= size;
|
||||
}
|
||||
|
||||
/********************
|
||||
* KernAux_MemStore *
|
||||
********************/
|
||||
|
||||
static int KernAux_MemStore_get_char(void *store);
|
||||
static int KernAux_MemStore_put_char(void *store, int c);
|
||||
static size_t KernAux_MemStore_read(void *store, void *buffer, size_t size);
|
||||
static size_t KernAux_MemStore_write(void *store, const void *buffer, size_t size);
|
||||
|
||||
struct KernAux_MemStore
|
||||
KernAux_MemStore_create(void *const start, const size_t size)
|
||||
|
@ -42,8 +65,8 @@ void KernAux_MemStore_init(
|
|||
) {
|
||||
KERNAUX_ASSERT(mem_store);
|
||||
|
||||
mem_store->store.get_char = KernAux_MemStore_get_char;
|
||||
mem_store->store.put_char = KernAux_MemStore_put_char;
|
||||
mem_store->store.write = KernAux_MemStore_write;
|
||||
mem_store->store.read = KernAux_MemStore_read;
|
||||
|
||||
mem_store->start = start;
|
||||
mem_store->size = size;
|
||||
|
@ -54,25 +77,42 @@ void KernAux_MemStore_init(
|
|||
KERNAUX_ASSERT(mem_store->start < mem_store->limit);
|
||||
}
|
||||
|
||||
int KernAux_MemStore_get_char(void *store)
|
||||
{
|
||||
size_t KernAux_MemStore_read(
|
||||
void *const store,
|
||||
void *const buffer,
|
||||
size_t size
|
||||
) {
|
||||
struct KernAux_MemStore *mem_store = store;
|
||||
KERNAUX_ASSERT(mem_store);
|
||||
KERNAUX_ASSERT(mem_store->ptr >= mem_store->start);
|
||||
KERNAUX_ASSERT(buffer);
|
||||
|
||||
if (mem_store->ptr > mem_store->end) return KERNAUX_EOF;
|
||||
uint8_t *buffer_ptr = buffer;
|
||||
|
||||
return *(mem_store->ptr++);
|
||||
while (size && mem_store->ptr <= mem_store->end) {
|
||||
--size;
|
||||
*(buffer_ptr++) = *(mem_store->ptr++);
|
||||
}
|
||||
|
||||
return (uint8_t*)buffer_ptr - (uint8_t*)buffer;
|
||||
}
|
||||
|
||||
int KernAux_MemStore_put_char(void *store, const int c)
|
||||
{
|
||||
size_t KernAux_MemStore_write(
|
||||
void *const store,
|
||||
const void *const buffer,
|
||||
size_t size
|
||||
) {
|
||||
struct KernAux_MemStore *mem_store = store;
|
||||
KERNAUX_ASSERT(mem_store);
|
||||
KERNAUX_ASSERT(buffer);
|
||||
|
||||
if (mem_store->ptr > mem_store->end) return KERNAUX_EOF;
|
||||
const uint8_t *buffer_ptr = buffer;
|
||||
|
||||
return *(mem_store->ptr++) = c;
|
||||
while (size && mem_store->ptr <= mem_store->end) {
|
||||
--size;
|
||||
*(mem_store->ptr++) = *(buffer_ptr++);
|
||||
}
|
||||
|
||||
return (uint8_t*)buffer_ptr - (uint8_t*)buffer;
|
||||
}
|
||||
|
||||
/***********
|
||||
|
|
Loading…
Add table
Reference in a new issue