AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Contain in .ini dont work (https://forums.alliedmods.net/showthread.php?t=333689)

The overrated maniac 07-29-2021 11:55

Contain in .ini dont work
 
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); 


fysiks 07-29-2021 14:48

Re: Contain in .ini dont work
 
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.

The overrated maniac 07-29-2021 15:40

Re: Contain in .ini dont work
 
Quote:

Originally Posted by fysiks (Post 2754029)
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

fysiks 07-29-2021 16:46

Re: Contain in .ini dont work
 
Quote:

Originally Posted by The overrated maniac (Post 2754031)
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 (Post 2754031)
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.

The overrated maniac 07-29-2021 17:34

Re: Contain in .ini dont work
 
Quote:

Originally Posted by fysiks (Post 2754038)
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 (Post 2754038)
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

fysiks 07-29-2021 17:56

Re: Contain in .ini dont work
 
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.

The overrated maniac 07-29-2021 18:03

Re: Contain in .ini dont work
 
Quote:

Originally Posted by fysiks (Post 2754043)
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.


All times are GMT -4. The time now is 23:41.

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