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.
 
 
 
 

89 lines
3.4 KiB

  1. #ifndef FMT_H
  2. #define FMT_H
  3. /* for time_t: */
  4. #include <sys/types.h>
  5. #define FMT_LONG 41 /* enough space to hold -2^127 in decimal, plus \0 */
  6. #define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
  7. #define FMT_8LONG 44 /* enough space to hold 2^128 - 1 in octal, plus \0 */
  8. #define FMT_XLONG 33 /* enough space to hold 2^128 - 1 in hexadecimal, plus \0 */
  9. #define FMT_LEN ((char *) 0) /* convenient abbreviation */
  10. /* The formatting routines do not append \0!
  11. * Use them like this: buf[fmt_ulong(buf,number)]=0; */
  12. /* convert signed src integer -23 to ASCII '-','2','3', return length.
  13. * If dest is not NULL, write result to dest */
  14. unsigned int fmt_long(char *dest,signed long src);
  15. /* convert unsigned src integer 23 to ASCII '2','3', return length.
  16. * If dest is not NULL, write result to dest */
  17. unsigned int fmt_ulong(char *dest,unsigned long src);
  18. /* convert unsigned src integer 0x23 to ASCII '2','3', return length.
  19. * If dest is not NULL, write result to dest */
  20. unsigned int fmt_xlong(char *dest,unsigned long src);
  21. /* convert unsigned src integer 023 to ASCII '2','3', return length.
  22. * If dest is not NULL, write result to dest */
  23. unsigned int fmt_8long(char *dest,unsigned long src);
  24. unsigned int fmt_longlong(char *dest,signed long long src);
  25. unsigned int fmt_ulonglong(char *dest,unsigned long long src);
  26. unsigned int fmt_xlonglong(char *dest,unsigned long long src);
  27. #define fmt_uint(dest,src) fmt_ulong(dest,src)
  28. #define fmt_int(dest,src) fmt_long(dest,src)
  29. #define fmt_xint(dest,src) fmt_xlong(dest,src)
  30. #define fmt_8int(dest,src) fmt_8long(dest,src)
  31. /* Like fmt_ulong, but prepend '0' while length is smaller than padto.
  32. * Does not truncate! */
  33. unsigned int fmt_ulong0(char *,unsigned long src,unsigned int padto);
  34. #define fmt_uint0(buf,src,padto) fmt_ulong0(buf,src,padto)
  35. /* convert src double 1.7 to ASCII '1','.','7', return length.
  36. * If dest is not NULL, write result to dest */
  37. unsigned int fmt_double(char *dest, double d,int max,int prec);
  38. /* if src is negative, write '-' and return 1.
  39. * if src is positive, write '+' and return 1.
  40. * otherwise return 0 */
  41. unsigned int fmt_plusminus(char *dest,int src);
  42. /* if src is negative, write '-' and return 1.
  43. * otherwise return 0. */
  44. unsigned int fmt_minus(char *dest,int src);
  45. /* copy str to dest until \0 byte, return number of copied bytes. */
  46. unsigned long fmt_str(char *dest,const char *src);
  47. /* copy str to dest until \0 byte or limit bytes copied.
  48. * return number of copied bytes. */
  49. unsigned long fmt_strn(char *dest,const char *src,unsigned long limit);
  50. /* "foo" -> " foo"
  51. * write padlen-srclen spaces, if that is >= 0. Then copy srclen
  52. * characters from src. Truncate only if total length is larger than
  53. * maxlen. Return number of characters written. */
  54. unsigned long fmt_pad(char* dest,const char* src,unsigned long srclen,unsigned long padlen,unsigned long maxlen);
  55. /* "foo" -> "foo "
  56. * append padlen-srclen spaces after dest, if that is >= 0. Truncate
  57. * only if total length is larger than maxlen. Return number of
  58. * characters written. */
  59. unsigned long fmt_fill(char* dest,unsigned long srclen,unsigned long padlen,unsigned long maxlen);
  60. /* 1 -> "1", 4900 -> "4.9k", 2300000 -> "2.3M" */
  61. unsigned int fmt_human(char* dest,unsigned long long l);
  62. /* 1 -> "1", 4900 -> "4.8k", 2300000 -> "2.2M" */
  63. unsigned int fmt_humank(char* dest,unsigned long long l);
  64. /* "Sun, 06 Nov 1994 08:49:37 GMT" */
  65. unsigned int fmt_httpdate(char* dest,time_t t);
  66. #endif