~ubuntu-branches/ubuntu/utopic/geany/utopic

« back to all changes in this revision

Viewing changes to .pc/git_build_with_latest_glib.patch/tagmanager/tm_source_file.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-04-19 21:00:25 UTC
  • Revision ID: package-import@ubuntu.com-20130419210025-tsuvhqufntxj5rsy
Tags: 1.22+dfsg-2ubuntu1
* debian/patches/git_build_with_latest_glib.patch:
  - Backport patch from 1.23 to fix build failure with glib 2.36

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*
 
3
*   Copyright (c) 2001-2002, Biswapesh Chattopadhyay
 
4
*
 
5
*   This source code is released for free distribution under the terms of the
 
6
*   GNU General Public License.
 
7
*
 
8
*/
 
9
 
 
10
/**
 
11
 * @file tm_source_file.h
 
12
 The TMSourceFile structure and associated functions are used to maintain
 
13
 tags for individual files.
 
14
*/
 
15
 
 
16
 
 
17
#include <stdio.h>
 
18
#include <limits.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
 
 
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"
 
28
 
 
29
#include "tm_source_file.h"
 
30
#include "tm_tag.h"
 
31
 
 
32
 
 
33
guint source_file_class_id = 0;
 
34
static TMSourceFile *current_source_file = NULL;
 
35
 
 
36
gboolean tm_source_file_init(TMSourceFile *source_file, const char *file_name
 
37
  , gboolean update, const char* name)
 
38
{
 
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);
 
42
 
 
43
#ifdef TM_DEBUG
 
44
        g_message("Source File init: %s", file_name);
 
45
#endif
 
46
 
 
47
        if (FALSE == tm_work_object_init(&(source_file->work_object),
 
48
                  source_file_class_id, file_name, FALSE))
 
49
                return FALSE;
 
50
 
 
51
        source_file->inactive = FALSE;
 
52
        if (NULL == LanguageTable)
 
53
        {
 
54
                initializeParsing();
 
55
                installLanguageMapDefaults();
 
56
                if (NULL == TagEntryFunction)
 
57
                        TagEntryFunction = tm_source_file_tags;
 
58
                if (NULL == TagEntrySetArglistFunction)
 
59
                        TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
 
60
        }
 
61
 
 
62
        if (name == NULL)
 
63
                source_file->lang = LANG_AUTO;
 
64
        else
 
65
                source_file->lang = getNamedLanguage(name);
 
66
 
 
67
        if (update)
 
68
                tm_source_file_update(TM_WORK_OBJECT(source_file), TRUE, FALSE, FALSE);
 
69
        return TRUE;
 
70
}
 
71
 
 
72
TMWorkObject *tm_source_file_new(const char *file_name, gboolean update, const char *name)
 
73
{
 
74
        TMSourceFile *source_file = g_new(TMSourceFile, 1);
 
75
        if (TRUE != tm_source_file_init(source_file, file_name, update, name))
 
76
        {
 
77
                g_free(source_file);
 
78
                return NULL;
 
79
        }
 
80
        return (TMWorkObject *) source_file;
 
81
}
 
82
 
 
83
void tm_source_file_destroy(TMSourceFile *source_file)
 
84
{
 
85
#ifdef TM_DEBUG
 
86
        g_message("Destroying source file: %s", source_file->work_object.file_name);
 
87
#endif
 
88
 
 
89
        if (NULL != TM_WORK_OBJECT(source_file)->tags_array)
 
90
        {
 
91
                tm_tags_array_free(TM_WORK_OBJECT(source_file)->tags_array, TRUE);
 
92
                TM_WORK_OBJECT(source_file)->tags_array = NULL;
 
93
        }
 
94
        tm_work_object_destroy(&(source_file->work_object));
 
95
}
 
96
 
 
97
void tm_source_file_free(gpointer source_file)
 
98
{
 
99
        if (NULL != source_file)
 
100
        {
 
101
                tm_source_file_destroy(source_file);
 
102
                g_free(source_file);
 
103
        }
 
104
}
 
105
 
 
106
gboolean tm_source_file_parse(TMSourceFile *source_file)
 
