AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Remove line from file (https://forums.alliedmods.net/showthread.php?t=336304)

Supremache 02-10-2022 13:11

Remove line from file
 
Hello alliedmoders :),
As what i typed in the title, iam try to remove line from the file.
I created a vip system and i want to add option for remove who has date expired and i have searched for how to do that and found some ways and i used one that recommend by HamletEagle but i got an issue and don't know how to fix it

PHP Code:

RemoveExpired( const szIdentity[ ] )
{
    new const 
szTempFileName[ ] = "tempfile.ini";

    new 
szFormat128 ], szData192 ], szIdentity2MAX_NAME_LENGTH ];
    
    new 
szTempFilePath64 ];
    
formatexszTempFilePathcharsmaxszTempFilePath ), "%s/%s"g_szConfigsszTempFileName );
    
formatexszFormatcharsmaxszFormat ), "%s/%s"g_szConfigsg_iAccountFile );
    
    new 
iFilePointer fopenszFormat"rt" );
    
    if( 
iFilePointer )
    {
        new 
iInputFilePointer fopenszTempFilePath"wt" )
        if( 
iInputFilePointer )
        {
            while( 
fgetsiFilePointerszDatacharsmaxszData ) ) )
            {
                if( 
szData] != EOS )
                {
                    
parseszDataszIdentity2charsmaxszIdentity2 ) );
                    
trimszIdentity2 );
                    
                    if( !
equaliszIdentityszIdentity2 ) ) 
                    {
                        
fprintfiInputFilePointerszData );
                    }
                }
            }
            
fcloseiInputFilePointer );
            
fcloseiFilePointer );
            
delete_fileszFormat ); 
            
rename_fileszTempFilePathszFormat);
        }
    }
    return 
0;


After creating the new file with remove the line of the user who expired the file does not deleted or renamed

Another question: Is it possible to do that by using "TrieDeleteKey" ?

fysiks 02-10-2022 23:46

Re: Remove line from file
 
If you're trying to remove entries based on the timestamp, maybe consider using nvault since it has a built in mechanism to prune the data based on timestamps.

Supremache 02-11-2022 12:18

Re: Remove line from file
 
Quote:

Originally Posted by fysiks (Post 2771109)
If you're trying to remove entries based on the timestamp, maybe consider using nvault since it has a built in mechanism to prune the data based on timestamps.

No, i created a vip system works like "Name|SteamID" "Pw" "Flags" "Expire Date", if the user has expired will lost his access but i try to add new option for remove the user from the file when expired.

I tried to use stock that i mentioned in the first post:
Code:
ReloadFile( ) {     TrieClear( g_tDatabase );         new g_szFile[ 128 ]         formatex( g_szFile, charsmax( g_szFile ), "%s/%s", g_szConfigs, g_iAccountFile )         new iFile = fopen( g_szFile, "rt" );         if( iFile )     {         new szData[ 512 ]                 while( fgets( iFile, szData, charsmax( szData ) ) )         {                 trim( szData );                         switch( szData[ 0 ] )             {                 case EOS, ';',  '#', '/':                 {                     continue;                 }                 default:                 {                     if                     (                         parse                         (                             szData, eData[ Player_Name ]        , charsmax( eData[ Player_Name ] ),                             eData[ Player_Password ]        , charsmax( eData[ Player_Password ] ),                             eData[ Player_AccessFlags ]     , charsmax( eData[ Player_AccessFlags ] ),                             eData[ Player_Expire_Date ]     , charsmax( eData[ Player_Expire_Date ] )                         ) < 4                     )                     {                         continue;                     }                                         if( eData[ Player_Expire_Date ][ 0 ] )                     {                         if( HasDateExpired( eData[ Player_Expire_Date ] ) )                         {                             eData[ Player_Suspended ] = true;                             RemoveExpired( eData[ Player_Name ] );                         }                     }                                         if( eData[ Player_Name ][ 0 ] )                     {                         TrieSetArray( g_tDatabase, eData[ Player_Name ], eData, sizeof eData );                     }                                         arrayset( eData, 0, sizeof( eData ) );                 }             }         }         fclose( iFile );     } }

It's work like create a new file without line of the user who expired and delete old file and rename the new file but as you see the old file didn't deleted etc..
https://i.ibb.co/6BLJfHs/Test.png

I'm not sure if i can do that using "TrieDeleteKey" but tried and it did not work:
PHP Code:

CheckPlayerVIPid )
{
    new 
szPassword18 ];

    
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
    if( 
TrieGetArrayg_tDatabaseg_iPlayerid ][ AuthID ], eDatasizeof eData ) || TrieGetArrayg_tDatabaseg_iPlayerid ][ Name ], eDatasizeof eData ) )
    {
        if( ( 
eDataPlayer_Password ][ ] && equaleDataPlayer_Password ], szPassword ) ) || !eDataPlayer_Password ][ ] )
        {
            if( 
eDataPlayer_Suspended ]  && ! eDataPlayer_AccessFlags ][ ] )
            {
                
g_iPlayerid ][ VIP ] = 0;
                
g_iPlayerid ][ VIP ] = read_flags"z" );
                
