librnr  1.14.5
RoadNarrows Robotics Common Library 1
char.c File Reference

Simple character manipulations. More...

#include <ctype.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "rnr/char.h"

Go to the source code of this file.

Functions

char * NewPrettyBuf (byte_t buf[], size_t len)
 Allocates a string buffer and copies a 'prettified' readable version of the contents of the given (binary) buffer. More...
 
int PrettyPrintBuf (FILE *fp, byte_t buf[], size_t len)
 Print out 'prettified' readable version of the contents of the given (binary) buffer. More...
 

Detailed Description

Simple character manipulations.

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file char.c.

Function Documentation

char* NewPrettyBuf ( byte_t  buf[],
size_t  len 
)

Allocates a string buffer and copies a 'prettified' readable version of the contents of the given (binary) buffer.

Parameters
bufBuffer to prettify
lenNumber of bytes in buf.
Returns
Allocated, null-terminated, prettified string. Use delete() to deallocate.

Definition at line 40 of file char.c.

References hexnibbletoa(), and NEWSTR.

Referenced by hexnibbletoa(), and PrettyPrintBuf().

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 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
static char hexnibbletoa(int h)
Convert hex nibble to ascii equivalent.
Definition: char.h:37
int PrettyPrintBuf ( FILE *  fp,
byte_t  buf[],
size_t  len 
)

Print out 'prettified' readable version of the contents of the given (binary) buffer.

Parameters
fpOutput file pointer.
bufBuffer to prettify.
lenNumber of bytes in buf.
Returns
Number of characters printed.

Definition at line 107 of file char.c.

References NewPrettyBuf().

Referenced by hexnibbletoa(), and test_char().

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 }
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