Raised This Month: $51 Target: $400
 12% 

Solved Text file organization


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 04-10-2020 , 17:54   Text file organization
Reply With Quote #1

I'm working on a plugin to add admin by menu, but it annoys me the lack of organization when adding the admin, because the file is always written at the end, and not in an organized way, example:

Code:
[ADMIN]
blablabla

[vip]
blabalbal
So I ask, how to do to achieve the goal of, for example, based on the flags inserted the pointer determine the correct location to add the admin in the correct location and no longer at the end of the file.

Below the code I currently use (for testing)

PHP Code:
AddAdmin(const auth[], const accessflags[], const password[], const flags[], const name[], iTime)
{
    new 
szConfig[MAX_FMT_LENGTH]
    
get_configsdir(szConfigcharsmax(szConfig))
    
add(szConfigcharsmax(szConfig), "/users.ini")
    
    new 
iFile fopen(szConfig"a+")
    
    if(
iFile)
    {
        if(
iTime != 0)
        {
            
fprintf(iFile"^n^"%s^" ^"%s^" ^"%s^" ^"%s^" ^"%d^"; %s^"", auth, password, accessflags, flags, iTime, name)
            server_cmd("
amx_reloadadmins")

            fclose(iFile)
        }

        fprintf(iFile, "
^n^"%s^" ^"%s^" ^"%s^" ^"%s^"; %s^""authpasswordaccessflagsflagsname)
        
server_cmd("amx_reloadadmins")

        
fclose(iFile)
    }

__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 04-12-2020 at 11:39.
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-10-2020 , 18:30   Re: Text file organization
Reply With Quote #2

I will show you how to do this.
__________________

Last edited by Bugsy; 04-10-2020 at 18:32.
Bugsy is offline
Old 04-10-2020, 18:30
Bugsy
This message has been deleted by Bugsy.
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-10-2020 , 19:53   Re: Text file organization
Reply With Quote #4

Not thoroughly tested. You must start out with an empty line after each set of users within a group and between groups. Back up your users.ini beforehand, I take no responsibility for lost data.

For example:
Code:
[Owners]
<admin data>
<admin data>
-blank line-
[Noobs]
<admin data>
-blank line-
PHP Code:
AddAdmin"[Owners]" "STEAM" "abc" "pwd" "def" "bugsy1" 555 );
AddAdmin"[Noobs]" "STEAM" "ghi" "pwd" "jkl" "bugsy2" );

//The idea with this is to write each line from users.ini to a temporary file users.ini_tmp, when the current line from users.ini matches the specified group name,
//the plugin will continue looping through until it finds an empty line (under the group name) -- here it will insert the new admin, it will then continue copying lines 
//from users.ini into users.ini_tmp. If a new admin insert occurred, users.ini will be deleted and users.ini_tmp is renamed to users.ini. If no insert occurred, users.ini_tmp is deleted.
AddAdmin( const szAdminGroup[] , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime )
{
    new 
iFile iTempFile szBuffer256 ] , iBracketEndPos szConfigMAX_FMT_LENGTH ] , szTmpConfigMAX_FMT_LENGTH ] , bool:bInGroup bool:bDone;
    
    
//Set full users.ini file into a string
    
get_configsdirszConfig charsmaxszConfig ) );
    
addszConfig charsmaxszConfig ) , "/users.ini" );
    
    
//Create a temp users.ini (users.ini_tmp) file which will eventually turn into users.ini at the end, if an entry occurred.
    
formatexszTmpConfig charsmaxszTmpConfig ) , "%s_tmp" szConfig ); 
    
    
//Open users.ini for read/text
    
if ( ( iFile fopenszConfig "rt" ) ) )
    {
        
//Open temp users.ini for write/text
        
if ( ( iTempFile fopenszTmpConfig "wt" ) ) )
        {
            
//Continue looping through each line of users.ini
            
while ( fgetsiFile szBuffer charsmaxszBuffer ) ) )
            {
                
//No analysis is needed on the line if it begins with a comment character, it will be copied to the temp file as-is.
                
if ( szBuffer] != ';' )
                {
                    
//Locate the end position of a group name within the line. This will be used in the equali() check to 
                    //make it compatible with Windows and Linux.
                    
iBracketEndPos strfindszBuffer "]" );
                    
                    
//IF
                    // - The plugin has not yet added the new admin
                    // - The szAdminGroup[] has not yet been found
                    // - The line of text equals the szAdminGroup. We must subtract one character from the end to remove ^n.
                    
if ( !bDone && !bInGroup && ( iBracketEndPos > -) && equaliszBuffer szAdminGroup iBracketEndPos ) )
                    {
                        
bInGroup true;
                    }
                    
//IF
                    // - The plugin has not yet added the new admin
                    // - The szAdminGroup[] has been found
                    // - The line of text equals new line which means this is an empty area that can be used for writing the new admin.
                    
