add fmt_pad and fmt_fill.

This commit is contained in:
leitner 2001-05-15 18:46:17 +00:00
parent 84cd4773f4
commit 6629de6768
5 changed files with 58 additions and 1 deletions

View File

@ -4,7 +4,7 @@ VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer:mmap
CC=egcc CC=egcc
#CFLAGS=-I. -pipe -Wall -Os -march=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall #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 -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 #CFLAGS=-I../dietlibc/include -pipe -Os -march=pentiumpro -mcpu=pentiumpro -fomit-frame-pointer -fschedule-insns2 -Wall

12
fmt.h
View File

@ -58,4 +58,16 @@ unsigned int fmt_str(char *dest,const char *src) __THROW;
* return number of copied bytes. */ * return number of copied bytes. */
unsigned int fmt_strn(char *dest,const char *src,unsigned int limit) __THROW; 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 #endif

20
fmt/fmt_fill.c Normal file
View File

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

20
fmt/fmt_pad.c Normal file
View File

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

5
t.c
View File

@ -14,9 +14,14 @@
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
int main(int argc,char* argv[]) { 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; unsigned long len;
char *c=mmap_read("/etc/passwd",&len); char *c=mmap_read("/etc/passwd",&len);
printf("got map %p of len %lu\n",c,len); printf("got map %p of len %lu\n",c,len);
#endif
#if 0 #if 0
char c; char c;
printf("%d\n",buffer_getc(buffer_0,&c)); printf("%d\n",buffer_getc(buffer_0,&c));