From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/commafmt.c | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/commafmt.c (limited to 'reference/C/CONTRIB/SNIP/commafmt.c') diff --git a/reference/C/CONTRIB/SNIP/commafmt.c b/reference/C/CONTRIB/SNIP/commafmt.c new file mode 100755 index 0000000..b5d24f7 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/commafmt.c @@ -0,0 +1,82 @@ +/* +** COMMAFMT.C +** +** Public domain by Bob Stout +** +** Notes: 1. Use static buffer to eliminate error checks on buffer overflow +** and reduce code size. +** 2. By making the numeric argument a long and prototyping it before +** use, passed numeric arguments will be implicitly cast to longs +** thereby avoiding int overflow. +** 3. Use the thousands grouping and thousands separator from the +** ANSI locale to make this more robust. +*/ + +#include +#ifdef TEST + #include +#endif + +#define NUL '\0' + +size_t commafmt(char *buf, /* Buffer for formatted string */ + int bufsize, /* Size of buffer */ + long N) /* Number to convert */ +{ + int len = 1, posn = 1, sign = 1; + char *ptr = buf + bufsize - 1; + + if (2 > bufsize) + { +ABORT: *buf = NUL; + return 0; + } + + *ptr-- = NUL; + --bufsize; + if (0L > N) + { + sign = -1; + N = -N; + } + + for ( ; len <= bufsize; ++len, ++posn) + { + *ptr-- = (char)((N % 10L) + '0'); + if (0L == (N /= 10L)) + break; + if (0 == (posn % 3)) + { + *ptr-- = ','; + ++len; + } + if (len >= bufsize) + goto ABORT; + } + + if (0 > sign) + { + if (0 == bufsize) + goto ABORT; + *ptr-- = '-'; + ++len; + } + + strcpy(buf, ++ptr); + return (size_t)len; +} + +#ifdef TEST + +void main(int argc, char *argv[]) +{ + size_t len; + char buf[20]; + long N; + + N = strtol(argv[1], NULL, 10); + len = commafmt(buf, 20, N); + printf("%s converts to %s and returned %d\n", argv[1], buf, len); +} + +#endif -- cgit v1.2.3-54-g00ecf