diff options
author | hunt <hunt> | 2006-05-30 15:47:58 +0000 |
---|---|---|
committer | hunt <hunt> | 2006-05-30 15:47:58 +0000 |
commit | 3a4677f9c7ca40eafc379e829f0f2a86eae7b661 (patch) | |
tree | e674cc069f78936e4e93017a7844e45e0aed5cd7 /runtime/string.c | |
parent | 78db65bdd61c837e730d5eecf00e3b4ff8da9d5b (diff) | |
download | systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.tar.gz systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.tar.xz systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.zip |
2006-05-30 Martin Hunt <hunt@redhat.com>
* string.c (_stp_text_str): New function.
* string.h (_stp_text_str): Declare.
Diffstat (limited to 'runtime/string.c')
-rw-r--r-- | runtime/string.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/runtime/string.c b/runtime/string.c index fa8e2dda..c354d86f 100644 --- a/runtime/string.c +++ b/runtime/string.c @@ -212,5 +212,104 @@ char * _stp_string_ptr (String str) } \ }) +/** Return a printable text string. + * + * Takes a string, and any ASCII characters that are not printable are + * replaced by the corresponding escape sequence in the returned + * string. + * + * @param out Output string pointer + * @param in Input string pointer + * @param len Maximum length of string to return not including terminating 0. + * 0 means MAXSTRINGLEN. + * @param quoted Put double quotes around the string. If input string is truncated + * in will have "..." after the second quote. + */ +void _stp_text_str(char *out, char *in, int len, int quoted) +{ + const int length = len; + if (len == 0 || len > STP_STRING_SIZE-1) + len = STP_STRING_SIZE-1; + if (quoted) { + len -= 2; + *out++ = '\"'; + } + + while (*in && len > 0) { + int num = 1; + if (isprint(*in) && isascii(*in)) + *out++ = *in; + else { + switch (*in) { + case '\a': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + num = 2; + break; + default: + if (*in > 077) + num = 4; + else if (*in > 07) + num = 3; + else + num = 2; + break; + } + + if (len < num) + break; + + *out++ = '\\'; + switch (*in) { + case '\a': + *out++ = 'a'; + break; + case '\b': + *out++ = 'b'; + break; + case '\f': + *out++ = 'f'; + break; + case '\n': + *out++ = 'n'; + break; + case '\r': + *out++ = 'r'; + break; + case '\t': + *out++ = 't'; + break; + case '\v': + *out++ = 'v'; + break; + default: /* output octal representation */ + if (*in > 077) + *out++ = to_oct_digit(*in >> 6); + if (*in > 07) + *out++ = to_oct_digit((*in & 070) >> 3); + *out++ = to_oct_digit(*in & 07); + break; + } + } + len -= num; + in++; + } + + if (quoted) { + if (*in) { + out = out - 3 + len; + *out++ = '\"'; + *out++ = '.'; + *out++ = '.'; + *out++ = '.'; + } else + *out++ = '\"'; + } + *out = '\0'; +} /** @} */ #endif /* _STRING_C_ */ |