change semantic of fmt_fill and add man pages.

master
leitner 24 years ago
parent 6629de6768
commit ada92190c4

@ -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; unsigned int fmt_pad(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW;
/* "foo" -> "foo " /* "foo" -> "foo "
* write padlen-srclen spaces, if that is >= 0. Then copy srclen * append padlen-srclen spaces after dest, if that is >= 0. Truncate
* characters from src. Truncate only if total length is larger than * only if total length is larger than maxlen. Return number of
* maxlen. Return number of characters written. */ * characters written. */
unsigned int fmt_fill(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW; unsigned int fmt_fill(char* dest,unsigned int srclen,unsigned int padlen,unsigned int maxlen) __THROW;
#endif #endif

@ -0,0 +1,22 @@
.TH fmt_fill 3
.SH NAME
fmt_fill \- append spaces to a string
.SH SYNTAX
.B #include <fmt.h>
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)

@ -1,17 +1,18 @@
#include "fmt.h" #include "fmt.h"
/* "foo" -> "foo " /* "foo" -> "foo "
* Copy srclen characters from src. write padlen-srclen spaces, if * append padlen-srclen spaces after dest, if that is >= 0. Truncate
* that is >= 0. Truncate only if total length is larger than maxlen. * only if total length is larger than maxlen. Return number of
* Return number of characters written. */ * characters written. */
unsigned int fmt_fill(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) { unsigned int fmt_fill(char* dest,unsigned int srclen,unsigned int padlen,unsigned int maxlen) {
int todo; int todo;
char* olddest=dest; char* olddest=dest;
char* max=dest+maxlen; char* max=dest+maxlen;
for (todo=srclen; todo>0; --todo) { if (dest==0) {
if (dest>max) break; int sum=srclen>padlen?srclen:padlen;
*dest=*src; ++dest; ++src; return sum>maxlen?maxlen:sum;
} }
dest+=srclen;
for (todo=padlen-srclen; todo>0; --todo) { for (todo=padlen-srclen; todo>0; --todo) {
if (dest>max) break; if (dest>max) break;
*dest=' '; ++dest; *dest=' '; ++dest;

@ -0,0 +1,22 @@
.TH fmt_pad 3
.SH NAME
fmt_pad \- pad a string with spaces.
.SH SYNTAX
.B #include <fmt.h>
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)

@ -8,7 +8,12 @@ unsigned int fmt_pad(char* dest,const char* src,unsigned int srclen,unsigned int
int todo; int todo;
char* olddest=dest; char* olddest=dest;
char* max=dest+maxlen; 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; if (dest>max) break;
*dest=' '; ++dest; *dest=' '; ++dest;
} }

@ -1,6 +1,6 @@
.TH fmt_strn 3 .TH fmt_strn 3
.SH NAME .SH NAME
fmt_str \- write an ASCII string fmt_strn \- write an ASCII string
.SH SYNTAX .SH SYNTAX
.B #include <fmt.h> .B #include <fmt.h>

3
t.c

@ -15,7 +15,8 @@
int main(int argc,char* argv[]) { int main(int argc,char* argv[]) {
char buf[100]; 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); printf("\"%s\"\n",buf);
#if 0 #if 0
unsigned long len; unsigned long len;

Loading…
Cancel
Save