Solved

Overloading streambuf

Posted on 1998-02-05
3
365 Views
Last Modified: 2010-07-27
I need to overload streambuf for use with WIN32 overlapped I/O. I know you have to override the "overflow","underflow", and "sync"
members, but I don't know how they are supposed to act.

Thanks,
Kevin Wooten
0
Comment
Question by:KevinWooten
  • 2
3 Comments
 
LVL 3

Expert Comment

by:q2guo
Comment Utility
I found this .... , hope it helps.

Terry
-------------------------------

Simple output re-direction by redefining `overflow'
===================================================

   Suppose you have a function `write_to_window' that writes characters
to a `window' object.  If you want to use the ostream function to write
to it, here is one (portable) way to do it.  This depends on the
default buffering (if any).

     #include <iostream.h>
     /* Returns number of characters successfully written to WIN. */
     extern int write_to_window (window* win, char* text, int length);
     
     class windowbuf : public streambuf {
         window* win;
       public:
         windowbuf (window* w) { win = w; }
         int sync ();
         int overflow (int ch);
         // Defining xsputn is an optional optimization.
         // (streamsize was recently added to ANSI C++, not portable yet.)
         streamsize xsputn (char* text, streamsize n);
     };
     
     int windowbuf::sync ()
     { streamsize n = pptr () - pbase ();
       return (n && write_to_window (win, pbase (), n) != n) ? EOF : 0;
     }
     
     int windowbuf::overflow (int ch)
     { streamsize n = pptr () - pbase ();
       if (n && sync ())
         return EOF;
       if (ch != EOF)
         {
           char cbuf[1];
           cbuf[0] = ch;
           if (write_to_window (win, cbuf, 1) != 1)
             return EOF;
         }
       pbump (-n);  // Reset pptr().
       return 0;
     }
     
     streamsize windowbuf::xsputn (char* text, streamsize n)
     { return sync () == EOF ? 0 : write_to_window (win, text, n); }
     
     int
     main (int argc, char**argv)
     {
       window *win = ...;
       windowbuf wbuf(win);
       ostream wstr(&wbuf);
       wstr << "Hello world!\n";
     }

This is Info file iostream.info, produced by Makeinfo-1.55 from the
input file ./iostream.texi.

START-INFO-DIR-ENTRY
* iostream: (iostream).                    The C++ input/output facility.
END-INFO-DIR-ENTRY

   This file describes libio, the GNU library for C++ iostreams and C
stdio.

   libio includes software developed by the University of California,
Berkeley.

   Copyright (C) 1993 Free Software Foundation, Inc.

   Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

   Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.

   Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions.


0
 

Author Comment

by:KevinWooten
Comment Utility
I need a little more explanation, on how sync, overflow, and underflow are supposed to work.
0
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
Comment Utility
virtual int sync();
Typically, sync() involves writing out any elements between the beginning and next pointers for the output buffer. It does not involve putting back any elements between the next and end pointers for the input buffer. If the function cannot succeed, it returns -1. The default behavior is to return zero.

virtual int_type underflow();
function underflow() extracts the current element c from the input stream, without advancing the current stream position.
c is the element stored in the read position.

typedef T traits_type
virtual int_type overflow(int_type c = T::eof());

If c does not compare equal to T::eof(), the function inserts the element ::to_char_type(c) into the output stream.

The example is given in my previous answer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now