add support for official SI prefixes in numeric option values

Robert P. J. Day rpjday at mindspring.com
Sun Mar 13 15:10:16 EST 2005


  sorry for the delay, but i did promise that i would whip something
up to support the official SI suffixes as defined at:

http://physics.nist.gov/cuu/Units/binary.html

  rather than using atoi() or strtol() for parsing, use the following
routine (obviously, this is pretty simplified -- extra processing or
error-checking can be added easily):

////////////////////////////////////////////////////////
//
//  Parse unsigned integer, supporting SI suffixes.
//
//  Suffixes recognized:
//
//	[kK]i:	1 kilo
//	[mM]i:	1 mega
//	[gG]i:	1 giga
//
////////////////////////////////////////////////////////

unsigned long int
atouli(const char *nptr)
{
	unsigned long uli = 0 ;

	char *suffixptr ;

	uli = strtoul(nptr, &suffixptr, 0) ;

	if (!strcmp(suffixptr, "ki") || !strcmp(suffixptr, "Ki"))
		uli *= 1024 ;
	else if (!strcmp(suffixptr, "mi") || !strcmp(suffixptr, "Mi"))
		uli *= (1024 * 1024) ;
	else if (!strcmp(suffixptr, "gi") || !strcmp(suffixptr, "Gi"))
		uli *= (1024 * 1024 * 1024) ;

	return uli ;
}





More information about the linux-mtd mailing list