From 6629de6768c41599d0b94ec3dffda2b4e86c2cc9 Mon Sep 17 00:00:00 2001 From: leitner Date: Tue, 15 May 2001 18:46:17 +0000 Subject: [PATCH] add fmt_pad and fmt_fill. --- Makefile | 2 +- fmt.h | 12 ++++++++++++ fmt/fmt_fill.c | 20 ++++++++++++++++++++ fmt/fmt_pad.c | 20 ++++++++++++++++++++ t.c | 5 +++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 fmt/fmt_fill.c create mode 100644 fmt/fmt_pad.c diff --git a/Makefile b/Makefile index 5b264a8..935f0c1 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer:mmap CC=egcc #CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall -CFLAGS=-I. -I../dietlibc/include -pipe -Wall -Os -march=athlon -mcpu=athlon -malign-functions=2 -fomit-frame-pointer -fschedule-insns2 -g +CFLAGS=-I. -I../dietlibc/include -pipe -Wall -Os -march=athlon -mcpu=athlon -malign-functions=2 -fschedule-insns2 -g #CFLAGS=-I../dietlibc/include -I. -pipe -Wall -Os -march=pentiumpro -mcpu=athlon -fomit-frame-pointer -fschedule-insns2 -Wall #CFLAGS=-I../dietlibc/include -pipe -Os -march=pentiumpro -mcpu=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall diff --git a/fmt.h b/fmt.h index b190742..88b4580 100644 --- a/fmt.h +++ b/fmt.h @@ -58,4 +58,16 @@ unsigned int fmt_str(char *dest,const char *src) __THROW; * return number of copied bytes. */ unsigned int fmt_strn(char *dest,const char *src,unsigned int limit) __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_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; + #endif diff --git a/fmt/fmt_fill.c b/fmt/fmt_fill.c new file mode 100644 index 0000000..5c90f86 --- /dev/null +++ b/fmt/fmt_fill.c @@ -0,0 +1,20 @@ +#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) { + int todo; + char* olddest=dest; + char* max=dest+maxlen; + for (todo=srclen; todo>0; --todo) { + if (dest>max) break; + *dest=*src; ++dest; ++src; + } + for (todo=padlen-srclen; todo>0; --todo) { + if (dest>max) break; + *dest=' '; ++dest; + } + return dest-olddest; +} diff --git a/fmt/fmt_pad.c b/fmt/fmt_pad.c new file mode 100644 index 0000000..90a02fa --- /dev/null +++ b/fmt/fmt_pad.c @@ -0,0 +1,20 @@ +#include "fmt.h" + +/* "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_pad(char* dest,const char* src,unsigned int srclen,unsigned int padlen,unsigned int maxlen) { + int todo; + char* olddest=dest; + char* max=dest+maxlen; + for (todo=padlen-srclen; todo>0; --todo) { + if (dest>max) break; + *dest=' '; ++dest; + } + for (todo=srclen; todo>0; --todo) { + if (dest>max) break; + *dest=*src; ++dest; ++src; + } + return dest-olddest; +} diff --git a/t.c b/t.c index 7564341..2e34438 100644 --- a/t.c +++ b/t.c @@ -14,9 +14,14 @@ __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") int main(int argc,char* argv[]) { + char buf[100]; + buf[fmt_fill(buf,"foobarbaz",3,5,100)]=0; + printf("\"%s\"\n",buf); +#if 0 unsigned long len; char *c=mmap_read("/etc/passwd",&len); printf("got map %p of len %lu\n",c,len); +#endif #if 0 char c; printf("%d\n",buffer_getc(buffer_0,&c));