Alter warning fix
[geany-mirror.git] / tagmanager / tm_source_file.c
blobebd4b546f3c7de83ea298ab1ac5644655ecfb11f
1 /*
3 * Copyright (c) 2001-2002, Biswapesh Chattopadhyay
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 */
10 /**
11 * @file tm_source_file.h
12 The TMSourceFile structure and associated functions are used to maintain
13 tags for individual files.
17 #include <stdio.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include "general.h"
23 #include "entry.h"
24 #include "parse.h"
25 #include "read.h"
26 #define LIBCTAGS_DEFINED
27 #include "tm_work_object.h"
29 #include "tm_source_file.h"
30 #include "tm_tag.h"
33 guint source_file_class_id = 0;
34 static TMSourceFile *current_source_file = NULL;
36 gboolean tm_source_file_init(TMSourceFile *source_file, const char *file_name
37 , gboolean update, const char* name)
39 if (0 == source_file_class_id)
40 source_file_class_id = tm_work_object_register(tm_source_file_free
41 , tm_source_file_update, NULL);
43 #ifdef TM_DEBUG
44 g_message("Source File init: %s", file_name);
45 #endif
47 if (FALSE == tm_work_object_init(&(source_file->work_object),
48 source_file_class_id, file_name, FALSE))
49 return FALSE;
51 source_file->inactive = FALSE;
52 if (NULL == LanguageTable)
54 initializeParsing();
55 installLanguageMapDefaults();
56 if (NULL == TagEntryFunction)
57 TagEntryFunction = tm_source_file_tags;
58 if (NULL == TagEntrySetArglistFunction)
59 TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
62 if (name == NULL)
63 source_file->lang = LANG_AUTO;
64 else
65 source_file->lang = getNamedLanguage(name);
67 if (update)
68 tm_source_file_update(TM_WORK_OBJECT(source_file), TRUE, FALSE, FALSE);
69 return TRUE;
72 TMWorkObject *tm_source_file_new(const char *file_name, gboolean update, const char *name)
74 TMSourceFile *source_file = g_new(TMSourceFile, 1);
75 if (TRUE != tm_source_file_init(source_file, file_name, update, name))
77 g_free(source_file);
78 return NULL;
80 return (TMWorkObject *) source_file;
83 void tm_source_file_destroy(TMSourceFile *source_file)
85 #ifdef TM_DEBUG
86 g_message("Destroying source file: %s", source_file->work_object.file_name);
87 #endif
89 if (NULL != TM_WORK_OBJECT(source_file)->tags_array)
91 tm_tags_array_free(TM_WORK_OBJECT(source_file)->tags_array, TRUE);
92 TM_WORK_OBJECT(source_file)->tags_array = NULL;
94 tm_work_object_destroy(&(source_file->work_object));
97 void tm_source_file_free(gpointer source_file)
99 if (NULL != source_file)
101 tm_source_file_destroy(source_file);
102 g_free(source_file);
106 gboolean tm_source_file_parse(TMSourceFile *source_file)
108 const char *file_name;
109 gboolean status = TRUE;
110 int passCount = 0;
112 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
114 g_warning("Attempt to parse NULL file");
115 return FALSE;
118 file_name = source_file->work_object.file_name;
119 if (NULL == LanguageTable)
121 initializeParsing();
122 installLanguageMapDefaults();
123 if (NULL == TagEntryFunction)
124 TagEntryFunction = tm_source_file_tags;
125 if (NULL == TagEntrySetArglistFunction)
126 TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
128 current_source_file = source_file;
130 if (LANG_AUTO == source_file->lang)
131 source_file->lang = getFileLanguage (file_name);
133 if (source_file->lang < 0 || ! LanguageTable [source_file->lang]->enabled)
134 return status;
136 while ((TRUE == status) && (passCount < 3))
138 if (source_file->work_object.tags_array)
139 tm_tags_array_free(source_file->work_object.tags_array, FALSE);
140 if (fileOpen (file_name, source_file->lang))
142 if (LanguageTable [source_file->lang]->parser != NULL)
144 LanguageTable [source_file->lang]->parser ();
145 fileClose ();
146 break;
148 else if (LanguageTable [source_file->lang]->parser2 != NULL)
149 status = LanguageTable [source_file->lang]->parser2 (passCount);
150 fileClose ();
152 else
154 g_warning("%s: Unable to open %s", G_STRFUNC, file_name);
155 return FALSE;
157 ++ passCount;
159 return status;
162 gboolean tm_source_file_buffer_parse(TMSourceFile *source_file, guchar* text_buf, gint buf_size)
164 const char *file_name;
165 gboolean status = TRUE;
167 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
169 g_warning("Attempt to parse NULL file");
170 return FALSE;
173 if ((NULL == text_buf) || (0 == buf_size))
175 g_warning("Attempt to parse a NULL text buffer");
178 file_name = source_file->work_object.file_name;
179 if (NULL == LanguageTable)
181 initializeParsing();
182 installLanguageMapDefaults();
183 if (NULL == TagEntryFunction)
184 TagEntryFunction = tm_source_file_tags;
185 if (NULL == TagEntrySetArglistFunction)
186 TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
188 current_source_file = source_file;
189 if (LANG_AUTO == source_file->lang)
190 source_file->lang = getFileLanguage (file_name);
191 if (source_file->lang == LANG_IGNORE)
193 #ifdef TM_DEBUG
194 g_warning("ignoring %s (unknown language)\n", file_name);
195 #endif
197 else if (! LanguageTable [source_file->lang]->enabled)
199 #ifdef TM_DEBUG
200 g_warning("ignoring %s (language disabled)\n", file_name);
201 #endif
203 else
205 int passCount = 0;
206 while ((TRUE == status) && (passCount < 3))
208 if (source_file->work_object.tags_array)
209 tm_tags_array_free(source_file->work_object.tags_array, FALSE);
210 if (bufferOpen (text_buf, buf_size, file_name, source_file->lang))
212 if (LanguageTable [source_file->lang]->parser != NULL)
214 LanguageTable [source_file->lang]->parser ();
215 bufferClose ();
216 break;
218 else if (LanguageTable [source_file->lang]->parser2 != NULL)
219 status = LanguageTable [source_file->lang]->parser2 (passCount);
220 bufferClose ();
222 else
224 g_warning("Unable to open %s", file_name);
225 return FALSE;
227 ++ passCount;
229 return TRUE;
231 return status;
234 void tm_source_file_set_tag_arglist(const char *tag_name, const char *arglist)
236 int count;
237 TMTag **tags, *tag;
239 if (NULL == arglist ||
240 NULL == tag_name ||
241 NULL == current_source_file ||
242 NULL == current_source_file->work_object.tags_array)
244 return;
247 tags = tm_tags_find(current_source_file->work_object.tags_array, tag_name, FALSE, &count);
248 if (tags != NULL && count == 1)
250 tag = tags[0];
251 g_free(tag->atts.entry.arglist);
252 tag->atts.entry.arglist = g_strdup(arglist);
256 int tm_source_file_tags(const tagEntryInfo *tag)
258 if (NULL == current_source_file)
259 return 0;
260 if (NULL == current_source_file->work_object.tags_array)
261 current_source_file->work_object.tags_array = g_ptr_array_new();
262 g_ptr_array_add(current_source_file->work_object.tags_array,
263 tm_tag_new(current_source_file, tag));
264 return TRUE;
267 gboolean tm_source_file_update(TMWorkObject *source_file, gboolean force
268 , gboolean __unused__ recurse, gboolean update_parent)
270 if (force)
272 tm_source_file_parse(TM_SOURCE_FILE(source_file));
273 tm_tags_sort(source_file->tags_array, NULL, FALSE);
274 /* source_file->analyze_time = tm_get_file_timestamp(source_file->file_name); */
275 if ((source_file->parent) && update_parent)
277 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
279 return TRUE;
281 else {
282 #ifdef TM_DEBUG
283 g_message ("no parsing of %s has been done", source_file->file_name);
284 #endif
285 return FALSE;
290 gboolean tm_source_file_buffer_update(TMWorkObject *source_file, guchar* text_buf,
291 gint buf_size, gboolean update_parent)
293 #ifdef TM_DEBUG
294 g_message("Buffer updating based on source file %s", source_file->file_name);
295 #endif
297 tm_source_file_buffer_parse (TM_SOURCE_FILE(source_file), text_buf, buf_size);
298 tm_tags_sort(source_file->tags_array, NULL, FALSE);
299 /* source_file->analyze_time = time(NULL); */
300 if ((source_file->parent) && update_parent)
302 #ifdef TM_DEBUG
303 g_message("Updating parent [project] from buffer..");
304 #endif
305 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
307 #ifdef TM_DEBUG
308 else
309 g_message("Skipping parent update because parent is %s and update_parent is %s"
310 , source_file->parent?"NOT NULL":"NULL", update_parent?"TRUE":"FALSE");
312 #endif
313 return TRUE;
317 gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
319 TMTag *tag;
320 guint i;
322 if (NULL != source_file)
324 if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL)))
326 tm_tag_write(tag, fp, tm_tag_attr_max_t);
327 tm_tag_unref(tag);
328 if (NULL != source_file->tags_array)
330 for (i=0; i < source_file->tags_array->len; ++i)
332 tag = TM_TAG(source_file->tags_array->pdata[i]);
333 if (TRUE != tm_tag_write(tag, fp, attrs))
334 return FALSE;
339 return TRUE;
342 const gchar *tm_source_file_get_lang_name(gint lang)
344 if (NULL == LanguageTable)
346 initializeParsing();
347 installLanguageMapDefaults();
348 if (NULL == TagEntryFunction)
349 TagEntryFunction = tm_source_file_tags;
350 if (NULL == TagEntrySetArglistFunction)
351 TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
353 return getLanguageName(lang);
356 gint tm_source_file_get_named_lang(const gchar *name)
358 if (NULL == LanguageTable)
360 initializeParsing();
361 installLanguageMapDefaults();
362 if (NULL == TagEntryFunction)
363 TagEntryFunction = tm_source_file_tags;
364 if (NULL == TagEntrySetArglistFunction)
365 TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
367 return getNamedLanguage(name);