Raised This Month: $ Target: $400
 0% 

[Req] Plugins.ini check & rewrite


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 07-25-2016 , 09:41   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #1

Quote:
Originally Posted by Adryyy View Post
Really thanks addons. I did not understand you never phase with files. I understand your method, but if I will delete and rewrite plugins.ini, the rest of it won't be him deleted?

BUT. Is a problem(with me, i think). Just look > https://vid.me/xbhw
So,
If your first line contains what you want to, "I do not want it on the first line", it will remove that line and put "What you want there".

But looks like it is:
If your first line not contains what you want to, "I want it on the first line", it will remove that line and put "What you want there".

Then just change this:
Code:
            if( currentLineIndex == indexOfTheLineToFind
                && containi( currentLineStringContents, textToFind ) > -1 )
-->
Code:
            if( currentLineIndex == indexOfTheLineToFind
                && containi( currentLineStringContents, textToFind ) < 0 )
And if you want to not delete the last first file:
Code:
            {
                fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
            }
-->
Code:
            {
                fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
Quote:
Originally Posted by Adryyy View Post
but if I will delete and rewrite plugins.ini, the rest of it won't be him deleted?
No, I copied all the contents of the original file to the new file.


Update:
I bad named the variable 'indexOfTheLineToFind', this more proper name is 'indexOfTheLineToFind'.
I will fix it on the code.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 07-25-2016 at 10:43. Reason: misspellings
addons_zz is offline
Adryyy
Member
Join Date: Oct 2011
Old 07-25-2016 , 10:23   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #2

Eh, for me no matter how it is named variables, just go. And i have 1 question :

If file 'TEXT_TO_FIND_ON_THE_LINE' not exists in plugins.ini ??. And 'TEMPORARY_FILE_PATH' is necessary?...
And find in the line, what line? Just 1 line?...

Line 97 - error :

DEFAULT:
fprintf( temporaryFileDescriptor, "%s", currentLineString );
TO:
fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );

See > https://vid.me/sXVi

Last edited by Adryyy; 07-25-2016 at 10:56.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 07-25-2016 , 10:32   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #3

Quote:
Originally Posted by Adryyy View Post
Eh, for me no matter how it is named variables, just go. And i have 1 question :
The variables naming are very important/helpful to understand the code, help to develop and make it properly work. At least, on my opinion.

Quote:
Originally Posted by Adryyy View Post
If file 'TEXT_TO_FIND_ON_THE_LINE' not exists in plugins.ini ??. And 'TEMPORARY_FILE_PATH' is necessary?...
That is not the right question. But as the algorithm is now, yes. The variable 'TEMPORARY_FILE_PATH' is necessary, despite the text exists or not.
But you can create a new algorithm which does not require the variable 'TEMPORARY_FILE_PATH' when the 'TEXT_TO_FIND_ON_THE_LINE' does not exists in 'plugins.ini'.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 07-25-2016 at 10:44. Reason: misspellings
addons_zz is offline
Adryyy
Member
Join Date: Oct 2011
Old 07-25-2016 , 10:57   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #4

Quote:
Originally Posted by addons_zz View Post
But you can create a new algorithm which does not require the variable 'TEMPORARY_FILE_PATH' when the 'TEXT_TO_FIND_ON_THE_LINE' does not exists in 'plugins.ini'.
Yes..i very can )). Maybe you, not me. So, if you can do this, please. AAAA read my last reply

EDIT: LOOK > https://vid.me/e0cD

Last edited by Adryyy; 07-25-2016 at 12:27.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 07-25-2016 , 13:02   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #5

Quote:
Originally Posted by Adryyy View Post
Yes..i very can )). Maybe you, not me. So, if you can do this, please. AAAA read my last reply

EDIT: LOOK > https://vid.me/e0cD
It will only replace that line, if and only if it does not find 'testX.amxx'.
But as it was on the first line, it does nothing.
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective
addons_zz is offline
Adryyy
Member
Join Date: Oct 2011
Old 07-25-2016 , 13:50   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #6

Sorry for bother you so much, and once again thanks for your help. Now it's ok, but another problem, when I give the ' replace ' this let too many spaces. You can fix line after replace?


Code:
/** AMX Mod X Script
 *  
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or ( at
 *  your option ) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***********************************************************
 */

/**
 * Necessary modules.
 */
#include <amxmodx>

/**
 * The maximum line acceptable when loading text files.
 */
#define MAX_LINE_LENGHT_ACCEPTABLE 512


