From 38ef27207a1c2f0438c5cc59f3dc972dc89d324c Mon Sep 17 00:00:00 2001 From: leitner Date: Thu, 25 Nov 2004 20:51:53 +0000 Subject: [PATCH] add buffer_putm and buffer_putmflush --- CHANGES | 2 ++ buffer.h | 5 +++++ buffer/buffer_getline.3 | 5 ++--- buffer/buffer_put.3 | 6 +++--- buffer/buffer_put8long.3 | 2 ++ buffer/buffer_putalign.3 | 2 ++ buffer/buffer_puterror.3 | 2 ++ buffer/buffer_puterror2.3 | 2 ++ buffer/buffer_putflush.3 | 2 ++ buffer/buffer_putlong.3 | 2 ++ buffer/buffer_putlonglong.3 | 2 ++ buffer/buffer_putm.3 | 15 +++++++++++++++ buffer/buffer_putm_internal.c | 16 ++++++++++++++++ buffer/buffer_putm_internal_flush.c | 17 +++++++++++++++++ buffer/buffer_putnlflush.3 | 2 ++ buffer/buffer_puts.3 | 3 +++ buffer/buffer_putsa.3 | 3 ++- buffer/buffer_putsaflush.3 | 3 ++- buffer/buffer_putsalign.3 | 2 ++ buffer/buffer_putspace.3 | 2 ++ buffer/buffer_putulong.3 | 2 ++ buffer/buffer_putulonglong.3 | 2 ++ buffer/buffer_putxlong.3 | 2 ++ stralloc.h | 10 ++++++++-- stralloc/stralloc_catm.3 | 17 +++++++++++++++++ stralloc/stralloc_catm_internal.c | 15 +++++++++++++++ stralloc/stralloc_copym.3 | 16 ++++++++++++++++ t.c | 8 ++++++++ 28 files changed, 157 insertions(+), 10 deletions(-) create mode 100644 buffer/buffer_putm.3 create mode 100644 buffer/buffer_putm_internal.c create mode 100644 buffer/buffer_putm_internal_flush.c create mode 100644 stralloc/stralloc_catm.3 create mode 100644 stralloc/stralloc_catm_internal.c create mode 100644 stralloc/stralloc_copym.3 diff --git a/CHANGES b/CHANGES index ef4ff94..4b946f9 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,8 @@ stralloc first. add stralloc_zero to clear a stralloc add buffer_putsflush + add stralloc_catm and stralloc_copym + add buffer_putm and buffer_putmflush 0.20: add errmsg API diff --git a/buffer.h b/buffer.h index 6037bb5..a85c84d 100644 --- a/buffer.h +++ b/buffer.h @@ -25,6 +25,11 @@ int buffer_puts(buffer* b,const char* x); int buffer_putsalign(buffer* b,const char* x); int buffer_putsflush(buffer* b,const char* x); +int buffer_putm_internal(buffer*b,...); +int buffer_putm_internal_flush(buffer*b,...); +#define buffer_putm(b,...) buffer_putm_internal(b,__VA_ARGS__,0) +#define buffer_putmflush(b,...) buffer_putm_internal_flush(b,__VA_ARGS__,0) + int buffer_putspace(buffer* b); int buffer_putnlflush(buffer* b); /* put \n and flush */ diff --git a/buffer/buffer_getline.3 b/buffer/buffer_getline.3 index fc1063c..cb304ab 100644 --- a/buffer/buffer_getline.3 +++ b/buffer/buffer_getline.3 @@ -11,10 +11,9 @@ buffer_getline copies data from \fIb\fR to \fIx\fR[0], \fIx\fR[1], ..., new-line character ('\\n') is encountered. That character is also copied. -buffer_getline returns the number of bytes read (excluding the '\\n') or --1 on error (setting \fIerrno\fR appropriately). - Note that line is not 0-terminated to make reading lines with 0-bytes possible through this interface. +.SH "RETURN VALUE" +Number of bytes read (without '\\n'), -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_init(3), buffer_feed(3), buffer_peek(3), buffer_seek(3), buffer(3) diff --git a/buffer/buffer_put.3 b/buffer/buffer_put.3 index 1e0baaf..93cbb03 100644 --- a/buffer/buffer_put.3 +++ b/buffer/buffer_put.3 @@ -12,8 +12,8 @@ The difference to buffer_putalign is that, when there isn't enough space for new data, buffer_put calls buffer_flush before copying any data, while buffer_putalign fills all available space with data before calling buffer_flush. - -On success, buffer_put returns 0. On error, buffer_put returns -1, -setting \fIerrno\fR appropriately. +.SH "RETURN VALUE" +buffer_put returns 0 if everything was fine, -1 on error (setting +errno). .SH "SEE ALSO" buffer_putalign(3), buffer_puts(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_put8long.3 b/buffer/buffer_put8long.3 index a1d9537..8811cea 100644 --- a/buffer/buffer_put8long.3 +++ b/buffer/buffer_put8long.3 @@ -9,5 +9,7 @@ int \fBbuffer_put8long\fP(buffer* \fIb\fR,unsigned long \fIx\fR); .SH DESCRIPTION buffer_put8long is similar to passing the result of fmt_8long to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_8long(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putalign.3 b/buffer/buffer_putalign.3 index 883422c..977cffd 100644 --- a/buffer/buffer_putalign.3 +++ b/buffer/buffer_putalign.3 @@ -12,5 +12,7 @@ The difference to buffer_put is that, when there isn't enough space for new data, buffer_put calls buffer_flush before copying any data, while buffer_putalign fills all available space with data before calling buffer_flush. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_put(3), buffer_putsalign(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_puterror.3 b/buffer/buffer_puterror.3 index a679537..c7ef97b 100644 --- a/buffer/buffer_puterror.3 +++ b/buffer/buffer_puterror.3 @@ -7,5 +7,7 @@ buffer_puterror \- write error string to buffer and flush int \fBbuffer_puterror\fP(buffer* \fIb\fR); .SH DESCRIPTION buffer_puterror is equivalent to calling buffer_puterror2 with errno. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puterror2(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_puterror2.3 b/buffer/buffer_puterror2.3 index 3e0996d..e64bb84 100644 --- a/buffer/buffer_puterror2.3 +++ b/buffer/buffer_puterror2.3 @@ -9,5 +9,7 @@ int \fBbuffer_puterror2\fP(buffer* \fIb\fR,int \fIerrnum\fR); buffer_puterror2 writes the error message corresponding to the error number in \fIerrnum\fR to the buffer (e.g. "No such file or directory" for ENOENT). +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puterror(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putflush.3 b/buffer/buffer_putflush.3 index 96be0f5..3adef3a 100644 --- a/buffer/buffer_putflush.3 +++ b/buffer/buffer_putflush.3 @@ -9,5 +9,7 @@ int \fBbuffer_putflush\fP(buffer* \fIb\fR, .SH DESCRIPTION buffer_putflush is similar to calling buffer_put(\fIb\fR,\fIx\fR,\fIlen\fR) and then buffer_flush(\fIb\fR). +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putlong.3 b/buffer/buffer_putlong.3 index 9e2e688..9dc9fb3 100644 --- a/buffer/buffer_putlong.3 +++ b/buffer/buffer_putlong.3 @@ -9,5 +9,7 @@ int \fBbuffer_putlong\fP(buffer* \fIb\fR,signed long \fIx\fR); .SH DESCRIPTION buffer_putlong is similar to passing the result of fmt_long to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_long(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putlonglong.3 b/buffer/buffer_putlonglong.3 index d0fff3b..5fb40a1 100644 --- a/buffer/buffer_putlonglong.3 +++ b/buffer/buffer_putlonglong.3 @@ -9,5 +9,7 @@ int \fBbuffer_putlonglong\fP(buffer* \fIb\fR,signed long long \fIx\fR); .SH DESCRIPTION buffer_putlonglong is equivalent to passing the result of fmt_longlong to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_longlong(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putm.3 b/buffer/buffer_putm.3 new file mode 100644 index 0000000..9fe6698 --- /dev/null +++ b/buffer/buffer_putm.3 @@ -0,0 +1,15 @@ +.TH buffer_putm 3 +.SH NAME +buffer_putm \- write ASCIIZ string(s) to buffer +.SH SYNTAX +.B #include + +int \fBbuffer_putm\fP(buffer* \fIb\fR,const char* \fIx\fR,...); +.SH DESCRIPTION +buffer_putm is like buffer_puts, but it can be passed more than one +string. +.SH "RETURN VALUE" +buffer_putm returns 0 if everything was fine, -1 on error (setting +errno). +.SH "SEE ALSO" +buffer_putsalign(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putm_internal.c b/buffer/buffer_putm_internal.c new file mode 100644 index 0000000..1370600 --- /dev/null +++ b/buffer/buffer_putm_internal.c @@ -0,0 +1,16 @@ +#include +#include "buffer.h" + +int buffer_putm_internal(buffer* b, ...) { + int r=0; + va_list a; + const char* s; + va_start(a,b); + while ((s=va_arg(a,const char*))) + if (buffer_puts(b,s)==-1) { + r=-1; + break; + } + va_end(a); + return r; +} diff --git a/buffer/buffer_putm_internal_flush.c b/buffer/buffer_putm_internal_flush.c new file mode 100644 index 0000000..a5afbcb --- /dev/null +++ b/buffer/buffer_putm_internal_flush.c @@ -0,0 +1,17 @@ +#include +#include "buffer.h" + +int buffer_putm_internal_flush(buffer* b, ...) { + int r=0; + va_list a; + const char* s; + va_start(a,b); + while ((s=va_arg(a,const char*))) + if (buffer_puts(b,s)==-1) { + r=-1; + break; + } + va_end(a); + buffer_flush(b); + return r; +} diff --git a/buffer/buffer_putnlflush.3 b/buffer/buffer_putnlflush.3 index d2d79fd..52f5532 100644 --- a/buffer/buffer_putnlflush.3 +++ b/buffer/buffer_putnlflush.3 @@ -8,5 +8,7 @@ int \fBbuffer_putnlflush\fP(buffer* \fIb\fR); .SH DESCRIPTION buffer_putnlflush writes a single ASCII new-line character ('\\n') to \fIb\fR and calls buffer_flush(\fIb\fR). +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_puts.3 b/buffer/buffer_puts.3 index c93b706..72a16c5 100644 --- a/buffer/buffer_puts.3 +++ b/buffer/buffer_puts.3 @@ -13,5 +13,8 @@ The difference to buffer_putsalign is that, when there isn't enough space for new data, buffer_puts calls buffer_flush before copying any data, while buffer_putsalign fills all available space with data before calling buffer_flush. +.SH "RETURN VALUE" +buffer_puts returns 0 if everything was fine, -1 on error (setting +errno). .SH "SEE ALSO" buffer_putsalign(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putsa.3 b/buffer/buffer_putsa.3 index df0e494..ed1c3c3 100644 --- a/buffer/buffer_putsa.3 +++ b/buffer/buffer_putsa.3 @@ -9,6 +9,7 @@ buffer_putsa \- write stralloc to buffer 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 "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puts(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putsaflush.3 b/buffer/buffer_putsaflush.3 index bf797fd..aed30d6 100644 --- a/buffer/buffer_putsaflush.3 +++ b/buffer/buffer_putsaflush.3 @@ -9,6 +9,7 @@ buffer_putsa \- write stralloc to buffer and flush int \fBbuffer_putsaflush\fP(buffer* \fIb\fR,const char* \fIx\fR); .SH DESCRIPTION buffer_putsaflush is equivalent to buffer_putflush(b,x.sa,x.len). - +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puts(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putsalign.3 b/buffer/buffer_putsalign.3 index fe53593..49912d8 100644 --- a/buffer/buffer_putsalign.3 +++ b/buffer/buffer_putsalign.3 @@ -8,5 +8,7 @@ int \fBbuffer_putsalign\fP(buffer* \fIb\fR,const char* \fIx\fR); .SH DESCRIPTION buffer_putsalign is like buffer_putalign with \fIlen\fR determined as the number of bytes before the first \\0 in \fIx\fR. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putspace.3 b/buffer/buffer_putspace.3 index 456fe72..dc94130 100644 --- a/buffer/buffer_putspace.3 +++ b/buffer/buffer_putspace.3 @@ -7,5 +7,7 @@ buffer_putspace \- write ASCII space to buffer int \fBbuffer_putspace\fP(buffer* \fIb\fR); .SH DESCRIPTION buffer_putspace writes a single ASCII space character to \fIb\fR. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putulong.3 b/buffer/buffer_putulong.3 index 812c0c2..aee2e9d 100644 --- a/buffer/buffer_putulong.3 +++ b/buffer/buffer_putulong.3 @@ -9,5 +9,7 @@ int \fBbuffer_putulong\fP(buffer* \fIb\fR,unsigned long \fIx\fR); .SH DESCRIPTION buffer_putulong is similar to passing the result of fmt_ulong to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_ulong(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putulonglong.3 b/buffer/buffer_putulonglong.3 index 3c7c387..c5bf94a 100644 --- a/buffer/buffer_putulonglong.3 +++ b/buffer/buffer_putulonglong.3 @@ -9,5 +9,7 @@ int \fBbuffer_putulonglong\fP(buffer* \fIb\fR,unsigned long long \fIx\fR); .SH DESCRIPTION buffer_putulonglong is equivalent to passing the result of fmt_ulonglong to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_ulonglong(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/buffer/buffer_putxlong.3 b/buffer/buffer_putxlong.3 index 1f6005e..3ca0c1b 100644 --- a/buffer/buffer_putxlong.3 +++ b/buffer/buffer_putxlong.3 @@ -9,5 +9,7 @@ int \fBbuffer_putxlong\fP(buffer* \fIb\fR,unsigned long \fIx\fR); .SH DESCRIPTION buffer_putxlong is similar to passing the result of fmt_xlong to buffer_put. +.SH "RETURN VALUE" +0 if everything is fine, -1 on error (setting \fIerrno\fR). .SH "SEE ALSO" fmt_xlong(3), buffer_put(3), buffer_flush(3), buffer(3) diff --git a/stralloc.h b/stralloc.h index 802a7a5..08c3754 100644 --- a/stralloc.h +++ b/stralloc.h @@ -63,6 +63,14 @@ int stralloc_catb(stralloc* sa,const char* in,unsigned int len); /* stralloc_cats is analogous to stralloc_copys */ int stralloc_cats(stralloc* sa,const char* in); +#define stralloc_zero(sa) stralloc_copys(sa,"") + +/* like stralloc_cats but can cat more than one string at once */ +int stralloc_catm_internal(stralloc* sa,...); + +#define stralloc_catm(sa,...) stralloc_catm_internal(sa,__VA_ARGS__,0) +#define stralloc_copym(sa,...) (stralloc_zero(sa) && stralloc_catm_internal(sa,__VA_ARGS__,0)) + /* stralloc_cat is analogous to stralloc_copy */ int stralloc_cat(stralloc* sa,stralloc* in); @@ -91,8 +99,6 @@ int stralloc_diffs(const stralloc* a,const char* b) __pure__; /* stralloc_0 appends \0 */ #define stralloc_0(sa) stralloc_append(sa,"") -#define stralloc_zero(sa) stralloc_copys(sa,"") - /* stralloc_catulong0 appends a '0' padded ASCII representation of in */ int stralloc_catulong0(stralloc* sa,unsigned long in,unsigned int n); diff --git a/stralloc/stralloc_catm.3 b/stralloc/stralloc_catm.3 new file mode 100644 index 0000000..9c7c331 --- /dev/null +++ b/stralloc/stralloc_catm.3 @@ -0,0 +1,17 @@ +.TH stralloc_catm 3 +.SH NAME +stralloc_catm \- append string(s) to a stralloc +.SH SYNTAX +.B #include + +int \fBstralloc_cats\fP(stralloc* \fIsa\fR,const char* \fIs\fR, ...); +.SH DESCRIPTION +stralloc_cats appends \\0-terminated strings from \fIs\fR... to the +end of the string stored in \fIsa\fR, allocating space if necessary, and +returns 1. If \fIsa\fR is unallocated, stralloc_cats is the same as +stralloc_copys. + +If it runs out of memory, stralloc_cats returns 0. At that point, it +may already have copied a few of the strings to sa. +.SH "SEE ALSO" +stralloc_cats(3) diff --git a/stralloc/stralloc_catm_internal.c b/stralloc/stralloc_catm_internal.c new file mode 100644 index 0000000..55beba8 --- /dev/null +++ b/stralloc/stralloc_catm_internal.c @@ -0,0 +1,15 @@ +#include +#include "stralloc.h" + +int stralloc_catm_internal(stralloc* sa, ...) { + va_list a; + const char* s; + va_start(a,sa); + while ((s=va_arg(a,const char*))) + if (stralloc_cats(sa,s)==0) { + va_end(a); + return 0; + } + va_end(a); + return 1; +} diff --git a/stralloc/stralloc_copym.3 b/stralloc/stralloc_copym.3 new file mode 100644 index 0000000..cc19334 --- /dev/null +++ b/stralloc/stralloc_copym.3 @@ -0,0 +1,16 @@ +.TH stralloc_catm 3 +.SH NAME +stralloc_copym \- copy string(s) to a stralloc +.SH SYNTAX +.B #include + +int \fBstralloc_copys\fP(stralloc* \fIsa\fR,const char* \fIs\fR, ...); +.SH DESCRIPTION +stralloc_cats copies \\0-terminated strings from \fIs\fR... to \fIsa\fR, +allocating space if necessary, and returns 1. If there is data in the +\fIsa\fR, it is cleared first. + +If it runs out of memory, stralloc_copys returns 0. At that point, it +may already have copied a few of the strings to sa. +.SH "SEE ALSO" +stralloc_copys(3) diff --git a/t.c b/t.c index fa16700..07420df 100644 --- a/t.c +++ b/t.c @@ -25,11 +25,19 @@ __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") int main(int argc,char* argv[]) { +#if 0 + static stralloc sa; + stralloc_copym(&sa,"foo ","bar ","baz.\n"); + write(1,sa.s,sa.len); +#endif + buffer_putmflush(buffer_1,"foo ","bar ","baz.\n"); +#if 0 char* c="fnord"; int fd=open_read(c); errmsg_iam(argv[0]); carp("could not open file `",c,"'"); diesys(23,"could not open file `",c,"'"); +#endif #if 0 errmsg_warn("could not open file `",c,"'",0); errmsg_warnsys("could not open file `",c,"'",0);