simple-discuss/src/utils.c

48 lines
1.0 KiB
C
Raw Normal View History

2023-07-03 14:58:09 +03:00
#include <stdlib.h>
2023-07-03 20:25:23 +03:00
#include <string.h>
2023-07-03 14:58:09 +03:00
#include "string_buffer.h"
#include "utils.h"
2023-07-03 20:25:23 +03:00
int
contain_special(const char *s)
{
return strchr(s, '&') || strchr(s, '"') || strchr(s, '\'') || strchr(s, '<') || strchr(s, '>');
}
2023-07-03 14:58:09 +03:00
char *
mk_specialchars(const char *input)
{
StringBuffer _buffer, *buffer;
char c;
char *retval;
buffer = &_buffer;
sb_init_empty(buffer);
while ((c = *(input++))) {
switch (c) {
case '&':
sb_add_string(buffer, "&amp;");
break;
case '"':
sb_add_string(buffer, "&quot;");
break;
case '\'':
sb_add_string(buffer, "&#039;");
break;
case '<':
sb_add_string(buffer, "&lt;");
break;
case '>':
sb_add_string(buffer, "&gt;");
break;
default:
sb_add_char(buffer, c);
}
}
SB_MOVE(buffer, retval);
return retval;
}