From ada92190c4e1b3260abfd4bcf6fcb4b5202800f2 Mon Sep 17 00:00:00 2001 From: leitner Date: Tue, 15 May 2001 19:04:58 +0000 Subject: [PATCH] change semantic of fmt_fill and add man pages. --- fmt.h | 8 ++++---- fmt/fmt_fill.3 | 22 ++++++++++++++++++++++ fmt/fmt_fill.c | 15 ++++++++------- fmt/fmt_pad.3 | 22 ++++++++++++++++++++++ fmt/fmt_pad.c | 7 ++++++- fmt/fmt_strn.3 | 2 +- t.c | 3 ++- 7 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 fmt/fmt_fill.3 create mode 100644 fmt/fmt_pad.3 diff --git a/fmt.h b/fmt.h index 88b4580..7a61af0 100644 --- a/fmt.h +++ b/fmt.h @@ -65,9 +65,9 @@ unsigned int fmt_strn(char *dest,const char *src,unsigned int limit) __THROW; unsigned int fmt_pad(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW; /* "foo" -> "foo " - * write padlen-srclen spaces, if that is >= 0. Then copy srclen - * characters from src. Truncate only if total length is larger than - * maxlen. Return number of characters written. */ -unsigned int fmt_fill(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW; + * append padlen-srclen spaces after dest, if that is >= 0. Truncate + * only if total length is larger than maxlen. Return number of + * characters written. */ +unsigned int fmt_fill(char* dest,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW; #endif diff --git a/fmt/fmt_fill.3 b/fmt/fmt_fill.3 new file mode 100644 index 0000000..e059afb --- /dev/null +++ b/fmt/fmt_fill.3 @@ -0,0 +1,22 @@ +.TH fmt_fill 3 +.SH NAME +fmt_fill \- append spaces to a string +.SH SYNTAX +.B #include + +unsigned int \fBfmt_fill\fP(char *\fIdest\fR, + unsigned int \fIsrclen\fR, unsigned int \fIpadlen\fR, + unsigned int \fImaxlen\fR); +.SH DESCRIPTION +fmt_fill appends \fIpadlen\fR-\fIsrclen\fR spaces (if that number is +positive) to \fIdest\fR (which holds \fIsrclen\fR bytes). It truncates +the output only if the length would exceed \fImaxlen\fR. + +It returns the number of bytes it wrote. + +fmt_fill does not append \\0. + +If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_fill returns the number +of bytes it would have written. +.SH "SEE ALSO" +fmt_strn(3), fmt_pad(3) diff --git a/fmt/fmt_fill.c b/fmt/fmt_fill.c index 5c90f86..9f63e88 100644 --- a/fmt/fmt_fill.c +++ b/fmt/fmt_fill.c @@ -1,17 +1,18 @@ #include "fmt.h" /* "foo" -> "foo " - * Copy srclen characters from src. write padlen-srclen spaces, if - * that is >= 0. Truncate only if total length is larger than maxlen. - * Return number of characters written. */ -unsigned int fmt_fill(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) { + * append padlen-srclen spaces after dest, if that is >= 0. Truncate + * only if total length is larger than maxlen. Return number of + * characters written. */ +unsigned int fmt_fill(char* dest,unsigned int srclen,unsigned int padlen,unsigned int maxlen) { int todo; char* olddest=dest; char* max=dest+maxlen; - for (todo=srclen; todo>0; --todo) { - if (dest>max) break; - *dest=*src; ++dest; ++src; + if (dest==0) { + int sum=srclen>padlen?srclen:padlen; + return sum>maxlen?maxlen:sum; } + dest+=srclen; for (todo=padlen-srclen; todo>0; --todo) { if (dest>max) break; *dest=' '; ++dest; diff --git a/fmt/fmt_pad.3 b/fmt/fmt_pad.3 new file mode 100644 index 0000000..934ae82 --- /dev/null +++ b/fmt/fmt_pad.3 @@ -0,0 +1,22 @@ +.TH fmt_pad 3 +.SH NAME +fmt_pad \- pad a string with spaces. +.SH SYNTAX +.B #include + +unsigned int \fBfmt_pad\fP(char *\fIdest\fR, const char *\fIsource\fR, + unsigned int \fIsrclen\fR, unsigned int \fIpadlen\fR, + unsigned int \fImaxlen\fR); +.SH DESCRIPTION +fmt_pad writes \fIpadlen\fR-\fIsrclen\fR spaces (if that number is +positive) and then \fIsrclen\fR characters from \fIsource\fR. It +truncates the output only if the length would exceed \fImaxlen\fR. + +It returns the number of bytes it wrote. + +fmt_pad does not append \\0. + +If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_pad returns the number +of bytes it would have written. +.SH "SEE ALSO" +fmt_strn(3), fmt_fill(3) diff --git a/fmt/fmt_pad.c b/fmt/fmt_pad.c index 90a02fa..7b56285 100644 --- a/fmt/fmt_pad.c +++ b/fmt/fmt_pad.c @@ -8,7 +8,12 @@ unsigned int fmt_pad(char* dest,const char* src,unsigned int srclen,unsigned int int todo; char* olddest=dest; char* max=dest+maxlen; - for (todo=padlen-srclen; todo>0; --todo) { + todo=padlen-srclen; + if (dest==0) { + int sum=srclen>padlen?srclen:padlen; + return sum>maxlen?maxlen:sum; + } + for (; todo>0; --todo) { if (dest>max) break; *dest=' '; ++dest; } diff --git a/fmt/fmt_strn.3 b/fmt/fmt_strn.3 index 1e53260..277f3da 100644 --- a/fmt/fmt_strn.3 +++ b/fmt/fmt_strn.3 @@ -1,6 +1,6 @@ .TH fmt_strn 3 .SH NAME -fmt_str \- write an ASCII string +fmt_strn \- write an ASCII string .SH SYNTAX .B #include diff --git a/t.c b/t.c index 2e34438..4a6e572 100644 --- a/t.c +++ b/t.c @@ -15,7 +15,8 @@ int main(int argc,char* argv[]) { char buf[100]; - buf[fmt_fill(buf,"foobarbaz",3,5,100)]=0; + strcpy(buf,"foobarbaz"); + buf[fmt_fill(buf,3,5,100)]=0; printf("\"%s\"\n",buf); #if 0 unsigned long len;