From ae4da6d73d784adc69c1a6e460ebb87cc9cca6de Mon Sep 17 00:00:00 2001 From: alessandrobason <1902881@uad.ac.uk> Date: Mon, 22 Aug 2022 18:27:41 +0100 Subject: [PATCH] added a few function to str_istream_t --- src/strstream.c | 22 ++++++++++++++++++++++ src/strstream.h | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/src/strstream.c b/src/strstream.c index aa3485c..2ddc40d 100644 --- a/src/strstream.c +++ b/src/strstream.c @@ -22,6 +22,17 @@ str_istream_t istrInitLen(const char *str, usize len) { return res; } +void istrScanf(str_istream_t *ctx, const char *fmt, ...) { + va_list va; + va_start(va, fmt); + istrScanfV(ctx, fmt, va); + va_end(va); +} + +void istrScanfV(str_istream_t *ctx, const char *fmt, va_list args) { + vsscanf(ctx->cur, fmt, args); +} + char istrGet(str_istream_t *ctx) { return *ctx->cur++; } @@ -34,6 +45,11 @@ void istrIgnore(str_istream_t *ctx, char delim) { ++i, ++ctx->cur); } +void istrIgnoreAndSkip(str_istream_t *ctx, char delim) { + istrIgnore(ctx, delim); + istrSkip(ctx, 1); +} + char istrPeek(str_istream_t *ctx) { return *ctx->cur; } @@ -75,6 +91,12 @@ void istrRewind(str_istream_t *ctx) { ctx->cur = ctx->start; } +void istrRewindN(str_istream_t *ctx, usize amount) { + usize remaining = ctx->size - (ctx->cur - ctx->start); + if (amount > remaining) amount = remaining; + ctx->cur -= amount; +} + usize istrTell(str_istream_t ctx) { return ctx.cur - ctx.start; } diff --git a/src/strstream.h b/src/strstream.h index 63e91e9..b754780 100644 --- a/src/strstream.h +++ b/src/strstream.h @@ -23,12 +23,17 @@ typedef struct { str_istream_t istrInit(const char *str); str_istream_t istrInitLen(const char *str, usize len); +void istrScanf(str_istream_t *ctx, const char *fmt, ...); +void istrScanfV(str_istream_t *ctx, const char *fmt, va_list args); + // get the current character and advance char istrGet(str_istream_t *ctx); // get the current character but don't advance char istrPeek(str_istream_t *ctx); // ignore characters until the delimiter void istrIgnore(str_istream_t *ctx, char delim); +// ignore characters until the delimiter and skip it +void istrIgnoreAndSkip(str_istream_t *ctx, char delim); // skip n characters void istrSkip(str_istream_t *ctx, usize n); // skips whitespace (' ', '\n', '\t', '\r') @@ -40,6 +45,8 @@ void istrRead(str_istream_t *ctx, char *buf, usize len); usize istrReadMax(str_istream_t *ctx, char *buf, usize len); // returns to the beginning of the stream void istrRewind(str_istream_t *ctx); +// returns back characters +void istrRewindN(str_istream_t *ctx, usize amount); // returns the number of bytes read from beginning of stream usize istrTell(str_istream_t ctx); // returns the number of bytes left to read in the stream