2005-02-05 02:12:01 +08:00
|
|
|
/*
|
|
|
|
* Id: $Id: utils.c,v 1.3 2005/02/04 18:12:01 bwalle Exp $
|
|
|
|
* -------------------------------------------------------------------------------------------------
|
2003-08-25 04:04:23 +08:00
|
|
|
*
|
2005-02-05 02:12:01 +08:00
|
|
|
* 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; You may only use
|
|
|
|
* version 2 of the License, you have no option to use any other version.
|
2003-08-25 04:04:23 +08:00
|
|
|
*
|
2005-02-05 02:12:01 +08:00
|
|
|
* 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.
|
2003-08-25 04:04:23 +08:00
|
|
|
*
|
2005-02-05 02:12:01 +08:00
|
|
|
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------------------------------
|
2003-08-25 04:04:23 +08:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BUFSIZ
|
|
|
|
#define BUFSIZ 512
|
|
|
|
#endif
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2003-08-27 04:34:46 +08:00
|
|
|
unsigned long min_array( unsigned long array[], int size )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long min = array[0];
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
for (i = 1; i < size; i++)
|
2003-08-27 04:34:46 +08:00
|
|
|
{
|
2005-02-05 02:12:01 +08:00
|
|
|
if (array[i] < min)
|
2003-08-27 04:34:46 +08:00
|
|
|
{
|
|
|
|
min = array[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2003-08-27 04:34:46 +08:00
|
|
|
unsigned long max_array( unsigned long array[], int size )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long max = array[0];
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
for (i = 1; i < size; i++)
|
2003-08-27 04:34:46 +08:00
|
|
|
{
|
|
|
|
if( array[i] > max )
|
|
|
|
{
|
|
|
|
max = array[i];
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 02:12:01 +08:00
|
|
|
|
2003-08-27 04:34:46 +08:00
|
|
|
return max;
|
|
|
|
}
|
2003-08-25 04:04:23 +08:00
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
char* format_with_thousandssep(char* string, int stringsize, double number, int digits)
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
|
|
|
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 */
|
2005-02-05 02:12:01 +08:00
|
|
|
if (digits < 0 || digits >= 10)
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
|
|
|
digits = 2;
|
|
|
|
}
|
|
|
|
|
2005-02-05 02:12:01 +08:00
|
|
|
snprintf(formatstring, BUFSIZ, "%%.%df", digits);
|
|
|
|
snprintf(buffer, BUFSIZ, formatstring, number);
|
2003-08-25 04:04:23 +08:00
|
|
|
|
|
|
|
/* 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 */
|
2005-02-05 02:12:01 +08:00
|
|
|
while (*bufptr != 0 && *bufptr != localeinfo->decimal_point[0])
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
2005-02-05 02:12:01 +08:00
|
|
|
if (count % grouping == 0 && count != numberOfIntegerChars)
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
2005-02-05 02:12:01 +08:00
|
|
|
for (i = 0; i < strlen( localeinfo->thousands_sep ); i++)
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
|
|
|
*str++ = localeinfo->thousands_sep[i];
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 02:12:01 +08:00
|
|
|
|
2003-08-25 04:04:23 +08:00
|
|
|
*str++ = *bufptr++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the rest */
|
2005-02-05 02:12:01 +08:00
|
|
|
while (digits > 0 && *bufptr != 0)
|
2003-08-25 04:04:23 +08:00
|
|
|
{
|
|
|
|
*str++ = *bufptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* terminate with 0 finally */
|
|
|
|
*str = 0;
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
2005-02-05 02:12:01 +08:00
|
|
|
|