Raised This Month: $7 Target: $400
 1% 

Solved Contain in .ini dont work


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
The overrated maniac
Member
Join Date: Jun 2021
Location: Argentina
Old 07-29-2021 , 11:55   Contain in .ini dont work
Reply With Quote #1

I have this and when I enter the game and test it says that the .ini file contains the user ip.. and it dosn't, it only contain "asd123 sada1231221 sdsd123".

Also when I put in the file
asd123
asd4321

and print it only says the first line, why?

PHP Code:
                new Path[256];
        
get_configsdir(Pathcharsmax(Path));
        
formatex(Pathcharsmax(Path),"%s/%s"PathgFileName);
    
        if(!
file_exists(Path)){
            return 
PLUGIN_HANDLED;
        }
    
        new 
fopen(Path"rt");
    
        new 
szFileData[160];
    
        while(!
feof(f)){
        
            
fgets(fszFileData50000);
            
trim(szFileData);
            
            if(
contain(szFileDatag_szUserIP)){
                
chatcolor(0"CONTAIN IP");
                return 
PLUGIN_HANDLED;
            }
            else{
                
chatcolor(0"NO CONTAIN");
                return 
PLUGIN_HANDLED;
            }
        }
        
fclose(f); 

Last edited by The overrated maniac; 07-29-2021 at 15:41. Reason: Solved
The overrated maniac is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-29-2021 , 14:48   Re: Contain in .ini dont work
Reply With Quote #2

contain() and containi() return the index of the match. If it does not contain the string, it returns -1 (which will return true if used directly as the condition of an if statement). So, to properly check if a string contains another string, you need to use:

Code:
if( contain() >= 0 )
or
Code:
if( contain() != -1 )
P.S. you are reading the file incorrectly in your loop. You should use fgets() as the while loop condition. Also, you are potentially going to cause an index out of bounds because you are using the wrong value for the third argument of fgets(). You need to use charsmax(szFileData) instead of 50000.
__________________

Last edited by fysiks; 07-29-2021 at 14:51.
fysiks is online now
The overrated maniac
Member
Join Date: Jun 2021
Location: Argentina
Old 07-29-2021 , 15:40   Re: Contain in .ini dont work
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
contain() and containi() return the index of the match. If it does not contain the string, it returns -1 (which will return true if used directly as the condition of an if statement). So, to properly check if a string contains another string, you need to use:

Code:
if( contain() >= 0 )
or
Code:
if( contain() != -1 )
P.S. you are reading the file incorrectly in your loop. You should use fgets() as the while loop condition. Also, you are potentially going to cause an index out of bounds because you are using the wrong value for the third argument of fgets(). You need to use charsmax(szFileData) instead of 50000.
Thanks all of it worked. When I put szFileData[50000] server crashed, I did it with 9000 and works, what is the max lenght?

Edit: testing more I became with a bigger problem.. now I dont have a output, it dosnt put anything in say.

PHP Code:
new Path[256];
        
get_configsdir(Pathcharsmax(Path));
        
formatex(Pathcharsmax(Path),"%s/%s"PathgFileName);
    
        if(!
file_exists(Path)){
            return 
PLUGIN_HANDLED;
        }
    
        new 
fopen(Path"rt");
    
        new 
szFileData[2000];
    
        while(
fgets(fszFileDatacharsmax(szFileData))){
        
            
trim(szFileData);
            
            if(
contain(szFileDatag_szUserIP) != 1){
                
client_print(0print_chat"CONTAIN IP");
                return 
PLUGIN_HANDLED;
            }
            else{
                
client_print(0print_chat"NO CONTAIN");
                return 
PLUGIN_HANDLED;
            }
        }
        
fclose(f); 
Even another message that isnt in the function dont work but I have a client_cmd in this function and it works.. its like the output stop working

Last edited by The overrated maniac; 07-29-2021 at 15:52.
The overrated maniac is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-29-2021 , 16:46   Re: Contain in .ini dont work
Reply With Quote #4

Quote:
Originally Posted by The overrated maniac View Post
Thanks all of it worked. When I put szFileData[50000] server crashed, I did it with 9000 and works, what is the max lenght?
The theoretical max is not really relevant here since it's much larger than you actually need. You should only use a value that make sense for the use case. For example, an IP address can only be 15 characters, at most. So, it depends on what you're storing in this file and how it's formatted. If you are storing IP addresses, you could get away with a size of 18 (to account for the newline and carriage return and the null character) but I would recommend doubling this value.


Quote:
Originally Posted by The overrated maniac View Post
Edit: testing more I became with a bigger problem.. now I dont have a output, it dosnt put anything in say.

PHP Code:
new Path[256];
        
get_configsdir(Pathcharsmax(Path));
        
formatex(Pathcharsmax(Path),"%s/%s"PathgFileName);
    
        if(!
file_exists(Path)){
            return 
PLUGIN_HANDLED;
        }
    
        new 
fopen(Path"rt");
    
        new 
szFileData[2000];
    
        while(
fgets(fszFileDatacharsmax(szFileData))){
        
            
trim(szFileData);
            
            if(
contain(szFileDatag_szUserIP) != 1){
                
client_print(0print_chat"CONTAIN IP");
                return 
PLUGIN_HANDLED;
            }
            else{
                
client_print(0print_chat"NO CONTAIN");
                return 
PLUGIN_HANDLED;
            }
        }
        
fclose(f); 
Even another message that isnt in the function dont work but I have a client_cmd in this function and it works.. its like the output stop working
Don't return in the middle of reading a file because you are creating a memory leak by not properly closing the file every time this code is called.

Also, you are using a value of 1 with contain() which is wrong, you need to use -1.

If you still can't fix it, it will make it much easier to help you if you attach your .sma file.
__________________
fysiks is online now
The overrated maniac
Member
Join Date: Jun 2021
Location: Argentina
Old 07-29-2021 , 17:34   Re: Contain in .ini dont work
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
The theoretical max is not really relevant here since it's much larger than you actually need. You should only use a value that make sense for the use case. For example, an IP address can only be 15 characters, at most. So, it depends on what you're storing in this file and how it's formatted. If you are storing IP addresses, you could get away with a size of 18 (to account for the newline and carriage return and the null character) but I would recommend doubling this value.
The thing is I dont need to only store one IP address.. I need to store houndreds. I thought of fvault or adv vault but it needs a key, and it was the name of the admin who put the ip there. So.. when the admin wasnt online I cant get the value. Thats why I need to use a .ini like a database.
So yes, I think the value 50000 make sense here, or even more. How can I use that value without making the server crash?


Quote:
Originally Posted by fysiks View Post
Don't return in the middle of reading a file because you are creating a memory leak by not properly closing the file every time this code is called.
That fixed the output thing
The overrated maniac is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-29-2021 , 17:56   Re: Contain in .ini dont work
Reply With Quote #6

fgets() only reads a single line at a time so the variable only needs to be as long as the longest single line (plus 3). If that doesn't explain it for you then you need to attach at least an example of this INI file. Also, there is no reason that you can't use nvault or something else. If you can explain better what exactly you are trying to do, we can provide a better solution than using an INI file.
__________________
fysiks is online now
The overrated maniac
Member
Join Date: Jun 2021
Location: Argentina
Old 07-29-2021 , 18:03   Re: Contain in .ini dont work
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
fgets() only reads a single line at a time so the variable only needs to be as long as the longest single line (plus 3).
Thats it, perfect, I thought it was the size of the ini file. Solved.
The overrated maniac is offline
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 00:57.


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