AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [ Solved ] is_user_in_file (https://forums.alliedmods.net/showthread.php?t=188124)

Aooka 06-22-2012 15:24

[ Solved ] is_user_in_file
 
That : :crab:

Code:
( auth[ ] ) // What is auth[ ] ? Why they are this param ? {     new ConfigsDir[ 64 ] , line[ 46 ]; // Why an array with 64 and one with 46 ? Why the name of the variable is ConfigsDir ?     new bool: found; // I understand very fast ... but ...         get_localinfo( "amxx_configsdir" , ConfigsDir , charsmax( ConfigsDir ) );     /*         I reade it : <a href="http://www.amxmodx.org/funcwiki.php?search=get_localinfo&go=search" target="_blank" rel="nofollow noopener">http://www.amxmodx.org/funcwiki.php?...info&go=search</a>         but i do not understand the objective of that. If someone can help me ^^     */     format( ConfigsDir , charsmax( ConfigsDir ) , "%s/vip.ini" , ConfigsDir );     // It is the location of my .ini filesi think its only that ?         new fh; // why the name is fh ?     fh = fopen( ConfigsDir , "r" );     /*         I reade it : <a href="http://www.amxmodx.org/funcwiki.php?go=func&id=92" target="_blank" rel="nofollow noopener">http://www.amxmodx.org/funcwiki.php?go=func&id=92</a>         But why it is a r and not a w ?     */         if( fh ) // If fh = ? what ? I do not understand this variable     {         while( !feof( fh ) ) // hum i think it is okay...         {             fgets( fh , line, charsmax( line ) ); // here nothings                         if( equal( auth , line , strlen( auth ) ) )             {                 found = true;                 break; // why they are a break; ?             }             else             {                 found = false;                 // Why they are no break; ?             }         }     }     fclose( fh ); // It's okay     return found; // Why not return 0; ? }

I think it can help a lots of people other than me.
Thank you in advance all !

The function with no comment :
Code:
is_user_in_file( auth[ ] ) {     new ConfigsDir[ 64 ] , line[ 46 ];     new bool: found;         get_localinfo( "amxx_configsdir" , ConfigsDir , charsmax( ConfigsDir ) );     format( ConfigsDir , charsmax( ConfigsDir ) , "%s/vip.ini" , ConfigsDir );         new fh;     fh = fopen( ConfigsDir , "r" );         if( fh )     {         while( !feof( fh ) )         {             fgets( fh , line, charsmax( line ) );                         if( equal( auth , line , strlen( auth ) ) )             {                 found = true;                 break;             }             else             {                 found = false;             }         }     }     fclose( fh );     return found; }

Liverwiz 06-22-2012 15:28

Re: [REQ] is_user_in_file
 
Code:

while( !feof(fh) && !found )
Code:

found = ( equal( auth , line , strlen( auth ) ) )
Just a few suggestions to keep the code lines down...might actually make it faster. In fact, i'm pretty sure it will make it faster.

Exolent[jNr] 06-22-2012 15:36

Quote:

Originally Posted by Liverwiz (Post 1734001)
Code:

while( !feof(fh) && !found )
Code:

found = ( equal( auth , line , strlen( auth ) ) )
Just a few suggestions to keep the code lines down...might actually make it faster. In fact, i'm pretty sure it will make it faster.

No it won't. Actually makes it slower.

@OP

Code:
( auth[ ] ) // What is auth[ ] ? Why they are this param ?

It is the auth string for the user. Could be a name, SteamID, or IP.

Code:
new ConfigsDir[ 64 ] , line[ 46 ]; // Why an array with 64 and one with 46 ? Why the name of the variable is ConfigsDir ?

64 is for a path size.
46 is the maximum length an auth key can have in the users.ini (see admin.sma)
ConfigsDir is named that way because... it is the location of the configs directory (addons/amxmodx/configs)

Code:
get_localinfo( "amxx_configsdir" , ConfigsDir , charsmax( ConfigsDir ) );     /*         I reade it : <a href="http://www.amxmodx.org/funcwiki.php?search=get_localinfo&go=search" target="_blank" rel="nofollow noopener">http://www.amxmodx.org/funcwiki.php?...info&go=search</a>         but i do not understand the objective of that. If someone can help me ^^     */

It gets the location of addons/amxmodx/configs and stores it in a string.
It is used to avoid hard-coding the string to be that path.

Code:
format( ConfigsDir , charsmax( ConfigsDir ) , "%s/vip.ini" , ConfigsDir );     // It is the location of my .ini filesi think its only that ?

It locates the file in the configs directory and completes the whole path to the file:
addons/amxmodx/configs/vip.ini

Code:
new fh; // why the name is fh ?

fh would mean file handle.

Code:
fh = fopen( ConfigsDir , "r" );     /*         I reade it : <a href="http://www.amxmodx.org/funcwiki.php?go=func&id=92" target="_blank" rel="nofollow noopener">http://www.amxmodx.org/funcwiki.php?go=func&id=92</a>         But why it is a r and not a w ?     */

r = read
w = write

This function looks inside a file for a string, so obviously you'll be reading, not writing.

Code:
if( fh ) // If fh = ? what ? I do not understand this variable

It checks if the file handle is valid and that the file is opened.

Code:
break; // why they are a break; ?

Code:
// Why they are no break; ?

It only stops looping (break) if the string was found in the file.

Code:
return found; // Why not return 0; ?

Because it returns whether or not it found it in the file.

ConnorMcLeod 06-22-2012 18:43

Re: [REQ] is_user_in_file
 
Quote:

Originally Posted by Exolent[jNr] (Post 1734008)
It gets the location of addons/amxmodx/configs and stores it in a string.
It is used to avoid hard-coding the string to be that path.

To complete this and to explain why it shouldn't be hardcoded, it's because administrators can change that path editing core.ini :

Code:

amxx_configsdir        addons/amxmodx/configs

May be you could use :

PHP Code:

is_user_in_cfgfile( const filename[], const authid[ ] )
{
    new 
ConfigsDir64 ] , buffer34 ];
    
    
get_localinfo"amxx_configsdir" ConfigsDir charsmaxConfigsDir ) );
    
    
formatConfigsDir charsmaxConfigsDir ) , "%s/%s" ConfigsDir,  filename);
    
    new 
