summaryrefslogtreecommitdiffstats
path: root/lasso/utils.c
blob: 7a97be3182f13b976151a3ddf5fe472150d49ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* $Id$
 *
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004-2007 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: See AUTHORS file in top-level directory.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "./utils.h"

/**
 * SECTION:utilities
 * @short_description: Misc functions used internally in Lasso
 * @stability: Internal
 * @include: utils.h
 */

/**
 * lasso_safe_prefix_string:
 * @str: a C string
 * @length: the maximum length of an extract of the string
 *
 * Produce a limite length safe extract of a string, for debugging purpose. Special characters are
 * replaced by their C string 'quoting'.
 *
 * Return value: a C string, of size < @length where newline, carriage returns and tabs are replaced
 * by their C quotes.
 */
gchar*
lasso_safe_prefix_string(const gchar *str, gsize length)
{
	GString *output;
	gchar *ret;
	gsize outputted = 0, i = 0;

	if (str == NULL) {
		return strdup("NULL");
	}
	output = g_string_sized_new(length);
	for (i = 0; i < length && str[i] && outputted < length; i++) {
		gchar c = 0;
		guint len;

		if ((guchar)str[i] < 128 && (guchar)str[i] > 31) {
			g_string_append_c(output, str[i]);
			outputted++;
			continue;
		}
		switch (str[i]) {
			case '\n':
				c = 'n';
				break;
			case '\t':
				c = 't';
				break;
			case '\r':
				c = 'r';
		}
		if (c) {
			if (outputted - length > 1) {
				g_string_append_c(output, '\\');
				g_string_append_c(output, c);
				outputted += 2;
				continue;
			}
		}
		if (c < 8) {
			len = 3;
		} else if (c < 64) {
			len = 4;
		} else {
			len = 5;
		}
		if (outputted - length >= len) {
			g_string_append_c(output, '\\');
			g_string_append_printf(output, "%o", (guint)str[i]);
		}
		break;
	}
	ret = output->str;
	lasso_release_gstring(output, FALSE);
	return ret;
}

/**
 * lasso_gobject_is_of_type:
 * @a: a #GObject object
 * @b: a #GType value
 *
 * Return true if object @a is of type @b.
 *
 * Return value: whether object @a is of type @b.
 */
int
lasso_gobject_is_of_type(GObject *a, GType b)
{
	GType typeid = (GType)b;

	if (a && G_IS_OBJECT(a)) {
		return G_OBJECT_TYPE(G_OBJECT(a)) == typeid ? 0 : 1;
	}
	return 1;
}

GObject*
lasso_extract_gtype_from_list(GType type, GList *list)
{
	GList *needle;


	needle = g_list_find_custom(list, (gconstpointer)type, (GCompareFunc)lasso_gobject_is_of_type);
	if (needle) {
		return needle->data;
	}
	return NULL;
}