Mercurial > audlegacy
annotate libaudacious/vfs_gnome.c @ 160:faa50a5760ee trunk
[svn] Do not try to build LIRC when not enabled.
author | chainsaw |
---|---|
date | Sat, 12 Nov 2005 17:20:49 -0800 |
parents | ae0af0523844 |
children | adee87ebe853 |
rev | line source |
---|---|
0 | 1 /* This program is free software; you can redistribute it and/or modify |
2 * it under the terms of the GNU General Public Licensse as published by | |
3 * the Free Software Foundation; either version 2 of the License, or | |
4 * (at your option) any later version. | |
5 * | |
6 * This program is distributed in the hope that it will be useful, | |
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 * GNU General Public License for more details. | |
10 * | |
11 * You should have received a copy of the GNU General Public License | |
12 * along with this program; if not, write to the Free Software | |
13 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
14 */ | |
15 | |
16 #include "vfs.h" | |
17 #include <string.h> | |
18 #include <libgnomevfs/gnome-vfs.h> | |
19 | |
20 | |
21 struct _VFSFile | |
22 { | |
23 GnomeVFSHandle *handle; | |
24 }; | |
25 | |
26 | |
27 static void mode_to_gnome_vfs(const gchar * mode, | |
28 GnomeVFSOpenMode * g_mode, | |
29 gboolean * truncate, | |
30 gboolean * append); | |
31 | |
32 gboolean | |
33 vfs_init(void) | |
34 { | |
35 if (!gnome_vfs_init()) | |
36 return FALSE; | |
37 | |
38 g_atexit(gnome_vfs_shutdown); | |
39 return TRUE; | |
40 } | |
41 | |
42 VFSFile * | |
43 vfs_fopen(const gchar * path, | |
44 const gchar * mode) | |
45 { | |
46 VFSFile *file; | |
47 GnomeVFSResult g_result; | |
48 GnomeVFSOpenMode g_mode; | |
49 gboolean truncate, append; | |
50 | |
51 file = g_new(VFSFile, 1); | |
52 | |
53 mode_to_gnome_vfs(mode, &g_mode, &truncate, &append); | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
54 gchar *escaped_file = gnome_vfs_escape_path_string(path); |
0 | 55 |
56 if (!truncate) { | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
57 g_result = gnome_vfs_open(&(file->handle), escaped_file, g_mode); |
0 | 58 if (append && g_result == GNOME_VFS_ERROR_NOT_FOUND) { |
59 g_result = gnome_vfs_create(&(file->handle), | |
60 path, g_mode, TRUE, | |
61 S_IRUSR | S_IWUSR | | |
62 S_IRGRP | S_IWGRP | | |
63 S_IROTH | S_IWOTH); | |
64 } | |
65 | |
66 if (append && g_result == GNOME_VFS_OK) { | |
67 g_result = gnome_vfs_seek(file->handle, GNOME_VFS_SEEK_END, 0); | |
68 if (g_result != GNOME_VFS_OK) | |
69 gnome_vfs_close(file->handle); | |
70 } | |
71 } | |
72 else { | |
73 g_result = gnome_vfs_create(&(file->handle), | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
74 escaped_file, g_mode, FALSE, |
0 | 75 S_IRUSR | S_IWUSR | |
76 S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
77 } | |
78 | |
79 if (g_result != GNOME_VFS_OK) | |
80 file->handle = NULL; | |
81 | |
82 if (file->handle == NULL) { | |
83 g_free(file); | |
84 file = NULL; | |
85 } | |
86 | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
87 g_free(escaped_file); |
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
88 |
0 | 89 return file; |
90 } | |
91 | |
92 gint | |
93 vfs_fclose(VFSFile * file) | |
94 { | |
95 gint ret = 0; | |
96 | |
97 if (file->handle) { | |
98 if (gnome_vfs_close(file->handle) != GNOME_VFS_OK) | |
99 ret = -1; | |
100 } | |
101 | |
102 g_free(file); | |
103 | |
104 return ret; | |
105 } | |
106 | |
107 size_t | |
108 vfs_fread(gpointer ptr, | |
109 size_t size, | |
110 size_t nmemb, | |
111 VFSFile * file) | |
112 { | |
113 GnomeVFSResult result; | |
114 GnomeVFSFileSize bytes_read; | |
115 | |
116 result = gnome_vfs_read(file->handle, ptr, size * nmemb, &bytes_read); | |
117 if (result == GNOME_VFS_OK) | |
118 return bytes_read; | |
119 if (result == GNOME_VFS_ERROR_EOF) | |
120 return 0; | |
121 else | |
122 return -1; | |
123 } | |
124 | |
125 size_t | |
126 vfs_fwrite(gconstpointer ptr, | |
127 size_t size, | |
128 size_t nmemb, | |
129 VFSFile * file) | |
130 { | |
131 GnomeVFSResult result; | |
132 GnomeVFSFileSize bytes_written; | |
133 | |
134 result = gnome_vfs_write(file->handle, ptr, size * nmemb, &bytes_written); | |
135 if (result == GNOME_VFS_OK) | |
136 return bytes_written; | |
137 else | |
138 return -1; | |
139 } | |
140 | |
141 gint | |
142 vfs_fseek(VFSFile * file, | |
143 glong offset, | |
144 gint whence) | |
145 { | |
146 GnomeVFSResult result; | |
147 GnomeVFSSeekPosition g_whence; | |
148 | |
149 switch (whence) { | |
150 case SEEK_SET: | |
151 g_whence = GNOME_VFS_SEEK_START; | |
152 break; | |
153 case SEEK_CUR: | |
154 g_whence = GNOME_VFS_SEEK_CURRENT; | |
155 break; | |
156 case SEEK_END: | |
157 g_whence = GNOME_VFS_SEEK_END; | |
158 break; | |
159 default: | |
160 g_warning("vfs_fseek: invalid whence value"); | |
161 return -1; | |
162 } | |
163 | |
164 result = gnome_vfs_seek(file->handle, g_whence, offset); | |
165 | |
166 if (result == GNOME_VFS_OK) | |
167 return 0; | |
168 else | |
169 return -1; | |
170 } | |
171 | |
172 void | |
173 vfs_rewind(VFSFile * file) | |
174 { | |
175 vfs_fseek(file, 0L, SEEK_SET); | |
176 } | |
177 | |
178 glong | |
179 vfs_ftell(VFSFile * file) | |
180 { | |
181 GnomeVFSResult result; | |
182 GnomeVFSFileSize position; | |
183 | |
184 result = gnome_vfs_tell(file->handle, &position); | |
185 | |
186 if (result == GNOME_VFS_OK) | |
187 return position; | |
188 else | |
189 return -1; | |
190 } | |
191 | |
192 gboolean | |
193 vfs_file_test(const gchar * path, | |
194 GFileTest test) | |
195 { | |
196 GnomeVFSResult result; | |
197 GnomeVFSFileInfo info; | |
198 GFileTest file_test; | |
199 | |
200 result = gnome_vfs_get_file_info(path, &info, | |
201 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS); | |
202 | |
203 if (test == G_FILE_TEST_EXISTS) | |
204 return (result == GNOME_VFS_OK) ? TRUE : FALSE; | |
205 else if (test == G_FILE_TEST_IS_EXECUTABLE) | |
206 return (info.permissions & GNOME_VFS_PERM_ACCESS_EXECUTABLE) | |
207 ? TRUE : FALSE; | |
208 | |
209 switch (info.type) { | |
210 case GNOME_VFS_FILE_TYPE_REGULAR: | |
211 file_test = G_FILE_TEST_IS_REGULAR; | |
212 break; | |
213 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: | |
214 file_test = G_FILE_TEST_IS_SYMLINK; | |
215 break; | |
216 case GNOME_VFS_FILE_TYPE_DIRECTORY: | |
217 file_test = G_FILE_TEST_IS_DIR; | |
218 default: | |
219 return FALSE; | |
220 } | |
221 | |
222 if (test == file_test) | |
223 return TRUE; | |
224 else | |
225 return FALSE; | |
226 } | |
227 | |
228 gboolean | |
229 vfs_is_writeable(const gchar * path) | |
230 { | |
231 GnomeVFSFileInfo info; | |
232 | |
233 if (gnome_vfs_get_file_info(path, &info, GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS) | |
234 != GNOME_VFS_OK) { | |
235 return FALSE; | |
236 } | |
237 | |
238 return (info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE ? TRUE : FALSE); | |
239 } | |
240 | |
241 gint | |
242 vfs_truncate(VFSFile * file, | |
243 glong length) | |
244 { | |
245 GnomeVFSResult result; | |
246 | |
247 result = gnome_vfs_truncate_handle(file->handle, (GnomeVFSFileSize) length); | |
248 | |
249 if (result == GNOME_VFS_OK) | |
250 return 0; | |
251 else | |
252 return -1; | |
253 } | |
254 | |
255 static gchar *strdup_exclude_chars(const gchar * s, | |
256 const gchar * c); | |
257 | |
258 static void | |
259 mode_to_gnome_vfs(const gchar * mode, | |
260 GnomeVFSOpenMode * g_mode, | |
261 gboolean * truncate, | |
262 gboolean * append) | |
263 { | |
264 gchar *s; | |
265 | |
266 *g_mode = GNOME_VFS_OPEN_RANDOM; | |
267 *truncate = *append = FALSE; | |
268 | |
269 s = strdup_exclude_chars(mode, "bt"); | |
270 switch (s[0]) { | |
271 case 'r': | |
272 *g_mode |= GNOME_VFS_OPEN_READ; | |
273 | |
274 if (s[1] == '+') | |
275 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
276 | |
277 break; | |
278 case 'w': | |
279 case 'a': | |
280 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
281 | |
282 if (s[0] == 'w') | |
283 *truncate = TRUE; | |
284 else | |
285 *append = TRUE; | |
286 | |
287 if (s[1] == '+') | |
288 *g_mode |= GNOME_VFS_OPEN_READ; | |
289 | |
290 break; | |
291 default: | |
292 g_warning("mode_to_gnome_vfs: unhandled mode character"); | |
293 } | |
294 g_free(s); | |
295 } | |
296 | |
297 static gchar * | |
298 strdup_exclude_chars(const gchar * s, | |
299 const gchar * c) | |
300 { | |
301 gint i, j, k; | |
302 gint newlen = 0; | |
303 gchar *newstr; | |
304 gboolean found; | |
305 | |
306 /* Calculate number of chars in new string */ | |
307 for (i = 0; s[i] != '\0'; i++) { | |
308 found = FALSE; | |
309 | |
310 for (j = 0; j < strlen(c) && !found; j++) | |
311 if (s[i] == c[j]) | |
312 found = TRUE; | |
313 | |
314 if (!found) | |
315 newlen++; | |
316 } | |
317 | |
318 newstr = g_malloc(newlen + 1); | |
319 | |
320 /* Copy valid chars to new string */ | |
321 for (i = k = 0; s[i] != '\0'; i++) { | |
322 found = FALSE; | |
323 | |
324 for (j = 0; j < strlen(c) && !found; j++) | |
325 if (s[i] == c[j]) | |
326 found = TRUE; | |
327 | |
328 if (!found) | |
329 newstr[k++] = s[i]; | |
330 } | |
331 | |
332 newstr[k] = '\0'; | |
333 | |
334 return newstr; | |
335 } |