add buffer_putsa, buffer_get_token_sa, buffer_getline_sa, stralloc_chomp

and stralloc_chop
master
leitner 22 years ago
parent 2354c458a8
commit 053aadde58

@ -1,4 +1,7 @@
0.15: 0.15:
man page update (document stralloc return values)
add stralloc_chop and stralloc_chomp
add buffer_putsa, buffer_get_token_sa and buffer_getline_sa
0.14: 0.14:
avoid bus errors in byte_copy avoid bus errors in byte_copy

@ -71,4 +71,13 @@ extern buffer *buffer_1;
extern buffer *buffer_1small; extern buffer *buffer_1small;
extern buffer *buffer_2; extern buffer *buffer_2;
#ifdef STRALLOC_H
/* write stralloc to buffer */
extern int buffer_putsa(buffer* b,stralloc* sa);
/* read token from buffer to stralloc */
extern int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen);
/* read line from buffer to stralloc */
extern int buffer_getline_sa(buffer* b,stralloc* sa);
#endif
#endif #endif

@ -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);
}

@ -80,4 +80,19 @@ extern void stralloc_free(stralloc* sa);
#define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n))) #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
#define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0)) #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
/* remove last char. Return removed byte as unsigned char (or -1 if stralloc was empty). */
extern int stralloc_chop(stralloc* sa);
/* remove trailing "\r\n", "\n" or "\r". Return number of removed chars (0,1 or 2) */
extern int stralloc_chomp(stralloc* sa);
#ifdef BUFFER_H
/* write stralloc to buffer */
extern int buffer_putsa(buffer* b,stralloc* sa);
/* read token from buffer to stralloc */
extern int buffer_get_token_sa(buffer* b,stralloc* sa,const char* charset,unsigned int setlen);
/* read line from buffer to stralloc */
extern int buffer_getline_sa(buffer* b,stralloc* sa);
#endif
#endif #endif

@ -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]);
}

@ -12,5 +12,8 @@ is the same as
\fIsafrom\fR must already be allocated. \fIsafrom\fR must already be allocated.
The data that \fIsa\fR previously contained is overwritten and truncated. The data that \fIsa\fR previously contained is overwritten and truncated.
If stralloc_copy has trouble allocating memory, it returns 0. Otherwise
it returns 1.
.SH "SEE ALSO" .SH "SEE ALSO"
stralloc_copyb(3) stralloc_copyb(3)

Loading…
Cancel
Save