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 "<>");
108 * String constant that embeds a UTF-8 encoded degree symbol (U+00B0).
109 * It is used to suffix by the default implementation of #AdgADim to
110 * suffix the set value, but can be also used manually:
113 * adg_dim_set_value(dim, "<>" ADG_UTF8_DEGREE);
118 #include "adg-internal.h"
119 #include "adg-utils.h"
124 #if GLIB_CHECK_VERSION(2, 16, 0)
128 * @str1: a C string or %NULL
129 * @str2: another C string or %NULL
131 * Compares @str1 and @str2 like strcmp(). Handles %NULL
132 * gracefully by sorting it before non-%NULL strings.
133 * This is a backward compatibility fallback for GLib
136 * Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
139 g_strcmp0(const char *str1
, const char *str2
)
142 return -(str1
!= str2
);
147 return strcmp(str1
, str2
);
152 * adg_is_string_empty:
153 * @str: the subject string
155 * Checks if @str is an empty string, that is if is %NULL or if
156 * its first character is %'\0'.
158 * Returns: %TRUE if @str is an empty string, %FALSE otherwise
161 adg_is_string_empty(const gchar
*str
)
163 return str
== NULL
|| str
[0] == '\0';
168 * @value: the enum value to check
169 * @enum_type: a #GEnum based type
171 * Checks if @value is a valid @enum_type value.
173 * Returns: %TRUE if @value is a valid @enum_type, %FALSE otherwise
176 adg_is_enum_value(int value
, GType enum_type
)
178 GEnumClass
*enum_class
;
181 enum_class
= g_type_class_ref(enum_type
);
182 g_return_val_if_fail(enum_class
!= NULL
, FALSE
);
186 if (value
>= enum_class
->minimum
&& value
<= enum_class
->maximum
) {
187 GEnumValue
*enum_value
;
190 for (n
= 0; !found
&& n
< enum_class
->n_values
; ++n
) {
191 enum_value
= enum_class
->values
+ n
;
192 found
= value
== enum_value
->value
;
196 g_type_class_unref(enum_class
);
202 * adg_is_boolean_value:
203 * @value: the gboolean value to check
205 * Checks if @value is a valid #gboolean value, that is if it is %TRUE
206 * or %FALSE. No other values are accepted.
208 * Returns: %TRUE if @value is a valid #gboolean, %FALSE otherwise
211 adg_is_boolean_value(gboolean value
)
213 return value
== TRUE
|| value
== FALSE
;
217 * adg_string_replace:
218 * @str: the original string
219 * @from: the substring to replace
220 * @to: the replacement string
222 * Replaces @from with @to inside @str and returns the result as a
223 * newly allocated string.
225 * @str and @from must be non-null valid C strings while @to can be
226 * %NULL, in which case an empty string ("") will be implied.
228 * Returns: a newly allocated string to be freed with g_free() or
232 adg_string_replace(const gchar
*str
, const gchar
*from
, const gchar
*to
)
236 gchar
*ptr
, *old_result
;
238 g_return_val_if_fail(str
!= NULL
, NULL
);
239 g_return_val_if_fail(from
!= NULL
, NULL
);
241 from_len
= strlen(from
);
243 g_return_val_if_fail(from_len
> 0, NULL
);
248 result
= g_strdup(str
);
250 while ((ptr
= strstr(result
, from
)) != NULL
) {
253 result
= g_strconcat(old_result
, to
, ptr
+ from_len
, NULL
);