Update to 6762
[qball-mpd.git] / src / inputStream_file.c
blob389aaad01fd45e59ea47f851f6b74796999d381d
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"
21 #include "log.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #define _XOPEN_SOURCE 600
29 #include <fcntl.h>
31 void inputStream_initFile(void)
35 int inputStream_fileOpen(InputStream * inStream, char *filename)
37 FILE *fp;
39 fp = fopen(filename, "r");
40 if (!fp) {
41 inStream->error = errno;
42 return -1;
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);
53 #endif
55 inStream->data = fp;
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;
62 return 0;
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);
69 } else {
70 inStream->error = errno;
71 return -1;
74 return 0;
77 size_t inputStream_fileRead(InputStream * inStream, void *ptr, size_t size,
78 size_t nmemb)
80 size_t readSize;
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);
91 return readSize;
94 int inputStream_fileClose(InputStream * inStream)
96 if (fclose((FILE *) inStream->data) < 0) {
97 inStream->error = errno;
98 return -1;
101 return 0;
104 int inputStream_fileAtEOF(InputStream * inStream)
106 if (feof((FILE *) inStream->data))
107 return 1;
109 if (ferror((FILE *) inStream->data) && inStream->error != EINTR) {
110 return 1;
113 return 0;
116 int inputStream_fileBuffer(InputStream * inStream)
118 return 0;