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.
11
* @file tm_source_file.h
12
The TMSourceFile structure and associated functions are used to maintain
13
tags for individual files.
26
#define LIBCTAGS_DEFINED
27
#include "tm_work_object.h"
29
#include "tm_source_file.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);
44
g_message("Source File init: %s", file_name);
47
if (FALSE == tm_work_object_init(&(source_file->work_object),
48
source_file_class_id, file_name, FALSE))
51
source_file->inactive = FALSE;
52
if (NULL == LanguageTable)
55
installLanguageMapDefaults();
56
if (NULL == TagEntryFunction)
57
TagEntryFunction = tm_source_file_tags;
58
if (NULL == TagEntrySetArglistFunction)
59
TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
63
source_file->lang = LANG_AUTO;
65
source_file->lang = getNamedLanguage(name);
68
tm_source_file_update(TM_WORK_OBJECT(source_file), TRUE, FALSE, FALSE);
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))
80
return (TMWorkObject *) source_file;
83
void tm_source_file_destroy(TMSourceFile *source_file)
86
g_message("Destroying source file: %s", source_file->work_object.file_name);
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);
106
gboolean tm_source_file_parse(TMSourceFile *source_file)
108
const char *file_name;
109
gboolean status = TRUE;
112
if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
114
g_warning("Attempt to parse NULL file");
118
file_name = source_file->work_object.file_name;
119
if (NULL == LanguageTable)
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)
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 ();
148
else if (LanguageTable [source_file->lang]->parser2 != NULL)
149
status = LanguageTable [source_file->lang]->parser2 (passCount);
154
g_warning("%s: Unable to open %s", G_STRFUNC, file_name);
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");
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)
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)
194
g_warning("ignoring %s (unknown language)\n", file_name);
197
else if (! LanguageTable [source_file->lang]->enabled)
200
g_warning("ignoring %s (language disabled)\n", file_name);
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 ();
218
else if (LanguageTable [source_file->lang]->parser2 != NULL)
219
status = LanguageTable [source_file->lang]->parser2 (passCount);
224
g_warning("Unable to open %s", file_name);
234
void tm_source_file_set_tag_arglist(const char *tag_name, const char *arglist)
239
if (NULL == arglist ||
241
NULL == current_source_file ||
242
NULL == current_source_file->work_object.tags_array)
247
tags = tm_tags_find(current_source_file->work_object.tags_array, tag_name, FALSE, &count);
248
if (tags != NULL && count == 1)
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)
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));
267
gboolean tm_source_file_update(TMWorkObject *source_file, gboolean force
268
, gboolean __unused__ recurse, gboolean update_parent)
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);
283
g_message ("no parsing of %s has been done", source_file->file_name);
290
gboolean tm_source_file_buffer_update(TMWorkObject *source_file, guchar* text_buf,
291
gint buf_size, gboolean update_parent)
294
g_message("Buffer updating based on source file %s", source_file->file_name);
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)
303
g_message("Updating parent [project] from buffer..");
305
tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
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");
317
gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
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);
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))
342
const gchar *tm_source_file_get_lang_name(gint lang)
344
if (NULL == LanguageTable)
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)
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);