PRODUCT : Borland C++ NUMBER : 1374 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/4 TITLE : An iostream manipulator for pointers This example contains two files, IOM_FPTR.H and IOM_FPTR.CPP ----BEGIN FILE: IOM_FPTR.H--- /* IOM_FPTR.H: fptr() is a manipulator displays a pointer in "xxxx:xxxx" format when in 16-bit mode (DOS) and "xxxxxxxx" format when in 32-bit mode (OS/2 and NT) PURPOSE: The fptr() manipulator displays a pointer in the common xxxx:xxxx format as provided by the printf() function's '%Fp' formatting options. In addition to being a useful manipulator, fptr() is an example of an output stream manipulator taking one argument which is portable across 16 and 32 bit compiler boundaries and across compilers which use either macro or template implementations of the IOSTREAM manipulator machinery. USAGE: char *astring="This is a string"; int anint; cout << "'astring' starts at " << fptr(astring) << endl << "'anint' starts at " << fptr(&anint) << endl; NOTES: (1) tested with Borland C++ 3.1 near and far data models and Borland C++ for OS/2 1.0. (2) for detailed usage information see UNIX System V AT&T C++ Language System Release 2.1, Library Manual, Chapter 3: Iostream Examples, pgs 3-17 through 3-20. 050693EAS */ #if !defined( __DOS_H ) #include // for FP_SEG, FP_OFF PRODUCT : Borland C++ NUMBER : 1374 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/4 TITLE : An iostream manipulator for pointers #endif // __DOS_H #if !defined( __IOMANIP_H ) #include // for IOSTREAM basics #endif // __IOMANIP_H #if !defined( __IOSTREAM_H ) #include // for manipulator macros/templates #endif // __IOSTREAM_H /*------------------------------------------------------- fptr - iostream manipulator that takes a pointer and reformats to xxxx:xxxx prior to sending out ostream 'o'. If invoked from a 32-bit (FLAT) program, an unformatted hexadecimal pointer value is output. */ ostream& fptr(ostream& o, void *p) { long f = o.flags(); cout.fill('0'); // 16-bit compilers display xxxx:xxxx format. #if !defined (__FLAT__) o << setw(4) << hex << FP_SEG(p) << ":" << setw(4) << FP_OFF(p); // 32-bit compilers (flat) just display 8 hex digits. #else o << setw(8) << hex << (unsigned) p; #endif o.flags(f); return o; } /* * If 'IOMANIPdeclare' isn't defined, templates are used. * Note that Borland compilers use lower case manipulator * names for templates. */ #if !defined (IOMANIPdeclare) PRODUCT : Borland C++ NUMBER : 1374 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/4 TITLE : An iostream manipulator for pointers omanip fptr(void *p) { return omanip(fptr, p); } #else /* * If IOMANIPdeclare is defined, macros are used. Note that * when macros are used, Borland compilers use upper case * names for macros. */ #if !defined (PVOID) typedef void *PVOID; #endif IOMANIPdeclare(PVOID); OMANIP(PVOID) fptr(PVOID p) { return OMANIP(PVOID) (fptr, p); } #endif // IOMANIPdeclare ---END FILE: IOM_FPTR.H----- PRODUCT : Borland C++ NUMBER : 1374 VERSION : All OS : All DATE : October 25, 1993 PAGE : 4/4 TITLE : An iostream manipulator for pointers ---BEGIN FILE: IOM_FPTR.CPP--- /* * IOM_FPTR.CPP: testbed for fptr() IOSTREAM manipulator */ #include "iom_fptr.h" //************************************************************** void main(void) { char *astring="This is a string"; int anint; cout.setf(ios::uppercase); cout << "'astring' starts at " << fptr(astring) << endl << "'anint' starts at " << fptr(&anint) << endl; } // end of main() ---END FILE: IOM_FPTR.CPP---- DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.