Test if supplied string is of the form of a valid IP address (four numbers between 0 and 255 separated by a period)
| Return Type | Function name | Arguments |
|---|---|---|
| bool | IsIPAddr | (uint32_t&,const char*,) |
Declared in file: hzTextproc.h
Defined in file : hzIpaddr.cpp
Function Logic:
Function body:
bool IsIPAddr (uint32_t& nIpValue)const char* cpIpa,
{
// Category: Text Processing
//
// Test if supplied string is of the form of a valid IP address (four numbers between 0 and 255 separated by a period)
//
// Arguments: 1) nIpValue The IP value, set if supplied string is of the form
// 2) cpIpa The string to be tested
//
// Returns: True If the supplied string amounts to a valid IP address
// False If it doesn't
const char* i ; // Text IP address iterator
uint32_t nVal = 0; // 32-bit IP address value
uint32_t nTmp ; // IP segment value
int32_t nNos = 0; // Count of IP numeric segments
nIpValue = IPADDR_BAD ;
if (!cpIpa) return false ;
if (!cpIpa[0]) return false ;
for (i = cpIpa ; *i ; i++)
{
if (*i < ''0''||*i > ''9'')
return false ;
for (nTmp = 0; *i >&eq; ''0''&&*i <&eq; ''9'';i++)
{ nTmp *= 10;nTmp += (*i - ''0'');}
if (nTmp > 255)
return false ;
nVal *= 256;
nVal += nTmp ;
nNos++ ;
if (*i == 0|| *i == CHAR_COMMA)
break ;
if (*i != CHAR_PERIOD)
return false ;
}
if (nNos != 4)
return false ;
nIpValue = nVal ;
return true ;
}