Check if string contains an IP address
#1

Although I have been developing the same gamemode since 2007, I am actually terrible at maths. This has got me stumped. I need to check if a string contains an IP address. How would I do this? I have found the following:

pawn Code:
#define MAX_NUM 10
#define MY_SERVER_IP "255.255.255.255:7777"
stock ContainsIP(const string[])
{
    new num[MAX_NUM][128];
    new numpos[MAX_NUM];
    new dotcount,i;
    if(strfind(string,MY_SERVER_IP)!=-1)return 0;
    for(new npos=0;npos<=MAX_NUM;npos++)
    {
        new c;
        for(c=0;i<strlen(string);i++)
        {

            new ch = string[i];

            if((string[i] == '.' || string[i] == ':') && ((i>0 && '0' <= string[i-1] <= '9') || (i>1 && '0' <= string[i-2] <= '9') ))
            {
                dotcount++;
                i++;
                break;
            }
            if ((npos==0 || i - numpos[npos - 1] < strlen(num[npos-1])+4) && '0' <= ch <= '9' || (c == 0 && ch == '-'))
            {
                num[npos][c] = string[i];
                numpos[npos] = i;
                c++;
            }


        }

    }
    new found=0;
    for(new npos=0;npos<MAX_NUM;npos++)
    {
        if( strlen(num[npos]) > 0 && 0 < strval(num[npos]) <= 225)
        {
            found++;
        }
    }
    if(found >= 3 && dotcount >= 3)return 1;
    return 0;
}
Should I use that, or does anyone have a better method?
Reply


Messages In This Thread
Check if string contains an IP address - by beckzy - 06.09.2014, 18:07
Re: Check if string contains an IP address - by Evocator - 06.09.2014, 18:52
Re: Check if string contains an IP address - by beckzy - 06.09.2014, 19:04
Re: Check if string contains an IP address - by Stinged - 06.09.2014, 19:55
Re: Check if string contains an IP address - by beckzy - 06.09.2014, 20:46
Re: Check if string contains an IP address - by silenthill - 06.09.2014, 21:15
Re: Check if string contains an IP address - by beckzy - 06.09.2014, 21:46
Re: Check if string contains an IP address - by Isolated - 06.09.2014, 21:53
Re: Check if string contains an IP address - by Vince - 06.09.2014, 22:02
Re: Check if string contains an IP address - by beckzy - 06.09.2014, 22:07

Forum Jump:


Users browsing this thread: 1 Guest(s)