summaryrefslogtreecommitdiff
path: root/src/bin/e_path.c
blob: 3c34f7adab255a1760923ad3aa2b3ae4242c2492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

/* local subsystem functions */
static void      _e_path_free(E_Path *ep);
static void      _e_path_cache_free(E_Path *ep);
static Evas_Bool _e_path_cache_free_cb(const Evas_Hash *hash, const char *key, void *data, void *fdata);

/* externally accessible functions */
EAPI E_Path *
e_path_new(void)
{
   E_Path *ep;
   
   ep = E_OBJECT_ALLOC(E_Path, E_PATH_TYPE, _e_path_free);
   return ep;
}

EAPI void
e_path_default_path_append(E_Path *ep, const char *path)
{
   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   if (!path) return;
   if (path[0] == '~')
     {
        E_Path_Dir *epd;
	char *new_path;
	const char *home_dir;
	int len1, len2;
	
	home_dir = e_user_homedir_get();
	len1 = strlen(home_dir);
	len2 = strlen(path);
	new_path = malloc(len1 + len2 + 1);
	if (!new_path) return;
	epd = malloc(sizeof(E_Path_Dir));
        if (!epd)
	  {
	     free(new_path);
	     return;
	  }

	strcpy(new_path, home_dir);
	strcat(new_path, path + 1);
        epd->dir = eina_stringshare_add(new_path);
	free(new_path);
	ep->default_dir_list = evas_list_append(ep->default_dir_list, epd);	
     }
   else
     {
	E_Path_Dir *epd;
	epd = malloc(sizeof(E_Path_Dir));
	if (!epd)
	  return;
	epd->dir = eina_stringshare_add(path);
	ep->default_dir_list = evas_list_append(ep->default_dir_list, epd);
     }
   _e_path_cache_free(ep);
}

EAPI void
e_path_user_path_set(E_Path *ep, Evas_List **user_dir_list)
{
   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   
   ep->user_dir_list = user_dir_list;
   _e_path_cache_free(ep);
}

EAPI void
e_path_user_path_append(E_Path *ep, const char *path)
{
   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   if (!path) return;
   if (path[0] == '~')
     {
        E_Path_Dir *epd;
	char *new_path;
	const char *home_dir;
	int len1, len2;
	
	home_dir = e_user_homedir_get();
	len1 = strlen(home_dir);
	len2 = strlen(path);
	new_path = malloc(len1 + len2 + 1);
	if (!new_path) return;
	epd = malloc(sizeof(E_Path_Dir));
        if (!epd)
	  {
	     free(new_path);
	     return;
	  }

	strcpy(new_path, home_dir);
	strcat(new_path, path + 1);
        epd->dir = eina_stringshare_add(new_path);
	free(new_path);
	*(ep->user_dir_list) = evas_list_append(*(ep->user_dir_list), epd);	
     }
   else
     {
	E_Path_Dir *epd;
	epd = malloc(sizeof(E_Path_Dir));
	if (!epd)
	  return;
	epd->dir = eina_stringshare_add(path);
	*(ep->user_dir_list) = evas_list_append(*(ep->user_dir_list), epd);
     }
   _e_path_cache_free(ep);
}

EAPI void
e_path_user_path_prepend(E_Path *ep, const char *path)
{
   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   if (!path) return;
   if (path[0] == '~')
     {
        E_Path_Dir *epd;
	char *new_path;
	const char *home_dir;
	int len1, len2;
	
	home_dir = e_user_homedir_get();
	len1 = strlen(home_dir);
	len2 = strlen(path);
	new_path = malloc(len1 + len2 + 1);
	if (!new_path) return;
	epd = malloc(sizeof(E_Path_Dir));
        if (!epd)
	  {
	     free(new_path);
	     return;
	  }

	strcpy(new_path, home_dir);
	strcat(new_path, path + 1);
        epd->dir = eina_stringshare_add(new_path);
	free(new_path);
	*(ep->user_dir_list) = evas_list_prepend(*(ep->user_dir_list), epd);	
     }
   else
     {
	E_Path_Dir *epd;
	epd = malloc(sizeof(E_Path_Dir));
	if (!epd)
	  return;
	epd->dir = eina_stringshare_add(path);
	*(ep->user_dir_list) = evas_list_prepend(*(ep->user_dir_list), epd);
     }
   _e_path_cache_free(ep);
}

EAPI void
e_path_user_path_remove(E_Path *ep, const char *path)
{
   Evas_List *l;

   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   if (!path) return;
   if (path[0] == '~')
     {
	char *new_path;
	const char *home_dir;
	int len1, len2;
	
	home_dir = e_user_homedir_get();
	len1 = strlen(home_dir);
	len2 = strlen(path);
	new_path = malloc(len1 + len2 + 1);
	if (!new_path) return;
	strcpy(new_path, home_dir);
	strcat(new_path, path + 1);
	for (l = *(ep->user_dir_list); l; l = l->next)
	  {
	     E_Path_Dir *epd;
	     
	     epd = l->data;
	     if (epd->dir)
	       {
		  if (!strcmp(epd->dir, new_path))
		    {
		       *(ep->user_dir_list) = evas_list_remove_list(
						*(ep->user_dir_list), l);
		       eina_stringshare_del(epd->dir);
		       free(epd);
		       free(new_path);
		       _e_path_cache_free(ep);
		       return;
		    }
	       }
	  }
	free(new_path);
      }
   else
     {
	for (l = *(ep->user_dir_list); l; l = l->next)
	  {
	     E_Path_Dir *epd;
	     epd = l->data;
	     if (epd->dir)
	       {
		  if (!strcmp(epd->dir, path))
		    {
		       *(ep->user_dir_list) = evas_list_remove_list(
						*(ep->user_dir_list), l);
		       eina_stringshare_del(epd->dir);
		       free(epd);
		       _e_path_cache_free(ep);
		       return;
		    }
	       }
	  }
     }
}

