Get rid of the vertical pane in the log viewer and just make it a regular box.
[anjuta-git-plugin.git] / tagmanager / tm_work_object.c
blob4ccaef5e5a947b6721b5821b5b727f53173fd215
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>
14 #include <sys/stat.h>
16 #include "tm_tag.h"
17 #include "tm_work_object.h"
19 static GPtrArray *s_work_object_subclasses = NULL;
21 gchar *tm_get_real_path(const gchar *file_name)
23 if (file_name)
25 gchar path[PATH_MAX+1];
26 memset(path, '\0', PATH_MAX+1);
27 realpath(file_name, path);
28 return g_strdup(path);
30 else
31 return NULL;
34 guint tm_work_object_register(GFreeFunc free_func, TMUpdateFunc update_func, TMFindFunc find_func)
36 TMWorkObjectClass *object_class;
37 if (NULL == s_work_object_subclasses)
39 s_work_object_subclasses = g_ptr_array_new();
40 object_class = g_new(TMWorkObjectClass, 1);
41 object_class->free_func = tm_work_object_free;
42 object_class->update_func = NULL;
43 object_class->find_func = NULL;
44 g_ptr_array_add(s_work_object_subclasses, object_class);
46 object_class = g_new(TMWorkObjectClass, 1);
47 object_class->free_func = free_func;
48 object_class->update_func = update_func;
49 object_class->find_func = find_func;
50 g_ptr_array_add(s_work_object_subclasses, object_class);
51 return (s_work_object_subclasses->len - 1);
54 gboolean tm_work_object_init(TMWorkObject *work_object, guint type, const char *file_name
55 , gboolean create)
57 struct stat s;
58 int status;
60 if (0 != (status = stat(file_name, &s)))
62 if (create)
64 FILE *f;
65 if (NULL == (f = fopen(file_name, "a+")))
67 g_warning("Unable to create file %s", file_name);
68 return FALSE;
70 fclose(f);
71 status = stat(file_name, &s);
74 if (0 != status)
76 /* g_warning("Unable to stat %s", file_name);*/
77 return FALSE;
79 if (!S_ISREG(s.st_mode))
81 g_warning("%s: Not a regular file", file_name);
82 return FALSE;
84 work_object->type = type;
85 work_object->file_name = tm_get_real_path(file_name);
86 work_object->short_name = strrchr(work_object->file_name, '/');
87 if (work_object->short_name)
88 ++ work_object->short_name;
89 else
90 work_object->short_name = work_object->file_name;
91 work_object->parent = NULL;
92 work_object->analyze_time = 0;
93 work_object->tags_array = NULL;
94 return TRUE;
97 time_t tm_get_file_timestamp(const char *file_name)
99 struct stat s;
101 g_return_val_if_fail(file_name, 0);
103 if (0 != stat(file_name, &s))
105 /*g_warning("Unable to stat %s", file_name);*/
106 return (time_t) 0;
108 else
109 return s.st_mtime;
112 gboolean tm_work_object_is_changed(TMWorkObject *work_object)
114 return (gboolean) (work_object->analyze_time < tm_get_file_timestamp(work_object->file_name));
117 TMWorkObject *tm_work_object_new(guint type, const char *file_name, gboolean create)
119 TMWorkObject *work_object = g_new(TMWorkObject, 1);
120 if (!tm_work_object_init(work_object, type, file_name, create))
122 g_free(work_object);
123 return NULL;
125 return work_object;
128 void tm_work_object_destroy(TMWorkObject *work_object)
130 if (work_object)
132 g_free(work_object->file_name);
133 if (work_object->tags_array)
134 g_ptr_array_free(work_object->tags_array, TRUE);
138 void tm_work_object_free(gpointer work_object)
140 if (NULL != work_object)
142 TMWorkObject *w = (TMWorkObject *) work_object;
143 if ((w->type > 0) && (w->type < s_work_object_subclasses->len) &&
144 (s_work_object_subclasses->pdata[w->type] != NULL))
146 GFreeFunc free_func =
147 ((TMWorkObjectClass *)s_work_object_subclasses->pdata[w->type])->free_func;
148 if (NULL != free_func)
149 free_func(work_object);
150 return;
152 tm_work_object_destroy(w);
153 g_free(work_object);
157 void tm_work_object_write_tags(TMWorkObject *work_object, FILE *file, guint attrs)
159 if (NULL != work_object->tags_array)
161 guint i;
162 for (i=0; i < work_object->tags_array->len; ++i)
163 tm_tag_write((TMTag *) g_ptr_array_index(work_object->tags_array, i)
164 , file, (TMTagAttrType) attrs);
168 gboolean tm_work_object_update(TMWorkObject *work_object, gboolean force
169 , gboolean recurse, gboolean update_parent)
171 if ((NULL != work_object) && (work_object->type > 0) &&
172 (work_object->type < s_work_object_subclasses->len) &&
173 (s_work_object_subclasses->pdata[work_object->type] != NULL))
175 TMUpdateFunc update_func =
176 ((TMWorkObjectClass *)s_work_object_subclasses->pdata[work_object->type])->update_func;
177 if (NULL != update_func)
178 return update_func(work_object, force, recurse, update_parent);
180 return FALSE;
183 TMWorkObject *tm_work_object_find(TMWorkObject *work_object, const char *file_name
184 , gboolean name_only)
186 if ((NULL != work_object) && (work_object->type > 0) &&
187 (work_object->type < s_work_object_subclasses->len) &&
188 (s_work_object_subclasses->pdata[work_object->type] != NULL))
190 TMFindFunc find_func =
191 ((TMWorkObjectClass *)s_work_object_subclasses->pdata[work_object->type])->find_func;
192 if (NULL == find_func)
194 if (name_only)
196 const char *short_name = strrchr(file_name, '/');
197 if (short_name)
198 ++ short_name;
199 else
200 short_name = file_name;
201 if (0 == strcmp(work_object->short_name, short_name))
202 return work_object;
203 else
204 return NULL;
206 else
208 char *path = tm_get_real_path(file_name);
209 int cmp = strcmp(work_object->file_name, file_name);
210 g_free(path);
211 if (0 == cmp)
212 return work_object;
213 else
214 return NULL;
217 else
218 return find_func(work_object, file_name, name_only);
220 return NULL;
223 void tm_work_object_dump(const TMWorkObject *w)
225 if (w)
227 fprintf(stderr, "%s", w->file_name);
228 if (w->parent)
229 fprintf(stderr, " <- %s\n", w->parent->file_name);
230 else
231 fprintf(stderr, " <- NULL\n");