From 5f8d057d39977f66f3e0436dfdf65be87ae22ee9 Mon Sep 17 00:00:00 2001 From: Bernhard Walle Date: Sun, 24 Aug 2003 20:04:23 +0000 Subject: [PATCH] Inital add -- thousands separator for printf (Old svn revision: 293) --- panel-plugin/utils.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ panel-plugin/utils.h | 17 +++++++++ 2 files changed, 108 insertions(+) create mode 100644 panel-plugin/utils.c create mode 100644 panel-plugin/utils.h diff --git a/panel-plugin/utils.c b/panel-plugin/utils.c new file mode 100644 index 0000000..dcb99e7 --- /dev/null +++ b/panel-plugin/utils.c @@ -0,0 +1,91 @@ +/* XFce 4 - Netload Plugin + * Copyright (c) 2003 Bernhard Walle + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifndef BUFSIZ +#define BUFSIZ 512 +#endif + + +char* format_with_thousandssep( char* string, int stringsize, double number, int digits ) +{ + char* str = string; + char buffer[BUFSIZ], formatstring[BUFSIZ]; + char* bufptr = buffer; + unsigned int i; + int numberOfIntegerChars, count; + struct lconv* localeinfo = localeconv(); + int grouping = (int)localeinfo->grouping[0] == 0 ? INT_MAX : (int)localeinfo->grouping[0]; + + + /* sensible value for digits */ + if( digits < 0 || digits >= 10 ) + { + digits = 2; + } + + snprintf( formatstring, BUFSIZ, "%%.%df", digits ); + snprintf( buffer, BUFSIZ, formatstring, number ); + + /* get the number of integer characters */ + count = numberOfIntegerChars = ( digits > 0 + ? ( strstr( buffer, localeinfo->decimal_point ) - buffer ) + : strlen( buffer ) ); + + + /* check for length */ + if( numberOfIntegerChars / grouping + (int)strlen( buffer ) > stringsize ) + { + return NULL; + } + + + /* insert the thousands separator */ + while( *bufptr != 0 && *bufptr != localeinfo->decimal_point[0] ) + { + if( count % grouping == 0 && count != numberOfIntegerChars ) + { + for( i = 0; i < strlen( localeinfo->thousands_sep ); i++ ) + { + *str++ = localeinfo->thousands_sep[i]; + } + } + *str++ = *bufptr++; + count--; + } + + /* Copy the rest */ + while( digits > 0 && *bufptr != 0 ) + { + *str++ = *bufptr++; + } + + /* terminate with 0 finally */ + *str = 0; + + return string; +} diff --git a/panel-plugin/utils.h b/panel-plugin/utils.h new file mode 100644 index 0000000..fc39b34 --- /dev/null +++ b/panel-plugin/utils.h @@ -0,0 +1,17 @@ + +#ifndef UTILS_H +#define UTILS_H + +/** + * Formats the number according the current locale with thousands separator. E.g. + * 1234.5678 is formated in a German locale with digits=2 to 1.234,57. If the size + * is too small, NULL is returned and the string contains garbage. + * @param string a character array in which the result is stored + * @param stringsize the size of the character array + * @param number the number that should be formated + * @param digits the number of digits after the decimal point + * @return the string to allow concatening buffers or null + */ +char* format_with_thousandssep( char* string, int stringsize, double number, int digits ); + +#endif /* UTILS_H */