add fmt_pad and fmt_fill.

master
leitner 24 years ago
parent 84cd4773f4
commit 6629de6768

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

12
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

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

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

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

Loading…
Cancel
Save