|
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
|

07-25-2016
, 15:09
Re: [Req] Plugins.ini check & rewrite
|
#9
|
Quote:
Originally Posted by Adryyy
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" );
__________________
Last edited by addons_zz; 07-25-2016 at 15:27.
Reason: update
|
|