1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 Nicola Fontana <ntd at entidi.it>
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * @Section_Id:utilities
25 * @short_description: Assorted macros and functions
27 * Collection of macros and functions that do not fit inside any other topic.
32 * @id: The name of a struct
34 * Forward declaration of struct @id. It is equivalent to a typical
35 * struct forward declaration, for example:
38 * ADG_FORWARD_DECL(test)
44 * typedef struct _test test
47 * This macro is needed to fake <command>gtk-doc</command>, because
48 * up to now (v.1.12) it generates two conflicting links when using
49 * forward declarations: the first in the source with the declaration
50 * and the second where the type is defined. Using ADG_FORWARD_DECL()
51 * instead of the usual typedef avoids the parsing of the declaration
52 * in the first file (<command>gtk-doc</command> is not able to do C
55 * The same principle can be applied in the definition file. Following
56 * the previous example, you can use something like this where struct
61 * // This is declared in another file
62 * typedef struct _type type;
73 * Symbolic constant for the right direction (in radians).
79 * Symbolic constant for the down direction (in radians).
85 * Symbolic constant for the left direction (in radians).
91 * Symbolic constant for the up direction (in radians).
97 * String constant that embeds a UTF-8 encoded diameter (U+2300).
98 * It can be used to prefix diameter quotes, such as:
101 * adg_dim_set_value(dim, ADG_UTF8_DIAMETER "<>");
106 #include "adg-internal.h"
107 #include "adg-utils.h"
112 #if GLIB_CHECK_VERSION(2, 16, 0)
116 * @str1: a C string or %NULL
117 * @str2: another C string or %NULL
119 * Compares @str1 and @str2 like strcmp(). Handles %NULL
120 * gracefully by sorting it before non-%NULL strings.
121 * This is a backward compatibility fallback for GLib
124 * Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
127 g_strcmp0(const char *str1
, const char *str2
)
130 return -(str1
!= str2
);
135 return strcmp(str1
, str2
);
140 * adg_is_string_empty:
141 * @str: the subject string
143 * Checks if @str is an empty string, that is if is %NULL or if
144 * its first character is %'\0'.
146 * Returns: %TRUE if @str is an empty string, %FALSE otherwise
149 adg_is_string_empty(const gchar
*str
)
151 return str
== NULL
|| str
[0] == '\0';
156 * @value: the enum value to check
157 * @enum_type: a #GEnum based type
159 * Checks if @value is a valid @enum_type value.
161 * Returns: %TRUE if @value is a valid @enum_type, %FALSE otherwise
164 adg_is_enum_value(int value
, GType enum_type
)
166 GEnumClass
*enum_class
;
169 enum_class
= g_type_class_ref(enum_type
);
170 g_return_val_if_fail(enum_class
!= NULL
, FALSE
);
174 if (value
>= enum_class
->minimum
&& value
<= enum_class
->maximum
) {
175 GEnumValue
*enum_value
;
178 for (n
= 0; !found
&& n
< enum_class
->n_values
; ++n
) {
179 enum_value
= enum_class
->values
+ n
;
180 found
= value
== enum_value
->value
;
184 g_type_class_unref(enum_class
);
190 * adg_is_boolean_value:
191 * @value: the gboolean value to check
193 * Checks if @value is a valid #gboolean value, that is if it is %TRUE
194 * or %FALSE. No other values are accepted.
196 * Returns: %TRUE if @value is a valid #gboolean, %FALSE otherwise
199 adg_is_boolean_value(gboolean value
)
201 return value
== TRUE
|| value
== FALSE
;
205 * adg_string_replace:
206 * @str: the original string
207 * @from: the substring to replace
208 * @to: the replacement string
210 * Replaces @from with @to inside @str and returns the result as a
211 * newly allocated string.
213 * @str and @from must be non-null valid C strings while @to can be
214 * %NULL, in which case an empty string ("") will be implied.
216 * Returns: a newly allocated string to be freed with g_free() or
220 adg_string_replace(const gchar
*str
, const gchar
*from
, const gchar
*to
)
224 gchar
*ptr
, *old_result
;
226 g_return_val_if_fail(str
!= NULL
, NULL
);
227 g_return_val_if_fail(from
!= NULL
, NULL
);
229 from_len
= strlen(from
);
231 g_return_val_if_fail(from_len
> 0, NULL
);
236 result
= g_strdup(str
);
238 while ((ptr
= strstr(result
, from
)) != NULL
) {
241 result
= g_strconcat(old_result
, to
, ptr
+ from_len
, NULL
);