Skip to content

Instantly share code, notes, and snippets.

@ziggi
Last active October 1, 2018 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziggi/72bed27353ed037cdc45 to your computer and use it in GitHub Desktop.
Save ziggi/72bed27353ed037cdc45 to your computer and use it in GitHub Desktop.
Return 1 if string is ip address else 0
#if !defined IS_IN_RANGE
#define IS_IN_RANGE(%0,%1,%2) (((%0) - ((%1) + cellmin)) < (((%2) + 1) - ((%1) + cellmin)))
#endif
stock IsIpAddress(const string[])
{
new
octet,
j = 100;
for (new i = 0; string[i] != '\0'; i++) {
if (j != 0 && IS_IN_RANGE(string[i], '0', '9')) {
octet += (string[i] - '0') * j;
j /= 10;
} else if (string[i] == '.') {
if (IS_IN_RANGE(octet, 0, 255)) {
j = 100;
octet = 0;
} else {
return 0;
}
} else {
return 0;
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment