From 5b78b7da02e476c0406a85f10998ef7f95244b77 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Mon, 26 Sep 2016 17:33:02 +0300 Subject: [PATCH] Add print helper functions --- src/print_helper.c | 27 +++++++++++++++++++++++++++ src/print_helper.h | 7 +++++++ 2 files changed, 34 insertions(+) create mode 100644 src/print_helper.c create mode 100644 src/print_helper.h diff --git a/src/print_helper.c b/src/print_helper.c new file mode 100644 index 0000000..15d409e --- /dev/null +++ b/src/print_helper.c @@ -0,0 +1,27 @@ +#include +#include "print_helper.h" + +int print_ascii_tbl (FILE *stream) { + for (char c = ' '; c <= '~'; c++) { + if(!fprintf(stream, "%c ", c)) { + return 0; + } + } + return fprintf(stream, "\n"); +} + + +int print_for_human (FILE *stream, const unsigned char *array, const int len) { + for (int i = 0; i < len; i++) { + if (array[i] >= ' ' && array[i] <= '~') { + if(!fprintf(stream, "%c ", array[i])) { + return 0; + } + } else { + if(!fprintf(stream, "\"0x%02x\"", array[i])) { + return 0; + } + } + } + return 0; +} diff --git a/src/print_helper.h b/src/print_helper.h new file mode 100644 index 0000000..415344a --- /dev/null +++ b/src/print_helper.h @@ -0,0 +1,7 @@ +#ifndef _PRINT_HELPER_H_ +#define _PRINT_HELPER_H_ + +int print_ascii_tbl (FILE *stream); +int print_for_human (FILE *stream, const unsigned char *array, const int len); + +#endif /* _PRINT_HELPER_H_ */