modified: myjupyterlab.sh
[GalaxyCodeBases.git] / c_cpp / readscorr / gtools.c
blob29905b257d23bc267f106b97f3683d82691a19cd
1 #include <locale.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdlib.h>
6 // http://c-faq.com/stdio/commaprint.html
7 char *commaprint(unsigned long n)
9 static int comma = '\0';
10 static char retbuf[4*(sizeof(unsigned long)*CHAR_BIT+2)/3/3+1]; // size=30
11 char *p = &retbuf[sizeof(retbuf)-1];
12 int i = 0;
14 if(comma == '\0') {
15 struct lconv *lcp = localeconv();
16 if(lcp != NULL) {
17 if(lcp->thousands_sep != NULL &&
18 *lcp->thousands_sep != '\0')
19 comma = *lcp->thousands_sep;
20 else comma = ',';
24 *p = '\0';
26 do {
27 if(i%3 == 0 && i != 0)
28 *--p = comma;
29 *--p = '0' + n % 10;
30 n /= 10;
31 i++;
32 } while(n != 0);
34 //return p;
35 char *pret = malloc(&retbuf[sizeof(retbuf)]-p); // well, just a tiny leak ...
36 return strcpy(pret,p);