Add print helper functions

This commit is contained in:
Arti Zirk 2016-09-26 17:33:02 +03:00
parent 713cc80417
commit 5b78b7da02
2 changed files with 34 additions and 0 deletions

27
src/print_helper.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#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;
}

7
src/print_helper.h Normal file
View File

@ -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_ */