inputStream_file.c 2.71 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * This project's homepage is: http://www.musicpd.org
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "inputStream_file.h"

21
#include "log.h"
22
#include "os_compat.h"
Warren Dukes's avatar
Warren Dukes committed
23

Avuton Olrich's avatar
Avuton Olrich committed
24 25
void inputStream_initFile(void)
{
26 27
}

Avuton Olrich's avatar
Avuton Olrich committed
28 29 30
int inputStream_fileOpen(InputStream * inStream, char *filename)
{
	FILE *fp;
Warren Dukes's avatar
Warren Dukes committed
31

Avuton Olrich's avatar
Avuton Olrich committed
32 33
	fp = fopen(filename, "r");
	if (!fp) {
Warren Dukes's avatar
Warren Dukes committed
34 35 36 37
		inStream->error = errno;
		return -1;
	}

Avuton Olrich's avatar
Avuton Olrich committed
38
	inStream->seekable = 1;
Warren Dukes's avatar
Warren Dukes committed
39

Avuton Olrich's avatar
Avuton Olrich committed
40
	fseek(fp, 0, SEEK_END);
Warren Dukes's avatar
Warren Dukes committed
41
	inStream->size = ftell(fp);
Avuton Olrich's avatar
Avuton Olrich committed
42
	fseek(fp, 0, SEEK_SET);
Warren Dukes's avatar
Warren Dukes committed
43

44 45 46 47
#ifdef POSIX_FADV_SEQUENTIAL
	posix_fadvise(fileno(fp), (off_t)0, inStream->size, POSIX_FADV_SEQUENTIAL);
#endif

Avuton Olrich's avatar
Avuton Olrich committed
48 49 50 51 52 53
	inStream->data = fp;
	inStream->seekFunc = inputStream_fileSeek;
	inStream->closeFunc = inputStream_fileClose;
	inStream->readFunc = inputStream_fileRead;
	inStream->atEOFFunc = inputStream_fileAtEOF;
	inStream->bufferFunc = inputStream_fileBuffer;
Warren Dukes's avatar
Warren Dukes committed
54

Max Kellermann's avatar
Max Kellermann committed
55 56
	inStream->ready = 1;

Warren Dukes's avatar
Warren Dukes committed
57 58 59
	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
60 61 62 63 64
int inputStream_fileSeek(InputStream * inStream, long offset, int whence)
{
	if (fseek((FILE *) inStream->data, offset, whence) == 0) {
		inStream->offset = ftell((FILE *) inStream->data);
	} else {
Warren Dukes's avatar
Warren Dukes committed
65 66 67 68 69 70 71
		inStream->error = errno;
		return -1;
	}

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
72 73
size_t inputStream_fileRead(InputStream * inStream, void *ptr, size_t size,
			    size_t nmemb)
Warren Dukes's avatar
Warren Dukes committed
74 75 76
{
	size_t readSize;

Avuton Olrich's avatar
Avuton Olrich committed
77 78 79 80 81 82
	readSize = fread(ptr, size, nmemb, (FILE *) inStream->data);
	if (readSize <= 0 && ferror((FILE *) inStream->data)) {
		inStream->error = errno;
		DEBUG("inputStream_fileRead: error reading: %s\n",
		      strerror(inStream->error));
	}
Warren Dukes's avatar
Warren Dukes committed
83

Avuton Olrich's avatar
Avuton Olrich committed
84
	inStream->offset = ftell((FILE *) inStream->data);
Warren Dukes's avatar
Warren Dukes committed
85 86 87 88

	return readSize;
}

Avuton Olrich's avatar
Avuton Olrich committed
89 90 91
int inputStream_fileClose(InputStream * inStream)
{
	if (fclose((FILE *) inStream->data) < 0) {
Warren Dukes's avatar
Warren Dukes committed
92
		inStream->error = errno;
93
		return -1;
Warren Dukes's avatar
Warren Dukes committed
94 95 96 97 98
	}

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
99 100 101 102
int inputStream_fileAtEOF(InputStream * inStream)
{
	if (feof((FILE *) inStream->data))
		return 1;
103

Avuton Olrich's avatar
Avuton Olrich committed
104 105 106
	if (ferror((FILE *) inStream->data) && inStream->error != EINTR) {
		return 1;
	}
107

Avuton Olrich's avatar
Avuton Olrich committed
108
	return 0;
Warren Dukes's avatar
Warren Dukes committed
109
}
110

111
int inputStream_fileBuffer(mpd_unused InputStream * inStream)
Avuton Olrich's avatar
Avuton Olrich committed
112 113
{
	return 0;
114
}