Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

27 lines
821 B

  1. #include "fmt.h"
  2. /* "foo" -> " foo"
  3. * write padlen-srclen spaces, if that is >= 0. Then copy srclen
  4. * characters from src. Truncate only if total length is larger than
  5. * maxlen. Return number of characters written. */
  6. size_t fmt_pad(char* dest,const char* src,size_t srclen,size_t padlen,size_t maxlen) {
  7. long todo;
  8. char* olddest=dest;
  9. char* max=dest+maxlen;
  10. if ((long)srclen<0 || (long)padlen<0 || (long)maxlen<0) return 0;
  11. todo=(long)(padlen-srclen);
  12. if (dest==0) {
  13. unsigned long sum=srclen>padlen?srclen:padlen;
  14. return sum>maxlen?maxlen:sum;
  15. }
  16. for (; todo>0; --todo) {
  17. if (dest>max) break;
  18. *dest=' '; ++dest;
  19. }
  20. for (todo=(long)(srclen>maxlen?maxlen:srclen); todo>0; --todo) {
  21. if (dest>max) break;
  22. *dest=*src; ++dest; ++src;
  23. }
  24. return (size_t)(dest-olddest);
  25. }