Sign in to follow this  
KulSeran

streambuf overloading

Recommended Posts

KulSeran    3267
Couple days back I made a post, and got refered to the idea of overloading a streambuf to provide some extra functionality in my file access, while maintaining the c++ look of fstreams. That said, I figured out how to overload it so that
  my_stream << "test" << endl;
will call "overflow" for each of t-e-s-t-NULL. That is nice, so I'm partway there. now when i try to do
  my_stream >> a_string;
it always pulls up an empty string, but the odd part is that it never calls "underflow" so Im wondering what i have to overload, or what code i have to add to "overflow" inorder to get it to return something; After that, Im assuming that "sync" is where i need to update my pointers and such. So, is sync called on a seekp/seekg? if so then how do i find the position so i can update my underlying file?
class vfs_file_buff : public streambuf
{
	private:
	public:
		vfs_file_buff();
		~vfs_file_buff();
		void	open		( string fname );
		void	close		( void );

		int		underflow	( void );		
		int		overflow	( int ch );
		//int		sync		( void );
	};

vfs_file_buff::vfs_file_buff()
{
}
vfs_file_buff::~vfs_file_buff()
{}

void vfs_file_buff::open	( string fname )
{
	cout << "open" << endl;
}

void vfs_file_buff::close ( void )
{
	cout << "close" << endl;
}


int vfs_file_buff::underflow ( void )
{
	cout << "underflow" << endl;
	return 0;
	}

int vfs_file_buff::overflow ( int ch )
{
	cout << "overflow" << endl;
	cout << (char)ch << endl;
	
	return ch;
	}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
sync is used when you call flush on a stream, and is optional to implement.

The 3 functions I implemented are overflow (for reading), and underflow + uflow for writing. After profiling my (very) simple game though, I think the one-char at a time method has too much overhead, and I will need to implement a buffered solution.

Share this post


Link to post
Share on other sites
KulSeran    3267
Anyone have a good link/code for that? cauze i just added uflow to my overloads, but it seems that it is never getting there either.


--edit
Ok, it appears to have something to do with my initialization.
If I seed a "ostream" then the overflow works.
If I seed a "istream" then the underflow works.
If I seed a "iostream" then the overflow works, but not the underflow.

--edit again
Ok, so the issue had everything to do with me attempting to do a seekg/p on the stream.
Wich leads be back to my last question. How do i get the streambuf position so i can sync that with
my underlying buffers/files?


[Edited by - KulSeran on March 4, 2007 9:03:06 PM]

Share this post


Link to post
Share on other sites
KulSeran    3267
For anyone interested:

underflow
uflow
overflow

appear to be the functions that have to be overwritten inorder to properly overload a streambuf
for input and output
BUT

seekpos
seekoff

also have to be overwritten inorder to allow the above stream to be seeked.

When i finish my code will likely post if for those intersted.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this