1/* Pango
2 * test-common.c: Common test code
3 *
4 * Copyright (C) 2014 Red Hat, Inc
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <glib.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <locale.h>
27
28#ifdef G_OS_WIN32
29#include <io.h>
30#else
31#include <unistd.h>
32#endif
33
34#include <pango/pangocairo.h>
35#include "test-common.h"
36
37char *
38diff_with_file (const char *file,
39 char *text,
40 gssize len,
41 GError **error)
42{
43 const char *command[] = { "diff", "-u", "-i", file, NULL, NULL };
44 char *diff, *tmpfile;
45 int fd;
46
47 diff = NULL;
48
49 if (len < 0)
50 len = strlen (s: text);
51
52 /* write the text buffer to a temporary file */
53 fd = g_file_open_tmp (NULL, name_used: &tmpfile, error);
54 if (fd < 0)
55 return NULL;
56
57 if (write (fd: fd, buf: text, n: len) != (int) len)
58 {
59 close (fd: fd);
60 g_set_error (err: error,
61 G_FILE_ERROR, code: G_FILE_ERROR_FAILED,
62 format: "Could not write data to temporary file '%s'", tmpfile);
63 goto done;
64 }
65 close (fd: fd);
66 command[4] = tmpfile;
67
68 /* run diff command */
69 g_spawn_sync (NULL,
70 argv: (char **) command,
71 NULL,
72 flags: G_SPAWN_SEARCH_PATH,
73 NULL, NULL,
74 standard_output: &diff,
75 NULL, NULL,
76 error);
77
78done:
79 unlink (name: tmpfile);
80 g_free (mem: tmpfile);
81
82 return diff;
83}
84
85char *
86diff_bytes (GBytes *b1,
87 GBytes *b2,
88 GError **error)
89{
90 const char *command[] = { "diff", "-u", "-i", NULL, NULL, NULL };
91 char *diff, *tmpfile, *tmpfile2;
92 int fd;
93 const char *text;
94 gsize len;
95
96 /* write the text buffer to a temporary file */
97 fd = g_file_open_tmp (NULL, name_used: &tmpfile, error);
98 if (fd < 0)
99 return NULL;
100
101 text = (const char *) g_bytes_get_data (bytes: b1, size: &len);
102 if (write (fd: fd, buf: text, n: len) != (int) len)
103 {
104 close (fd: fd);
105 g_set_error (err: error,
106 G_FILE_ERROR, code: G_FILE_ERROR_FAILED,
107 format: "Could not write data to temporary file '%s'", tmpfile);
108 goto done;
109 }
110 close (fd: fd);
111
112 fd = g_file_open_tmp (NULL, name_used: &tmpfile2, error);
113 if (fd < 0)
114 return NULL;
115
116 text = (const char *) g_bytes_get_data (bytes: b2, size: &len);
117 if (write (fd: fd, buf: text, n: len) != (int) len)
118 {
119 close (fd: fd);
120 g_set_error (err: error,
121 G_FILE_ERROR, code: G_FILE_ERROR_FAILED,
122 format: "Could not write data to temporary file '%s'", tmpfile2);
123 goto done;
124 }
125 close (fd: fd);
126
127 command[3] = tmpfile;
128 command[4] = tmpfile2;
129
130 /* run diff command */
131 g_spawn_sync (NULL,
132 argv: (char **) command,
133 NULL,
134 flags: G_SPAWN_SEARCH_PATH,
135 NULL, NULL,
136 standard_output: &diff,
137 NULL, NULL,
138 error);
139
140done:
141 unlink (name: tmpfile);
142 g_free (mem: tmpfile);
143 unlink (name: tmpfile2);
144 g_free (mem: tmpfile2);
145
146 return diff;
147}
148
149gboolean
150file_has_prefix (const char *filename,
151 const char *str,
152 GError **error)
153{
154 char *contents;
155 gsize len;
156 gboolean ret;
157
158 if (!g_file_get_contents (filename, contents: &contents, length: &len, error))
159 return FALSE;
160
161 ret = g_str_has_prefix (str: contents, prefix: str);
162
163 g_free (mem: contents);
164
165 return ret;
166}
167
168void
169print_attribute (PangoAttribute *attr, GString *string)
170{
171 PangoAttrList *l = pango_attr_list_new ();
172 char *s;
173
174 pango_attr_list_insert (list: l, attr: pango_attribute_copy (attr));
175 s = pango_attr_list_to_string (list: l);
176 g_string_append (string, val: s);
177 g_free (mem: s);
178 pango_attr_list_unref (list: l);
179}
180
181void
182print_attr_list (PangoAttrList *attrs, GString *string)
183{
184 PangoAttrIterator *iter;
185
186 if (!attrs)
187 return;
188
189 iter = pango_attr_list_get_iterator (list: attrs);
190 do {
191 gint start, end;
192 GSList *list, *l;
193
194 pango_attr_iterator_range (iterator: iter, start: &start, end: &end);
195 g_string_append_printf (string, format: "range %d %d\n", start, end);
196 list = pango_attr_iterator_get_attrs (iterator: iter);
197 for (l = list; l; l = l->next)
198 {
199 PangoAttribute *attr = l->data;
200 print_attribute (attr, string);
201 g_string_append (string, val: "\n");
202 }
203 g_slist_free_full (list, free_func: (GDestroyNotify)pango_attribute_destroy);
204 } while (pango_attr_iterator_next (iterator: iter));
205
206 pango_attr_iterator_destroy (iterator: iter);
207}
208
209void
210print_attributes (GSList *attrs, GString *string)
211{
212 GSList *l;
213
214 for (l = attrs; l; l = l->next)
215 {
216 PangoAttribute *attr = l->data;
217
218 print_attribute (attr, string);
219 g_string_append (string, val: "\n");
220 }
221}
222
223const char *
224get_script_name (GUnicodeScript s)
225{
226 GEnumClass *class;
227 GEnumValue *value;
228 const char *nick;
229
230 class = g_type_class_ref (type: g_unicode_script_get_type ());
231 value = g_enum_get_value (enum_class: class, value: s);
232 nick = value->value_nick;
233 g_type_class_unref (g_class: class);
234 return nick;
235}
236
237

source code of gtk/subprojects/pango/tests/test-common.c