/***************************************************************************
                          astoi.c  -  description
                             -------------------
    begin                : dom ago 25 2002
    copyright            : (C) 2002 by deabru
    email                : deabru@nubbis.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double astod (char string[])
{
/* convert Array with Suffix TO Double (ASTOD) */

	double result = 0;
	int control, coma, i;


	i=0;
	control=1;
	result = 0;
	coma = 0;
    while(i<strlen(string) && control)
    {
			if (string[i] <= '9' && string[i] >='0')
			{
				if (!coma)
				{
					result = (string[i] - '0') + result*10;
					i++;
				}
				else
				{
					result = result +((double)(string[i] - '0')) / ( pow(10,coma));
					coma++;
					i++;
				}
			}
			else
			{
				if ((string[i] =='.' || string[i] == ',' )&& !coma )
				{
					coma = 1;
					i++;
				}
				else
				{
					switch (string[i])
					{
						case 'g':
						case 'G':
						result = result * 1024;
	
						case 'm':
						case 'M':
						result = result * 1024;

						case 'k':
						case 'K':
						result = result * 1024;

						case 'b':
						case 'B':
						result = result * 1;
						control = 0;
						break;
						
						default:
						control =0;
						result = -1;
						break;
					}
				}
			}
		}
	

    return result;
}