2008-05-01 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / tagmanager / tm_source_file.c
blob856bcad9d4240f398ed4f3470bfa3275c933abae
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 #include <stdio.h>
11 #include <limits.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include "general.h"
16 #include "entry.h"
17 #include "parse.h"
18 #include "read.h"
19 #define LIBCTAGS_DEFINED
20 #include "tm_work_object.h"
22 #include "tm_source_file.h"
23 #include "tm_tag.h"
26 guint source_file_class_id = 0;
27 static TMSourceFile *current_source_file = NULL;
29 gboolean tm_source_file_init(TMSourceFile *source_file, const char *file_name
30 , gboolean update)
32 if (0 == source_file_class_id)
33 source_file_class_id = tm_work_object_register(tm_source_file_free
34 , tm_source_file_update, NULL);
36 #ifdef TM_DEBUG
37 g_message("Source File init: %s", file_name);
38 #endif
40 if (FALSE == tm_work_object_init(&(source_file->work_object),
41 source_file_class_id, file_name, FALSE))
42 return FALSE;
43 source_file->lang = LANG_AUTO;
44 source_file->inactive = FALSE;
45 if (update)
46 tm_source_file_update(TM_WORK_OBJECT(source_file), FALSE, FALSE, FALSE);
47 return TRUE;
50 TMWorkObject *tm_source_file_new(const char *file_name, gboolean update)
52 TMSourceFile *source_file = g_new(TMSourceFile, 1);
53 if (TRUE != tm_source_file_init(source_file, file_name, update))
55 g_free(source_file);
56 return NULL;
58 return (TMWorkObject *) source_file;
61 void tm_source_file_destroy(TMSourceFile *source_file)
63 #ifdef TM_DEBUG
64 g_message("Destroying source file: %s", source_file->work_object.file_name);
65 #endif
67 if (NULL != TM_WORK_OBJECT(source_file)->tags_array)
69 tm_tags_array_free(TM_WORK_OBJECT(source_file)->tags_array, TRUE);
70 TM_WORK_OBJECT(source_file)->tags_array = NULL;
72 tm_work_object_destroy(&(source_file->work_object));
75 void tm_source_file_free(gpointer source_file)
77 if (NULL != source_file)
79 tm_source_file_destroy(source_file);
80 g_free(source_file);
84 gboolean tm_source_file_parse(TMSourceFile *source_file)
86 const char *file_name;
87 gboolean status = TRUE;
89 source_file->lang = LANG_AUTO;
91 source_file->lang = LANG_AUTO;
93 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
95 g_warning("Attempt to parse NULL file");
96 return FALSE;
99 #ifdef TM_DEBUG
100 g_message("Parsing source file %s", source_file->work_object.file_name);
101 #endif
102 file_name = source_file->work_object.file_name;
103 if (NULL == LanguageTable)
105 initializeParsing();
106 installLanguageMapDefaults();
107 if (NULL == TagEntryFunction)
108 TagEntryFunction = tm_source_file_tags;
111 current_source_file = source_file;
112 if (LANG_AUTO == source_file->lang) {
113 source_file->lang = getFileLanguage (file_name);
114 #ifdef TM_DEBUG
115 g_message("New language detected %d", source_file->lang);
116 #endif
121 if (source_file->lang == LANG_IGNORE)
123 #ifdef TM_DEBUG
124 g_warning("ignoring %s (unknown language)\n", file_name);
125 #endif
127 else if (! LanguageTable [source_file->lang]->enabled)
129 #ifdef TM_DEBUG
130 g_warning("ignoring %s (language disabled)\n", file_name);
131 #endif
133 else
135 int passCount = 1;
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 #ifdef TM_DEBUG
141 printf("DEBUG: opening file: %s\n", file_name );
142 #endif
143 if (fileOpen (file_name, source_file->lang))
145 if (LanguageTable [source_file->lang]->parser != NULL)
146 LanguageTable [source_file->lang]->parser ();
147 else if (LanguageTable [source_file->lang]->parser2 != NULL)
148 status = LanguageTable [source_file->lang]->parser2 (passCount);
149 fileClose ();
151 else
153 g_warning("Unable to open %s", file_name);
154 return FALSE;
156 ++ passCount;
158 return TRUE;
160 return status;
163 gboolean tm_source_file_buffer_parse(TMSourceFile *source_file, unsigned char* text_buf
164 , int text_buf_size)
166 const char *file_name;
167 gboolean status = TRUE;
169 if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
171 g_warning("Attempt to parse NULL file");
172 return FALSE;
175 if ((NULL == text_buf) || (0 == text_buf_size))
177 g_warning("Attempt to parse a NULL text buffer");
180 file_name = source_file->work_object.file_name;
181 if (NULL == LanguageTable)
183 initializeParsing();
184 installLanguageMapDefaults();
185 if (NULL == TagEntryFunction)
186 TagEntryFunction = tm_source_file_tags;
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 = 1;
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, text_buf_size, file_name, source_file->lang))
212 if (LanguageTable [source_file->lang]->parser != NULL)
213 LanguageTable [source_file->lang]->parser ();
214 else if (LanguageTable [source_file->lang]->parser2 != NULL)
215 status = LanguageTable [source_file->lang]->parser2 (passCount);
216 bufferClose ();
218 else
220 g_warning("Unable to open %s", file_name);
221 return FALSE;
223 ++ passCount;
225 return TRUE;
227 return status;
230 int tm_source_file_tags(const tagEntryInfo *tag)
232 if (NULL == current_source_file)
233 return 0;
234 if (NULL == current_source_file->work_object.tags_array)
235 current_source_file->work_object.tags_array = g_ptr_array_new();
236 g_ptr_array_add(current_source_file->work_object.tags_array,
237 tm_tag_new(current_source_file, tag));
238 return TRUE;
241 gboolean tm_source_file_update(TMWorkObject *source_file, gboolean force
242 , gboolean __unused__ recurse, gboolean update_parent)
244 #ifdef TM_DEBUG
245 g_message("Updating source file %s", source_file->file_name);
246 #endif
247 if (force || (tm_work_object_is_changed(source_file)))
249 tm_source_file_parse(TM_SOURCE_FILE(source_file));
250 tm_tags_sort(source_file->tags_array, NULL, FALSE);
251 source_file->analyze_time = time(NULL);
252 if ((source_file->parent) && update_parent)
254 #ifdef TM_DEBUG
255 g_message("Updating parent [project]..");
256 #endif
257 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
259 #ifdef TM_DEBUG
260 else
261 g_message("Skipping parent update because parent is %s and update_parent is %s"
262 , source_file->parent?"NOT NULL":"NULL", update_parent?"TRUE":"FALSE");
263 #endif
264 return TRUE;
266 else {
267 #ifdef TM_DEBUG
268 g_message ("no parsing of %s has been done", source_file->file_name);
269 #endif
270 return FALSE;
275 gboolean tm_source_file_buffer_update(TMWorkObject *source_file, unsigned char* text_buf
276 , int text_buf_size, gboolean update_parent)
278 #ifdef TM_DEBUG
279 g_message("Buffer updating based on source file %s", source_file->file_name);
280 #endif
282 tm_source_file_buffer_parse (TM_SOURCE_FILE(source_file), text_buf, text_buf_size);
283 tm_tags_sort(source_file->tags_array, NULL, FALSE);
284 source_file->analyze_time = time(NULL);
285 if ((source_file->parent) && update_parent)
287 #ifdef TM_DEBUG
288 g_message("Updating parent [project] from buffer..");
289 #endif
290 tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
292 #ifdef TM_DEBUG
293 else
294 g_message("Skipping parent update because parent is %s and update_parent is %s"
295 , source_file->parent?"NOT NULL":"NULL", update_parent?"TRUE":"FALSE");
297 #endif
298 return TRUE;
302 gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
304 TMTag *tag;
305 guint i;
307 if (NULL != source_file)
309 if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL)))
311 tm_tag_write(tag, fp, tm_tag_attr_max_t);
312 tm_tag_free(tag);
313 if (NULL != source_file->tags_array)
315 for (i=0; i < source_file->tags_array->len; ++i)
317 tag = TM_TAG(source_file->tags_array->pdata[i]);
318 if (TRUE != tm_tag_write(tag, fp, attrs))
319 return FALSE;
324 return TRUE;