Debian Codegraph @

geanygendoc/src/ggd-doc-setting.c

raw
1 /*
2 *
3 * GeanyGenDoc, a Geany plugin to ease generation of source code documentation
4 * Copyright (C) 2010-2011 Colomban Wendling <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21
22 #include "ggd-doc-setting.h"
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <ctpl/ctpl.h>
28 #include <geanyplugin.h>
29
30
31 /**
32 * ggd_doc_setting_new:
33 * @match: A setting match pattern
34 *
35 * Creates a new #GgdDocSetting.
36 *
37 * Returns: The newly created #GgdDocSetting that should be unref'd with
38 * ggd_doc_setting_unref() when no longer needed.
39 */
40 GgdDocSetting *
41 ggd_doc_setting_new (const gchar *match)
42 {
43 GgdDocSetting *setting;
44
45 setting = g_slice_alloc (sizeof *setting);
46 setting->ref_count = 1;
47 setting->match = g_strdup (match);
48 setting->template = NULL;
49 setting->position = GGD_POS_BEFORE;
50 setting->policy = GGD_POLICY_KEEP;
51 setting->merge_children = FALSE;
52 setting->matches = tm_tag_max_t;
53 setting->autodoc_children = FALSE;
54
55 /*g_debug ("New setting matching '%s'", match);*/
56
57 return setting;
58 }
59
60 /**
61 * ggd_doc_setting_ref:
62 * @setting: A #GgdDocSetting
63 *
64 * Adds a reference to a #GgdDocSetting.
65 *
66 * Returns: The setting.
67 */
68 GgdDocSetting *
69 ggd_doc_setting_ref (GgdDocSetting *setting)
70 {
71 g_return_val_if_fail (setting != NULL, NULL);
72
73 g_atomic_int_inc (&setting->ref_count);
74
75 return setting;
76 }
77
78 /**
79 * ggd_doc_setting_unref:
80 * @setting: A #GgdDocSetting
81 *
82 * Drops a reference from a #GgdDocSetting. When its reference count drops to 0,
83 * the setting is destroyed.
84 */
85 void
86 ggd_doc_setting_unref (GgdDocSetting *setting)
87 {
88 g_return_if_fail (setting != NULL);
89
90 if (g_atomic_int_dec_and_test (&setting->ref_count)) {
91 g_free (setting->match);
92 ctpl_token_free (setting->template);
93 g_slice_free1 (sizeof *setting, setting);
94 }
95 }
96
97 /**
98 * ggd_doc_setting_dump:
99 * @setting: A #GgdDocSetting
100 * @stream: A file stream to which write the dump
101 *
102 * Dumps a #GgdDocSetting in a human-readable form. This is mostly meant for
103 * debugging purposes.
104 */
105 void
106 ggd_doc_setting_dump (const GgdDocSetting *setting,
107 FILE *stream)
108 {
109 g_return_if_fail (setting != NULL);
110
111 fprintf (stream, " %s [%p]:\n"
112 " template: %p\n"
113 " position: %u\n"
114 " policy: %u\n"
115 " auto-doc-children: %s\n",
116 setting->match, (gpointer)setting,
117 (gpointer)setting->template,
118 setting->position,
119 setting->policy,
120 setting->autodoc_children ? "True" : "False");
121 }
122
123 /**
124 * ggd_doc_setting_matches:
125 * @setting: A #GgdDocSetting
126 * @match: A match pattern
127 * @match_len: Length of @match or -1 if zero-terminated.
128 *
129 * Checks whether a #GgdDocSetting matches a given pattern.
130 *
131 * Returns: %TRUE if @setting matches the given pattern, %FALSE otherwise
132 */
133 gboolean
134 ggd_doc_setting_matches (const GgdDocSetting *setting,
135 const gchar *match,
136 gssize match_len)
137 {
138 gssize intern_i = strlen (setting->match);
139 gssize extern_i = match_len >= 0 ? match_len : (gssize)strlen (match);
140 gboolean matches = TRUE;
141
142 g_return_val_if_fail (setting != NULL, FALSE);
143
144 for (; intern_i >= 0 && extern_i >= 0 && matches; intern_i--, extern_i--) {
145 matches = setting->match[intern_i] == match[extern_i];
146 }
147
148 return matches && intern_i <= 0;
149 }
150
151
152
153 /**
154 * ggd_position_from_string:
155 * @string: A string representing a #GgdPosition
156 *
157 * Translates a string to a #GgdPosition.
158 *
159 * Returns: The corresponding #GgdPosition, or -1 on error.
160 */
161 GgdPosition
162 ggd_position_from_string (const gchar *string)
163 {
164 static const struct
165 {
166 const gchar *name;
167 GgdPosition pos;
168 } positions[] = {
169 { "BEFORE", GGD_POS_BEFORE },
170 { "AFTER", GGD_POS_AFTER },
171 { "CURSOR", GGD_POS_CURSOR }
172 };
173 guint i;
174
175 g_return_val_if_fail (string != NULL, -1);
176
177 for (i = 0; i < G_N_ELEMENTS (positions); i++) {
178 if (strcmp (string, positions[i].name) == 0) {
179 /*g_debug ("matching position name: %s", positions[i].name);*/
180 return positions[i].pos;
181 }
182 }
183
184 return -1;
185 }
186
187 /**
188 * ggd_policy_from_string:
189 * @string: A string representing a #GgdPolicy
190 *
191 * Translates a string to a #GgdPolicy.
192 *
193 * Returns: The corresponding #GgdPolicy, or -1 on error.
194 */
195 GgdPolicy
196 ggd_policy_from_string (const gchar *string)
197 {
198 static const struct
199 {
200 const gchar *name;
201 GgdPolicy policy;
202 } policies[] = {
203 { "KEEP", GGD_POLICY_KEEP },
204 { "FORWARD", GGD_POLICY_FORWARD },
205 { "PASS", GGD_POLICY_PASS }/*,
206 { "DUPLICATE", GGD_POLICY_DUPLICATE }*/
207 };
208 guint i;
209
210 g_return_val_if_fail (string != NULL, -1);
211
212 for (i = 0; i < G_N_ELEMENTS (policies); i++) {
213 if (strcmp (string, policies[i].name) == 0) {
214 /*g_debug ("matching policy name: %s", policies[i].name);*/
215 return policies[i].policy;
216 }
217 }
218
219 return -1;
220 }
221
222 /**
223 * ggd_merge_policy_from_string:
224 * @string: A string representing a merge policy
225 *
226 * Translates a string to a merge policy.
227 *
228 * Returns: The merge policy, or -1 on error.
229 */
230 gboolean
231 ggd_merge_policy_from_string (const gchar *string)
232 {
233 static const struct
234 {
235 const gchar *name;
236 gboolean merge;
237 } policies[] = {
238 { "MERGE", TRUE },
239 { "SPLIT", FALSE }
240 };
241 guint i;
242
243 g_return_val_if_fail (string != NULL, -1);
244
245 for (i = 0; i < G_N_ELEMENTS (policies); i++) {
246 if (strcmp (string, policies[i].name) == 0) {
247 /*g_debug ("matching merge policy name: %s", policies[i].name);*/
248 return policies[i].merge;
249 }
250 }
251
252 return -1;
253 }
254
255 /**
256 * ggd_match_from_string:
257 * @name: A string representing a match
258 *
259 * Translates a string to a match.
260 *
261 * Returns: The match, or 0 on error.
262 */
263 /* documents a define found in the header */