1 /*
2   This file is part of Deadbeef Player source code
3   http://deadbeef.sourceforge.net
4 
5   high-level vfs access interface
6 
7   Copyright (C) 2009-2013 Alexey Yakovenko
8 
9   This software is provided 'as-is', without any express or implied
10   warranty.  In no event will the authors be held liable for any damages
11   arising from the use of this software.
12 
13   Permission is granted to anyone to use this software for any purpose,
14   including commercial applications, and to alter it and redistribute it
15   freely, subject to the following restrictions:
16 
17   1. The origin of this software must not be misrepresented; you must not
18      claim that you wrote the original software. If you use this software
19      in a product, an acknowledgment in the product documentation would be
20      appreciated but is not required.
21   2. Altered source versions must be plainly marked as such, and must not be
22      misrepresented as being the original software.
23   3. This notice may not be removed or altered from any source distribution.
24 
25   Alexey Yakovenko waker@users.sourceforge.net
26 */
27 #include <string.h>
28 #include <stdio.h>
29 #include "vfs.h"
30 #include "plugins.h"
31 
32 //#define trace(...) { fprintf(stderr, __VA_ARGS__); }
33 #define trace(fmt,...)
34 
35 DB_FILE *
vfs_fopen(const char * fname)36 vfs_fopen (const char *fname) {
37     trace ("vfs_open %s\n", fname);
38     DB_vfs_t **plugs = plug_get_vfs_list ();
39     DB_vfs_t *fallback = NULL;
40     int i;
41     for (i = 0; plugs[i]; i++) {
42         DB_vfs_t *p = plugs[i];
43         if (!p->get_schemes) {
44             fallback = p;
45             continue;
46         }
47         int n;
48         const char **scheme_names = p->get_schemes ();
49         for (n = 0; scheme_names[n]; n++) {
50             size_t l = strlen (scheme_names[n]);
51             if (!strncasecmp (scheme_names[n], fname, l)) {
52                 return p->open (fname);
53             }
54         }
55     }
56     if (fallback) {
57         return fallback->open (fname);
58     }
59     return NULL;
60 }
61 
vfs_set_track(DB_FILE * stream,DB_playItem_t * it)62 void vfs_set_track (DB_FILE *stream, DB_playItem_t *it) {
63     if (stream->vfs->set_track) {
64         stream->vfs->set_track (stream, it);
65     }
66 }
67 
68 void
vfs_fclose(DB_FILE * stream)69 vfs_fclose (DB_FILE *stream) {
70     return stream->vfs->close (stream);
71 }
72 
73 size_t
vfs_fread(void * ptr,size_t size,size_t nmemb,DB_FILE * stream)74 vfs_fread (void *ptr, size_t size, size_t nmemb, DB_FILE *stream) {
75     return stream->vfs->read (ptr, size, nmemb, stream);
76 }
77 
78 int
vfs_fseek(DB_FILE * stream,int64_t offset,int whence)79 vfs_fseek (DB_FILE *stream, int64_t offset, int whence) {
80     return stream->vfs->seek (stream, offset, whence);
81 }
82 
83 int64_t
vfs_ftell(DB_FILE * stream)84 vfs_ftell (DB_FILE *stream) {
85     return stream->vfs->tell (stream);
86 }
87 
88 void
vfs_rewind(DB_FILE * stream)89 vfs_rewind (DB_FILE *stream) {
90     stream->vfs->rewind (stream);
91 }
92 
93 int64_t
vfs_fgetlength(DB_FILE * stream)94 vfs_fgetlength (DB_FILE *stream) {
95     return stream->vfs->getlength (stream);
96 }
97 
98 const char *
vfs_get_content_type(DB_FILE * stream)99 vfs_get_content_type (DB_FILE *stream) {
100     return stream->vfs->get_content_type (stream);
101 }
102 
103 void
vfs_fabort(DB_FILE * stream)104 vfs_fabort (DB_FILE *stream) {
105     if (stream->vfs->abort) {
106         stream->vfs->abort (stream);
107     }
108 }
109