| 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-type.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <glib.h> |
| 27 |
|
| 28 |
#include "ggd-doc-setting.h" |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* ggd_doc_type_new: |
| 33 |
* @name: Documentation type's name |
| 34 |
* |
| 35 |
* Creates a new documentation type representation. |
| 36 |
* |
| 37 |
* Returns: The newly created #GgdDocType. Free with ggd_doc_type_free(). |
| 38 |
*/ |
| 39 |
GgdDocType * |
| 40 |
ggd_doc_type_new (const gchar *name) |
| 41 |
{ |
| 42 |
GgdDocType *doctype; |
| 43 |
|
| 44 |
g_return_val_if_fail (name != NULL, NULL); |
| 45 |
|
| 46 |
doctype = g_slice_alloc (sizeof *doctype); |
| 47 |
doctype->ref_count = 1; |
| 48 |
doctype->name = g_strdup (name); |
| 49 |
doctype->settings = NULL; |
| 50 |
|
| 51 |
/*g_debug ("New doc type '%s' [%p]", name, doctype);*/ |
| 52 |
|
| 53 |
return doctype; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* ggd_doc_type_ref: |
| 58 |
* @doctype: A #GgdDocType |
| 59 |
* |
| 60 |
* Adds a reference to @doctype |
| 61 |
* |
| 62 |
* Returns: the given @doctype |
| 63 |
*/ |
| 64 |
GgdDocType * |
| 65 |
ggd_doc_type_ref (GgdDocType *doctype) |
| 66 |
{ |
| 67 |
g_return_val_if_fail (doctype != NULL, NULL); |
| 68 |
|
| 69 |
g_atomic_int_inc (&doctype->ref_count); |
| 70 |
|
| 71 |
return doctype; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* ggd_doc_type_unref: |
| 76 |
* @doctype: A #GgdDocType |
| 77 |
* |
| 78 |
* Removes a reference from @doctype and destroy it if it reaches 0. |
| 79 |
*/ |
| 80 |
void |
| 81 |
ggd_doc_type_unref (GgdDocType *doctype) |
| 82 |
{ |
| 83 |
g_return_if_fail (doctype != NULL); |
| 84 |
|
| 85 |
if (g_atomic_int_dec_and_test (&doctype->ref_count)) { |
| 86 |
g_free (doctype->name); |
| 87 |
while (doctype->settings) { |
| 88 |
GList *tmp = doctype->settings; |
| 89 |
|
| 90 |
doctype->settings = g_list_next (doctype->settings); |
| 91 |
ggd_doc_setting_unref (tmp->data); |
| 92 |
g_list_free_1 (tmp); |
| 93 |
} |
| 94 |
g_slice_free1 (sizeof *doctype, doctype); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* ggd_doc_type_dump: |
| 100 |
* @doctype: A #GgdDocType object |
| 101 |
* @fp: File stream where write doctype dump. |
| 102 |
* |
| 103 |
* Dumps @doctype to @stream. |
| 104 |
*/ |
| 105 |
void |
| 106 |
ggd_doc_type_dump (const GgdDocType *doctype, |
| 107 |
FILE *stream) |
| 108 |
{ |
| 109 |
g_return_if_fail (doctype != NULL); |
| 110 |
|
| 111 |
fprintf (stream, "DocType %s [%p]:\n", doctype->name, (gpointer)doctype); |
| 112 |
g_list_foreach (doctype->settings, (GFunc)ggd_doc_setting_dump, stream); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* ggd_doc_type_get_setting: |
| 117 |
* @doctype: A #GgdDocType |
| 118 |
* @match: A match "pattern" |
| 119 |
* |
| 120 |
* Gets the setting that matches @match (the one that should be used for the |
| 121 |
* given match). |
| 122 |
* |
| 123 |
* Returns: the first matching #GgdDocSetting or %NULL if none matches. |
| 124 |
*/ |
| 125 |
GgdDocSetting * |
| 126 |
ggd_doc_type_get_setting (const GgdDocType *doctype, |
| 127 |
const gchar *match) |
| 128 |
{ |
| 129 |
GgdDocSetting *setting = NULL; |
| 130 |
GList *tmp; |
| 131 |
gssize match_len; |
| 132 |
|
| 133 |
g_return_val_if_fail (doctype != NULL, NULL); |
| 134 |
g_return_val_if_fail (match != NULL, NULL); |
| 135 |
|
| 136 |
match_len = (gssize) strlen (match); |
| 137 |
|
| 138 |
for (tmp = doctype->settings; tmp && ! setting; tmp = g_list_next (tmp)) { |
| 139 |
if (ggd_doc_setting_matches (tmp->data, match, match_len)) { |
| 140 |
setting = tmp->data; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return setting; |
| 145 |
} |
| 146 |
|
| 147 |
/* gets the match for the parent of the given one (e.g. foo.bar when given |
| 148 |
* foo.bar.baz) */ |
| 149 |
static gchar * |
| 150 |
get_parent_match (const gchar *match) |
| 151 |
{ |
| 152 |
gchar *last_dot; |
| 153 |
|
| 154 |
last_dot = strrchr (match, '.'); |
| 155 |
|
| 156 |
return last_dot ? g_strndup (match, (gsize)(last_dot - match)) : NULL; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* ggd_doc_type_resolve_setting: |
| 161 |
* @doctype: A #GgdDocType |
| 162 |
* @match: A match "pattern" |
| 163 |
* @nth_child: return location for the number of redirection to go to the final |
| 164 |
* match |
| 165 |
* |
| 166 |
* Resolve the setting that should be applied of match @match. This is similar |
| 167 |
* to ggd_doc_type_get_setting() but applies FORWARD policies too (e.g. if |
| 168 |
* @match matches a setting of which the policy is FORWARD, this function will |
| 169 |
* try to resolve the parent and return it, and so on). |
| 170 |
* |
| 171 |
* Returns: the matching #GgdDocSetting or %NULL if none matches. |
| 172 |
*/ |
| 173 |
/* FIXME: this doesn't support DUPLICATE, but it is quite useless... */ |
| 174 |
GgdDocSetting * |
| 175 |
ggd_doc_type_resolve_setting (const GgdDocType *doctype, |
| 176 |
const gchar *match, |
| 177 |
gint *nth_child) |
| 178 |
{ |
| 179 |
GgdDocSetting *setting = NULL; |
| 180 |
gchar *child_match = NULL; |
| 181 |
|
| 182 |
g_return_val_if_fail (doctype != NULL, NULL); |
| 183 |
g_return_val_if_fail (match != NULL, NULL); |
| 184 |
|
| 185 |
/*g_debug ("Resolving match \"%s\"...", child_match);*/ |
| 186 |
if (nth_child) (*nth_child) = 0; |
| 187 |
child_match = g_strdup (match); |
| 188 |
setting = ggd_doc_type_get_setting (doctype, child_match); |
| 189 |
while (setting && setting->policy == GGD_POLICY_FORWARD) { |
| 190 |
gchar *parent_match = get_parent_match (child_match); |
| 191 |
|
| 192 |
if (nth_child) (*nth_child)++; |
| 193 |
/*g_debug ("going to parent (%s)", parent_match);*/ |
| 194 |
if (! parent_match) { |
| 195 |
setting = NULL; |
| 196 |
} else { |
| 197 |
setting = ggd_doc_type_get_setting (doctype, parent_match); |
| 198 |
g_free (child_match); |
| 199 |
child_match = parent_match; |
| 200 |
} |
| 201 |
} |
| 202 |
g_free (child_match); |
| 203 |
/*g_debug ("Result is: %s (%p)", |
| 204 |
setting ? setting->match : "(null)", |
| 205 |
(gpointer)setting);*/ |
| 206 |
|
| 207 |
return setting; |
| 208 |
} |