From 74d7bbf66b3dfec5688087395850c526f6c014ad Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 10 Sep 2009 12:01:47 +0100 Subject: [PATCH] Print ASCIIfied version of byte arrays where possible --- tools/dbus-print-message.c | 44 ++++++++++++++++++++++++++++++++++---------- 1 files changed, 34 insertions(+), 10 deletions(-) diff --git a/tools/dbus-print-message.c b/tools/dbus-print-message.c index 4672732..5e65e45 100644 --- a/tools/dbus-print-message.c +++ b/tools/dbus-print-message.c @@ -49,21 +49,43 @@ indent (int depth) } static void +print_ascii (unsigned char *pending, int n) +{ + int i; + + for (i = 0; i < n; i++) + { + if (32 <= pending[i] && pending[i] <= 126) + printf ("%c", pending[i]); + else + printf ("."); + } +} + +#define MIN_COLUMNS 8 +#define MAX_COLUMNS 20 + +static void print_ay (DBusMessageIter *iter, int depth) { int current_type; - int n = 0; int columns; + unsigned char pending[MAX_COLUMNS]; + int n = 0; printf("array of bytes [\n"); indent(depth + 1); - /* Each byte takes 3 cells (two hexits, and a space), except the last one. */ - columns = (80 - ((depth + 1) * INDENT)) / 3; + /* Each byte takes 4 cells (two hexits, a space, and one ASCII char on the + * RHS) + */ + columns = (80 - ((depth + 1) * INDENT)) / 4; - if (columns < 8) - columns = 8; + if (columns < MIN_COLUMNS) + columns = MIN_COLUMNS; + else if (columns > MAX_COLUMNS) + columns = MAX_COLUMNS; current_type = dbus_message_iter_get_arg_type (iter); @@ -71,7 +93,8 @@ print_ay (DBusMessageIter *iter, int depth) { unsigned char val; dbus_message_iter_get_basic (iter, &val); - printf ("%02x", val); + printf ("%02x ", val); + pending[n] = val; n++; @@ -82,17 +105,18 @@ print_ay (DBusMessageIter *iter, int depth) { if (n == columns) { + print_ascii (pending, n); + printf ("\n"); indent (depth + 1); n = 0; } - else - { - printf (" "); - } } } + indent (columns - n); + print_ascii (pending, n); + printf ("\n"); indent(depth); printf("]\n"); -- 1.6.3.3