/**
 * Called just after server activation.
 * 
 * Good place to initialize most of the plugin, such as registering
 * cvars, commands or forwards, creating data structures for later use, or
 * generating and loading other required configurations.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_init()
{
    register_plugin( "Laughing Line Replacer", "1.0", "Addons zz" );
}

/**
 * Called when all plugins went through plugin_init()
 * 
 * When this forward is called, most plugins should have registered their
 * cvars and commands already.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_cfg()
{
    // The text which will be used by the trigger the replacement.
    new const TEXT_TO_FIND_ON_THE_LINE[] = "TEST";
    
    // The text to replace after a successful find.
    new const TEXT_TO_REPLACE_ON_SUCCESS[] = "TESTX";
    
    // The file path to perform the in-place replacement.
    new const TEXT_FILE_PATH_FOR_REPLACEMENT[] = "addons\amxmodx\configs\plugins.ini";
    
    replace_line_when( 1, TEXT_FILE_PATH_FOR_REPLACEMENT, TEXT_TO_FIND_ON_THE_LINE, TEXT_TO_REPLACE_ON_SUCCESS );
}

/**
 * To do a in-place file line replacement.
 * 
 * @param indexOfTheLineToFind     specifies on which line, starting on 1, it will be done the text replacement.
 * @param textFilePath[]          the relative file path using the moddir as a base directory.
 * @param textToFind[]            the text to search.
 * @param textToReplace[]         the text to replace.
 * 
 * @return 0 on success and -1 on failure.
 */
stock replace_line_when( const indexOfTheLineToFind, const textFilePath[] , const textToFind[], const textToReplace[] )
{
    new const TEMPORARY_FILE_PATH[]   = "LaughingMyAssOffTemporarily.txt";
    new       textFileDescriptor      = fopen( textFilePath, "rt" );
    new       temporaryFileDescriptor = fopen( TEMPORARY_FILE_PATH, "wt" );
    
    if( textFileDescriptor
        && temporaryFileDescriptor )
    {
        new currentLineIndex = 0;
        new currentLineStringContents[ MAX_LINE_LENGHT_ACCEPTABLE ];
        
        while( !feof( textFileDescriptor ) )
        {
            ++currentLineIndex;
            fgets( textFileDescriptor, currentLineStringContents, charsmax( currentLineStringContents ) );

	    if( !currentLineStringContents[ 0 ] || currentLineStringContents[ 0 ] == ';' || currentLineStringContents[ 0 ] == '#'
	    || ( currentLineStringContents[ 0 ] == '/' && currentLineStringContents[ 1 ] == '/' ) )
		continue;

            //fprintf( textFileDescriptor, "PULA" );
            //replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
            replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" ); // <  This make replace, but let too many spaces
	    //fprintf( textFileDescriptor, "%s", currentLineStringContents );
            
            if( currentLineIndex == indexOfTheLineToFind
                && containi( currentLineStringContents, textToFind ) < 0 )
            {
                fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
		fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
	    else
            {
                //fprintf( temporaryFileDescriptor, "%s^n", textToReplace );  0FF
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
        }
        
        fclose( textFileDescriptor );
        fclose( temporaryFileDescriptor );
        
        delete_file( textFilePath );
        rename_file( TEMPORARY_FILE_PATH, textFilePath, true );
    }
    else
    {
        log_error( AMX_ERR_NOTFOUND, "The file '%s' was not found!", textFilePath );
        return -1;
    }
    
    return 0;
}

Last edited by Adryyy; 07-25-2016 at 13:57.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 07-25-2016 , 14:12   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #7

Quote:
Originally Posted by Adryyy View Post
Sorry for bother you so much, and once again thanks for your help. Now it's ok, but another problem, when I give the ' replace ' this let too many spaces. You can fix line after replace?
// Do not replace 'textToFind' by nothing here, because it will always succeed on 'containi( currentLineStringContents, textToFind ) < 0'.
// on this case, you would not need the 'containi( currentLineStringContents, textToFind ) < 0'.
// replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
// If you want to remove all lines containing 'textToFind' and only let the 'textToReplace' on the
// first line, you need to change the if's. I fixed it already.
PHP Code:
/** AMX Mod X Script
 *  
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or ( at
 *  your option ) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***********************************************************
 */

/**
 * Necessary modules.
 */
#include <amxmodx>

/**
 * The maximum line acceptable when loading text files.
 */
#define MAX_LINE_LENGHT_ACCEPTABLE 512


/**
 * Called just after server activation.
 * 
 * Good place to initialize most of the plugin, such as registering
 * cvars, commands or forwards, creating data structures for later use, or
 * generating and loading other required configurations.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_init()
{
    
register_plugin"Laughing Line Replacer""1.0""Addons zz" );
}

/**
 * Called when all plugins went through plugin_init()
 * 
 * When this forward is called, most plugins should have registered their
 * cvars and commands already.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_cfg()
{
    
// The text which will be used by the trigger the replacement.
    
new const TEXT_TO_FIND_ON_THE_LINE[] = "TEST";
    
    
// The text to replace after a successful find.
    
new const TEXT_TO_REPLACE_ON_SUCCESS[] = "TESTX";
    
    
// The file path to perform the in-place replacement.
    
new const TEXT_FILE_PATH_FOR_REPLACEMENT[] = "addons\amxmodx\configs\plugins.ini";
    
    
replace_line_when1TEXT_FILE_PATH_FOR_REPLACEMENTTEXT_TO_FIND_ON_THE_LINETEXT_TO_REPLACE_ON_SUCCESS );
}

/**
 * To do a in-place file line replacement.
 * 
 * @param indexOfTheLineToFind     specifies on which line, starting on 1, it will be done the text replacement.
 * @param textFilePath[]          the relative file path using the moddir as a base directory.
 * @param textToFind[]            the text to search.
 * @param textToReplace[]         the text to replace.
 * 
 * @return 0 on success and -1 on failure.
 */
stock replace_line_when( const indexOfTheLineToFind, const textFilePath[] , const textToFind[], const textToReplace[] )
{
    new const 
TEMPORARY_FILE_PATH[]   = "LaughingMyAssOffTemporarily.txt";
    new       
textFileDescriptor      fopentextFilePath"rt" );
    new       
temporaryFileDescriptor fopenTEMPORARY_FILE_PATH"wt" );
    
    if( 
textFileDescriptor
        
&& temporaryFileDescriptor )
    {
        new 
currentLineIndex 0;
        new 
currentLineStringContentsMAX_LINE_LENGHT_ACCEPTABLE ];
        
        while( !
feoftextFileDescriptor ) )
        {
            ++
currentLineIndex;
            
fgetstextFileDescriptorcurrentLineStringContentscharsmaxcurrentLineStringContents ) );

            
//fprintf( textFileDescriptor, "PULA" );
            //replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
            
            // Do not replace 'textToFind' by nothing here, because it will always succeed on 'containi( currentLineStringContents, textToFind ) < 0'.
            // on this case, you would not need the 'containi( currentLineStringContents, textToFind ) < 0'.
            // replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
            // If you want to remove all lines containing 'textToFind' and only let the 'textToReplace' on the
            // first line, you need to change the if's. I fixed it already.
            
            //fprintf( textFileDescriptor, "%s", currentLineStringContents );
            
            
