Debian Codegraph @

geanygendoc/src/ggd-file-type.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-file-type.h"
23
24 #include <stdio.h>
25 #include <glib.h>
26 #include <geanyplugin.h>
27
28 #include "ggd-doc-type.h"
29
30
31 /**
32 * SECTION: ggd-file-type
33 * @include: ggd-file-type.h
34 *
35 * A #GgdFileType represents the rules for a language type.
36 *
37 * It uses GObject-style reference counting through ggd_file_type_ref() and
38 * ggd_file_type_unref().
39 */
40
41
42 /**
43 * ggd_file_type_new:
44 * @type: A #GeanyFiletype ID
45 *
46 * Creates a new #GgdFileType for a Geany's file type.
47 *
48 * Returns: A newly created #GgdFileType.
49 */
50 GgdFileType *
51 ggd_file_type_new (GeanyFiletypeID type)
52 {
53 GgdFileType *ft;
54
55 ft = g_slice_alloc (sizeof *ft);
56 ft->ref_count = 1;
57 ft->geany_ft = type;
58 ft->match_function_arguments = NULL;
59 ft->user_env = ctpl_environ_new ();
60 ft->doctypes = g_hash_table_new_full (g_str_hash, g_str_equal,
61 NULL, (GDestroyNotify)ggd_doc_type_unref);
62
63 return ft;
64 }
65
66 /**
67 * ggd_file_type_ref:
68 * @filetype: A #GgdFileType
69 *
70 * Adds a reference to a #GgdFileType.
71 *
72 * Returns: The filetype
73 */
74 GgdFileType *
75 ggd_file_type_ref (GgdFileType *filetype)
76 {
77 g_return_val_if_fail (filetype != NULL, NULL);
78
79 g_atomic_int_inc (&filetype->ref_count);
80
81 return filetype;
82 }
83
84 /**
85 * ggd_file_type_unref:
86 * @filetype: A #GgdFileType
87 *
88 * Drops a reference from a #GgdFileType. When a file type's reference count
89 * drops to zero, the file type is destroyed.
90 */
91 void
92 ggd_file_type_unref (GgdFileType *filetype)
93 {
94 g_return_if_fail (filetype != NULL);
95
96 if (g_atomic_int_dec_and_test (&filetype->ref_count)) {
97 g_hash_table_destroy (filetype->doctypes);
98 if (filetype->match_function_arguments) {
99 g_regex_unref (filetype->match_function_arguments);
100 }
101 ctpl_environ_unref (filetype->user_env);
102 g_slice_free1 (sizeof *filetype, filetype);
103 }
104 }
105
106 /* function to call on each element of the doc type table in order to dump it */
107 static void
108 dump_doctypes_hfunc (gpointer key G_GNUC_UNUSED,
109 gpointer value,
110 gpointer data)
111 {
112 ggd_doc_type_dump (value, data);
113 }
114
115 /**
116 * ggd_file_type_dump:
117 * @filetype: A #GgdFileType
118 * @stream: A file stream to which write the dump
119 *
120 * A user-readable dump of a #GgdFileType, mostly for debugging purposes.
121 */
122 void
123 ggd_file_type_dump (const GgdFileType *filetype,
124 FILE *stream)
125 {
126 g_return_if_fail (filetype != NULL);
127
128 fprintf (stream, "FileType %d [%p]:\n",
129 filetype->geany_ft,
130 (gpointer)filetype);
131 fprintf (stream, "Settings:\n"
132 " function arguments RE: [%p]\n"
133 " user environ: [%p]\n",
134 (gpointer)filetype->match_function_arguments,
135 (gpointer)filetype->user_env);
136 g_hash_table_foreach (filetype->doctypes, dump_doctypes_hfunc, stream);
137 }
138
139 /**
140 * ggd_file_type_add_doc:
141 * @filetype: A #GgdFileType
142 * @doctype: A #GgdDocType
143 *
144 * Adds a #GgdDocType to a #GgdFileType.
145 */
146 void
147 ggd_file_type_add_doc (GgdFileType *filetype,
148 GgdDocType *doctype)
149 {
150 g_return_if_fail (filetype != NULL);
151
152 if (! ggd_file_type_get_doc (filetype, doctype->name)) {
153 g_hash_table_insert (filetype->doctypes, doctype->name,
154 ggd_doc_type_ref (doctype));
155 }
156 }
157
158 /**
159 * ggd_file_type_get_doc:
160 * @filetype: A #GgdFileType
161 * @name: A documentation type identifier
162 *
163 * Gets the #GgdDocType of a #GgdFileType that corresponds to the given
164 * documentation type identifier.
165 *
166 * Returns: The #GgdDocType corresponding to @name that shouldn't be unref'd, or
167 * %NULL if not found in @filetype.
168 */
169 GgdDocType *
170 ggd_file_type_get_doc (const GgdFileType *filetype,
171 const gchar *name)
172 {
173 g_return_val_if_fail (filetype != NULL, NULL);
174 g_return_val_if_fail (name != NULL, NULL);
175
176 return g_hash_table_lookup (filetype->doctypes, name);
177 }