[tests] Added test-canvas
[adg.git] / src / adg / adg-util.c
blobe9468836bf775fb3434dd753aea934692d989d08
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.
21 /**
22 * SECTION:adg-util
23 * @Section_Id:utilities
24 * @title: Utilities
25 * @short_description: Assorted macros and functions
27 * Collection of macros and functions that do not fit inside any other topic.
28 **/
30 /**
31 * ADG_DIR_RIGHT:
33 * Symbolic constant for the right direction (in radians).
34 **/
36 /**
37 * ADG_DIR_DOWN:
39 * Symbolic constant for the down direction (in radians).
40 **/
42 /**
43 * ADG_DIR_LEFT:
45 * Symbolic constant for the left direction (in radians).
46 **/
48 /**
49 * ADG_DIR_UP:
51 * Symbolic constant for the up direction (in radians).
52 **/
54 /**
55 * ADG_FORWARD_DECL:
56 * @id: The name of a struct
58 * Forward declaration of struct @id. It is equivalent to a typical
59 * struct forward declaration, for example:
61 * |[
62 * ADG_FORWARD_DECL(test)
63 * ]|
65 * will expand to:
67 * |[
68 * typedef struct _test test
69 * ]|
71 * This macro is needed to fake <command>gtk-doc</command>, because
72 * up to now (v.1.12) it generates two conflicting links when using
73 * forward declarations: the first in the source with the declaration
74 * and the second where the type is defined. Using ADG_FORWARD_DECL()
75 * instead of the usual typedef avoids the parsing of the declaration
76 * in the first file (<command>gtk-doc</command> is not able to do C
77 * preprocessing).
79 * The same principle can be applied in the definition file. Following
80 * the previous example, you can use something like this where struct
81 * _type is defined:
83 * |[
84 * #if 0
85 * // This is declared in another file
86 * typedef struct _type type;
87 * #endif
88 * struct _type {
89 * ...
90 * };
91 * ]|
92 **/
95 #include "adg-internal.h"
96 #include "adg-util.h"
97 #include <string.h>
98 #include <limits.h>
101 #if GLIB_CHECK_VERSION(2, 16, 0)
102 #else
104 * g_strcmp0:
105 * @str1: a C string or %NULL
106 * @str2: another C string or %NULL
108 * Compares @str1 and @str2 like strcmp(). Handles %NULL
109 * gracefully by sorting it before non-%NULL strings.
110 * This is a backward compatibility fallback for GLib
111 * prior to 2.16.0
113 * Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
116 g_strcmp0(const char *str1, const char *str2)
118 if (!str1)
119 return -(str1 != str2);
121 if (!str2)
122 return str1 != str2;
124 return strcmp(str1, str2);
126 #endif
129 * adg_is_string_empty:
130 * @str: the subject string
132 * Checks if @str is an empty string, that is if is %NULL or if
133 * its first character is %'\0'.
135 * Returns: %TRUE if @str is an empty string, %FALSE otherwise
137 gboolean
138 adg_is_string_empty(const gchar *str)
140 return str == NULL || str[0] == '\0';
144 * adg_is_enum_value:
145 * @value: the enum value to check
146 * @enum_type: a #GEnum based type
148 * Checks if @value is a valid @enum_type value.
150 * Returns: %TRUE if @value is a valid @enum_type, %FALSE otherwise
152 gboolean
153 adg_is_enum_value(int value, GType enum_type)
155 GEnumClass *enum_class;
156 gboolean found;
158 enum_class = g_type_class_ref(enum_type);
159 g_return_val_if_fail(enum_class != NULL, FALSE);
161 found = FALSE;
163 if (value >= enum_class->minimum && value <= enum_class->maximum) {
164 GEnumValue *enum_value;
165 guint n;
167 for (n = 0; !found && n < enum_class->n_values; ++n) {
168 enum_value = enum_class->values + n;
169 found = value == enum_value->value;
173 g_type_class_unref(enum_class);
175 return found;
179 * adg_is_boolean_value:
180 * @value: the gboolean value to check
182 * Checks if @value is a valid #gboolean value, that is if it is %TRUE
183 * or %FALSE. No other values are accepted.
185 * Returns: %TRUE if @value is a valid #gboolean, %FALSE otherwise
187 gboolean
188 adg_is_boolean_value(gboolean value)
190 return value == TRUE || value == FALSE;