else if ( !bDone && bInGroup && ( ( szBuffer] == '^r' ) || ( szBuffer] == '^n' ) ) )
                    {
                        
WriteUseriTempFile szAuth szAccessFlags szPassword szAcctFlags szName iTime );
                        
bDone true;
                        continue;
                    }
                }
                
                
//This writes the line from users.ini to temp users.ini. There is only one instance where we do not want the exact line
                //from the original users.ini and this is when inserting the new admin. 
                
fputsiTempFile szBuffer );
            }
            
            
//This indicates we reached the end of the file, the szAdminGroup[] group was found, but a subsequent empty line was not found.
            //When this occurs, the new admin is written to the end of the file. This will occur when the group is at the bottom of users.ini
            
if ( bInGroup && !bDone )
            {
                
log_to_file"AddAdmin" "Adding user to end of file" );
                
WriteUseriTempFile szAuth szAccessFlags szPassword szAcctFlags szName iTime );
                
bDone true;
            }
            
            
//Close both files.
            
fcloseiFile );
            
fcloseiTempFile );
            
            
//If a new admin was inserted; delete the original users.ini, rename the temp users.ini to users.ini, and call reload admins.
            
if ( bDone )
            {
                
delete_fileszConfig );
                
rename_fileszTmpConfig szConfig true );
                
server_cmd"amx_reloadadmins" );
            }
            
//A new admin was not inserted so the temp users.ini is deleted, leaving users.ini unmodified.
            
else
            {
                
delete_fileszTmpConfig );
            }
        }
    }


WriteUseriFile , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime )
{
    new 
szBuffer128 ] , szTime10 ]; 
    
    
//If a time value is specified, format it in a string in the following format (including quotes): "123"
    
if ( iTime 
        
formatexszTime charsmaxszTime ) , "^"%d^"" iTime );
    
    
//Format the entire line, optionally including time if it was a non-zero value. Two new-lines are included on the end.
    
formatexszBuffer charsmaxszBuffer ) , "^"%s^" ^"%s^" ^"%s^" ^"%s^" %s; %s^n^n" szAuth szPassword szAccessFlags szAcctFlags iTime szTime "" szName );
    
    
//Write admin line to file.
    
fputsiFile szBuffer );

__________________

Last edited by Bugsy; 04-12-2020 at 12:17.
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 04-10-2020 , 20:31   Re: Text file organization
Reply With Quote #5

It worked perfectly Bugsy, you as always a genius! I will refine the code further and will soon launch it if it is of interest to the community.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-10-2020 , 20:52   Re: Text file organization
Reply With Quote #6

No problem, it can be enhanced a bit, but should be a good starting point.
__________________
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 04-11-2020 , 10:36   Re: Text file organization
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
No problem, it can be enhanced a bit, but should be a good starting point.
Reopening the post for a strange error... On the windows server everything works normally (personal test server), however when placing on a hosted server (linux base) the .temp file is created (but when opening it is identical to the users.ini) is not destroyed and the admin is not inserted. Reinforcing that in windows works normally, I leave the code below (still BETA version) for better visualization of possible problem.


users.ini

Code:
[Dono]

[Sub Dono]



[Supremo]


[Master]


[Admin]


[Vip]


[Girl]






//[Dono] [m] [bcdefghijklmuv]
//[Sub Dono] [n] [bcdefghijklnuv]
//[Supremo] [o] [bcdefghijklouv]
//[Master] [p] [bcdefghijklpuv]
//[Admin] [q] [bcdefghijklquv]
//[Vip] [r] [bq]
//[Girl] [s] [bcdefghijklsuv]
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 04-12-2020 at 11:38.
iceeedr is offline
Send a message via Skype™ to iceeedr
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 #8

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
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 04-11-2020 , 12:30   Re: Text file organization
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
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 on the original plugin when it was unable to add the new admin users.

I will also add comments the 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[0]=%d" szBuffer[0] );
                
                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 );
            }
        }
    }

Problem persists, except for the .temp file was deleted this time.

PHP Code:
L 04/11/2020 13:27:47Log file started (file "cstrike/addons/amxmodx/logs/AddAdmin") (game "cstrike") (amx "1.10.0.5392")
L 04/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=34
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=91
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=13
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line
L 04
/11/2020 13:27:47Line[0]=47
L 04
/11/2020 13:27:47Writing unchanged line 
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by Bugsy; 04-11-2020 at 12:49.
iceeedr is offline
Send a message via Skype™ to iceeedr
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-11-2020 , 02:25   Re: Text file organization
Reply With Quote #10

I just want to point out that if you are "annoyed" by how file works then you do not understand how they work. You may want to read some tutorials(preferably not from this forum) to learn what a file really is and why it works the way it does.
A file is not an array, you can't directly go to position x in the file and write something.
__________________

Last edited by HamletEagle; 04-11-2020 at 02:26.
HamletEagle is offline
Reply



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 13:43.


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