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
;
61 source_file
->lang
= LANG_AUTO
;
63 source_file
->lang
= getNamedLanguage(name
);
66 tm_source_file_update(TM_WORK_OBJECT(source_file
), TRUE
, FALSE
, FALSE
);
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
))
78 return (TMWorkObject
*) source_file
;
81 void tm_source_file_destroy(TMSourceFile
*source_file
)
84 g_message("Destroying source file: %s", source_file
->work_object
.file_name
);
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
);
104 gboolean
tm_source_file_parse(TMSourceFile
*source_file
)
106 const char *file_name
;
107 gboolean status
= TRUE
;
110 if ((NULL
== source_file
) || (NULL
== source_file
->work_object
.file_name
))
112 g_warning("Attempt to parse NULL file");
116 file_name
= source_file
->work_object
.file_name
;
117 if (NULL
== LanguageTable
)
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
)
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 ();
144 else if (LanguageTable
[source_file
->lang
]->parser2
!= NULL
)
145 status
= LanguageTable
[source_file
->lang
]->parser2 (passCount
);
150 g_warning("%s: Unable to open %s", G_STRFUNC
, file_name
);
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");
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
)
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
)
188 g_warning("ignoring %s (unknown language)\n", file_name
);
191 else if (! LanguageTable
[source_file
->lang
]->enabled
)
194 g_warning("ignoring %s (language disabled)\n", file_name
);
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 ();
212 else if (LanguageTable
[source_file
->lang
]->parser2
!= NULL
)
213 status
= LanguageTable
[source_file
->lang
]->parser2 (passCount
);
218 g_warning("Unable to open %s", file_name
);
228 int tm_source_file_tags(const tagEntryInfo
*tag
)
230 if (NULL
== current_source_file
)
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
));
239 gboolean
tm_source_file_update(TMWorkObject
*source_file
, gboolean force
240 , gboolean __unused__ recurse
, gboolean update_parent
)
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
);
255 g_message ("no parsing of %s has been done", source_file
->file_name
);
262 gboolean
tm_source_file_buffer_update(TMWorkObject
*source_file
, guchar
* text_buf
,
263 gint buf_size
, gboolean update_parent
)
266 g_message("Buffer updating based on source file %s", source_file
->file_name
);
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
)
275 g_message("Updating parent [project] from buffer..");
277 tm_work_object_update(source_file
->parent
, TRUE
, FALSE
, TRUE
);
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");
289 gboolean
tm_source_file_write(TMWorkObject
*source_file
, FILE *fp
, guint attrs
)
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
);
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
))
314 const gchar
*tm_source_file_get_lang_name(gint lang
)
316 if (NULL
== LanguageTable
)
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
)
331 installLanguageMapDefaults();
332 if (NULL
== TagEntryFunction
)
333 TagEntryFunction
= tm_source_file_tags
;
335 return getNamedLanguage(name
);