1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "inputStream_file.h"
25 #include <sys/types.h>
28 #define _XOPEN_SOURCE 600
31 void inputStream_initFile(void)
35 int inputStream_fileOpen(InputStream * inStream, char *filename)
39 fp = fopen(filename, "r");
41 inStream->error = errno;
45 inStream->seekable = 1;
47 fseek(fp, 0, SEEK_END);
48 inStream->size = ftell(fp);
49 fseek(fp, 0, SEEK_SET);
51 #ifdef POSIX_FADV_SEQUENTIAL
52 posix_fadvise(fileno(fp), (off_t)0, inStream->size, POSIX_FADV_SEQUENTIAL);
56 inStream->seekFunc = inputStream_fileSeek;
57 inStream->closeFunc = inputStream_fileClose;
58 inStream->readFunc = inputStream_fileRead;
59 inStream->atEOFFunc = inputStream_fileAtEOF;
60 inStream->bufferFunc = inputStream_fileBuffer;
65 int inputStream_fileSeek(InputStream * inStream, long offset, int whence)
67 if (fseek((FILE *) inStream->data, offset, whence) == 0) {
68 inStream->offset = ftell((FILE *) inStream->data);
70 inStream->error = errno;
77 size_t inputStream_fileRead(InputStream * inStream, void *ptr, size_t size,
82 readSize = fread(ptr, size, nmemb, (FILE *) inStream->data);
83 if (readSize <= 0 && ferror((FILE *) inStream->data)) {
84 inStream->error = errno;
85 DEBUG("inputStream_fileRead: error reading: %s\n",
86 strerror(inStream->error));
89 inStream->offset = ftell((FILE *) inStream->data);
94 int inputStream_fileClose(InputStream * inStream)
96 if (fclose((FILE *) inStream->data) < 0) {
97 inStream->error = errno;
104 int inputStream_fileAtEOF(InputStream * inStream)
106 if (feof((FILE *) inStream->data))
109 if (ferror((FILE *) inStream->data) && inStream->error != EINTR) {
116 int inputStream_fileBuffer(InputStream * inStream)