Raised This Month: $32 Target: $400
 8% 

Read file problem.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 12-08-2009 , 18:49   Read file problem.
Reply With Quote #1

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.
ILUSION is offline
Old 12-08-2009, 20:40
joropito
This message has been deleted by joropito.
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-09-2009 , 01:37   Re: Read file problem.
Reply With Quote #2

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)
__________________
Arkshine is offline
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 12-09-2009 , 14:12   Re: Read file problem.
Reply With Quote #3

Thanks Arkshine but now only read the first line.
ILUSION is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-09-2009 , 14:46   Re: Read file problem.
Reply With Quote #4

Show your code
__________________
Arkshine is offline
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 12-09-2009 , 14:46   Re: Read file problem.
Reply With Quote #5

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


Last edited by ILUSION; 12-09-2009 at 15:04.
ILUSION is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-09-2009 , 14:52   Re: Read file problem.
Reply With Quote #6

Remove szLine[0] == '^n'
__________________
Arkshine is offline
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 12-09-2009 , 15:00   Re: Read file problem.
Reply With Quote #7

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


Last edited by ILUSION; 12-09-2009 at 15:03.
ILUSION is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-09-2009 , 15:04   Re: Read file problem.
Reply With Quote #8

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.
__________________
Arkshine is offline
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 12-09-2009 , 15:12   Re: Read file problem.
Reply With Quote #9

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"
ILUSION is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-09-2009 , 15:13   Re: Read file problem.
Reply With Quote #10

So, it works fine. Each line is read without problems.
__________________
Arkshine is offline
Old 12-09-2009, 15:47
ILUSION
This message has been deleted by ILUSION.
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:33.


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