Raised This Month: $ Target: $400
 0% 

[ Solved ] is_user_in_file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 06-22-2012 , 15:24   [ Solved ] is_user_in_file
Reply With Quote #1

That :

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; }
__________________
Pawn ? Useless
Aooka is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-22-2012 , 15:28   Re: [REQ] is_user_in_file
Reply With Quote #2

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.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-22-2012 at 15:29.
Liverwiz is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-22-2012 , 15:36  
Reply With Quote #3

Quote:
Originally Posted by Liverwiz View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-22-2012 at 15:37.
Exolent[jNr] is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-22-2012 , 18:43   Re: [REQ] is_user_in_file
Reply With Quote #4

Quote:
Originally Posted by Exolent[jNr] View Post
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;

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 06-22-2012 at 20:01.
ConnorMcLeod is offline
kramesa
Veteran Member
Join Date: Feb 2011
Location: Brazil
Old 06-23-2012 , 10:51   Re: [REQ] is_user_in_file
Reply With Quote #5

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; }
__________________
kramesa is offline
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 06-24-2012 , 05:50   Re: [REQ] is_user_in_file
Reply With Quote #6

Oh Thanks a lots Exolent it's very helpfull for me and i think a lots of peoples

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
__________________
Pawn ? Useless

Last edited by Aooka; 06-24-2012 at 05:50.
Aooka is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-24-2012 , 05:53   Re: [REQ] is_user_in_file
Reply With Quote #7

Quote:
Originally Posted by Aooka View Post
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 View Post
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 View Post
And to conclude here :
Code:
fgets( FileHandle , line, charsmax( line ) );
Gets the current line that the file handle is pointing to.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 06-24-2012 , 06:15   Re: [REQ] is_user_in_file
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
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 !
__________________
Pawn ? Useless
Aooka is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-24-2012 , 06:28   Re: [REQ] is_user_in_file
Reply With Quote #9

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 06-24-2012 , 06:38   Re: [REQ] is_user_in_file
Reply With Quote #10

Oh ! Now it's good I understood everything of this code ! I could rewrite it with eyes closed

Thanks a lot !

Solved.
__________________
Pawn ? Useless
Aooka 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 06:10.


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