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.
17 #include "tm_work_object.h"
19 static GPtrArray
*s_work_object_subclasses
= NULL
;
21 gchar
*tm_get_real_path(const gchar
*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
);
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
60 if (0 != (status
= stat(file_name
, &s
)))
65 if (NULL
== (f
= fopen(file_name
, "a+")))
67 g_warning("Unable to create file %s", file_name
);
71 status
= stat(file_name
, &s
);
76 /* g_warning("Unable to stat %s", file_name);*/
79 if (!S_ISREG(s
.st_mode
))
81 g_warning("%s: Not a regular file", file_name
);
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
;
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
;
97 time_t tm_get_file_timestamp(const char *file_name
)
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);*/
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
))
128 void tm_work_object_destroy(TMWorkObject
*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
);
152 tm_work_object_destroy(w
);
157 void tm_work_object_write_tags(TMWorkObject
*work_object
, FILE *file
, guint attrs
)
159 if (NULL
!= work_object
->tags_array
)
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
);
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
)
196 const char *short_name
= strrchr(file_name
, '/');
200 short_name
= file_name
;
201 if (0 == strcmp(work_object
->short_name
, short_name
))
208 char *path
= tm_get_real_path(file_name
);
209 int cmp
= strcmp(work_object
->file_name
, file_name
);
218 return find_func(work_object
, file_name
, name_only
);
223 void tm_work_object_dump(const TMWorkObject
*w
)
227 fprintf(stderr
, "%s", w
->file_name
);
229 fprintf(stderr
, " <- %s\n", w
->parent
->file_name
);
231 fprintf(stderr
, " <- NULL\n");