1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-06-16 06:15:24 +00:00

Feature: Support alternative quoting with %#H

This commit is contained in:
Philipp Schafft 2018-09-12 09:14:27 +00:00
parent ceab1c8cf5
commit db51153683

View File

@ -445,6 +445,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
int block_size = 0; int block_size = 0;
int block_len = 0; int block_len = 0;
int block_space = 0; int block_space = 0;
int block_alt = 0;
const char * arg; const char * arg;
char buf[80]; char buf[80];
@ -457,6 +458,7 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
block_size = 0; block_size = 0;
block_len = 0; block_len = 0;
block_space = 0; block_space = 0;
block_alt = 0;
} }
else else
{ {
@ -485,6 +487,9 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
case ' ': case ' ':
block_space = 1; block_space = 1;
break; break;
case '#':
block_alt = 1;
break;
case '1': case '1':
case '2': case '2':
case '3': case '3':
@ -544,15 +549,36 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
// TODO. // TODO.
if (!arg) if (!arg)
arg = va_arg(ap, const char *); arg = va_arg(ap, const char *);
if (!arg) if (*format != 'H') {
block_alt = 0;
}
if (!arg && !block_alt)
arg = "(null)"; arg = "(null)";
if (!block_len) if (!block_len) {
block_len = strlen(arg); if (arg) {
block_len = strlen(arg) + ((block_alt) ? 2 : 0);
} else {
block_len = 1;
}
}
// the if() is the outer structure so the inner for() // the if() is the outer structure so the inner for()
// is branch optimized. // is branch optimized.
if (*format == 'H') if (*format == 'H' && !arg)
{ {
if (size && block_len) {
*(str++) = '-';
size--;
block_len--;
}
}
else if (*format == 'H')
{
if (block_alt && size && block_len) {
*(str++) = '"';
size--;
block_len--;
}
for (; *arg && block_len && size; arg++, size--, block_len--) for (; *arg && block_len && size; arg++, size--, block_len--)
{ {
if ((*arg <= '"' || *arg == '`' || *arg == '\\') && !(block_space && *arg == ' ')) { if ((*arg <= '"' || *arg == '`' || *arg == '\\') && !(block_space && *arg == ' ')) {
@ -572,6 +598,11 @@ static void __vsnprintf(char *str, size_t size, const char *format, va_list ap)
*(str++) = *arg; *(str++) = *arg;
} }
} }
if (block_alt && size && block_len) {
*(str++) = '"';
size--;
block_len--;
}
} }
else else
{ {