Implement `ctlseqs_purge()`.

This commit is contained in:
CismonX 2020-12-10 17:27:34 +08:00
parent 98af814b19
commit 510fae5b0a
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
4 changed files with 69 additions and 2 deletions

View File

@ -6,7 +6,14 @@
# this notice are preserved. This file is offered as-is, without any warranty.
#
dist_man3_MANS = ctlseqs_matcher_init.3 ctlseqs_matcher_setopt.3 ctlseqs_matcher_free.3 \
ctlseqs_reader_init.3 ctlseqs_reader_setopt.3 ctlseqs_read.3 ctlseqs_reader_free.3
dist_man3_MANS = \
ctlseqs_matcher_init.3 \
ctlseqs_matcher_setopt.3 \
ctlseqs_matcher_free.3 \
ctlseqs_reader_init.3 \
ctlseqs_reader_setopt.3 \
ctlseqs_read.3 \
ctlseqs_purge.3 \
ctlseqs_reader_free.3
dist_man7_MANS = ctlseqs.7

40
man/ctlseqs_purge.3 Normal file
View File

@ -0,0 +1,40 @@
.TH CTLSEQS_PURGE 3 "Sep 01, 2020" 0.1.0 "Ctlseqs Library Manual"
.
.SH NAME
ctlseqs_purge - purge read buffer
.
.SH SYNOPSYS
.nf
.B #include <ctlseqs.h>
.PP
.BI "void ctlseqs_purge(struct ctlseqs_reader *" reader ", size_t " nbytes );
.fi
.
.SH DESCRIPTION
Removes up to
.I nbytes
number of leading bytes from the internal read buffer of given
.IR reader ,
and resets the current state of the reader.
.PP
If
.I nbytes
is zero, this function has no effect.
.
.SH RETURN VALUE
This function has no return values.
.
.SH NOTES
This function is useful when dealing with partial sequences returned by
.BR ctlseqs_read ()
whose future completion is not desired.
.
.SH SEE ALSO
.BR ctlseqs_read (3),
.BR ctlseqs (7)
.
.SH COPYRIGHT
Copyright (c) 2020 CismonX <admin@cismon.net>
.PP
Copying and distribution of this file, with or without modification, are permitted in any medium without royalty, provided the copyright notice and this notice are preserved.
This file is offered as-is, without any warranty.

View File

@ -518,6 +518,23 @@ ctlseqs_read(struct ctlseqs_reader *reader, struct ctlseqs_matcher const *matche
return ctlseqs_match(reader, matcher);
}
void
ctlseqs_purge(struct ctlseqs_reader *reader, size_t nbytes)
{
size_t bufsize = reader->buf_end - reader->buf_start;
nbytes = nbytes > bufsize ? bufsize : nbytes;
if (nbytes == 0) {
return;
}
reader->buf_start += nbytes;
if (reader->buf_start == reader->buf_end) {
reader->buf_start = 0;
reader->buf_end = 0;
}
reader->last_idx = 0;
reader->state = ctlseqs_state_none;
}
CTLSEQS_COLD void
ctlseqs_reader_free(struct ctlseqs_reader *reader)
{

View File

@ -408,6 +408,9 @@ ctlseqs_reader_setopt(struct ctlseqs_reader *reader, struct ctlseqs_reader_opts
ssize_t
ctlseqs_read(struct ctlseqs_reader *reader, struct ctlseqs_matcher const *matcher, int timeout);
void
ctlseqs_purge(struct ctlseqs_reader *reader, size_t nbytes);
void
ctlseqs_reader_free(struct ctlseqs_reader *reader);