Mercurial > geeqie.yaz
annotate src/ui_fileops.c @ 1051:2a02fa29478b
The comment section in the meta file is the last section. However it do
not accept comment lines starting with a '['. This patch will fix that.
- patch by Klaus Ethgen
author | nadvornik |
---|---|
date | Mon, 29 Sep 2008 21:17:19 +0000 |
parents | 4fe8f9656107 |
children | 1646720364cf |
rev | line source |
---|---|
9 | 1 /* |
2 * (SLIK) SimpLIstic sKin functions | |
69
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
40
diff
changeset
|
3 * (C) 2006 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
13 #ifdef HAVE_CONFIG_H | |
14 # include "config.h" | |
15 #endif | |
16 | |
17 #include <pwd.h> | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <string.h> | |
21 #include <unistd.h> | |
22 #include <sys/param.h> | |
23 #include <dirent.h> | |
24 #include <utime.h> | |
25 | |
26 #include <glib.h> | |
27 #include <gtk/gtk.h> /* for locale warning dialog */ | |
28 | |
281 | 29 #include "main.h" |
9 | 30 #include "ui_fileops.h" |
31 | |
32 #include "ui_utildlg.h" /* for locale warning dialog */ | |
33 | |
34 /* | |
35 *----------------------------------------------------------------------------- | |
36 * generic file information and manipulation routines (public) | |
37 *----------------------------------------------------------------------------- | |
442 | 38 */ |
9 | 39 |
40 | |
41 | |
42 void print_term(const gchar *text_utf8) | |
43 { | |
44 gchar *text_l; | |
45 | |
46 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL); | |
759 | 47 fputs((text_l) ? text_l : text_utf8, stdout); |
9 | 48 g_free(text_l); |
49 } | |
50 | |
51 static void encoding_dialog(const gchar *path); | |
52 | |
53 static gint encoding_dialog_idle(gpointer data) | |
54 { | |
55 gchar *path = data; | |
56 | |
57 encoding_dialog(path); | |
58 g_free(path); | |
59 | |
60 return FALSE; | |
61 } | |
62 | |
63 static gint encoding_dialog_delay(gpointer data) | |
64 { | |
65 g_idle_add(encoding_dialog_idle, data); | |
66 | |
67 return 0; | |
68 } | |
69 | |
70 static void encoding_dialog(const gchar *path) | |
71 { | |
72 static gint warned_user = FALSE; | |
73 GenericDialog *gd; | |
74 GString *string; | |
75 const gchar *lc; | |
76 const gchar *bf; | |
77 | |
78 /* check that gtk is initialized (loop is level > 0) */ | |
79 if (gtk_main_level() == 0) | |
80 { | |
81 /* gtk not initialized */ | |
82 gtk_init_add(encoding_dialog_delay, g_strdup(path)); | |
83 return; | |
84 } | |
85 | |
86 if (warned_user) return; | |
87 warned_user = TRUE; | |
88 | |
89 lc = getenv("LANG"); | |
90 bf = getenv("G_BROKEN_FILENAMES"); | |
91 | |
92 string = g_string_new(""); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
93 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
94 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
95 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
96 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
97 if (bf) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
98 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
99 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
100 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
101 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
102 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined"); |
9 | 103 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8"))) |
104 { | |
105 gchar *name; | |
106 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
107 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
108 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
109 |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
110 if (g_utf8_validate(path, -1, NULL)) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
111 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
112 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
113 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
114 g_string_append(string, "\n"); |
9 | 115 g_free(name); |
116 } | |
117 | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
118 gd = generic_dialog_new(_("Filename encoding locale mismatch"), |
254
9faf34f047b1
Make the wmclass value unique among the code by defining
zas_
parents:
138
diff
changeset
|
119 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL); |
9 | 120 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE); |
121 | |
122 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING, | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
123 _("Filename encoding locale mismatch"), string->str); |
9 | 124 |
125 gtk_widget_show(gd->dialog); | |
126 | |
127 g_string_free(string, TRUE); | |
128 } | |
129 | |
130 gchar *path_to_utf8(const gchar *path) | |
131 { | |
132 gchar *utf8; | |
133 GError *error = NULL; | |
134 | |
135 if (!path) return NULL; | |
136 | |
137 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error); | |
138 if (error) | |
139 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
140 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message); |
9 | 141 g_error_free(error); |
142 encoding_dialog(path); | |
143 } | |
144 if (!utf8) | |
145 { | |
146 /* just let it through, but bad things may happen */ | |
147 utf8 = g_strdup(path); | |
148 } | |
149 | |
150 return utf8; | |
151 } | |
152 | |
153 gchar *path_from_utf8(const gchar *utf8) | |
154 { | |
155 gchar *path; | |
156 GError *error = NULL; | |
157 | |
158 if (!utf8) return NULL; | |
159 | |
160 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error); | |
161 if (error) | |
162 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
163 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message); |
9 | 164 g_error_free(error); |
165 } | |
166 if (!path) | |
167 { | |
168 /* if invalid UTF-8, text probaby still in original form, so just copy it */ | |
169 path = g_strdup(utf8); | |
170 } | |
171 | |
172 return path; | |
173 } | |
174 | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
175 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */ |
9 | 176 const gchar *homedir(void) |
177 { | |
178 static gchar *home = NULL; | |
179 | |
180 if (!home) | |
181 home = path_to_utf8(getenv("HOME")); | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
182 |
9 | 183 if (!home) |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
184 home = path_to_utf8(g_get_home_dir()); |
9 | 185 |
186 return home; | |
187 } | |
188 | |
189 gint stat_utf8(const gchar *s, struct stat *st) | |
190 { | |
191 gchar *sl; | |
192 gint ret; | |
193 | |
194 if (!s) return FALSE; | |
195 sl = path_from_utf8(s); | |
196 ret = (stat(sl, st) == 0); | |
197 g_free(sl); | |
198 | |
199 return ret; | |
200 } | |
201 | |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
202 gint lstat_utf8(const gchar *s, struct stat *st) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
203 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
204 gchar *sl; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
205 gint ret; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
206 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
207 if (!s) return FALSE; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
208 sl = path_from_utf8(s); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
209 ret = (lstat(sl, st) == 0); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
210 g_free(sl); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
211 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
212 return ret; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
213 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
214 |
9 | 215 gint isname(const gchar *s) |
216 { | |
217 struct stat st; | |
218 | |
219 return stat_utf8(s, &st); | |
220 } | |
221 | |
222 gint isfile(const gchar *s) | |
223 { | |
224 struct stat st; | |
225 | |
226 return (stat_utf8(s, &st) && S_ISREG(st.st_mode)); | |
227 } | |
228 | |
229 gint isdir(const gchar *s) | |
230 { | |
231 struct stat st; | |
442 | 232 |
9 | 233 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode)); |
234 } | |
235 | |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
236 gint islink(const gchar *s) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
237 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
238 struct stat st; |
442 | 239 |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
240 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode)); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
241 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
242 |
9 | 243 gint64 filesize(const gchar *s) |
244 { | |
245 struct stat st; | |
442 | 246 |
9 | 247 if (!stat_utf8(s, &st)) return 0; |
983 | 248 return st.st_size; |
9 | 249 } |
250 | |
251 time_t filetime(const gchar *s) | |
252 { | |
442 | 253 struct stat st; |
9 | 254 |
255 if (!stat_utf8(s, &st)) return 0; | |
256 return st.st_mtime; | |
257 } | |
258 | |
259 gint filetime_set(const gchar *s, time_t tval) | |
260 { | |
261 gint ret = FALSE; | |
262 | |
263 if (tval > 0) | |
264 { | |
265 struct utimbuf ut; | |
266 gchar *sl; | |
267 | |
268 ut.actime = ut.modtime = tval; | |
269 | |
270 sl = path_from_utf8(s); | |
271 ret = (utime(sl, &ut) == 0); | |
272 g_free(sl); | |
273 } | |
274 | |
275 return ret; | |
276 } | |
277 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
278 gint access_file(const gchar *s, gint mode) |
9 | 279 { |
280 gchar *sl; | |
281 gint ret; | |
282 | |
283 if (!s) return FALSE; | |
284 | |
285 sl = path_from_utf8(s); | |
286 ret = (access(sl, mode) == 0); | |
287 g_free(sl); | |
288 | |
289 return ret; | |
290 } | |
291 | |
292 gint unlink_file(const gchar *s) | |
293 { | |
294 gchar *sl; | |
295 gint ret; | |
296 | |
297 if (!s) return FALSE; | |
298 | |
299 sl = path_from_utf8(s); | |
300 ret = (unlink(sl) == 0); | |
301 g_free(sl); | |
302 | |
303 return ret; | |
304 } | |
305 | |
306 gint symlink_utf8(const gchar *source, const gchar *target) | |
307 { | |
308 gchar *sl; | |
309 gchar *tl; | |
310 gint ret; | |
311 | |
312 if (!source || !target) return FALSE; | |
313 | |
314 sl = path_from_utf8(source); | |
315 tl = path_from_utf8(target); | |
316 | |
317 ret = (symlink(sl, tl) == 0); | |
318 | |
319 g_free(sl); | |
320 g_free(tl); | |
321 | |
322 return ret; | |
323 } | |
324 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
325 gint mkdir_utf8(const gchar *s, gint mode) |
9 | 326 { |
327 gchar *sl; | |
328 gint ret; | |
329 | |
330 if (!s) return FALSE; | |
331 | |
332 sl = path_from_utf8(s); | |
333 ret = (mkdir(sl, mode) == 0); | |
334 g_free(sl); | |
335 return ret; | |
336 } | |
337 | |
338 gint rmdir_utf8(const gchar *s) | |
339 { | |
340 gchar *sl; | |
341 gint ret; | |
342 | |
343 if (!s) return FALSE; | |
344 | |
345 sl = path_from_utf8(s); | |
346 ret = (rmdir(sl) == 0); | |
347 g_free(sl); | |
348 | |
349 return ret; | |
350 } | |
351 | |
352 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime) | |
353 { | |
354 struct stat st; | |
355 gchar *sl, *tl; | |
356 gint ret = FALSE; | |
357 | |
358 if (!s || !t) return FALSE; | |
359 | |
360 sl = path_from_utf8(s); | |
361 tl = path_from_utf8(t); | |
362 | |
363 if (stat(sl, &st) == 0) | |
364 { | |
365 struct utimbuf tb; | |
366 | |
367 ret = TRUE; | |
368 | |
369 /* set the dest file attributes to that of source (ignoring errors) */ | |
370 | |
371 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE; | |
372 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE; | |
373 | |
374 tb.actime = st.st_atime; | |
375 tb.modtime = st.st_mtime; | |
376 if (mtime && utime(tl, &tb) < 0) ret = FALSE; | |
377 } | |
378 | |
379 g_free(sl); | |
380 g_free(tl); | |
381 | |
382 return ret; | |
383 } | |
384 | |
385 /* paths are in filesystem encoding */ | |
386 static gint hard_linked(const gchar *a, const gchar *b) | |
387 { | |
388 struct stat sta; | |
389 struct stat stb; | |
390 | |
391 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE; | |
392 | |
393 return (sta.st_dev == stb.st_dev && | |
394 sta.st_ino == stb.st_ino); | |
395 } | |
396 | |
397 gint copy_file(const gchar *s, const gchar *t) | |
398 { | |
399 FILE *fi = NULL; | |
400 FILE *fo = NULL; | |
401 gchar *sl, *tl; | |
402 gchar buf[4096]; | |
704
839525c4e85f
Use size_t instead of gint, it silents a signed vs unsigned warning.
zas_
parents:
703
diff
changeset
|
403 size_t b; |
9 | 404 |
405 sl = path_from_utf8(s); | |
406 tl = path_from_utf8(t); | |
407 | |
408 if (hard_linked(sl, tl)) | |
409 { | |
410 g_free(sl); | |
411 g_free(tl); | |
412 return TRUE; | |
413 } | |
414 | |
415 fi = fopen(sl, "rb"); | |
416 if (fi) | |
417 { | |
418 fo = fopen(tl, "wb"); | |
419 if (!fo) | |
420 { | |
421 fclose(fi); | |
422 fi = NULL; | |
423 } | |
424 } | |
425 | |
426 g_free(sl); | |
427 g_free(tl); | |
428 | |
429 if (!fi || !fo) return FALSE; | |
430 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
431 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0) |
9 | 432 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
433 if (fwrite(buf, sizeof(gchar), b, fo) != b) |
9 | 434 { |
435 fclose(fi); | |
436 fclose(fo); | |
437 return FALSE; | |
438 } | |
439 } | |
440 | |
441 fclose(fi); | |
442 fclose(fo); | |
443 | |
444 copy_file_attributes(s, t, TRUE, TRUE); | |
445 | |
446 return TRUE; | |
447 } | |
448 | |
449 gint move_file(const gchar *s, const gchar *t) | |
450 { | |
451 gchar *sl, *tl; | |
452 gint ret = TRUE; | |
453 | |
454 if (!s || !t) return FALSE; | |
455 | |
456 sl = path_from_utf8(s); | |
457 tl = path_from_utf8(t); | |
458 if (rename(sl, tl) < 0) | |
459 { | |
460 /* this may have failed because moving a file across filesystems | |
461 was attempted, so try copy and delete instead */ | |
462 if (copy_file(s, t)) | |
463 { | |
464 if (unlink(sl) < 0) | |
465 { | |
466 /* err, now we can't delete the source file so return FALSE */ | |
467 ret = FALSE; | |
468 } | |
469 } | |
470 else | |
471 { | |
472 ret = FALSE; | |
473 } | |
474 } | |
475 g_free(sl); | |
476 g_free(tl); | |
477 | |
478 return ret; | |
479 } | |
480 | |
481 gint rename_file(const gchar *s, const gchar *t) | |
482 { | |
483 gchar *sl, *tl; | |
484 gint ret; | |
485 | |
486 if (!s || !t) return FALSE; | |
487 | |
488 sl = path_from_utf8(s); | |
489 tl = path_from_utf8(t); | |
490 ret = (rename(sl, tl) == 0); | |
491 g_free(sl); | |
492 g_free(tl); | |
493 | |
494 return ret; | |
495 } | |
496 | |
497 gchar *get_current_dir(void) | |
498 { | |
499 gchar *pathl; | |
500 gchar *path8; | |
501 | |
502 pathl = g_get_current_dir(); | |
503 path8 = path_to_utf8(pathl); | |
504 g_free(pathl); | |
505 | |
506 return path8; | |
507 } | |
508 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
545
diff
changeset
|
509 void string_list_free(GList *list) |
9 | 510 { |
511 g_list_foreach(list, (GFunc)g_free, NULL); | |
512 g_list_free(list); | |
513 } | |
514 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
545
diff
changeset
|
515 GList *string_list_copy(GList *list) |
9 | 516 { |
517 GList *new_list = NULL; | |
518 GList *work; | |
519 | |
520 work = list; | |
521 while (work) | |
522 { | |
523 gchar *path; | |
442 | 524 |
9 | 525 path = work->data; |
526 work = work->next; | |
442 | 527 |
9 | 528 new_list = g_list_prepend(new_list, g_strdup(path)); |
529 } | |
442 | 530 |
9 | 531 return g_list_reverse(new_list); |
532 } | |
533 | |
534 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad) | |
535 { | |
536 gchar *unique; | |
537 gint n = 1; | |
538 | |
539 if (!ext) ext = ""; | |
540 if (!divider) divider = ""; | |
541 | |
542 unique = g_strconcat(path, ext, NULL); | |
543 while (isname(unique)) | |
544 { | |
545 g_free(unique); | |
546 if (pad) | |
547 { | |
548 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext); | |
549 } | |
550 else | |
551 { | |
552 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext); | |
553 } | |
554 n++; | |
555 if (n > 999) | |
556 { | |
557 /* well, we tried */ | |
558 g_free(unique); | |
559 return NULL; | |
560 } | |
561 } | |
562 | |
563 return unique; | |
564 } | |
565 | |
566 gchar *unique_filename_simple(const gchar *path) | |
567 { | |
568 gchar *unique; | |
569 const gchar *name; | |
570 const gchar *ext; | |
571 | |
572 if (!path) return NULL; | |
573 | |
574 name = filename_from_path(path); | |
575 if (!name) return NULL; | |
576 | |
577 ext = extension_from_path(name); | |
578 | |
579 if (!ext) | |
580 { | |
581 unique = unique_filename(path, NULL, "_", TRUE); | |
582 } | |
583 else | |
584 { | |
585 gchar *base; | |
586 | |
587 base = remove_extension_from_path(path); | |
588 unique = unique_filename(base, ext, "_", TRUE); | |
589 g_free(base); | |
590 } | |
591 | |
592 return unique; | |
593 } | |
594 | |
595 const gchar *filename_from_path(const gchar *path) | |
596 { | |
597 const gchar *base; | |
598 | |
599 if (!path) return NULL; | |
600 | |
701 | 601 base = strrchr(path, G_DIR_SEPARATOR); |
9 | 602 if (base) return base + 1; |
603 | |
604 return path; | |
605 } | |
606 | |
607 gchar *remove_level_from_path(const gchar *path) | |
608 { | |
543 | 609 gint p = 0, n = -1; |
9 | 610 |
611 if (!path) return NULL; | |
612 | |
543 | 613 while (path[p]) |
614 { | |
701 | 615 if (path[p] == G_DIR_SEPARATOR) n = p; |
543 | 616 p++; |
617 } | |
618 if (n <= 0) n++; | |
619 | |
620 return g_strndup(path, (gsize) n); | |
9 | 621 } |
622 | |
623 const gchar *extension_from_path(const gchar *path) | |
624 { | |
625 if (!path) return NULL; | |
626 return strrchr(path, '.'); | |
627 } | |
628 | |
629 gint file_extension_match(const gchar *path, const gchar *ext) | |
630 { | |
631 gint p; | |
632 gint e; | |
633 | |
634 if (!path) return FALSE; | |
635 if (!ext) return TRUE; | |
636 | |
637 p = strlen(path); | |
638 e = strlen(ext); | |
639 | |
605
651ae2be1031
Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents:
576
diff
changeset
|
640 /* FIXME: utf8 */ |
9 | 641 return (p > e && strncasecmp(path + p - e, ext, e) == 0); |
642 } | |
643 | |
644 gchar *remove_extension_from_path(const gchar *path) | |
645 { | |
544 | 646 gint p = 0, n = -1; |
9 | 647 |
648 if (!path) return NULL; | |
649 | |
544 | 650 while (path[p]) |
651 { | |
652 if (path[p] == '.') n = p; | |
653 p++; | |
654 } | |
655 if (n < 0) n = p; | |
656 | |
657 return g_strndup(path, (gsize) n); | |
9 | 658 } |
659 | |
660 void parse_out_relatives(gchar *path) | |
661 { | |
662 gint s, t; | |
663 | |
664 if (!path) return; | |
665 | |
666 s = t = 0; | |
667 | |
668 while (path[s] != '\0') | |
669 { | |
919 | 670 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.') |
9 | 671 { |
919 | 672 /* /. occurence, let's see more */ |
673 gint p = s + 2; | |
674 | |
675 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0') | |
676 { | |
677 /* /./ or /., just skip this part */ | |
678 s = p; | |
679 continue; | |
680 } | |
681 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0')) | |
682 { | |
683 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */ | |
684 s = p + 1; | |
685 if (t > 0) t--; | |
686 while (path[t] != G_DIR_SEPARATOR && t > 0) t--; | |
687 continue; | |
688 } | |
9 | 689 } |
919 | 690 |
691 if (s != t) path[t] = path[s]; | |
692 t++; | |
693 s++; | |
9 | 694 } |
919 | 695 |
701 | 696 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++; |
697 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--; | |
9 | 698 path[t] = '\0'; |
699 } | |
700 | |
701 gint file_in_path(const gchar *name) | |
702 { | |
703 gchar *path; | |
704 gchar *namel; | |
705 gint p, l; | |
706 gint ret = FALSE; | |
707 | |
708 if (!name) return FALSE; | |
709 path = g_strdup(getenv("PATH")); | |
710 if (!path) return FALSE; | |
711 namel = path_from_utf8(name); | |
712 | |
713 p = 0; | |
714 l = strlen(path); | |
715 while (p < l && !ret) | |
716 { | |
717 gchar *f; | |
718 gint e = p; | |
719 while (path[e] != ':' && path[e] != '\0') e++; | |
720 path[e] = '\0'; | |
721 e++; | |
703 | 722 f = g_build_filename(path + p, namel, NULL); |
9 | 723 if (isfile(f)) ret = TRUE; |
724 g_free(f); | |
725 p = e; | |
726 } | |
727 g_free(namel); | |
728 g_free(path); | |
729 | |
730 return ret; | |
731 } |