if( currentLineIndex == indexOfTheLineToFind )
            {
                if( 
containicurrentLineStringContentstextToFind ) < )
                {
                    
fprintftemporaryFileDescriptor"%s^n"textToReplace );
                }
                
                
fprintftemporaryFileDescriptor"%s"currentLineStringContents );
            }
            else
            {
                
replacecurrentLineStringContentscharsmaxcurrentLineStringContents ), textToFind"" );
                
fprintftemporaryFileDescriptor"%s"currentLineStringContents );
                
                
// if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                //if( containi( currentLineStringContents, textToFind ) < 0 )
                //{
                //    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                //}
            
}
        }
        
        
fclosetextFileDescriptor );
        
fclosetemporaryFileDescriptor );
        
        
delete_filetextFilePath );
        
rename_fileTEMPORARY_FILE_PATHtextFilePathtrue );
    }
    else
    {
        
log_errorAMX_ERR_NOTFOUND"The file '%s' was not found!"textFilePath );
        return -
1;
    }
    
    return 
0;

__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 07-25-2016 at 14:13. Reason: update
addons_zz is offline
Adryyy
Member
Join Date: Oct 2011
Old 07-25-2016 , 14:49   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #8

This method does not work. I have TEST but does not replace with the TESTX. But this code(https://forums.alliedmods.net/showpo...0&postcount=46) work...

BUT:

Code:
/** AMX Mod X Script
 *  
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or ( at
 *  your option ) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***********************************************************
 */

/**
 * Necessary modules.
 */
#include <amxmodx>

/**
 * The maximum line acceptable when loading text files.
 */
#define MAX_LINE_LENGHT_ACCEPTABLE 512


/**
 * Called just after server activation.
 * 
 * Good place to initialize most of the plugin, such as registering
 * cvars, commands or forwards, creating data structures for later use, or
 * generating and loading other required configurations.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_init()
{
    register_plugin( "Laughing Line Replacer", "1.0", "Addons zz" );
}

/**
 * Called when all plugins went through plugin_init()
 * 
 * When this forward is called, most plugins should have registered their
 * cvars and commands already.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_cfg()
{
    // The text which will be used by the trigger the replacement.
    new const TEXT_TO_FIND_ON_THE_LINE[] = "TEST";
    
    // The text to replace after a successful find.
    new const TEXT_TO_REPLACE_ON_SUCCESS[] = "TESTX";
    
    // The file path to perform the in-place replacement.
    new const TEXT_FILE_PATH_FOR_REPLACEMENT[] = "addons\amxmodx\configs\plugins.ini";
    
    replace_line_when( 1, TEXT_FILE_PATH_FOR_REPLACEMENT, TEXT_TO_FIND_ON_THE_LINE, TEXT_TO_REPLACE_ON_SUCCESS );
}

/**
 * To do a in-place file line replacement.
 * 
 * @param indexOfTheLineToFind     specifies on which line, starting on 1, it will be done the text replacement.
 * @param textFilePath[]          the relative file path using the moddir as a base directory.
 * @param textToFind[]            the text to search.
 * @param textToReplace[]         the text to replace.
 * 
 * @return 0 on success and -1 on failure.
 */
stock replace_line_when( const indexOfTheLineToFind, const textFilePath[] , const textToFind[], const textToReplace[] )
{
    new const TEMPORARY_FILE_PATH[]   = "LaughingMyAssOffTemporarily.txt";
    new       textFileDescriptor      = fopen( textFilePath, "rt" );
    new       temporaryFileDescriptor = fopen( TEMPORARY_FILE_PATH, "wt" );
    
    if( textFileDescriptor
        && temporaryFileDescriptor )
    {
        new currentLineIndex = 0;
        new currentLineStringContents[ MAX_LINE_LENGHT_ACCEPTABLE ];
        
        while( !feof( textFileDescriptor ) )
        {
            ++currentLineIndex;
            fgets( textFileDescriptor, currentLineStringContents, charsmax( currentLineStringContents ) );

            //fprintf( textFileDescriptor, "PULA" );
            replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" ); // you really need
            
            // Do not replace 'textToFind' by nothing here, because it will always succeed on 'containi( currentLineStringContents, textToFind ) < 0'.
            // on this case, you would not need the 'containi( currentLineStringContents, textToFind ) < 0'.
            // replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
            // If you want to remove all lines containing 'textToFind' and only let the 'textToReplace' on the
            // first line, you need to change the if's. I fixed it already.
            
            //fprintf( textFileDescriptor, "%s", currentLineStringContents );
            
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
            }
        }
        
        fclose( textFileDescriptor );
        fclose( temporaryFileDescriptor );
        
        delete_file( textFilePath );
        rename_file( TEMPORARY_FILE_PATH, textFilePath, true );
    }
    else
    {
        log_error( AMX_ERR_NOTFOUND, "The file '%s' was not found!", textFilePath );
        return -1;
    }
    
    return 0;
}
By the way, after you remove remain among blank (space)
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 07-25-2016 , 15:09   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #9