TrieDeleteKeyg_tDatabaseeDataPlayer_Name ] )
                return 
PLUGIN_HANDLED;
            }
            else
            {
                
g_iPlayerid ][ VIP ] |= read_flagseDataPlayer_AccessFlags ] );
                return 
PLUGIN_HANDLED;
            }
        }
        else if( 
eDataPlayer_Password ][ ] && ! equaleDataPlayer_Password ], szPassword ) )
        {
            
server_cmd"kick #%d ^"You have no entry to this server^""get_user_useridid ) );   
        }
    }
    
    return 
PLUGIN_CONTINUE;



OciXCrom 02-11-2022 14:43

Re: Remove line from file
 
TrieDeleteKey() will remove the item from the trie, not the actual file. You must recreate the entire file in order to remove a line.

Not sure what's exactly wrong in your code, but I can offer you a "cleaner" way of doing it.

Instead of making a temporary file, renaming and deleting it, store all lines of your file in a dynamic array when reading it, EXCEPT the lines that are already expired.

Then, after closing the file, open it again in write mode, loop through the array and write all lines. Pseudo-code:

Code:
new szFile[256] get_configsdir(szFile, charsmax(szFile)) add(szFile, charsmax(szFile), "/YourFile.ini") // cleaner way with only 1 variable new iFilePointer = fopen(szFile, "r") // open the file in "read" mode if(iFilePointer) {     const MAX_LINE_LENGTH = 256     new Array:aArray = ArrayCreate(MAX_LINE_LENGTH)     new iLinesInFile // this will count the number of non-expired lines in the file when reading     new bool:bShouldRewrite // this will determine whether the file should be rewritten     new szData[MAX_LINE_LENGTH]     while(!feof(iFilePointer))     {         fgets(iFilePointer, szData, charsmax(szData))         trim(szData)         if(/* line is expired */)         {             // at least one line has expired - entire file needs to be rewritten in order to remove the line(s)             bShouldRewrite = true         }         else         {             // store the non-expired line in the array             iLinesInFile++             ArrayPushString(aArray, szData)         }     }     fclose(iFilePointer)     if(bShouldRewrite)     {         iFilePointer = fopen(szFile, "w") // open the file in "write" mode         for(new i; i < iLinesInFile; i++) // loop through the array and write each line in the file         {             fprintf(iFilePointer, "%a^n", ArrayGetStringHandle(aArray, i)) // %a is used to get a string directly from a dynamic array         }         fclose(iFilePointer)     }     ArrayDestroy(aArray) }

Supremache 02-11-2022 20:42

Re: Remove line from file
 
I tested your code but i got a warrning message while complie the plugin in this line:
PHP Code:

fprintf(iFilePointer"%a^n"ArrayGetStringHandle(g_aFileContentsi)) 

I changed g_aFileContents to aArray but i get this error and the file rewrite as empty without store the data.
Quote:

L 02/12/2022 - 03:26:16: Invalid index 3 (count: 3)
L 02/12/2022 - 03:26:16: [AMXX] Displaying debug trace (plugin "VipSystem.amxx", version "1.0.3")
L 02/12/2022 - 03:26:16: [AMXX] Run time error 10: native error (native "ArrayGetStringHandle")
L 02/12/2022 - 03:26:16: [AMXX] [0] VIPSystem.sma::ReloadFile (line 494)
L 02/12/2022 - 03:26:16: [AMXX] [1] VIPSystem.sma::plugin_init (line 96)

OciXCrom 02-12-2022 07:27

Re: Remove line from file
 
Try removing the +1 in the loop.
Code updated.

Supremache 02-12-2022 09:33

Re: Remove line from file
 
Quote:

Originally Posted by OciXCrom (Post 2771250)
Try removing the +1 in the loop.
Code updated.

It's work like delete all lines except for who are not expired but i think it is not helpful because if someone put this symbol ; for disabling something will be deleted too.

Exmaple:
Quote:

#Confing file

"Pro1" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2018 00:00:00"
"Pro2" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2019 00:00:00"
;"Pro3" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2020 00:00:00"
"Pro4" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2021 00:00:00"
"Pro5" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2022 00:00:00"
"Pro6" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2023 00:00:00"
;"Pro7" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2026 00:00:00"
:arrow:
Quote:

"Pro5" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2022 00:00:00"
"Pro6" "123+" "abcdefghijklmnopqrstuvwxy" "12/31/2023 00:00:00"
Is there another way to do that ? or what is the wrong i did in stock that i mentioned in the first post

OciXCrom 02-12-2022 09:56

Re: Remove line from file
 
Simply check for that symbol and make an exception?... You already have a code that checks if it is a comment - store the comments in the array too.

Supremache 02-12-2022 17:32

Re: Remove line from file
 
Quote:

Originally Posted by OciXCrom (Post 2771263)
Simply check for that symbol and make an exception?... You already have a code that checks if it is a comment - store the comments in the array too.

I did but i checked five symbols also using array with trie in the same function i think this is not good, anyway i known a new thing.
Thank you :)


All times are GMT -4. The time now is 11:43.

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