2021-06-07 16:55:13 +02:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file bufio.c
|
|
|
|
*
|
|
|
|
* I/O stream buffering utilities implementation.
|
|
|
|
*
|
|
|
|
* \copyright The DoubleFourteen Code Forge (C) All Rights Reserved
|
|
|
|
* \author Lorenzo Cogotti
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys/sys_local.h" // for Sys_SetErrStat() - vsnprintf()
|
|
|
|
#include "bufio.h"
|
|
|
|
#include "numlib.h"
|
2021-10-18 12:04:07 +02:00
|
|
|
#include "smallbytecopy.h"
|
2021-06-07 16:55:13 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
static Judgement Bufio_FillRdBuf(Stmrdbuf *sb)
|
|
|
|
{
|
|
|
|
assert(sb->pos == sb->availIn);
|
|
|
|
|
|
|
|
Sint64 n = sb->ops->Read(sb->streamp, sb->buf, sizeof(sb->buf));
|
|
|
|
if (n < 0) {
|
|
|
|
sb->hasError = TRUE;
|
|
|
|
return NG;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb->isEof = (n == 0);
|
|
|
|
|
|
|
|
sb->availIn = n;
|
|
|
|
sb->totalIn += n;
|
|
|
|
sb->pos = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sint64 Bufio_Read(Stmrdbuf *sb, void *buf, size_t nbytes)
|
|
|
|
{
|
|
|
|
assert(sb->ops->Read);
|
|
|
|
|
|
|
|
if (sb->hasError)
|
|
|
|
return -1; // refuse unless somebody clears error
|
|
|
|
|
|
|
|
Uint8 *dest = (Uint8 *) buf;
|
|
|
|
while (nbytes > 0) {
|
|
|
|
if (sb->pos >= sb->availIn) {
|
|
|
|
if (Bufio_FillRdBuf(sb) != OK) {
|
|
|
|
if (dest > (Uint8 *) buf)
|
|
|
|
break; // delay error to next read
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sb->isEof)
|
|
|
|
break; // end of file
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size = MIN((size_t) (sb->availIn - sb->pos), nbytes);
|
|
|
|
|
|
|
|
memcpy(dest, sb->buf + sb->pos, size);
|
|
|
|
sb->pos += size;
|
|
|
|
|
|
|
|
nbytes -= size;
|
|
|
|
dest += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest - (Uint8 *) buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bufio_Close(Stmrdbuf *sb)
|
|
|
|
{
|
|
|
|
if (sb->ops->Close) sb->ops->Close(sb->streamp);
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:04:07 +02:00
|
|
|
NOINLINE Sint64 Bufio_Flush(Stmwrbuf *sb)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
|
|
|
assert(sb->ops->Write);
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
while (sb->availOut > 0) {
|
|
|
|
Sint64 n = sb->ops->Write(sb->streamp, sb->buf, sb->availOut);
|
2021-06-07 16:55:13 +02:00
|
|
|
if (n < 0)
|
2021-10-15 12:18:35 +02:00
|
|
|
return -1;
|
2021-06-07 16:55:13 +02:00
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
memmove(sb->buf, sb->buf + n, sb->availOut - n);
|
|
|
|
sb->availOut -= n;
|
|
|
|
sb->totalOut += n;
|
2021-06-07 16:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
return sb->totalOut;
|
2021-06-07 16:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:04:07 +02:00
|
|
|
Sint64 _Bufio_SmallPutsn(Stmwrbuf *sb, const char *s, size_t nbytes)
|
|
|
|
{
|
|
|
|
if (sb->availOut + nbytes > sizeof(sb->buf) && Bufio_Flush(sb) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
_smallbytecopy64(sb->buf + sb->availOut, s, nbytes);
|
|
|
|
sb->availOut += nbytes;
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 _Bufio_Putsn(Stmwrbuf *sb, const char *s, size_t nbytes)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
2021-10-15 12:18:35 +02:00
|
|
|
if (sb->availOut + nbytes > sizeof(sb->buf) && Bufio_Flush(sb) == -1)
|
2021-06-07 16:55:13 +02:00
|
|
|
return -1;
|
|
|
|
if (nbytes > sizeof(sb->buf))
|
|
|
|
return sb->ops->Write(sb->streamp, sb, nbytes);
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
memcpy(sb->buf + sb->availOut, s, nbytes);
|
|
|
|
sb->availOut += nbytes;
|
2021-06-07 16:55:13 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Putu(Stmwrbuf *sb, unsigned long long val)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
2021-10-18 12:04:07 +02:00
|
|
|
if (sb->availOut + DIGS(val) + 1 > sizeof(sb->buf) && Bufio_Flush(sb) == -1)
|
|
|
|
return -1;
|
2021-06-07 16:55:13 +02:00
|
|
|
|
2021-10-18 12:04:07 +02:00
|
|
|
char *sptr = sb->buf + sb->availOut;
|
|
|
|
char *eptr = Utoa(val, sptr);
|
|
|
|
Sint64 n = eptr - sptr;
|
|
|
|
|
|
|
|
sb->availOut += n;
|
|
|
|
return n;
|
2021-06-07 16:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Putx(Stmwrbuf *sb, unsigned long long val)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
2021-10-18 12:04:07 +02:00
|
|
|
if (sb->availOut + XDIGS(val) + 1 > sizeof(sb->buf) && Bufio_Flush(sb) == -1)
|
|
|
|
return -1;
|
2021-06-07 16:55:13 +02:00
|
|
|
|
2021-10-18 12:04:07 +02:00
|
|
|
char *sptr = sb->buf + sb->availOut;
|
|
|
|
char *eptr = Xtoa(val, sptr);
|
|
|
|
Sint64 n = eptr - sptr;
|
|
|
|
|
|
|
|
sb->availOut += n;
|
|
|
|
return n;
|
2021-06-07 16:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Puti(Stmwrbuf *sb, long long val)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
2021-10-18 12:04:07 +02:00
|
|
|
if (sb->availOut + 1 + DIGS(val) + 1 > sizeof(sb->buf) && Bufio_Flush(sb) == -1)
|
|
|
|
return -1;
|
2021-06-07 16:55:13 +02:00
|
|
|
|
2021-10-18 12:04:07 +02:00
|
|
|
char *sptr = sb->buf + sb->availOut;
|
|
|
|
char *eptr = Itoa(val, sptr);
|
|
|
|
Sint64 n = eptr - sptr;
|
|
|
|
|
|
|
|
sb->availOut += n;
|
|
|
|
return n;
|
2021-06-07 16:55:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Putf(Stmwrbuf *sb, double val)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
|
|
|
char buf[DOUBLE_STRLEN + 1];
|
|
|
|
|
|
|
|
char *eptr = Ftoa(val, buf);
|
|
|
|
return Bufio_Putsn(sb, buf, eptr - buf);
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Printf(Stmwrbuf *sb, const char *fmt, ...)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
Sint64 n;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
n = Bufio_Vprintf(sb, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
Sint64 Bufio_Vprintf(Stmwrbuf *sb, const char *fmt, va_list va)
|
2021-06-07 16:55:13 +02:00
|
|
|
{
|
|
|
|
va_list vc;
|
|
|
|
char *buf;
|
|
|
|
int n1, n2;
|
|
|
|
|
|
|
|
va_copy(vc, va);
|
|
|
|
n1 = vsnprintf(NULL, 0, fmt, vc);
|
|
|
|
va_end(vc);
|
|
|
|
if (n1 < 0) {
|
|
|
|
Sys_SetErrStat(errno, "vsnprintf() failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (char *) alloca(n1 + 1);
|
|
|
|
n2 = vsnprintf(buf, n1 + 1, fmt, va);
|
|
|
|
if (n2 < 0) {
|
|
|
|
Sys_SetErrStat(errno, "vsnprintf() failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(n1 == n2);
|
|
|
|
|
|
|
|
return Bufio_Putsn(sb, buf, n2);
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:18:35 +02:00
|
|
|
static Sint64 Bufio_StmRead(void *streamp, void *buf, size_t nbytes)
|
|
|
|
{
|
|
|
|
return Bufio_Read((Stmrdbuf *) streamp, buf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Sint64 Bufio_StmTell(void *streamp)
|
|
|
|
{
|
|
|
|
Stmrdbuf *sb = (Stmrdbuf *) streamp;
|
|
|
|
|
|
|
|
return (sb->hasError) ? -1 : sb->totalIn + (Sint64) sb->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Bufio_StmClose(void *streamp)
|
|
|
|
{
|
|
|
|
Bufio_Close((Stmrdbuf *) streamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const StmOps _Stm_RdBufOps = {
|
|
|
|
Bufio_StmRead,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
Bufio_StmTell,
|
|
|
|
NULL,
|
|
|
|
Bufio_StmClose
|
|
|
|
};
|
|
|
|
static const StmOps _Stm_NcRdBufOps = {
|
|
|
|
Bufio_StmRead,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
Bufio_StmTell,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const StmOps *const Stm_RdBufOps = &_Stm_RdBufOps;
|
|
|
|
const StmOps *const Stm_NcRdBufOps = &_Stm_NcRdBufOps;
|