Quote:
Originally Posted by Adryyy View Post
This method does not work. I have TEST but does not replace with the TESTX. But this code(https://forums.alliedmods.net/showpo...0&postcount=46) work...

BUT:

Code:
/** AMX Mod X Script
 *  
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or ( at
 *  your option ) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***********************************************************
 */

/**
 * Necessary modules.
 */
#include <amxmodx>

/**
 * The maximum line acceptable when loading text files.
 */
#define MAX_LINE_LENGHT_ACCEPTABLE 512


/**
 * Called just after server activation.
 * 
 * Good place to initialize most of the plugin, such as registering
 * cvars, commands or forwards, creating data structures for later use, or
 * generating and loading other required configurations.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_init()
{
    register_plugin( "Laughing Line Replacer", "1.0", "Addons zz" );
}

/**
 * Called when all plugins went through plugin_init()
 * 
 * When this forward is called, most plugins should have registered their
 * cvars and commands already.
 * 
 * @return This forward ignores the returned value.
 */
public plugin_cfg()
{
    // The text which will be used by the trigger the replacement.
    new const TEXT_TO_FIND_ON_THE_LINE[] = "TEST";
    
    // The text to replace after a successful find.
    new const TEXT_TO_REPLACE_ON_SUCCESS[] = "TESTX";
    
    // The file path to perform the in-place replacement.
    new const TEXT_FILE_PATH_FOR_REPLACEMENT[] = "addons\amxmodx\configs\plugins.ini";
    
    replace_line_when( 1, TEXT_FILE_PATH_FOR_REPLACEMENT, TEXT_TO_FIND_ON_THE_LINE, TEXT_TO_REPLACE_ON_SUCCESS );
}

/**
 * To do a in-place file line replacement.
 * 
 * @param indexOfTheLineToFind     specifies on which line, starting on 1, it will be done the text replacement.
 * @param textFilePath[]          the relative file path using the moddir as a base directory.
 * @param textToFind[]            the text to search.
 * @param textToReplace[]         the text to replace.
 * 
 * @return 0 on success and -1 on failure.
 */
stock replace_line_when( const indexOfTheLineToFind, const textFilePath[] , const textToFind[], const textToReplace[] )
{
    new const TEMPORARY_FILE_PATH[]   = "LaughingMyAssOffTemporarily.txt";
    new       textFileDescriptor      = fopen( textFilePath, "rt" );
    new       temporaryFileDescriptor = fopen( TEMPORARY_FILE_PATH, "wt" );
    
    if( textFileDescriptor
        && temporaryFileDescriptor )
    {
        new currentLineIndex = 0;
        new currentLineStringContents[ MAX_LINE_LENGHT_ACCEPTABLE ];
        
        while( !feof( textFileDescriptor ) )
        {
            ++currentLineIndex;
            fgets( textFileDescriptor, currentLineStringContents, charsmax( currentLineStringContents ) );

            //fprintf( textFileDescriptor, "PULA" );
            replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" ); // you really need
            
            // Do not replace 'textToFind' by nothing here, because it will always succeed on 'containi( currentLineStringContents, textToFind ) < 0'.
            // on this case, you would not need the 'containi( currentLineStringContents, textToFind ) < 0'.
            // replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
            // If you want to remove all lines containing 'textToFind' and only let the 'textToReplace' on the
            // first line, you need to change the if's. I fixed it already.
            
            //fprintf( textFileDescriptor, "%s", currentLineStringContents );
            
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
            }
        }
        
        fclose( textFileDescriptor );
        fclose( temporaryFileDescriptor );
        
        delete_file( textFilePath );
        rename_file( TEMPORARY_FILE_PATH, textFilePath, true );
    }
    else
    {
        log_error( AMX_ERR_NOTFOUND, "The file '%s' was not found!", textFilePath );
        return -1;
    }
    
    return 0;
}
By the way, after you remove remain among blank (space)
Removed the that line which I already removed, and you put it back saying really need it from the code above, and it will work:
Code:
            replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" ); // you really need
