Menu

[r1523]: / advanced / lib / libutil / make_printable.c  Maximize  Restore  History

Download this file

102 lines (81 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#define _GNU_SOURCE
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "xmlrpc_config.h"
#include "xmlrpc-c/string_int.h"
const char *
xmlrpc_makePrintable_lp(const char * const input,
size_t const inputLength) {
/*----------------------------------------------------------------------------
Convert an arbitrary string of characters in length-pointer form to
printable ASCII. E.g. convert newlines to "\n".
Return the result in newly malloc'ed storage. Return NULL if we can't
get the storage.
-----------------------------------------------------------------------------*/
char * output;
output = malloc(inputLength*4+1);
/* Worst case, we render a character like \x01 -- 4 characters */
if (output != NULL) {
unsigned int inputCursor, outputCursor;
for (inputCursor = 0, outputCursor = 0;
inputCursor < inputLength;
++inputCursor) {
if (0) {
} else if (input[inputCursor] == '\\') {
output[outputCursor++] = '\\';
output[outputCursor++] = '\\';
} else if (input[inputCursor] == '\n') {
output[outputCursor++] = '\\';
output[outputCursor++] = 'n';
} else if (input[inputCursor] == '\t') {
output[outputCursor++] = '\\';
output[outputCursor++] = 't';
} else if (input[inputCursor] == '\a') {
output[outputCursor++] = '\\';
output[outputCursor++] = 'a';
} else if (input[inputCursor] == '\r') {
output[outputCursor++] = '\\';
output[outputCursor++] = 'r';
} else if (isprint(input[inputCursor])) {
output[outputCursor++] = input[inputCursor];
} else {
snprintf(&output[outputCursor], 5, "\\x%02x",
input[inputCursor]);
outputCursor += 4;
}
}
output[outputCursor++] = '\0';
}
return output;
}
const char *
xmlrpc_makePrintable(const char * const input) {
/*----------------------------------------------------------------------------
Convert an arbitrary string of characters (NUL-terminated, though) to
printable ASCII. E.g. convert newlines to "\n".
Return the result in newly malloc'ed storage. Return NULL if we can't
get the storage.
-----------------------------------------------------------------------------*/
return xmlrpc_makePrintable_lp(input, strlen(input));
}
const char *
xmlrpc_makePrintableChar(char const input) {
/*----------------------------------------------------------------------------
Return an ASCIIZ string consisting of the character 'input',
properly escaped so as to be printable. E.g., in C notation, '\n'
turns into "\\n"
-----------------------------------------------------------------------------*/
const char * retval;
if (input == '\0')
retval = strdup("\\0");
else {
char buffer[2];
buffer[0] = input;
buffer[1] = '\0';
retval = xmlrpc_makePrintable(buffer);
}
return retval;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.