add buffer_putm and buffer_putmflush

master
leitner 20 years ago
parent 5dfea5617e
commit 38ef27207a

@ -5,6 +5,8 @@
stralloc first. stralloc first.
add stralloc_zero to clear a stralloc add stralloc_zero to clear a stralloc
add buffer_putsflush add buffer_putsflush
add stralloc_catm and stralloc_copym
add buffer_putm and buffer_putmflush
0.20: 0.20:
add errmsg API add errmsg API

@ -25,6 +25,11 @@ int buffer_puts(buffer* b,const char* x);
int buffer_putsalign(buffer* b,const char* x); int buffer_putsalign(buffer* b,const char* x);
int buffer_putsflush(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_putspace(buffer* b);
int buffer_putnlflush(buffer* b); /* put \n and flush */ int buffer_putnlflush(buffer* b); /* put \n and flush */

@ -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 new-line character ('\\n') is encountered. That character is also
copied. 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 Note that line is not 0-terminated to make reading lines with 0-bytes
possible through this interface. possible through this interface.
.SH "RETURN VALUE"
Number of bytes read (without '\\n'), -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
buffer_init(3), buffer_feed(3), buffer_peek(3), buffer_seek(3), buffer(3) buffer_init(3), buffer_feed(3), buffer_peek(3), buffer_seek(3), buffer(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, for new data, buffer_put calls buffer_flush before copying any data,
while buffer_putalign fills all available space with data before calling while buffer_putalign fills all available space with data before calling
buffer_flush. buffer_flush.
.SH "RETURN VALUE"
On success, buffer_put returns 0. On error, buffer_put returns -1, buffer_put returns 0 if everything was fine, -1 on error (setting
setting \fIerrno\fR appropriately. errno).
.SH "SEE ALSO" .SH "SEE ALSO"
buffer_putalign(3), buffer_puts(3), buffer_flush(3), buffer(3) buffer_putalign(3), buffer_puts(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_put8long\fP(buffer* \fIb\fR,unsigned long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_put8long is similar to passing the result of fmt_8long to buffer_put8long is similar to passing the result of fmt_8long to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_8long(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_8long(3), buffer_put(3), buffer_flush(3), buffer(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 new data, buffer_put calls buffer_flush before copying any data, while
buffer_putalign fills all available space with data before calling buffer_putalign fills all available space with data before calling
buffer_flush. buffer_flush.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
buffer_put(3), buffer_putsalign(3), buffer_flush(3), buffer(3) buffer_put(3), buffer_putsalign(3), buffer_flush(3), buffer(3)

@ -7,5 +7,7 @@ buffer_puterror \- write error string to buffer and flush
int \fBbuffer_puterror\fP(buffer* \fIb\fR); int \fBbuffer_puterror\fP(buffer* \fIb\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_puterror is equivalent to calling buffer_puterror2 with errno. 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" .SH "SEE ALSO"
buffer_puterror2(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_puterror2(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(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 buffer_puterror2 writes the error message corresponding to the error
number in \fIerrnum\fR to the buffer (e.g. "No such file or directory" number in \fIerrnum\fR to the buffer (e.g. "No such file or directory"
for ENOENT). for ENOENT).
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
buffer_puterror(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_puterror(3), buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putflush\fP(buffer* \fIb\fR,
.SH DESCRIPTION .SH DESCRIPTION
buffer_putflush is similar to calling buffer_putflush is similar to calling
buffer_put(\fIb\fR,\fIx\fR,\fIlen\fR) and then buffer_flush(\fIb\fR). 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" .SH "SEE ALSO"
buffer_put(3), buffer_flush(3), buffer(3) buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putlong\fP(buffer* \fIb\fR,signed long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putlong is similar to passing the result of fmt_long to buffer_putlong is similar to passing the result of fmt_long to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_long(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_long(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putlonglong\fP(buffer* \fIb\fR,signed long long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putlonglong is equivalent to passing the result of fmt_longlong to buffer_putlonglong is equivalent to passing the result of fmt_longlong to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_longlong(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_longlong(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -0,0 +1,15 @@
.TH buffer_putm 3
.SH NAME
buffer_putm \- write ASCIIZ string(s) to buffer
.SH SYNTAX
.B #include <buffer.h>
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)

@ -0,0 +1,16 @@
#include <stdarg.h>
#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;
}

@ -0,0 +1,17 @@
#include <stdarg.h>
#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;
}

@ -8,5 +8,7 @@ int \fBbuffer_putnlflush\fP(buffer* \fIb\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putnlflush writes a single ASCII new-line character ('\\n') to buffer_putnlflush writes a single ASCII new-line character ('\\n') to
\fIb\fR and calls buffer_flush(\fIb\fR). \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" .SH "SEE ALSO"
buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(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, for new data, buffer_puts calls buffer_flush before copying any data,
while buffer_putsalign fills all available space with data before calling while buffer_putsalign fills all available space with data before calling
buffer_flush. buffer_flush.
.SH "RETURN VALUE"
buffer_puts returns 0 if everything was fine, -1 on error (setting
errno).
.SH "SEE ALSO" .SH "SEE ALSO"
buffer_putsalign(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_putsalign(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,6 +9,7 @@ buffer_putsa \- write stralloc to buffer
int \fBbuffer_putsa\fP(buffer* \fIb\fR,const char* \fIx\fR); int \fBbuffer_putsa\fP(buffer* \fIb\fR,const char* \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putsa is equivalent to buffer_put(b,x.sa,x.len). 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" .SH "SEE ALSO"
buffer_puts(3), buffer_flush(3), buffer(3) buffer_puts(3), buffer_flush(3), buffer(3)

@ -9,6 +9,7 @@ buffer_putsa \- write stralloc to buffer and flush
int \fBbuffer_putsaflush\fP(buffer* \fIb\fR,const char* \fIx\fR); int \fBbuffer_putsaflush\fP(buffer* \fIb\fR,const char* \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putsaflush is equivalent to buffer_putflush(b,x.sa,x.len). 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" .SH "SEE ALSO"
buffer_puts(3), buffer_flush(3), buffer(3) buffer_puts(3), buffer_flush(3), buffer(3)

@ -8,5 +8,7 @@ int \fBbuffer_putsalign\fP(buffer* \fIb\fR,const char* \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putsalign is like buffer_putalign with \fIlen\fR determined as buffer_putsalign is like buffer_putalign with \fIlen\fR determined as
the number of bytes before the first \\0 in \fIx\fR. 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" .SH "SEE ALSO"
buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -7,5 +7,7 @@ buffer_putspace \- write ASCII space to buffer
int \fBbuffer_putspace\fP(buffer* \fIb\fR); int \fBbuffer_putspace\fP(buffer* \fIb\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putspace writes a single ASCII space character to \fIb\fR. 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" .SH "SEE ALSO"
buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3) buffer_puts(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putulong\fP(buffer* \fIb\fR,unsigned long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putulong is similar to passing the result of fmt_ulong to buffer_putulong is similar to passing the result of fmt_ulong to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_ulong(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_ulong(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putulonglong\fP(buffer* \fIb\fR,unsigned long long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putulonglong is equivalent to passing the result of fmt_ulonglong to buffer_putulonglong is equivalent to passing the result of fmt_ulonglong to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_ulonglong(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_ulonglong(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -9,5 +9,7 @@ int \fBbuffer_putxlong\fP(buffer* \fIb\fR,unsigned long \fIx\fR);
.SH DESCRIPTION .SH DESCRIPTION
buffer_putxlong is similar to passing the result of fmt_xlong to buffer_putxlong is similar to passing the result of fmt_xlong to
buffer_put. buffer_put.
.SH "RETURN VALUE"
0 if everything is fine, -1 on error (setting \fIerrno\fR).
.SH "SEE ALSO" .SH "SEE ALSO"
fmt_xlong(3), buffer_put(3), buffer_flush(3), buffer(3) fmt_xlong(3), buffer_put(3), buffer_flush(3), buffer(3)

@ -63,6 +63,14 @@ int stralloc_catb(stralloc* sa,const char* in,unsigned int len);
/* stralloc_cats is analogous to stralloc_copys */ /* stralloc_cats is analogous to stralloc_copys */
int stralloc_cats(stralloc* sa,const char* in); 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 */ /* stralloc_cat is analogous to stralloc_copy */
int stralloc_cat(stralloc* sa,stralloc* in); 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 */ /* stralloc_0 appends \0 */
#define stralloc_0(sa) stralloc_append(sa,"") #define stralloc_0(sa) stralloc_append(sa,"")
#define stralloc_zero(sa) stralloc_copys(sa,"")
/* stralloc_catulong0 appends a '0' padded ASCII representation of in */ /* stralloc_catulong0 appends a '0' padded ASCII representation of in */
int stralloc_catulong0(stralloc* sa,unsigned long in,unsigned int n); int stralloc_catulong0(stralloc* sa,unsigned long in,unsigned int n);

@ -0,0 +1,17 @@
.TH stralloc_catm 3
.SH NAME
stralloc_catm \- append string(s) to a stralloc
.SH SYNTAX
.B #include <stralloc.h>
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)

@ -0,0 +1,15 @@
#include <stdarg.h>
#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;
}

@ -0,0 +1,16 @@
.TH stralloc_catm 3
.SH NAME
stralloc_copym \- copy string(s) to a stralloc
.SH SYNTAX
.B #include <stralloc.h>
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)

8
t.c

@ -25,11 +25,19 @@
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
int main(int argc,char* argv[]) { 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"; char* c="fnord";
int fd=open_read(c); int fd=open_read(c);
errmsg_iam(argv[0]); errmsg_iam(argv[0]);
carp("could not open file `",c,"'"); carp("could not open file `",c,"'");
diesys(23,"could not open file `",c,"'"); diesys(23,"could not open file `",c,"'");
#endif
#if 0 #if 0
errmsg_warn("could not open file `",c,"'",0); errmsg_warn("could not open file `",c,"'",0);
errmsg_warnsys("could not open file `",c,"'",0); errmsg_warnsys("could not open file `",c,"'",0);

Loading…
Cancel
Save