Refactor snippets_complete_constructs().
[geany-mirror.git] / tagmanager / tm_source_file.c
blob2e7601bf6d86995225b9cee8650965d4def6bcf9
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;
60 if (name == NULL)
61 source_file->lang = LANG_AUTO;
62 else
63 source_file->lang = getNamedLanguage(name);
65 if (update)
66 tm_source_file_update(TM_WORK_OBJECT(source_file), TRUE, FALSE, FALSE);
67 return TRUE;
70 TMWorkObject *tm_source_file_new(const char *file_name, gboolean update, const char *name)
72 TMSourceFile *source_file = g_new(TMSourceFile, 1);
73 if (TRUE != tm_source_file_init(source_file, file_name, update, name))
75 g_free(source_file);
76 return NULL;
78 return (TMWorkObject *) source_file;
81 void tm_source_file_destroy(TMSourceFile *source_file)
83 #ifdef TM_DEBUG
84 g_message("Destroying source file: %s", source_file->work_object.file_name);
85 #endif
87 if (NULL != TM_WORK_OBJECT(source_file)->tags_array)
89 tm_tags_array_free(TM_WORK_OBJECT(source_file)->tags_array, TRUE);
90 TM_WORK_OBJECT(source_file)->tags_array = NULL;
92 tm_work_object_destroy(&(source_file->work_object));
95 void tm_source_file_free(gpointer source_file)
97 if (NULL != source_file)
99 tm_source_file_destroy(source_file);
100 g_free(source_file);
104 gboolean tm_source_file_parse(TMSourceFile *source_file)
106 const char *file_name;
107 gboolean status = TRUE;
108 int passCount = 0;
110 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
112 g_warning("Attempt to parse NULL file");
113 return FALSE;
116 file_name = source_file->work_object.file_name;
117 if (NULL == LanguageTable)
119 initializeParsing();
120 installLanguageMapDefaults();
121 if (NULL == TagEntryFunction)
122 TagEntryFunction = tm_source_file_tags;
124 current_source_file = source_file;
126 if (LANG_AUTO == source_file->lang)
127 source_file->lang = getFileLanguage (file_name);
129 if (source_file->lang < 0 || ! LanguageTable [source_file->lang]->enabled)
130 return status;
132 while ((TRUE == status) && (passCount < 3))
134 if (source_file->work_object.tags_array)
135 tm_tags_array_free(source_file->work_object.tags_array, FALSE);
136 if (fileOpen (file_name, source_file->lang))
138 if (LanguageTable [source_file->lang]->parser != NULL)
140 LanguageTable [source_file->lang]->parser ();
141 fileClose ();
142 break;
144 else if (LanguageTable [source_file->lang]->parser2 != NULL)
145 status = LanguageTable [source_file->lang]->parser2 (passCount);
146 fileClose ();
148 else
150 g_warning("%s: Unable to open %s", G_STRFUNC, file_name);
151 return FALSE;
153 ++ passCount;
155 return status;
158 gboolean tm_source_file_buffer_parse(TMSourceFile *source_file, guchar* text_buf, gint buf_size)
160 const char *file_name;
161 gboolean status = TRUE;
163 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
165 g_warning("Attempt to parse NULL file");
166 return FALSE;
169 if ((NULL == text_buf) || (0 == buf_size))
171 g_warning("Attempt to parse a NULL text buffer");
174 file_name = source_file->work_object.file_name;
175 if (NULL == LanguageTable)
177 initializeParsing();
178 installLanguageMapDefaults();
179 if (NULL == TagEntryFunction)
180 TagEntryFunction = tm_source_file_tags;
182 current_source_file = source_file;
183 if (LANG_AUTO == source_file->lang)
184 source_file->lang = getFileLanguage (file_name);
185 if (source_file->lang == LANG_IGNORE)
187 #ifdef TM_DEBUG
188 g_warning("ignoring %s (unknown language)\n", file_name);
189 #endif
191 else if (! LanguageTable [source_file->lang]->enabled)
193 #ifdef TM_DEBUG
194 g_warning("ignoring %s (language disabled)\n", file_name);
195 #endif
197 else
199 int passCount = 0;
200 while ((TRUE == status) && (passCount < 3))
202 if (source_file->work_object.tags_array)
203 tm_tags_array_free(source_file->work_object.tags_array, FALSE);
204 if (bufferOpen (text_buf, buf_size, file_name, source_file->lang))
206 if (LanguageTable [source_file->lang]->parser != NULL)
208 LanguageTable [source_file->lang]->parser ();
209 bufferClose ();
210 break;
212 else if (LanguageTable [source_file->lang]->parser2 != NULL)
213 status = LanguageTable [source_file->lang]->parser2 (passCount);
214 bufferClose ();
216 else
218 g_warning("Unable to open %s", file_name);
219 return FALSE;
221 ++ passCount;
223 return TRUE;
225 return status;
228 int tm_source_file_tags(const tagEntryInfo *tag)
230 if (NULL == current_source_file)
231 return 0;
232 if (NULL == current_source_file->work_object.tags_array)
233 current_source_file->work_object.tags_array = g_ptr_array_new();
234 g_ptr_array_add(current_source_file->work_object.tags_array,
235 tm_tag_new(current_source_file, tag));
236 return TRUE;
239 gboolean tm_source_file_update(TMWorkObject *source_file, gboolean force
240 , gboolean __unused__ recurse, gboolean update_parent)
242 if (force)
244 tm_source_file_parse(TM_SOURCE_FILE(source_file));
245 tm_tags_sort(source_file->tags_array, NULL, FALSE);
246 /* source_file->analyze_time = tm_get_file_timestamp(source_file->file_name); */
247 if ((source_file->parent) && update_parent)
249 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
251 return TRUE;
253 else {
254 #ifdef TM_DEBUG
255 g_message ("no parsing of %s has been done", source_file->file_name);
256 #endif
257 return FALSE;
262 gboolean tm_source_file_buffer_update(TMWorkObject *source_file, guchar* text_buf,
263 gint buf_size, gboolean update_parent)
265 #ifdef TM_DEBUG
266 g_message("Buffer updating based on source file %s", source_file->file_name);
267 #endif
269 tm_source_file_buffer_parse (TM_SOURCE_FILE(source_file), text_buf, buf_size);
270 tm_tags_sort(source_file->tags_array, NULL, FALSE);
271 /* source_file->analyze_time = time(NULL); */
272 if ((source_file->parent) && update_parent)
274 #ifdef TM_DEBUG
275 g_message("Updating parent [project] from buffer..");
276 #endif
277 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
279 #ifdef TM_DEBUG
280 else
281 g_message("Skipping parent update because parent is %s and update_parent is %s"
282 , source_file->parent?"NOT NULL":"NULL", update_parent?"TRUE":"FALSE");
284 #endif
285 return TRUE;
289 gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
291 TMTag *tag;
292 guint i;
294 if (NULL != source_file)
296 if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL)))
298 tm_tag_write(tag, fp, tm_tag_attr_max_t);
299 tm_tag_free(tag);
300 if (NULL != source_file->tags_array)
302 for (i=0; i < source_file->tags_array->len; ++i)
304 tag = TM_TAG(source_file->tags_array->pdata[i]);
305 if (TRUE != tm_tag_write(tag, fp, attrs))
306 return FALSE;
311 return TRUE;
314 const gchar *tm_source_file_get_lang_name(gint lang)
316 if (NULL == LanguageTable)
318 initializeParsing();
319 installLanguageMapDefaults();
320 if (NULL == TagEntryFunction)
321 TagEntryFunction = tm_source_file_tags;
323 return getLanguageName(lang);
326 gint tm_source_file_get_named_lang(const gchar *name)
328 if (NULL == LanguageTable)
330 initializeParsing();
331 installLanguageMapDefaults();
332 if (NULL == TagEntryFunction)
333 TagEntryFunction = tm_source_file_tags;
335 return getNamedLanguage(name);