diff options
author | Davide Beatrici <davidebeatrici@users.noreply.github.com> | 2016-05-27 20:40:25 +0300 |
---|---|---|
committer | Mikkel Krautz <mikkel@krautz.dk> | 2016-05-29 01:57:57 +0300 |
commit | 7591b5c153d3ca59c7cb97f71bb6c046d4489ae4 (patch) | |
tree | f7e117b8ac7f7877baace7dbaa56361382c3c36c /plugins/mumble_plugin_utils.h | |
parent | decab3ffcb69cfc33ccaa8621eb0816505eba4f2 (diff) |
Move "escape" function to mumble_plugin_utils.h header
Diffstat (limited to 'plugins/mumble_plugin_utils.h')
-rw-r--r-- | plugins/mumble_plugin_utils.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/plugins/mumble_plugin_utils.h b/plugins/mumble_plugin_utils.h new file mode 100644 index 000000000..6dd76a9e8 --- /dev/null +++ b/plugins/mumble_plugin_utils.h @@ -0,0 +1,45 @@ +// Copyright 2005-2016 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at <https://www.mumble.info/LICENSE>. + +#ifndef MUMBLE_MUMBLE_PLUGIN_UTILS_H_ +#define MUMBLE_MUMBLE_PLUGIN_UTILS_H_ + +// escape lossily converts the given +// string to ASCII, replacing any +// character not within the printable +// ASCII region (32-126) with an ASCII +// space character. +// +// escape also replaces any double quote +// characters with an ASCII space. This +// allows the string to be safely used +// when constructing JSON documents via +// string concatenation. +static void escape(char *str) { + char *c = str; + + while (*c != '\0') { + // For JSON compatibility, the string + // can't contain double quotes. + // If a double quote is found, replace + // it with an ASCII space. + if (*c == '"') { + *c = ' '; + } + + // Ensure the string is within printable + // ASCII. If not, replace the offending + // byte with an ASCII space. + if (*c < 32) { + *c = ' '; + } else if (*c > 126) { + *c = ' '; + } + + c += 1; + } +} + +#endif |