*****Listing 4*****


// a buffer is a kind of yacht of characters, which can optionally be
// associated with a File
class Buffer : public Yacht {
public:
	Buffer() : () {}  // constructor for buffers not connected to a file

	Buffer( char *filename) : ()
	{
		if( filename == 0 )
			;
		else if( read( filename) )
			;
		else
			error( "could not open %s\n", filename);

		begin();
	}

	Truth read( char *filename)
	{
		int r = 1;
		if( filename ) {
			File f( filename);
			if( f.isok() ) {
				for( ; !f.iseof(); next() )
					putb( f.get() );
			} else r = 0;
		}
		return r;
	}

	Truth write( char *filename)
	{
		int r = 1;
		if( filename ) {
			File f( filename, "w");
			if( f.isok() ) {
				for( ; !isend(); next() )
					f.put( geta() );                      
			} else r = 0;
		}
		return r;
	}
private:
	void error( char *msg, char *arg) { printf( msg, arg); }
};

