1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* file.c
* Copyright (C) 2002 Evgeny Chukreev <codedj@echo.ru>
*
* This file is part of AlsaPlayer.
*
* AlsaPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* AlsaPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "reader.h"
#include "string.h"
#include "utilities.h"
#include "alsaplayer_error.h"
#include "ap_string.h"
#include "ap_unused.h"
static void decode_uri(const char *src, char *dst, int len)
{
int j;
if (!is_uri(src)) {
ap_strlcpy(dst, src, len);
return;
}
for (j=0; j<len && *src; j++, src++) {
if (*src == '%') {
int c;
char *pos;
char buf [3] = {src[1], src[2], '\0'};
if (src[1] == '%') {
dst [j] = '%';
src++;
continue;
}
c = strtoul (buf, &pos, 16);
if (*pos == '\0') {
dst [j] = c;
src+=2;
continue;
}
}
dst [j] = *src;
}
dst [j] = '\0';
}
/* open stream, may return NULL */
static void *file_open(const char *uri, reader_status_type UNUSED (status), void * UNUSED (data))
{
char decoded_uri[1024];
int offset = 0;
decode_uri (uri, decoded_uri, 1020);
if (strncmp(decoded_uri, "file:", 5) == 0) {
offset = 5;
}
return fopen (&decoded_uri[offset], "r");
}
/* close stream */
static void file_close(void *d)
{
fclose ((FILE*)d);
}
/* test function */
static float file_can_handle(const char *uri)
{
struct stat buf;
char decoded_uri[1024];
int offset = 0;
decode_uri (uri, decoded_uri, 1020);
/* Check for prefix */
if (strncmp (decoded_uri, "file:", 5) == 0)
offset = 5;
if (stat(&decoded_uri[offset], &buf)) return 0.0;
/* Is it a type we might have a chance of reading?
* (Some plugins may cope with playing special devices, eg, a /dev/scd) */
if (!(S_ISREG(buf.st_mode) ||
S_ISCHR(buf.st_mode) ||
S_ISBLK(buf.st_mode) ||
S_ISFIFO(buf.st_mode) ||
S_ISSOCK(buf.st_mode))) return 0.0;
return 1.0;
}
/* init plugin */
static int file_init(void)
{
return 1;
}
/* shutdown plugin */
static void file_shutdown(void)
{
return;
}
static size_t file_metadata (void * UNUSED (ptr), size_t UNUSED (size), void * UNUSED (d))
{
/* Not implemented */
return 0;
}
/* read from stream */
static size_t file_read (void *ptr, size_t size, void *d)
{
return fread (ptr, 1, size, (FILE*)d) ;
}
/* seek in stream */
static int file_seek (void *d, long offset, int whence)
{
return fseek ((FILE*)d, offset, whence);
}
/*
* Return current position in stream.
*/
static long file_tell (void *d)
{
return ftell ((FILE*)d);
}
/* directory test */
static float file_can_expand (const char *uri)
{
const char *path;
struct stat buf;
char decoded_uri[1024];
decode_uri (uri, decoded_uri, 1020);
/* Check for prefix */
if (strncmp (decoded_uri, "file:", 5)) return 0.0;
/* Real path */
path = &decoded_uri[5];
if (!*path) return 0.0;
// Stat file, and don't follow symlinks
if (lstat(path, &buf)) return 0.0;
if (!S_ISDIR(buf.st_mode)) return 0.0;
return 1.0;
}
/* expand directory */
static char **file_expand (const char *uri)
{
struct dirent *entry;
DIR *dir;
char **expanded = NULL;
int count = 0;
char decoded_uri[1024];
char tmp [512];
decode_uri (uri, decoded_uri, 1020);
dir = opendir (&decoded_uri[5]);
/* Allocate memory for empty list */
expanded = malloc (sizeof(char*));
*expanded = NULL;
/* return empty list on error */
if (!dir) return expanded;
/* iterate over a dir */
while ((entry = readdir(dir)) != NULL) {
/* don't include . and .. entries */
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
/* compose */
snprintf (tmp, sizeof (tmp), "%s/%s", decoded_uri + 5, entry->d_name);
expanded [count++] = strdup (tmp);
/* grow up our list */
expanded = realloc (expanded, (count+1)*sizeof(char*));
}
/* set the end mark */
expanded [count] = NULL;
closedir(dir);
return expanded;
}
/* feof wrapper */
static int file_eof (void *d)
{
return feof ((FILE*)d);
}
/* stream is seekable */
static int file_seekable (void * UNUSED (d))
{
return 1;
}
static long file_length (void *d)
{
long len, old=ftell ((FILE*)d);
len = fseek ((FILE*)d, 0, SEEK_END);
fseek ((FILE*)d, old, SEEK_SET);
return len;
}
/* info about this plugin */
reader_plugin file_plugin = {
READER_PLUGIN_VERSION,
"File reader v1.1",
"Evgeny Chukreev",
NULL,
file_init,
file_shutdown,
file_can_handle,
file_open,
file_close,
file_read,
file_metadata,
file_seek,
file_tell,
file_can_expand,
file_expand,
file_length,
file_eof,
file_seekable
};
/* return info about this plugin */
reader_plugin *reader_plugin_info(void)
{
return &file_plugin;
}
|