AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read file problem. (https://forums.alliedmods.net/showthread.php?t=111256)

ILUSION 12-08-2009 18:49

Read file problem.
 
Hi I want to make a white list for an antispam. The plugin reads fine the file but only read the last line.

Example of listablanca.cfg:

"127.0.0.1"
"200.0.0.1"

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

#define lista_blanca "listablanca.cfg"

#define MAX 32

new IP[MAX][255]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say""hook_say")
    
    
// Add your code here...
}

public 
plugin_cfg()
    
load_file()

load_file()
{
    if (!
file_exists(lista_blanca))
        
write_file(lista_blanca"; Lista blanca")
    
    new 
szLine[255]

    new 
file fopen(lista_blanca"rt")
    
    while (!
feof(file))
    {
        
szLine[0] = '^0'
        
        
fgets(fileszLinecharsmax(szLine))
        
trim(szLine)
        
        
// Lineas en blanco o comentarios
        
if (szLine[0] == ';' || !szLine[0] || szLine[0] == '^n')
            continue;
        else
        {
            for (new 
0MAXi++)
                
copy(IP[i], 254szLine[i])
        }
    }
    
    
fclose(file)
}

public 
hook_say(id)
{
    new 
text[255]
    
read_args(textcharsmax(text))
    
    for (new 
0MAXi++)
        if (
containi(textIP[i]) != -1)
            return 
0
        
else
            return -
1
    
    
return 0


If I write 200.0.0.1 I can saw the message but If I write 127.0.0.1 the message is blocked.

Anyone can help me?

Thanks.

Arkshine 12-09-2009 01:37

Re: Read file problem.
 
Code:
        else         {             for (new i = 0; i < MAX; i++)                 copy(IP[i], 254, szLine[i])         }

This code is wrong. You loop already with the while() and for each line. Why do you loop again with the same string in the whole array ? The result is the array will be filled with the same string, the last.

You should have only something :

Code:
copy(IP[LineCount++], 254, szLine)

But since you have "" in your config file, you will get double "" ; you need to use remove_quotes() before :

Code:
remove_quotes(szLine) copy(IP[LineCount++], 254, szLine)

ILUSION 12-09-2009 14:12

Re: Read file problem.
 
Thanks Arkshine but now only read the first line.

Arkshine 12-09-2009 14:46

Re: Read file problem.
 
Show your code

ILUSION 12-09-2009 14:46

Re: Read file problem.
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

#define lista_blanca "listablanca.cfg"

#define MAX 32

new IP[MAX][255]
new 
LineCount

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say""hook_say")
    
    
// Add your code here...
}

public 
plugin_cfg()
    
load_file()

load_file()
{
    if (!
file_exists(lista_blanca))
        
write_file(lista_blanca"; Lista blanca")
    
    new 
szLine[255]

    new 
file fopen(lista_blanca"rt")
    
    while (!
feof(file))
    {
        
szLine[0] = '^0'
        
        
fgets(fileszLinecharsmax(szLine))
        
trim(szLine)
        
        
// Lineas en blanco o comentarios
        
if (szLine[0] == ';' || !szLine[0] || szLine[0] == '^n')
            continue;
        else
        {
            
remove_quotes(szLine)
            
copy(IP[LineCount++], 254szLine)
        }
    }
    
    
fclose(file)
}

public 
hook_say(id)
{
    new 
text[255]
    
read_args(textcharsmax(text))
    
    for (new 
0MAXi++)
        if (
containi(textIP[i]) != -1)
            return 
0
        
else
            return -
1
    
    
return 0



Arkshine 12-09-2009 14:52

Re: Read file problem.
 
Remove szLine[0] == '^n'

ILUSION 12-09-2009 15:00

Re: Read file problem.
 
Same problem, only read the first line.

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

#define lista_blanca "listablanca.cfg"

#define MAX 32

new IP[MAX][255]
new 
LineCount

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say""hook_say")
    
    
// Add your code here...
}

public 
plugin_cfg()
    
load_file()

load_file()
{
    if (!
file_exists(lista_blanca))
        
write_file(lista_blanca"; Lista blanca")
    
    new 
szLine[255]

    new 
file fopen(lista_blanca"rt")
    
    while (!
feof(file))
    {
        
szLine[0] = '^0'
        
        
fgets(fileszLinecharsmax(szLine))
        
trim(szLine)
        
        
// Lineas en blanco o comentarios
        
if (szLine[0] == ';' || !szLine[0])
            continue;
        else
        {
            
remove_quotes(szLine)
            
copy(IP[LineCount++], 254szLine)
        }
    }
    
    
fclose(file)
}

public 
hook_say(id)
{
    new 
text[255]
    
read_args(textcharsmax(text))
    
    for (new 
0MAXi++)
        if (
containi(textIP[i]) != -1)
            return 
0
        
else
            return -
1
    
    
return 0



Arkshine 12-09-2009 15:04

Re: Read file problem.
 
I see nothing wrong. Make sure you use the right code.
You can also debug your code by putting a log_amx() after trim() and to see the value of szLine.

ILUSION 12-09-2009 15:12

Re: Read file problem.
 
Quote:

L 12/09/2009 - 17:11:54: [readfile.amxx] ; Lista blanca
L 12/09/2009 - 17:11:54: [readfile.amxx] "200.200.0.1"
L 12/09/2009 - 17:11:54: [readfile.amxx] "192.168.1.1"
L 12/09/2009 - 17:11:54: [readfile.amxx] "127.0.0.1"

Arkshine 12-09-2009 15:13

Re: Read file problem.
 
So, it works fine. Each line is read without problems.


All times are GMT -4. The time now is 03:00.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.