fh fopenConfigsDir "r" );
    if( 
fh )
    {
        while( !
feoffh ) )
        {
            
fgetsfh buffercharsmaxbuffer ) );
            
trim(buffer);
            
remove_quotes(buffer);
            
            if( 
equalauthid buffer ) )
            {
                
fclosefh );
                return 
1;
            }
        }
        
fclosefh );
    }

    return 
0;



kramesa 06-23-2012 10:51

Re: [REQ] is_user_in_file
 
I use this:

Code:
stock bool:is_admin(id) {     static Trie:tSteamIDs;         if(!tSteamIDs)     {         tSteamIDs = TrieCreate();         new g_configdir[128], f;         get_configsdir(g_configdir, charsmax(g_configdir));         add(g_configdir, charsmax(g_configdir), "/admins.ini");                 if((f = fopen(g_configdir, "rt")))         {             new data[32];             while(!feof(f))             {                 fgets(f, data, charsmax(data));                 trim(data);                 if(data[0])                 {                     TrieSetCell(tSteamIDs, data, 1);                 }             }             fclose(f);         }         else         {             return false;         }     }         new szSteamID[32];     get_user_authid(id, szSteamID, charsmax(szSteamID));         if( TrieKeyExists(tSteamIDs, szSteamID) )     {         return true;     }         return false; }

Aooka 06-24-2012 05:50

Re: [REQ] is_user_in_file
 
Oh Thanks a lots Exolent it's very helpfull for me and i think a lots of peoples :D

I would like just to review a few points like that :

I just want to start with this :
Code:
new ConfigsDir[ 64 ]
You've explained the 64 .. But i do not understand ... So ... Please :)

Here :
Code:
while( !feof( FileHandle ) )

And to conclude here :
Code:
fgets( FileHandle , line, charsmax( line ) );


Thank you in advance.

@Connor: What is the difference with your stock ?
Thanks

Exolent[jNr] 06-24-2012 05:53

Re: [REQ] is_user_in_file
 
Quote:

Originally Posted by Aooka (Post 1734999)
Code:
new ConfigsDir[ 64 ]
You've explained the 64 .. But i do not understand ... So ... Please :)

It's just a string length, you can use whatever you want.
The real length for "addons/amxmodx/configs/vip.ini" is 30 characters, so the size can be 31.
However, since the configs directory is customizable, developers like myself use 64 for path sizes since that is a comfortable maximum for path lengths.

Quote:

Originally Posted by Aooka (Post 1734999)
Here :
Code:
while( !feof( FileHandle ) )

feof() returns whether or not the current file handle has reached the end of the file.
This line continues looping until the end of the file has been reached.

"While we have not reached the end of the file..."

Quote:

Originally Posted by Aooka (Post 1734999)
And to conclude here :
Code:
fgets( FileHandle , line, charsmax( line ) );

Gets the current line that the file handle is pointing to.

Aooka 06-24-2012 06:15

Re: [REQ] is_user_in_file
 
Quote:

Originally Posted by Exolent[jNr] (Post 1735000)
Gets the current line that the file handle is pointing to.

For that :
Code:
fgets( FileHandle , line, charsmax( line ) );
fgets reads a line from a text file... but that's just for what exaclty ?

Thank you for your help. It is unusual to meet people like you ! :bacon!:

Exolent[jNr] 06-24-2012 06:28

Re: [REQ] is_user_in_file
 
It's obvious. The function checks if the user's authorization is in the file.
Well, the only way to do that is to check every line to see if it has that user's authorization.

Aooka 06-24-2012 06:38

Re: [REQ] is_user_in_file
 
Oh ! Now it's good I understood everything of this code ! I could rewrite it with eyes closed :crab:

Thanks a lot !

Solved.


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

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