change semantic of fmt_fill and add man pages.

This commit is contained in:
leitner 2001-05-15 19:04:58 +00:00
parent 6629de6768
commit ada92190c4
7 changed files with 65 additions and 14 deletions

8
fmt.h
View File

@ -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

22
fmt/fmt_fill.3 Normal file
View File

@ -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)

View File

@ -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;

22
fmt/fmt_pad.3 Normal file
View File

@ -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)

View File

@ -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;
}

View File

@ -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 <fmt.h>

3
t.c
View File

@ -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;