www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - problem with overwriting Class

reply Charma <Motoko_Kusanagi web.de> writes:
Hello,
I have a problem with overwriting a class. I want to make a class from
BufferedFile in which i can use all it's functions but i want the starting
position to be shifted to a given position (Vfpos) and to go to a given maximum
(Vfsize). For example i got a 100kb-file and i want it to start from position
10kb and load maximum of 20kb, i want it to act as if there is a 20kb-file only
and it should go from 10-30 with positions 0-20. I started overwriting the
read-functions and adding stuff so that it doesn't read more than the wanted
area and so on. My problem is with the position(ulong offset) function. For
some reason I can't correctly overwrite it...
I got this code...

class Vfile : BufferedFile
{
	uint Vfpos; // in my case this is 41
	uint Vfsize; // and this is 17
	
	ulong position() // this one works perfectly!
	{
		return ( super.seek(0, SeekPos.Current) - Vfpos );
	}
	
	void position(ulong newpos)
	{
		if( newpos > Vfsize )
		{
			...
		}
		else
		{
			std.stdio.writefln("New pos:", Vfpos+newpos );
			// now this doesn'T work
			std.stdio.writefln("super-seek: ", super.seek( Vfpos+newpos, SeekPos.Set ) );
			// this one doesn't work either
			// super.position(Vfpos + newpos);
			std.stdio.writefln("My new pos: ", super.position() );
		}
...
}

Vfile bla = new Vfile(..);
writefln( bla.position() );
bla.postition(9)
writefln( bla.position() );

this would give something like:
41 <-- my starting position
New pos: 50
super-seek: 50
My new pos: 41
41

the function super.seek(...) DOES return the correct new position but i can't
understand why the position is allways changed back to the first...
Please help!!
May 28 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Charma wrote:
 Hello,
 I have a problem with overwriting a class. I want to make a class from
BufferedFile in which i can use all it's functions but i want the starting
position to be shifted to a given position (Vfpos) and to go to a given maximum
(Vfsize). For example i got a 100kb-file and i want it to start from position
10kb and load maximum of 20kb, i want it to act as if there is a 20kb-file only
and it should go from 10-30 with positions 0-20. I started overwriting the
read-functions and adding stuff so that it doesn't read more than the wanted
area and so on. My problem is with the position(ulong offset) function. For
some reason I can't correctly overwrite it...
Have you considered just using std.stream.SliceStream? It pretty much does what you want, but for any stream and any limits (specified as constructor arguments). If you insist on another class, try something like this: --- class VFile : SliceStream { this(...) { // base stream, lower & upper bound super(new BufferedStream(...), 10*1024, 30*1024); } } --- Add some constructor arguments for VFile and/or the "new BufferedStream" and you're pretty much done...
May 28 2007