PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/9 TITLE : Three example manipulators (binary, pointer, comma) /**** BEGIN MANIPS.H /* MANIPS.CPP: 3 ostream manipulator examples binary() ostream manipulator outputs values in binary binary() is a manipulator which displays a value in binary and permits control of the number of binary digits displayed. In addition to being a useful manipulator, binary() is an example of an output stream manipulator taking two arguments which is portable across compilers which use either macro or template implementations of the IOSTREAM manipulator machinery. USAGE: int value = 0x54; cout << "The five most signicant binary digits " << " of " << hex << value << " are " << binary(value, 5) << ".\n" << "The 12 most signficant digits of " << hex << value << " are " << binary(value, 12) << ".\n"; fptr() ostream manipulator for pointers fptr() is a manipulator which 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) 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"; PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/9 TITLE : Three example manipulators (binary, pointer, comma) int anint; cout << "'astring' starts at " << fptr(astring) << endl << "'anint' starts at " << fptr(&anint) << endl; comma() ostream manipulator to comma format a value comma() is an example of overloaded iostream manipulators. It will format either a string (assumed to be numeric but this is not tested), or an integer by inserting commas every third digit. These manipulators work in conjunction with the two over- loaded comma() functions in MANIPS.CPP which offer the same comma formatting services for use outside of iostream operations. USAGE: cout << "\n Testing comma(long) manipulator: " << comma(3453899907); cout << "\n Testing comma(char*) manipulator: " << comma("-12345678901234567890") << 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. (3) #define MANIPS_MAIN to compile and link the testbded main(). (4) Use of the comma() ostream manipulator requires the inclusion of MANIPS.CPP, either in the project or on the command line. fptr() and binary() can be used simply by #including this header file. */ #if !defined (MANIPS_H) #define MANIPS_H #if !defined ( __STDLIB_H ) #include // for itoa() PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/9 TITLE : Three example manipulators (binary, pointer, comma) #endif // __DOS_H #if !defined( __DOS_H ) #include // for FP_SEG, FP_OFF #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. */ inline ostream& fptr(ostream& o, void *p) { long f = o.flags(); o.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 PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 4/9 TITLE : Three example manipulators (binary, pointer, comma) * names for templates. */ #if !defined (IOMANIPdeclare) inline 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); inline OMANIP(PVOID) fptr(PVOID p) { return OMANIP(PVOID) (fptr, p); } #endif // IOMANIPdeclare char *comma(const long value, char *buffer); char *comma(const char *value, char *buffer); /*------------------------------------------------------- comma - inserts commas at every third digit in the string representation of 'value'. */ inline ostream& comma(ostream& o, long value) { char outbuf[30]; comma(value, &outbuf[0]); o << outbuf; return o; } #if !defined (IOMANIPdeclare) inline omanip comma(long l) { return omanip(comma, l); } PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 5/9 TITLE : Three example manipulators (binary, pointer, comma) #else inline OMANIP(long) comma(long l) { return OMANIP(long) (comma, l); } #endif // IOMANIPdeclare /*------------------------------------------------------- comma - inserts commas at every third position in the string 'value'. */ inline ostream& comma(ostream& o, char *value) { // long f = o.flags(); char outbuf[30]; comma(value, &outbuf[0]); o << outbuf; // o.flags(f); return o; } #if !defined (IOMANIPdeclare) inline omanip comma(char *s) { return omanip(comma, s); } #else #if !defined (PCHAR) typedef char *PCHAR; #endif IOMANIPdeclare(PCHAR); inline OMANIP(PCHAR) comma(PCHAR s) { return OMANIP(PCHAR) (comma, s); } #endif //////////////////////////////////////////// binarypair //////// struct binarypair { unsigned long value; int digits; PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 6/9 TITLE : Three example manipulators (binary, pointer, comma) }; //========================================== binarypair ======== inline static ostream& binary(ostream& o, binarypair p) { char tmp[30]; long f = o.flags(); char c = o.fill(); ltoa(p.value, tmp, 2); tmp[p.digits] = '\0'; o.fill('0'); o.setf(ios::right); o << setw(p.digits) << tmp; o.flags(f); o.fill(c); return o; } //////////////////////////////////////////// binarypair //////// #if !defined (IOMANIPdeclare) inline omanip binary(unsigned long l, int n) { binarypair p; p.value=l; p.digits=n; return omanip(binary, p); } #else IOMANIPdeclare(binarypair); inline OMANIP (binarypair) binary(unsigned long l, int n) { binarypair p; p.value=l; p.digits=n; return OMANIP(binarypair)(binary, p); } #endif // IOMANIPdeclare #endif // MANIPS_H /***** END MANIPS.H /***** BEGIN MANIPS.CPP #include PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 7/9 TITLE : Three example manipulators (binary, pointer, comma) #include #include //-------------------------------------------------------------- char *comma(const char *value, char *buffer) { char *bptr = buffer; short len = (short)strlen(value); int nlen = len; *(bptr++) = *(value++); if (buffer[0] == '-') { *(bptr++) = *(value++); --nlen; } for (int j=1; *value; j++, value++, bptr++) { if (!((nlen-j)%3)) *(bptr++) = ','; *bptr = *value; } *bptr = 0; return buffer; } // end of comma() //-------------------------------------------------------------- char *comma(const long value, char *buffer) { char string[30]; sprintf(string, "%ld", value); return comma(string, buffer); } #if defined (MANIPS_MAIN) //************************************************************** #include // for clrscr() #include "manips.h" // for comma(), fptr() and binary() // manipulators int main(void) { #if defined (__FLAT__) int ival[] = { 2147, -2147, 21474, -21474, 214748, -214748, 2147483, -2147483, 21474836, -21474836, PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 8/9 TITLE : Three example manipulators (binary, pointer, comma) 214748364, -214748364, 2147483647, -2147483647, 0 }; long lval[] = { 2147, -2147, 21474, -21474, 214748, -214748, 2147483, -2147483, 21474836, -21474836, 214748364, -214748364, 2147483647, -2147483647, 0 }; #else int ival[] = { 3276, -3276, 32767, -32767, 10000, -10000, 0 }; long lval[] = { 2147, -2147, 21474, -21474, 217478l, -217478l, 2147483l, -2147483l, 21474836l, -21474836l, 214748364l, -214748364l, 2147483647l, -2147483647l, 1000000000l, 0 }; #endif char buffer[30]; clrscr(); // Test commafication of integer range. printf("\n Comma formatted integer values:"); for (int i=0; ival[i]; i++) { if (!(i%3)) printf("\n "); printf(" %17s\t", comma(ival[i], buffer)); } // Test commafication of long integer range. printf("\n\n Comma formatted long values:"); for (i=0; lval[i]; i++) { if (!(i%3)) printf("\n "); printf(" %17s\t", comma(lval[i], buffer)); } // Test commafication of numeric string. printf("\n\n Comma formatted numeric string: %s", comma("12345678901234567890", buffer)); cout << "\n\n Testing comma(long) manipulator: " << comma(lval[19]); cout << "\n Testing comma(char*) manipulator: " << comma("-12345678901234567890") << endl; // Testing binary() manipulator PRODUCT : Borland C++ NUMBER : 1545 VERSION : All OS : All DATE : October 25, 1993 PAGE : 9/9 TITLE : Three example manipulators (binary, pointer, comma) int value = 0x54; cout << "\n The five most signicant binary digits of " << hex << value << " are " << binary(value, 5) << ".\n" << " The 12 most signficant binary digits of " << hex << value << " are " << binary(value, 12) << ".\n"; // Testing fptr() manipulator char *astring="This is a string"; int anint; cout.setf(ios::uppercase); cout << "\n 'astring' starts at " << fptr(astring) << endl << " 'anint' starts at " << fptr(&anint); return 0; } // end of main() #endif // MANIPS_MAIN 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.