107
{
 
108
        const char *file_name;
 
109
        gboolean status = TRUE;
 
110
        int passCount = 0;
 
111
 
 
112
        if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
 
113
        {
 
114
                g_warning("Attempt to parse NULL file");
 
115
                return FALSE;
 
116
        }
 
117
 
 
118
        file_name = source_file->work_object.file_name;
 
119
        if (NULL == LanguageTable)
 
120
        {
 
121
                initializeParsing();
 
122
                installLanguageMapDefaults();
 
123
                if (NULL == TagEntryFunction)
 
124
                        TagEntryFunction = tm_source_file_tags;
 
125
                if (NULL == TagEntrySetArglistFunction)
 
126
                        TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
 
127
        }
 
128
        current_source_file = source_file;
 
129
 
 
130
        if (LANG_AUTO == source_file->lang)
 
131
                source_file->lang = getFileLanguage (file_name);
 
132
 
 
133
        if (source_file->lang < 0 || ! LanguageTable [source_file->lang]->enabled)
 
134
                return status;
 
135
 
 
136
        while ((TRUE == status) && (passCount < 3))
 
137
        {
 
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))
 
141
                {
 
142
                        if (LanguageTable [source_file->lang]->parser != NULL)
 
143
                        {
 
144
                                LanguageTable [source_file->lang]->parser ();
 
145
                                fileClose ();
 
146
                                break;
 
147
                        }
 
148
                        else if (LanguageTable [source_file->lang]->parser2 != NULL)
 
149
                                status = LanguageTable [source_file->lang]->parser2 (passCount);
 
150
                        fileClose ();
 
151
                }
 
152
                else
 
153
                {
 
154
                        g_warning("%s: Unable to open %s", G_STRFUNC, file_name);
 
155
                        return FALSE;
 
156
                }
 
157
                ++ passCount;
 
158
        }
 
159
        return status;
 
160
}
 
161
 
 
162
gboolean tm_source_file_buffer_parse(TMSourceFile *source_file, guchar* text_buf, gint buf_size)
 
163
{
 
164
        const char *file_name;
 
165
        gboolean status = TRUE;
 
166
 
 
167
        if ((NULL == source_file) || (NULL == source_file->work_object.file_name))
 
168
        {
 
169
                g_warning("Attempt to parse NULL file");
 
170
                return FALSE;
 
171
        }
 
172
 
 
173
        if ((NULL == text_buf) || (0 == buf_size))
 
174
        {
 
175
                g_warning("Attempt to parse a NULL text buffer");
 
176
        }
 
177
 
 
178
        file_name = source_file->work_object.file_name;
 
179
        if (NULL == LanguageTable)
 
180
        {
 
181
                initializeParsing();
 
182
                installLanguageMapDefaults();
 
183
                if (NULL == TagEntryFunction)
 
184
                        TagEntryFunction = tm_source_file_tags;
 
185
                if (NULL == TagEntrySetArglistFunction)
 
186
                        TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
 
187
        }
 
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)
 
192
        {
 
193
#ifdef TM_DEBUG
 
194
                g_warning("ignoring %s (unknown language)\n", file_name);
 
195
#endif
 
196
        }
 
197
        else if (! LanguageTable [source_file->lang]->enabled)
 
198
        {
 
199
#ifdef TM_DEBUG
 
200
                g_warning("ignoring %s (language disabled)\n", file_name);
 
201
#endif
 
202
        }
 
203
        else
 
204
        {
 
205
                int passCount = 0;
 
206
                while ((TRUE == status) && (passCount < 3))
 
207
                {
 
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))
 
211
                        {
 
212
                                if (LanguageTable [source_file->lang]->parser != NULL)
 
213
                                {
 
214
                                        LanguageTable [source_file->lang]->parser ();
 
215
                                        bufferClose ();
 
216
                                        break;
 
217
                                }
 
218
                                else if (LanguageTable [source_file->lang]->parser2 != NULL)
 
219
                                        status = LanguageTable [source_file->lang]->parser2 (passCount);
 
220
                                bufferClose ();
 
221
                        }
 
222
                        else
 
223
                        {
 
224
                                g_warning("Unable to open %s", file_name);
 
225
                                return FALSE;
 
226
                        }
 
227
                        ++ passCount;
 
228
                }
 
229
                return TRUE;
 
230
        }
 
231
        return status;
 
232
}
 
233
 
 
234
void tm_source_file_set_tag_arglist(const char *tag_name, const char *arglist)
 
