Sie sind auf Seite 1von 3

//-----------------------------------------------------------------------------

// y Ö ¼ z ParseInt
//
// y à e z Converts input string to integer.
//
// y ø z [i] pszInputString : Input String.
// [o] pNInteger : Integer
//
// y ß è l z returns TRUE(1) on success, otherwise FALSE(0).
//
// y ð z 2010/10/12 Jatin Mistry
//-----------------------------------------------------------------------------
BOOL ParseInt(char* pszInputString, int* pNInteger)
{
int nLength = MAX_DIGITS; //Max digits for integer
s
int i = 0; //Loop v
ariable
int j = 0; //Loop v
ariable
double dblSize = 0; //To store value
from atof()
BOOL bFinish = FALSE; //Boolean to indicate fi
nish of string
char szNumSize[MAX_DIGITS] = {0}; //To store the numbers from stri
ng
//Check input parameters
if(pszInputString == NULL || pNInteger == NULL)
{
return FALSE;
}
szNumSize[j] = PLUS_SIGN;
j++;
//Till End of String not reached...
while(pszInputString[i] != END_OF_STRING && (j < nLength ))
{
//Till we have leading spaces...
while(pszInputString[i] == SPACES || pszInputString[i] == TAB)
{
i++;
}
//If '+/-' sign is present
if(pszInputString[i] == PLUS_SIGN ||
(pszInputString[i] == MINUS_SIGN))
{
j = 0;
szNumSize[j] = pszInputString[i];
i++;
j++;
}
//spaces or tab after sign
while(pszInputString[i] == SPACES || pszInputString[i] == TAB)
{
i++;
}
//If we have leading zeroes
while(pszInputString[i] == ZERO)
{
i++;
}
//If a End of string is found
if(pszInputString[i] == END_OF_STRING)
{
szNumSize[j] = END_OF_STRING;
bFinish = TRUE;
}
//If a Number is not found..
if(bFinish == FALSE &&
(pszInputString[i] > NINE || pszInputString[i] < ONE))
{
printf(INVALID_INTEGER);
return FALSE;
}
//If a Number is found and is in the range...
while((pszInputString[i] <= NINE && pszInputString[i] >= ZERO) &
&
(j < nLength ))
{
//Read the number in szNumSize
szNumSize[j] = pszInputString[i];
j++;
i++;
//If next character is '\0'
if(pszInputString[i] == END_OF_STRING)
{
szNumSize[j] = END_OF_STRING;
break;
}
//If next Charcter is "spaces"
while(pszInputString[i] == SPACES)
{
i++;
//If next characyter is '\0'
if(pszInputString[i] == END_OF_STRING)
{
szNumSize[j] = END_OF_STRING;
break;
}
//Check if next character is also a "space"
if(pszInputString[i] != SPACES)
{
printf(INVALID_INTEGER);
return FALSE;
}
}
}
}
//Convert string to integer
dblSize = atof(szNumSize);
if(dblSize > INTEGER_RANGE)
{
printf(BIG_NUMBER);
return FALSE;
}
*pNInteger = (int)dblSize;
return TRUE;
}

Das könnte Ihnen auch gefallen