It will delete all lines with 'textToFind', except the first file.
If you want to place put a empty when find 'textToFind', change:
Code:
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
            }
-->
Code:
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
                else
                {
                    fprintf( temporaryFileDescriptor, "^n" );
                }
            }
Update:

If you want to just remove the 'textToFind', change this:
Code:
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
            }
-->
Code:
            if( currentLineIndex == indexOfTheLineToFind )
            {
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s^n", textToReplace );
                }
                
                fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
            }
            else
            {
                //replace( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                //fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                
                // if you want to remove the line, that contains 'textToFind' instead of replace it by nothing,
                // to comment the above lines and uncomment these:
                if( containi( currentLineStringContents, textToFind ) < 0 )
                {
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
                else
                {
                    replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" );
                    fprintf( temporaryFileDescriptor, "%s", currentLineStringContents );
                }
            }
Also, do not forget to change this:
Code:
            ++currentLineIndex;
            fgets( textFileDescriptor, currentLineStringContents, charsmax( currentLineStringContents ) );

            //fprintf( textFileDescriptor, "PULA" );
            replace_all( currentLineStringContents, charsmax( currentLineStringContents ), textToFind, "" ); // you really need
-->
Code:
            ++currentLineIndex;
            fgets( textFileDescriptor, currentLineStringContents, charsmax( currentLineStringContents ) );

            //fprintf( textFileDescriptor, "PULA" );
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 07-25-2016 at 15:27. Reason: update
addons_zz is offline
Adryyy
Member
Join Date: Oct 2011
Old 07-25-2016 , 16:13   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #10

Another bug > https://vid.me/zw2H

I tested with :

Code:
TEST ABC
PLUGINS
..
>

Code:
PLUGINS
..
TEST ABC
>>

Code:
TEST ABC
PLUGINS
..
 ABC
When trying to delete and to move that text on the first line, not to delete all of the text.

Is 'TESTX' and after try to delete and move(manages), it remains only 'X' since that text to be deleted and moved. Don't read all of the text, it's something wrong with the value? Need something bigger?

A be here? :

Code:
/**
 * The maximum line acceptable when loading text files.
 */
#define MAX_LINE_LENGHT_ACCEPTABLE 512

Last edited by Adryyy; 07-25-2016 at 16:37.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
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 03:05.


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