[FilterScript] Text replacer - replace a string in text by another string easily!
#1

Hey guys! Haven't been here for a long time since I don't pawn anymore, I am just helping my friend with something, and I got this idea.

How does it work:

This FS is controlled through RCON commands (/rcon replace [filename] [string to be replaced] [new string]).

filename = the name of the file in scriptfiles that you want to edit (including suffix and correct case)
string to be replaced = if there is any string in the file like that, it will be replaced by the new string

Functions used:

Y_Less's Split function (I edited it a little bit because I like to edit everything. It has some advantages, but that's not important now)

pawn Code:
stock bool:Split(const string[], strdest[][], destsize = sizeof(strdest), mdestsize = sizeof(strdest[]), separator = 32)
{
new lenght = strlen(string);
new index;
if(!lenght || !destsize || !separator || !mdestsize) return false;
for(new i; i<destsize; i++)
{
while(index < lenght && string[index] == separator) index++;
new offset = index;
if(i < destsize - 1) while(index < lenght && string[index] != separator && index - offset < mdestsize) strdest[i][index - offset] = string[index++];
else while(index <= lenght && index - offset < mdestsize) strdest[i][index - offset] = string[index++];
strdest[i][index - offset] = 0;
}
return true;
}
My strreplace function. Very simple and very useful. The parameter ReplaceAll, if set to false, will replace only the first string that it finds in the file, so don't change it. You can use this function for anything else as well.

pawn Code:
stock bool:strreplace(dest[], const string[], const replacestr[], bool:ignorecase = true, bool:ReplaceAll = true, maxlenght = sizeof(dest))
{
new pos = strfind(dest, string, ignorecase);
if(!strlen(dest) || !strlen(string) || pos == -1 || (!strcmp(string, replacestr, ignorecase) && strlen(replacestr))) return false;
if(ReplaceAll) while(pos != -1)
{
strdel(dest, pos, pos + strlen(string));
strins(dest, replacestr, pos, maxlenght);
pos = strfind(dest, string, ignorecase);
}
else strdel(dest, pos, pos + strlen(string)), strins(dest, replacestr, pos, maxlenght);
return true;
}
What you may also need to know:

The separator is set to 32 (space) by default, but if you want a string with spaces in it to be replaced, change it to other character. If just the new string contains spaces, it is OK, because the Split function only splits the text into as many parts as the string has (4).

pawn Code:
new spc[4][80]; // The string has 4 parts (command, file, string to be replaced and new string)
pawn Code:
Split(cmd, spc, sizeof(spc), sizeof(spc[]), 32); // 32 is the same as ' ' (space). You can change this to whatever you want, but you must keep in mind that the command would have to be written like this, for example (/rcon replace*filename*string to be replaced*new string)
Examples of use:

You can use this when you made a new function and you don't want to rewrite everything manually in your script.
Of course, you can use this for any text file, for example to replace bad words

Disadvantages:

The only main disadvantage is that it messes up other than basic characters (й, н, б, ћ, etc.), but in English scripts, this works perfect.

Download:

Replacer.pwn

Thanks to:

Y_Less for his original Split function, I just made it a little better

If you have noticed something that I didn't, feel free to comment! You can also edit this script anyhow depending on your personal needs and make it even better! This is just the basic form for you to start with. I hope I helped at least some people.
Reply
#2

Very good dude!
That will make it more easier
Keep it up
Reply
#3

Quote:
Originally Posted by Mark_Samp
View Post
Very good dude!
That will make it more easier
Keep it up
Thanks I also planned to release something else, which is very special (and secret now), but, unfortunately, there is some kind of bug which I wasn't able to fix + I am unsure whether to release it. Maybe one day. Some people have already seen that work, but I am not very much into releasing until it works really 100%. :/
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)