librnr  1.14.5
RoadNarrows Robotics Common Library 1
char.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // Library: librnr
6 //
7 // File: char.h
8 //
9 /*! \file
10  *
11  * \brief Simple character manipulations.
12  *
13  * \author Robin Knight (robin.knight@roadnarrows.com)
14  *
15  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
16  *
17  * \EulaBegin
18  * See the README and EULA files for any copyright and licensing information.
19  * \EulaEnd
20  */
21 ////////////////////////////////////////////////////////////////////////////////
22 
23 #include <ctype.h>
24 
25 #include "rnr/rnrconfig.h"
26 #include "rnr/new.h"
27 #include "rnr/char.h"
28 
29 /*!
30  *
31  * \brief Allocates a string buffer and copies a 'prettified' readable
32  * version of the contents of the given (binary) buffer.
33  *
34  * \param buf Buffer to prettify
35  * \param len Number of bytes in buf.
36  *
37  * \return
38  * Allocated, null-terminated, prettified string. Use delete() to deallocate.
39  */
40 char *NewPrettyBuf(byte_t buf[], size_t len)
41 {
42  char *sPretty = NEWSTR(len*4);
43  char *s;
44  int i, c;
45 
46  for(i=0, s=sPretty; i<len; ++i)
47  {
48  c = (int)buf[i];
49  if( c == ' ' )
50  {
51  *s++ = ' ';
52  }
53  else if( isspace(c) )
54  {
55  *s++ = '\\';
56  switch(c)
57  {
58  case '\f':
59  *s++ = 'f';
60  break;
61  case '\n':
62  *s++ = 'n';
63  break;
64  case '\r':
65  *s++ = 'r';
66  break;
67  case '\t':
68  *s++ = 't';
69  break;
70  case '\v':
71  *s++ = 'v';
72  break;
73  default:
74  *s++ = hexnibbletoa( (c>>4) & 0x0f );
75  *s++ = hexnibbletoa( c & 0x0f );
76  break;
77  }
78  }
79  else if( isprint(c) )
80  {
81  *s++ = (char)c;
82  }
83  else
84  {
85  *s++ = '\\';
86  *s++ = 'x';
87  *s++ = hexnibbletoa( (c>>4) & 0x0f );
88  *s++ = hexnibbletoa( c & 0x0f );
89  }
90  }
91  *s = 0;
92  return sPretty;
93 }
94 
95 /*!
96  *
97  * \brief Print out 'prettified' readable version of the contents of the
98  * given (binary) buffer.
99  *
100  * \param fp Output file pointer.
101  * \param buf Buffer to prettify.
102  * \param len Number of bytes in buf.
103  *
104  * \return
105  * Number of characters printed.
106  */
107 int PrettyPrintBuf(FILE *fp, byte_t buf[], size_t len)
108 {
109  char *sPretty;
110  int n;
111 
112  sPretty = NewPrettyBuf(buf, len);
113 
114  n = fprintf(fp, "%s", sPretty);
115 
116  delete(sPretty);
117 
118  return n;
119 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
Memory allocation and deallocation declarations.
Simple character manipulations.
int PrettyPrintBuf(FILE *fp, byte_t buf[], size_t len)
Print out &#39;prettified&#39; readable version of the contents of the given (binary) buffer.
Definition: char.c:107
RoadNarrows Robotics common configuration file.
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
char * NewPrettyBuf(byte_t buf[], size_t len)
Allocates a string buffer and copies a &#39;prettified&#39; readable version of the contents of the given (bi...
Definition: char.c:40
static char hexnibbletoa(int h)
Convert hex nibble to ascii equivalent.
Definition: char.h:37