add buffer_putsa, buffer_get_token_sa, buffer_getline_sa, stralloc_chomp
and stralloc_chopmaster
parent
2354c458a8
commit
053aadde58
@ -0,0 +1,23 @@
|
||||
.TH buffer_get_token_sa 3
|
||||
.SH NAME
|
||||
buffer_get_token_sa \- read token from buffer
|
||||
.SH SYNTAX
|
||||
.nf
|
||||
.B #include <stralloc.h>
|
||||
.B #include <buffer.h>
|
||||
|
||||
int \fBbuffer_get_token_sa\fP(buffer* \fIb\fR,stralloc* \fIsa\fR,
|
||||
const char* \fIcharset\fR,unsigned int \fIsetlen\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_get_token_sa copies data from the \fIb\fR to \fIsa\fR until one
|
||||
of the delimiters in \fIcharset\fR is found, overwriting the previous
|
||||
content of \fIsa\fR. That delimiter is also appended to \fIsa\fR.
|
||||
|
||||
If reading from the buffer or allocating memory fails,
|
||||
buffer_get_token_sa returns -1 and sets \fIerrno\fR appropriately. At
|
||||
that point \fIsa\fR may be empty or it may already contain a partial
|
||||
token.
|
||||
|
||||
On success, buffer_get_token_sa returns 0.
|
||||
.SH "SEE ALSO"
|
||||
buffer_getline_sa(3), buffer_get_token(3), buffer(3)
|
@ -0,0 +1,22 @@
|
||||
#include "byte.h"
|
||||
#include "stralloc.h"
|
||||
#include "buffer.h"
|
||||
#include <errno.h>
|
||||
|
||||
int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen) {
|
||||
sa->len=0;
|
||||
for (;;) {
|
||||
char x;
|
||||
if (!stralloc_readyplus(sa,1)) goto nomem;
|
||||
switch (buffer_getc(b,&x)) {
|
||||
case -1: return -1;
|
||||
case 0: return 0;
|
||||
}
|
||||
stralloc_append(sa,&x);
|
||||
if (byte_chr(charset,setlen,x)<setlen) break;
|
||||
}
|
||||
return 0;
|
||||
nomem:
|
||||
errno=ENOMEM;
|
||||
return -1;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
.TH buffer_getline_sa 3
|
||||
.SH NAME
|
||||
buffer_getline_sa \- read line from buffer
|
||||
.SH SYNTAX
|
||||
.nf
|
||||
.B #include <stralloc.h>
|
||||
.B #include <buffer.h>
|
||||
|
||||
int \fBbuffer_getline_sa\fP(buffer* \fIb\fR,stralloc* \fIsa\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_getline_sa copies data from the \fIb\fR to \fIsa\fR until a '\\n'
|
||||
is found, overwriting the previous content of \fIsa\fR. The new-line
|
||||
is also appended to \fIsa\fR.
|
||||
|
||||
If reading from the buffer or allocating memory fails,
|
||||
buffer_getline_sa returns -1 and sets \fIerrno\fR appropriately. At
|
||||
that point \fIsa\fR may be empty or it may already contain a partial
|
||||
token.
|
||||
|
||||
On success, buffer_getline_sa returns 0.
|
||||
.SH "SEE ALSO"
|
||||
buffer_get_token_sa(3), buffer(3)
|
@ -0,0 +1,6 @@
|
||||
#include "stralloc.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_getline_sa(buffer* b,stralloc* sa) {
|
||||
return buffer_get_token_sa(b,sa,"\n",1);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
.TH buffer_putsa 3
|
||||
.SH NAME
|
||||
buffer_putsa \- write stralloc to buffer
|
||||
.SH SYNTAX
|
||||
.nf
|
||||
.B #include <stralloc.h>
|
||||
.B #include <buffer.h>
|
||||
|
||||
int \fBbuffer_putsa\fP(buffer* \fIb\fR,const char* \fIx\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_putsa is equivalent to buffer_put(b,x.sa,x.len).
|
||||
|
||||
.SH "SEE ALSO"
|
||||
buffer_puts(3), buffer_flush(3), buffer(3)
|
@ -0,0 +1,6 @@
|
||||
#include "stralloc.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int buffer_putsa(buffer* b,stralloc* sa) {
|
||||
return buffer_put(b,sa->s,sa->len);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
.TH stralloc_chomp 3
|
||||
.SH NAME
|
||||
stralloc_chomp \- remove trailing CR, LF or CRLF
|
||||
.SH SYNTAX
|
||||
.B #include <stralloc.h>
|
||||
|
||||
extern int \fBstralloc_chomp\fP(stralloc* \fIsa\fR);
|
||||
.SH DESCRIPTION
|
||||
stralloc_chomp removes trailing CRLF, CR or LF from \fIsa\fR and returns
|
||||
the number of removed characters (i.e. 0, 1 or 2).
|
||||
.SH "SEE ALSO"
|
||||
stralloc_chop(3)
|
@ -0,0 +1,19 @@
|
||||
#include <stralloc.h>
|
||||
|
||||
int stralloc_chomp(stralloc* sa) {
|
||||
unsigned int max=sa->len;
|
||||
if (max>0) {
|
||||
register char x;
|
||||
--max;
|
||||
x=sa->s[max];
|
||||
if (x=='\n' || x=='\r') {
|
||||
if (x=='\n' && max>1 && sa->s[max-1]=='\r') {
|
||||
sa->len-=2;
|
||||
return 2;
|
||||
}
|
||||
--sa->len;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
.TH stralloc_chop 3
|
||||
.SH NAME
|
||||
stralloc_chop \- remove and return last char
|
||||
.SH SYNTAX
|
||||
.B #include <stralloc.h>
|
||||
|
||||
extern int \fBstralloc_chop\fP(stralloc* \fIsa\fR);
|
||||
.SH DESCRIPTION
|
||||
stralloc_chop removes the last char in the stralloc (if it is empty,
|
||||
stralloc_chop does nothing and returns -1). This character is cast to
|
||||
unsigned char and returned.
|
||||
.SH "SEE ALSO"
|
||||
stralloc_chomp(3)
|
@ -0,0 +1,7 @@
|
||||
#include <stralloc.h>
|
||||
|
||||
int stralloc_chop(stralloc* sa) {
|
||||
if (sa->len==0) return -1;
|
||||
--sa->len;
|
||||
return (unsigned char)(sa->s[sa->len]);
|
||||
}
|
Loading…
Reference in New Issue