From 053aadde58cddb35faec0ef899df904da3d58eff Mon Sep 17 00:00:00 2001 From: leitner Date: Thu, 17 Oct 2002 20:32:03 +0000 Subject: [PATCH] add buffer_putsa, buffer_get_token_sa, buffer_getline_sa, stralloc_chomp and stralloc_chop --- CHANGES | 3 +++ buffer.h | 9 +++++++++ buffer/buffer_get_token_sa.3 | 23 +++++++++++++++++++++++ buffer/buffer_get_token_sa.c | 22 ++++++++++++++++++++++ buffer/buffer_getline_sa.3 | 22 ++++++++++++++++++++++ buffer/buffer_getline_sa.c | 6 ++++++ buffer/buffer_putsa.3 | 14 ++++++++++++++ buffer/buffer_putsa.c | 6 ++++++ stralloc.h | 15 +++++++++++++++ stralloc/stralloc_chomp.3 | 12 ++++++++++++ stralloc/stralloc_chomp.c | 19 +++++++++++++++++++ stralloc/stralloc_chop.3 | 13 +++++++++++++ stralloc/stralloc_chop.c | 7 +++++++ stralloc/stralloc_copy.3 | 3 +++ 14 files changed, 174 insertions(+) create mode 100644 buffer/buffer_get_token_sa.3 create mode 100644 buffer/buffer_get_token_sa.c create mode 100644 buffer/buffer_getline_sa.3 create mode 100644 buffer/buffer_getline_sa.c create mode 100644 buffer/buffer_putsa.3 create mode 100644 buffer/buffer_putsa.c create mode 100644 stralloc/stralloc_chomp.3 create mode 100644 stralloc/stralloc_chomp.c create mode 100644 stralloc/stralloc_chop.3 create mode 100644 stralloc/stralloc_chop.c diff --git a/CHANGES b/CHANGES index 9bd9ab1..41352ed 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,7 @@ 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: avoid bus errors in byte_copy diff --git a/buffer.h b/buffer.h index 8383a47..964b4f2 100644 --- a/buffer.h +++ b/buffer.h @@ -71,4 +71,13 @@ extern buffer *buffer_1; extern buffer *buffer_1small; 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 diff --git a/buffer/buffer_get_token_sa.3 b/buffer/buffer_get_token_sa.3 new file mode 100644 index 0000000..99e7276 --- /dev/null +++ b/buffer/buffer_get_token_sa.3 @@ -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 +.B #include + +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) diff --git a/buffer/buffer_get_token_sa.c b/buffer/buffer_get_token_sa.c new file mode 100644 index 0000000..51b59a4 --- /dev/null +++ b/buffer/buffer_get_token_sa.c @@ -0,0 +1,22 @@ +#include "byte.h" +#include "stralloc.h" +#include "buffer.h" +#include + +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) +.B #include + +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) diff --git a/buffer/buffer_getline_sa.c b/buffer/buffer_getline_sa.c new file mode 100644 index 0000000..b249d6d --- /dev/null +++ b/buffer/buffer_getline_sa.c @@ -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); +} diff --git a/buffer/buffer_putsa.3 b/buffer/buffer_putsa.3 new file mode 100644 index 0000000..df0e494 --- /dev/null +++ b/buffer/buffer_putsa.3 @@ -0,0 +1,14 @@ +.TH buffer_putsa 3 +.SH NAME +buffer_putsa \- write stralloc to buffer +.SH SYNTAX +.nf +.B #include +.B #include + +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) diff --git a/buffer/buffer_putsa.c b/buffer/buffer_putsa.c new file mode 100644 index 0000000..f1e346b --- /dev/null +++ b/buffer/buffer_putsa.c @@ -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); +} diff --git a/stralloc.h b/stralloc.h index 5bd63e6..715f404 100644 --- a/stralloc.h +++ b/stralloc.h @@ -80,4 +80,19 @@ extern void stralloc_free(stralloc* sa); #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n))) #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 diff --git a/stralloc/stralloc_chomp.3 b/stralloc/stralloc_chomp.3 new file mode 100644 index 0000000..3d06a0e --- /dev/null +++ b/stralloc/stralloc_chomp.3 @@ -0,0 +1,12 @@ +.TH stralloc_chomp 3 +.SH NAME +stralloc_chomp \- remove trailing CR, LF or CRLF +.SH SYNTAX +.B #include + +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) diff --git a/stralloc/stralloc_chomp.c b/stralloc/stralloc_chomp.c new file mode 100644 index 0000000..0262af2 --- /dev/null +++ b/stralloc/stralloc_chomp.c @@ -0,0 +1,19 @@ +#include + +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; +} diff --git a/stralloc/stralloc_chop.3 b/stralloc/stralloc_chop.3 new file mode 100644 index 0000000..7bf9f3f --- /dev/null +++ b/stralloc/stralloc_chop.3 @@ -0,0 +1,13 @@ +.TH stralloc_chop 3 +.SH NAME +stralloc_chop \- remove and return last char +.SH SYNTAX +.B #include + +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) diff --git a/stralloc/stralloc_chop.c b/stralloc/stralloc_chop.c new file mode 100644 index 0000000..daf8001 --- /dev/null +++ b/stralloc/stralloc_chop.c @@ -0,0 +1,7 @@ +#include + +int stralloc_chop(stralloc* sa) { + if (sa->len==0) return -1; + --sa->len; + return (unsigned char)(sa->s[sa->len]); +} diff --git a/stralloc/stralloc_copy.3 b/stralloc/stralloc_copy.3 index 49e4a6c..944bf82 100644 --- a/stralloc/stralloc_copy.3 +++ b/stralloc/stralloc_copy.3 @@ -12,5 +12,8 @@ is the same as \fIsafrom\fR must already be allocated. 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" stralloc_copyb(3)