EAPI void
e_path_user_path_clear(E_Path *ep)
{	
   while (*(ep->user_dir_list))
     {
	E_Path_Dir *epd;
	epd = (*(ep->user_dir_list))->data;
	*(ep->user_dir_list) = evas_list_remove_list(*(ep->user_dir_list), *(ep->user_dir_list));
	eina_stringshare_del(epd->dir);
	free(epd);     
     }
   _e_path_cache_free(ep);
}

EAPI const char *
e_path_find(E_Path *ep, const char *file)
{
   Evas_List *l;
   char *str;
   char buf[PATH_MAX] = "";
   
   E_OBJECT_CHECK_RETURN(ep, NULL);
   E_OBJECT_TYPE_CHECK_RETURN(ep, E_PATH_TYPE, NULL);

   if (!file) return NULL;
   str = evas_hash_find(ep->hash, file);
   if (str) return eina_stringshare_add(str);
   /* Look in the default dir list */
   for (l = ep->default_dir_list; l; l = l->next)
     {
	E_Path_Dir *epd;

	epd = l->data;
	if (epd->dir)
	  {
	     snprintf(buf, sizeof(buf), "%s/%s", epd->dir, file);
	     if (ecore_file_exists(buf))
	       {
		  if (evas_hash_size(ep->hash) >= 512)
		    _e_path_cache_free(ep);
		  ep->hash = evas_hash_add(ep->hash, file,
					   eina_stringshare_add(buf));
		  return eina_stringshare_add(buf);
	       }
	  }
     }
   /* Look in the users dir list */
   for (l = *(ep->user_dir_list); l; l = l->next)
     {
	E_Path_Dir *epd;
	
	epd = l->data;
	if (epd->dir)
	  {
	     snprintf(buf, sizeof(buf), "%s/%s", epd->dir, file);
	     if (ecore_file_exists(buf))
	       {
		  if (evas_hash_size(ep->hash) >= 512)
		    _e_path_cache_free(ep);
		  ep->hash = evas_hash_add(ep->hash, file,
					   eina_stringshare_add(buf));
		  return eina_stringshare_add(buf);
	       }
	  }
     }
   return NULL;
}

EAPI void
e_path_evas_append(E_Path *ep, Evas *evas)
{
   Evas_List *l;
   Evas_List *dir_list;
   
   E_OBJECT_CHECK(ep);
   E_OBJECT_TYPE_CHECK(ep, E_PATH_TYPE);
   if (!evas) return;

   dir_list = e_path_dir_list_get(ep);

   for (l = dir_list; l; l = l->next)
     {
	E_Path_Dir *epd;
	
	epd = l->data;
	if (epd->dir) evas_font_path_append(evas, epd->dir);
	eina_stringshare_del(epd->dir);
	free(epd);
     }
   if (dir_list) evas_list_free(dir_list);
}

/* combine default_list and and user_list in and easy to use list */
EAPI Evas_List *
e_path_dir_list_get(E_Path *ep)
{
   Evas_List	*dir_list;
   Evas_List	*l;
   E_Path_Dir	*new_epd;
   E_Path_Dir	*epd;

   dir_list = NULL;

   if (ep->user_dir_list)
     {
	for (l = *(ep->user_dir_list); l; l = l->next)
	  {
	     epd = l->data;
	     new_epd = malloc(sizeof(E_Path_Dir));
	     new_epd->dir = eina_stringshare_add(epd->dir);
	     dir_list = evas_list_append(dir_list, new_epd);
	  }
     }

   for (l = ep->default_dir_list; l; l = l->next)
     {
	epd = l->data;
	new_epd = malloc(sizeof(E_Path_Dir));
	new_epd->dir = eina_stringshare_add(epd->dir);
	dir_list = evas_list_append(dir_list, new_epd);
     }

   return dir_list;
}

EAPI void
e_path_dir_list_free(Evas_List *dir_list)
{
   E_Path_Dir *epd;

   while (dir_list)
     {
	epd = dir_list->data;
	dir_list = evas_list_remove_list(dir_list, dir_list);
	eina_stringshare_del(epd->dir);
	free(epd);
     }
}

/* local subsystem functions */
static void 
_e_path_free(E_Path *ep)
{
   _e_path_cache_free(ep);
   while (ep->default_dir_list)
     {
	E_Path_Dir *epd;
	epd = ep->default_dir_list->data;
	eina_stringshare_del(epd->dir);
	free(epd);
	ep->default_dir_list = evas_list_remove_list(ep->default_dir_list, 
						     ep->default_dir_list);
     }
   free(ep);
}

static void
_e_path_cache_free(E_Path *ep)
{
   if (!ep->hash) return;
   evas_hash_foreach(ep->hash, _e_path_cache_free_cb, NULL);
   evas_hash_free(ep->hash);
   ep->hash = NULL;
}

static Evas_Bool
_e_path_cache_free_cb(const Evas_Hash *hash __UNUSED__, const char *key __UNUSED__, void *data, void *fdata __UNUSED__)
{
   eina_stringshare_del(data);
   return 1;
}