Browse Source

add fmt_pad and fmt_fill.

master
Felix von Leitner 19 years ago
parent
commit
e29be9e016
5 changed files with 58 additions and 1 deletions
  1. +1
    -1
      Makefile
  2. +12
    -0
      fmt.h
  3. +20
    -0
      fmt/fmt_fill.c
  4. +20
    -0
      fmt/fmt_pad.c
  5. +5
    -0
      t.c

+ 1
- 1
Makefile View File

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

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

+ 20
- 0
fmt/fmt_fill.c 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
- 0
fmt/fmt_pad.c 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
- 0
t.c View File

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