mirror of https://github.com/djcb/mu.git
* mu-output.c, mu-str.[ch]: fix escaping for JSON, sexps (add mu_str_escape_c_literal)
This commit is contained in:
parent
cafba1327f
commit
f8af665151
|
@ -285,8 +285,8 @@ print_attr_json (const char* elm, const char *str, gboolean comma)
|
|||
|
||||
if (!str || strlen(str) == 0)
|
||||
return; /* empty: don't include */
|
||||
|
||||
esc = g_strescape (str, NULL);
|
||||
|
||||
esc = mu_str_escape_c_literal (str);
|
||||
g_print ("\t\t\t\"%s\":\"%s\"%s\n", elm, esc, comma ? "," : "");
|
||||
g_free (esc);
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ print_attr_sexp (const char* elm, const char *str, gboolean nl)
|
|||
if (!str || strlen(str) == 0)
|
||||
return; /* empty: don't include */
|
||||
|
||||
esc = g_strescape (str, NULL);
|
||||
esc = mu_str_escape_c_literal (str);
|
||||
g_print ("\t\t\t(:%s \"%s\")%s", elm, esc, nl ? "\n" : "");
|
||||
g_free (esc);
|
||||
}
|
||||
|
|
20
src/mu-str.c
20
src/mu-str.c
|
@ -368,3 +368,23 @@ mu_str_fullpath_s (const char* path, const char* name)
|
|||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_str_escape_c_literal (const gchar* str)
|
||||
{
|
||||
const char* cur;
|
||||
GString *tmp;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
|
||||
tmp = g_string_sized_new (2 * strlen(str));
|
||||
for (cur = str; *cur; ++cur)
|
||||
switch (*cur) {
|
||||
case '\\': tmp = g_string_append (tmp, "\\\\");
|
||||
case '\"': tmp = g_string_append (tmp, "\\\"");
|
||||
default: tmp = g_string_append_c (tmp, *cur);
|
||||
}
|
||||
|
||||
return g_string_free (tmp, FALSE);
|
||||
}
|
||||
|
|
10
src/mu-str.h
10
src/mu-str.h
|
@ -221,6 +221,16 @@ time_t mu_str_date_parse_hdwmy (const char* str);
|
|||
const char* mu_str_fullpath_s (const char* path, const char* name);
|
||||
|
||||
|
||||
/**
|
||||
* escape a string like a string literal in C; ie. replace \ with \\,
|
||||
* and " with \"
|
||||
*
|
||||
* @param str a non-NULL str
|
||||
*
|
||||
* @return the escaped string, newly allocated (free with g_free)
|
||||
*/
|
||||
char* mu_str_escape_c_literal (const gchar* str);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /*__MU_STR_H__*/
|
||||
|
|
Loading…
Reference in New Issue