Numeric detection limit on a string. +REP
#5

https://sampforum.blast.hk/showthread.php?tid=568668

PAWN version:
pawn Код:
// ** INCLUDES

#include <a_samp>
#include <sscanf>

// ** DEFINES

// *** FUNCTIONS

#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))

// ** MAIN

main()
{
    print("Loaded \"anti_advert.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{  
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    if(IsAdvertisement(text))
    {
        SendClientMessage(playerid, 0xFF0000FF, "Your message is considered an advertisement, please review it.");
        return 0;
    }
    return 1;
}

// ** FUNCTIONS

forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
    new message[128], extract[2], element[4][4], count_1, count_2, temp, bool:number_next = false, bool:next_number = false, bool:advert = false;
    strcpy(message, text, sizeof(message));

    for(new i = 0, j = strlen(message); i < j; i ++)
    {
        switch(message[i])
        {
            case '0'..'9':
            {
                if(next_number) continue;

                number_next = false;

                strmid(extract, message[i], 0, 1);
                strcat(element[count_1], extract);
               
                count_2 ++;

                if(count_2 == 3 || message[i + 1] == EOS)
                {
                    strmid(extract, message[i + 1], 0, 1);

                    if(IsNumeric(extract))
                    {
                        element[0][0] = EOS;
                        element[1][0] = EOS;
                        element[2][0] = EOS;
                        element[3][0] = EOS;

                        count_1 = 0;
                        count_2 = 0;

                        next_number = true;
                        continue;
                    }

                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;

                            next_number = true;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;

                            next_number = true;
                        }
                    }
                }

                if(count_1 == 4)
                {
                    advert = true;
                    break;
                }
            }
            default:
            {
                next_number = false;

                if(number_next) continue;

                if(!isnull(element[count_1]))
                {
                    temp = strval(element[count_1]);

                    if(count_1 == 0)
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[count_1][0] = EOS;

                            count_2 = 0;
                        }
                    }
                    else
                    {
                        if(temp <= 255)
                        {
                            count_1 ++;
                            count_2 = 0;

                            number_next = true;
                        }
                        else
                        {
                            element[0][0] = EOS;
                            element[1][0] = EOS;
                            element[2][0] = EOS;
                            element[3][0] = EOS;

                            count_1 = 0;
                            count_2 = 0;
                        }
                    }

                    if(count_1 == 4)
                    {
                        advert = true;
                        break;
                    }
                }
            }
        }
    }
    return advert;
}

stock IsNumeric(const string[])
{
    return !sscanf(string, "{d}");
}
Reply


Messages In This Thread
Numeric detection limit on a string. +REP - by Harty - 25.03.2016, 17:48
Re: Numeric detection limit on a string. +REP - by Mencent - 25.03.2016, 17:54
Re: Numeric detection limit on a string. +REP - by SickAttack - 25.03.2016, 17:56
Re: Numeric detection limit on a string. +REP - by Harty - 25.03.2016, 17:58
Re: Numeric detection limit on a string. +REP - by SickAttack - 25.03.2016, 18:01
Re: Numeric detection limit on a string. +REP - by Harty - 25.03.2016, 18:03
Re: Numeric detection limit on a string. +REP - by SickAttack - 25.03.2016, 18:15
Re: Numeric detection limit on a string. +REP - by Harty - 25.03.2016, 18:17
Re: Numeric detection limit on a string. +REP - by Harty - 25.03.2016, 18:30
Re: Numeric detection limit on a string. +REP - by SickAttack - 25.03.2016, 18:35

Forum Jump:


Users browsing this thread: 3 Guest(s)