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.
19 #define LIBCTAGS_DEFINED
20 #include "tm_work_object.h"
22 #include "tm_source_file.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
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
);
37 g_message("Source File init: %s", file_name
);
40 if (FALSE
== tm_work_object_init(&(source_file
->work_object
),
41 source_file_class_id
, file_name
, FALSE
))
43 source_file
->lang
= LANG_AUTO
;
44 source_file
->inactive
= FALSE
;
46 tm_source_file_update(TM_WORK_OBJECT(source_file
), FALSE
, FALSE
, FALSE
);
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
))
58 return (TMWorkObject
*) source_file
;
61 void tm_source_file_destroy(TMSourceFile
*source_file
)
64 g_message("Destroying source file: %s", source_file
->work_object
.file_name
);
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
);
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");
100 g_message("Parsing source file %s", source_file
->work_object
.file_name
);
102 file_name
= source_file
->work_object
.file_name
;
103 if (NULL
== LanguageTable
)
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
);
115 g_message("New language detected %d", source_file
->lang
);
121 if (source_file
->lang
== LANG_IGNORE
)
124 g_warning("ignoring %s (unknown language)\n", file_name
);
127 else if (! LanguageTable
[source_file
->lang
]->enabled
)
130 g_warning("ignoring %s (language disabled)\n", file_name
);
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
);
141 printf("DEBUG: opening file: %s\n", file_name
);
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
);
153 g_warning("Unable to open %s", file_name
);
163 gboolean
tm_source_file_buffer_parse(TMSourceFile
*source_file
, unsigned char* text_buf
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");
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
)
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
)
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
, 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
);
220 g_warning("Unable to open %s", file_name
);
230 int tm_source_file_tags(const tagEntryInfo
*tag
)
232 if (NULL
== current_source_file
)
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
));
241 gboolean
tm_source_file_update(TMWorkObject
*source_file
, gboolean force
242 , gboolean __unused__ recurse
, gboolean update_parent
)
245 g_message("Updating source file %s", source_file
->file_name
);
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
)
255 g_message("Updating parent [project]..");
257 tm_work_object_update(source_file
->parent
, TRUE
, FALSE
, TRUE
);
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");
268 g_message ("no parsing of %s has been done", source_file
->file_name
);
275 gboolean
tm_source_file_buffer_update(TMWorkObject
*source_file
, unsigned char* text_buf
276 , int text_buf_size
, gboolean update_parent
)
279 g_message("Buffer updating based on source file %s", source_file
->file_name
);
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
)
288 g_message("Updating parent [project] from buffer..");
290 tm_work_object_update(source_file
->parent
, TRUE
, FALSE
, TRUE
);
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");
302 gboolean
tm_source_file_write(TMWorkObject
*source_file
, FILE *fp
, guint attrs
)
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
);
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
))