235
{
 
236
        int count;
 
237
        TMTag **tags, *tag;
 
238
 
 
239
        if (NULL == arglist ||
 
240
                NULL == tag_name ||
 
241
                NULL == current_source_file ||
 
242
                NULL == current_source_file->work_object.tags_array)
 
243
        {
 
244
                return;
 
245
        }
 
246
 
 
247
        tags = tm_tags_find(current_source_file->work_object.tags_array, tag_name, FALSE, &count);
 
248
        if (tags != NULL && count == 1)
 
249
        {
 
250
                tag = tags[0];
 
251
                g_free(tag->atts.entry.arglist);
 
252
                tag->atts.entry.arglist = g_strdup(arglist);
 
253
        }
 
254
}
 
255
 
 
256
int tm_source_file_tags(const tagEntryInfo *tag)
 
257
{
 
258
        if (NULL == current_source_file)
 
259
                return 0;
 
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));
 
264
        return TRUE;
 
265
}
 
266
 
 
267
gboolean tm_source_file_update(TMWorkObject *source_file, gboolean force
 
268
  , gboolean __unused__ recurse, gboolean update_parent)
 
269
{
 
270
        if (force)
 
271
        {
 
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)
 
276
                {
 
277
                        tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
 
278
                }
 
279
                return TRUE;
 
280
        }
 
281
        else {
 
282
#ifdef  TM_DEBUG
 
283
                g_message ("no parsing of %s has been done", source_file->file_name);
 
284
#endif
 
285
                return FALSE;
 
286
        }
 
287
}
 
288
 
 
289
 
 
290
gboolean tm_source_file_buffer_update(TMWorkObject *source_file, guchar* text_buf,
 
291
                        gint buf_size, gboolean update_parent)
 
292
{
 
293
#ifdef TM_DEBUG
 
294
        g_message("Buffer updating based on source file %s", source_file->file_name);
 
295
#endif
 
296
 
 
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)
 
301
        {
 
302
#ifdef TM_DEBUG
 
303
                g_message("Updating parent [project] from buffer..");
 
304
#endif
 
305
                tm_work_object_update(source_file->parent, TRUE, FALSE, TRUE);
 
306
        }
 
307
#ifdef TM_DEBUG
 
308
        else
 
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");
 
311
 
 
312
#endif
 
313
        return TRUE;
 
314
}
 
315
 
 
316
 
 
317
gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
 
318
{
 
319
        TMTag *tag;
 
320
        guint i;
 
321
 
 
322
        if (NULL != source_file)
 
323
        {
 
324
                if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL)))
 
325
                {
 
326
                        tm_tag_write(tag, fp, tm_tag_attr_max_t);
 
327
                        tm_tag_unref(tag);
 
328
                        if (NULL != source_file->tags_array)
 
329
                        {
 
330
                                for (i=0; i < source_file->tags_array->len; ++i)
 
331
                                {
 
332
                                        tag = TM_TAG(source_file->tags_array->pdata[i]);
 
333
                                        if (TRUE != tm_tag_write(tag, fp, attrs))
 
334
                                                return FALSE;
 
335
                                }
 
336
                        }
 
337
                }
 
338
        }
 
339
        return TRUE;
 
340
}
 
341
 
 
342
const gchar *tm_source_file_get_lang_name(gint lang)
 
343
{
 
344
        if (NULL == LanguageTable)
 
345
        {
 
346
                initializeParsing();
 
347
                installLanguageMapDefaults();
 
348
                if (NULL == TagEntryFunction)
 
349
                        TagEntryFunction = tm_source_file_tags;
 
350
                if (NULL == TagEntrySetArglistFunction)
 
351
                        TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
 
352
        }
 
353
        return getLanguageName(lang);
 
354
}
 
355
 
 
356
gint tm_source_file_get_named_lang(const gchar *name)
 
357
{
 
358
        if (NULL == LanguageTable)
 
359
        {
 
360
                initializeParsing();
 
361
                installLanguageMapDefaults();
 
362
                if (NULL == TagEntryFunction)
 
363
                        TagEntryFunction = tm_source_file_tags;
 
364
                if (NULL == TagEntrySetArglistFunction)
 
365
                        TagEntrySetArglistFunction = tm_source_file_set_tag_arglist;
 
366
        }
 
367
        return getNamedLanguage(name);
 
368
}
 
369