BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_print

Detailed Description

Outline

Usage

Purpose

Provide platform-independent stream utilities.

Classes

Description

This component provides a namespace, bdlb::Print, containing utility functions for formatting data to bsl::ostream objects. These functions provide several variations of hexadecimal format, allow platform-independent representation of void * pointers, and can help with the indentation of hierarchical data.

This component also provides two helper classes, bdlb::PrintStringHexDumper and bdlb::PrintStringSingleLineHexDumper, that define operator<< so they can be used in chains of << operations. The bdlb::PrintStringHexDumper class produces formatted, possibly multi-line output, whereas the bdlb::PrintStringSingleLineHexDumper class produces a simple sequence of hexadecimal digits (and no newline).

xxd-Compatible hexDump

The output generated by the hexDump functions is not xxd-compatible (see http://gd.tuwien.ac.at/linuxcommand.org/man_pages/xxd1.html). The following perl script is provided that will convert hexDump output into xxd-compatible form. Run the script with a file containing the hexDump output as the first argument.

#!/usr/bin/perl -w
use strict;
my $num = 0;
while (<>) {
next if (!$_);
my $str = $_;
next if !($str =~ s/^[^:]*?:\s*//);
my $h = sprintf("%08X",$num);
$str =~ s/(\S{4})([\S\W]{4})\s?([\S\W]{4})([\S\W]{4})\s?([\S\W]{4})?
([\S\W]{4})?\s?([\S\W]{4})?([\S\W]{4})?/$1 $2 $3 $4 $5 $6 $7 $8/;
$str =~ s/\s \|([^|]+)\|.*$/ $1/;
print "$h: ";
print $str;
$num = $num + 16;
}
bsl::ostream & print(bsl::ostream &stream, const TYPE &object, int level=0, int spacesPerLevel=4)
Definition bdlb_printmethods.h:719

Usage

This section illustrates intended use of this component.

Example 1: Using printPtr

The default output produced from pointer values is non-standard across vendor platforms. The printPtr method addresses this inconsistency by always producing a consistent format for a given pointer size:

const void *a = reinterpret_cast<void *>(0x0);
const void *b = reinterpret_cast<void *>(0xf2ff);
const void *c = reinterpret_cast<void *>(0x0123);
const void *d = reinterpret_cast<void *>(0xf1f2abc9);
bdlb::Print::printPtr(out1, a); out1 << endl;
bdlb::Print::printPtr(out1, b); out1 << endl;
bdlb::Print::printPtr(out1, c); out1 << endl;
bdlb::Print::printPtr(out1, d); out1 << endl;
assert("0" "\n"
"f2ff" "\n"
"123" "\n"
"f1f2abc9" "\n" == out1.str());
Definition bslstl_ostringstream.h:175
void str(const StringType &value)
Definition bslstl_ostringstream.h:581
static void printPtr(bsl::ostream &stream, const void *value)

Example 2: Using the Helper Classes

The two helper classes allow users to stream a hexadecimal representation of a sequence of bytes into an output stream.

The bdlb::PrintStringHexDumper provides a formatted, possibly multi-line representation:

char buf[] = "abcdefghijklmnopqrstuvwxyz";
out2a << bdlb::PrintStringHexDumper(buf, sizeof buf);
assert(
" 0: 61626364 65666768 696A6B6C 6D6E6F70 |abcdefghijklmnop|\n"
" 16: 71727374 75767778 797A00 |qrstuvwxyz. |\n"
== out2a.str());
out2b << bdlb::PrintStringSingleLineHexDumper(buf, sizeof buf);
Definition bdlb_print.h:288
Definition bdlb_print.h:326

The bdlb::PrintStringSingleLineHexDumper provides a simple, single-line representation.

assert("6162636465666768696A6B6C6D6E6F707172737475767778797A00"
== out2b.str());