1 | /* GTK - The GIMP Toolkit |
2 | * Copyright (C) 2011 Benjamin Otte <otte@gnome.org> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "config.h" |
19 | |
20 | #include "gtkcssserializerprivate.h" |
21 | |
22 | /* Escape a string so that it can be parsed |
23 | * as a css string again. |
24 | */ |
25 | void |
26 | gtk_css_print_string (GString *str, |
27 | const char *string, |
28 | gboolean multiline) |
29 | { |
30 | gsize len; |
31 | |
32 | g_return_if_fail (str != NULL); |
33 | g_return_if_fail (string != NULL); |
34 | |
35 | g_string_append_c (str, '"'); |
36 | |
37 | do { |
38 | len = strcspn (s: string, reject: "\\\"\n\r\f" ); |
39 | g_string_append_len (string: str, val: string, len); |
40 | string += len; |
41 | switch (*string) |
42 | { |
43 | case '\0': |
44 | goto out; |
45 | case '\n': |
46 | if (multiline) |
47 | g_string_append (string: str, val: "\\A\\\n" ); |
48 | else |
49 | g_string_append (string: str, val: "\\A " ); |
50 | break; |
51 | case '\r': |
52 | g_string_append (string: str, val: "\\D " ); |
53 | break; |
54 | case '\f': |
55 | g_string_append (string: str, val: "\\C " ); |
56 | break; |
57 | case '\"': |
58 | g_string_append (string: str, val: "\\\"" ); |
59 | break; |
60 | case '\\': |
61 | g_string_append (string: str, val: "\\\\" ); |
62 | break; |
63 | default: |
64 | g_assert_not_reached (); |
65 | break; |
66 | } |
67 | string++; |
68 | } while (*string); |
69 | |
70 | out: |
71 | g_string_append_c (str, '"'); |
72 | } |
73 | |