Thread: [Solved] Text file organization
View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-11-2020 , 12:17   Re: Text file organization
Reply With Quote #9

Please run this version which will log the steps so we can figure out why it's not working. Also, I fixed the issue where it was not deleting the temp file. This would occur when it was unable to add the new admin user.

I added comments to the above code so you can understand what is going on, since the point of this forum is to help not give.

PHP Code:
AddAdmin( const szAdminGroup[] , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime )
{
    new 
iFile iTempFile iLineLen szBuffer256 ] , szConfigMAX_FMT_LENGTH ] , szTmpConfigMAX_FMT_LENGTH ] , bool:bInGroup bool:bDone;
    
    
get_configsdirszConfig charsmaxszConfig ) );
    
addszConfig charsmaxszConfig ) , "/users.ini" );
    
formatexszTmpConfig charsmaxszTmpConfig ) , "%s_tmp" szConfig ); 
    
    if ( ( 
iFile fopenszConfig "rt" ) ) )
    {
        if ( ( 
iTempFile fopenszTmpConfig "wt" ) ) )
        {
            while ( ( 
iLineLen fgetsiFile szBuffer charsmaxszBuffer ) ) ) )
            {
                
log_to_file"AddAdmin" "Line=[%s]" szBuffer );
                
                if ( !
bDone && !bInGroup && equaliszBuffer szAdminGroup iLineLen ) )
                {
                    
log_to_file"AddAdmin" "In Group = true" );
                    
bInGroup true;
                }
                else if ( !
bDone && bInGroup && ( szBuffer] == '^n' ) )
                {
                    
log_to_file"AddAdmin" "Adding new user" );
                    
WriteUseriTempFile szAuth szAccessFlags szPassword szAcctFlags szName iTime );
                    
bDone true;
                    continue;
                }
                
                
//log_to_file( "AddAdmin" , "Writing unchanged line" );
                
fputsiTempFile szBuffer );
            }
            
            if ( 
bInGroup && !bDone )
            {
                
log_to_file"AddAdmin" "Adding user to end of file" );
                
WriteUseriTempFile szAuth szAccessFlags szPassword szAcctFlags szName iTime );
                
bDone true;
            }
            
            
fcloseiFile );
            
fcloseiTempFile );
            
            if ( 
bDone )
            {
                
delete_fileszConfig );
                
rename_fileszTmpConfig szConfig true );
                
server_cmd"amx_reloadadmins" );
            }
            else
            {
                
delete_fileszTmpConfig );
            }
        }
    }

__________________

Last edited by Bugsy; 04-11-2020 at 12:51.
Bugsy is offline