No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* eel-vfs-extensions.c - gnome-vfs extensions. Its likely some of these will
4 be part of gnome-vfs in the future.
5
6 Copyright (C) 1999, 2000 Eazel, Inc.
7
8 The Gnome Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The Gnome Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with the Gnome Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22
23 Authors: Darin Adler <darin@eazel.com>
24 Pavel Cisler <pavel@eazel.com>
25 Mike Fleming <mfleming@eazel.com>
26 John Sullivan <sullivan@eazel.com>
27 */
28
29 #include <config.h>
30 #include "eel-vfs-extensions.h"
31 #include "eel-glib-extensions.h"
32 #include "eel-lib-self-check-functions.h"
33
34 #include <glib.h>
35 #include <glib/gi18n-lib.h>
36 #include <gio/gio.h>
37
38 #include "eel-string.h"
39
40 #include <string.h>
41 #include <stdlib.h>
42
43 gboolean
44 eel_uri_is_trash (const char *uri)
45 {
46 return g_str_has_prefix (uri, "trash:");
47 }
48
49 gboolean
50 eel_uri_is_recent (const char *uri)
51 {
52 return g_str_has_prefix (uri, "recent:");
53 }
54
55 gboolean
56 eel_uri_is_search (const char *uri)
57 {
58 return g_str_has_prefix (uri, EEL_SEARCH_URI);
59 }
60
61 gboolean
62 eel_uri_is_desktop (const char *uri)
63 {
64 return g_str_has_prefix (uri, EEL_DESKTOP_URI);
65 }
66
67 char *
68 eel_make_valid_utf8 (const char *name)
69 {
70 GString *string;
71 const char *remainder, *invalid;
72 int remaining_bytes, valid_bytes;
73
74 string = NULL;
75 remainder = name;
76 remaining_bytes = strlen (name);
77
78 while (remaining_bytes != 0) {
79 if (g_utf8_validate (remainder, remaining_bytes, &invalid)) {
80 break;
81 }
82 valid_bytes = invalid - remainder;
83
84 if (string == NULL) {
85 string = g_string_sized_new (remaining_bytes);
86 }
87 g_string_append_len (string, remainder, valid_bytes);
88 g_string_append_c (string, '?');
89
90 remaining_bytes -= valid_bytes + 1;
91 remainder = invalid + 1;
92 }
93
94 if (string == NULL) {
95 return g_strdup (name);
96 }
97
98 g_string_append (string, remainder);
99 g_string_append (string, _(" (invalid Unicode)"));
100 g_assert (g_utf8_validate (string->str, -1, NULL));
101
102 return g_string_free (string, FALSE);
103 }
104
105 char *
106 eel_filename_get_extension_offset (const char *filename)
107 {
108 char *end, *end2;
109 const char *start;
110
111 if (filename == NULL || filename[0] == '\0') {
112 return NULL;
113 }
114
115 /* basename must have at least one char */
116 start = filename + 1;
117
118 end = strrchr (start, '.');
119 if (end == NULL || end[1] == '\0') {
120 return NULL;
121 }
122
123 if (end != start) {
124 if (strcmp (end, ".gz") == 0 ||
125 strcmp (end, ".bz2") == 0 ||
126 strcmp (end, ".sit") == 0 ||
127 strcmp (end, ".Z") == 0) {
128 end2 = end - 1;
129 while (end2 > start &&
130 *end2 != '.') {
131 end2--;
132 }
133 if (end2 != start) {
134 end = end2;
135 }
136 }
137 }
138
139 return end;
140 }
141
142 char *
143 eel_filename_strip_extension (const char * filename_with_extension)
144 {
145 char *filename, *end;
146
147 if (filename_with_extension == NULL) {
148 return NULL;
149 }
150
151 filename = g_strdup (filename_with_extension);
152 end = eel_filename_get_extension_offset (filename);
153
154 if (end && end != filename) {
155 *end = '\0';
156 }
157
158 return filename;
159 }
160
161 void
162 eel_filename_get_rename_region (const char *filename,
163 int *start_offset,
164 int *end_offset)
165 {
166 char *filename_without_extension;
167
168 g_return_if_fail (start_offset != NULL);
169 g_return_if_fail (end_offset != NULL);
170
171 *start_offset = 0;
172 *end_offset = 0;
173
174 g_return_if_fail (filename != NULL);
175
176 filename_without_extension = eel_filename_strip_extension (filename);
177 *end_offset = g_utf8_strlen (filename_without_extension, -1);
178
179 g_free (filename_without_extension);
180 }