Btw my mind wasn't in normal state when i wrote my post and read your code.
It was making sense but there were some bugs in it. ( you fixed the one already. )
Anyways ... i created this but i quickly gave up since there was no way
to resize the file
even tho:
https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
http://linux.die.net/man/2/truncate
And i got stuck on bug ( while what is smaller than with )
So i found that it wasn't worth of my time.
Anyways, here's my code:
Code:
#include <amxmodx>
public plugin_init()
{
if( file_exists( "addons/logs.txt" ) ) delete_file("addons/logs.txt")
replaceinfile( "addons/test.txt", "unab1234567asb", "3" )
}
#define _rif_block_size 32
stock replaceinfile( const filename[], const what[], const with[] )
{
new f = fopen( filename, "rb" );
new whatsize = strlen( what );
new withsize = strlen( with );
new blocksize = _rif_block_size - 1;
new readsize[2] = {0,0};
new mypos = 0;
new foundin = -1;
new myblock[2][_rif_block_size * 2];
new b = 0;
while((readsize[b] = fread_blocks( f, myblock[b], blocksize, BLOCK_CHAR )))
{
zs_log( "b:%d,%d", b, readsize[b] )
myblock[b][readsize[b] + 1] = 0;
if( mypos )
{
add(myblock[b], readsize[b], myblock[(!b)]);
myblock[(!b)][0] = 0;
zs_log( "check:%s from:%s", what, myblock[b] )
foundin = contain(myblock[b], what);
}
else foundin = contain(myblock[b], what);
if( foundin != -1 )
{
mypos += foundin;
fclose(f);
f = fopen( filename, "r+b" );
fseek(f, 0, SEEK_END);
new fsize = ftell(f);
new current = fsize;
new stepsize;
readsize[0] = _rif_block_size - 1;
if( withsize > whatsize )
{
mypos += whatsize;
stepsize = withsize - whatsize;
while( current > mypos )
{
if( mypos > current - readsize[0] ) readsize[0] = current - mypos;
current -= readsize[0];
fseek(f, current, SEEK_SET);
fread_blocks( f, myblock[0], readsize[0], BLOCK_CHAR );
fseek(f, current + stepsize, SEEK_SET);
fwrite_blocks(f, myblock[0], readsize[0], BLOCK_CHAR );
}
fseek(f, mypos - whatsize, SEEK_SET);
fwrite_blocks(f, with, withsize, BLOCK_CHAR );
break;
}
stepsize = whatsize - withsize;
current = mypos + whatsize;
new pr = fsize - stepsize - 1;
while( current < pr )
{
zs_log( "distance:%d/%d pr:%d", pr - current, current, pr )
if( current + readsize[0] > pr) readsize[0] = pr - current;
zs_log( "writesize:%d", readsize[0])
fseek(f, current, SEEK_SET);
fread_blocks( f, myblock[0], readsize[0], BLOCK_CHAR );
fseek(f, current - stepsize, SEEK_SET);
fwrite_blocks(f, myblock[0], readsize[0], BLOCK_CHAR );
current += readsize[0];
}
fseek(f, mypos, SEEK_SET);
fwrite_blocks(f, with, withsize, BLOCK_CHAR );
for( new b = 0; b <= stepsize; b++ ) myblock[0][b] = 0;
fseek(f, fsize - stepsize, SEEK_SET);
fwrite_blocks(f, myblock[0], stepsize, BLOCK_CHAR );
break;
}
zs_log( "here" );
b = (!b);
myblock[b][0] = 0;
mypos += readsize[0];
}
fclose(f);
}
public zs_log( in[], any:... )
{
static a[1024]
vformat(a, charsmax(a), in, 2)
write_file( "addons/logs.txt", a, -1)
}
oh btw @Bugsy
that what you didn't had patience to understand.
Half of it didn't make any sense. It made sense but a bad sense i guess.
I'm not going to say what, where, i took or did but by that reason, my ability to think deep was terrible.
Sowwy about everything xD