1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
3 /* eel-vfs-extensions.c - mate-vfs extensions.  Its likely some of these will
4                           be part of mate-vfs in the future.
5 
6    Copyright (C) 1999, 2000 Eazel, Inc.
7 
8    The Mate 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 Mate 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 Mate Library; see the file COPYING.LIB.  If not,
20    write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21    Boston, MA 02110-1301, 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 #include <glib.h>
34 #include <glib/gi18n-lib.h>
35 #include <gio/gio.h>
36 
37 #include "eel-string.h"
38 
39 #include <string.h>
40 #include <stdlib.h>
41 
42 gboolean
eel_uri_is_trash(const char * uri)43 eel_uri_is_trash (const char *uri)
44 {
45     return eel_istr_has_prefix (uri, "trash:");
46 }
47 
48 gboolean
eel_uri_is_search(const char * uri)49 eel_uri_is_search (const char *uri)
50 {
51     return eel_istr_has_prefix (uri, EEL_SEARCH_URI);
52 }
53 
54 gboolean
eel_uri_is_desktop(const char * uri)55 eel_uri_is_desktop (const char *uri)
56 {
57     return eel_istr_has_prefix (uri, EEL_DESKTOP_URI);
58 }
59 
60 char *
eel_make_valid_utf8(const char * name)61 eel_make_valid_utf8 (const char *name)
62 {
63     GString *string;
64     const char *remainder, *invalid;
65     int remaining_bytes;
66 
67     string = NULL;
68     remainder = name;
69     remaining_bytes = strlen (name);
70 
71     while (remaining_bytes != 0)
72     {
73         int valid_bytes;
74 
75         if (g_utf8_validate (remainder, remaining_bytes, &invalid))
76         {
77             break;
78         }
79         valid_bytes = invalid - remainder;
80 
81         if (string == NULL)
82         {
83             string = g_string_sized_new (remaining_bytes);
84         }
85         g_string_append_len (string, remainder, valid_bytes);
86         g_string_append_c (string, '?');
87 
88         remaining_bytes -= valid_bytes + 1;
89         remainder = invalid + 1;
90     }
91 
92     if (string == NULL)
93     {
94         return g_strdup (name);
95     }
96 
97     g_string_append (string, remainder);
98     g_string_append (string, _(" (invalid Unicode)"));
99     g_assert (g_utf8_validate (string->str, -1, NULL));
100 
101     return g_string_free (string, FALSE);
102 }
103 
104 char *
eel_filename_strip_extension(const char * filename_with_extension)105 eel_filename_strip_extension (const char * filename_with_extension)
106 {
107     char *filename, *end, *end2;
108 
109     if (filename_with_extension == NULL)
110     {
111         return NULL;
112     }
113 
114     filename = g_strdup (filename_with_extension);
115 
116     end = strrchr (filename, '.');
117 
118     if (end && end != filename)
119     {
120         if (strcmp (end, ".gz") == 0 ||
121                 strcmp (end, ".bz2") == 0 ||
122                 strcmp (end, ".sit") == 0 ||
123                 strcmp (end, ".Z") == 0)
124         {
125             end2 = end - 1;
126             while (end2 > filename &&
127                     *end2 != '.')
128             {
129                 end2--;
130             }
131             if (end2 != filename)
132             {
133                 end = end2;
134             }
135         }
136         *end = '\0';
137     }
138 
139     return filename;
140 }
141 
142 void
eel_filename_get_rename_region(const char * filename,int * start_offset,int * end_offset)143 eel_filename_get_rename_region (const char           *filename,
144                                 int                  *start_offset,
145                                 int                  *end_offset)
146 {
147     char *filename_without_extension;
148 
149     g_return_if_fail (start_offset != NULL);
150     g_return_if_fail (end_offset != NULL);
151 
152     *start_offset = 0;
153     *end_offset = 0;
154 
155     g_return_if_fail (filename != NULL);
156 
157     filename_without_extension = eel_filename_strip_extension (filename);
158     *end_offset = g_utf8_strlen (filename_without_extension, -1);
159 
160     g_free (filename_without_extension);
161 }
162