1 /* $Id$
2  *
3  * Lasso - A free implementation of the Liberty Alliance specifications.
4  *
5  * Copyright (C) 2004-2007 Entr'ouvert
6  * http://lasso.entrouvert.org
7  *
8  * Authors: See AUTHORS file in top-level directory.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <glib.h>
28 #include "utils.h"
29 
30 /**
31  * SECTION:utilities
32  * @short_description: Misc functions used internally in Lasso
33  * @stability: Internal
34  * @include: utils.h
35  */
36 
37 /**
38  * lasso_safe_prefix_string:
39  * @str: a C string
40  * @length: the maximum length of an extract of the string
41  *
42  * Produce a limite length safe extract of a string, for debugging purpose. Special characters are
43  * replaced by their C string 'quoting'.
44  *
45  * Return value: a C string, of size < @length where newline, carriage returns and tabs are replaced
46  * by their C quotes.
47  */
48 gchar*
lasso_safe_prefix_string(const gchar * str,gsize length)49 lasso_safe_prefix_string(const gchar *str, gsize length)
50 {
51 	GString *output;
52 	gchar *ret;
53 	gsize outputted = 0, i = 0;
54 
55 	if (str == NULL) {
56 		return strdup("NULL");
57 	}
58 	output = g_string_sized_new(length);
59 	for (i = 0; i < length && str[i] && outputted < length; i++) {
60 		gchar c = 0;
61 		guint len;
62 
63 		if ((guchar)str[i] < 128 && (guchar)str[i] > 31) {
64 			g_string_append_c(output, str[i]);
65 			outputted++;
66 			continue;
67 		}
68 		switch (str[i]) {
69 			case '\n':
70 				c = 'n';
71 				break;
72 			case '\t':
73 				c = 't';
74 				break;
75 			case '\r':
76 				c = 'r';
77 		}
78 		if (c) {
79 			if (outputted - length > 1) {
80 				g_string_append_c(output, '\\');
81 				g_string_append_c(output, c);
82 				outputted += 2;
83 				continue;
84 			}
85 		}
86 		if (c < 8) {
87 			len = 3;
88 		} else if (c < 64) {
89 			len = 4;
90 		} else {
91 			len = 5;
92 		}
93 		if (outputted - length >= len) {
94 			g_string_append_c(output, '\\');
95 			g_string_append_printf(output, "%o", (guint)str[i]);
96 		}
97 		break;
98 	}
99 	ret = output->str;
100 	lasso_release_gstring(output, FALSE);
101 	return ret;
102 }
103 
104 /**
105  * lasso_gobject_is_of_type:
106  * @a: a #GObject object
107  * @b: a #GType value
108  *
109  * Return true if object @a is of type @b.
110  *
111  * Return value: whether object @a is of type @b.
112  */
113 int
lasso_gobject_is_of_type(GObject * a,GType b)114 lasso_gobject_is_of_type(GObject *a, GType b)
115 {
116 	GType typeid = (GType)b;
117 
118 	if (a && G_IS_OBJECT(a)) {
119 		return G_OBJECT_TYPE(G_OBJECT(a)) == typeid ? 0 : 1;
120 	}
121 	return 1;
122 }
123 
124 GObject*
lasso_extract_gtype_from_list(GType type,GList * list)125 lasso_extract_gtype_from_list(GType type, GList *list)
126 {
127 	GList *needle;
128 
129 	needle = g_list_find_custom(list, (gconstpointer)type, (GCompareFunc)lasso_gobject_is_of_type);
130 	if (needle) {
131 		return needle->data;
132 	}
133 	return NULL;
134 }
135 
136 /**
137  * lasso_extract_gtype_from_list_or_new:
138  * @type: a #GType
139  * @list: a pointer to a #GList pointer variable
140  * @create: whether to look up an object whose #GType is type, or to just create it.
141  *
142  * If create is TRUE, add a new object of type @type to @list and return it.
143  * Otherwise try to look up an object of type @type, and if none is found add a new one and return
144  * it.
145  *
146  * Return value: a #GObject of type @type.
147  */
148 GObject *
lasso_extract_gtype_from_list_or_new(GType type,GList ** list,gboolean create)149 lasso_extract_gtype_from_list_or_new(GType type, GList **list, gboolean create)
150 {
151 	GObject *result = NULL;
152 	g_assert (list);
153 
154 	if (! create) {
155 		result = lasso_extract_gtype_from_list(type, *list);
156 	}
157 	if (result == NULL) {
158 		result = g_object_new(type, NULL);
159 		lasso_list_add_new_gobject(*list, result);
160 	}
161 	return result;
162 }
163