inputStream_file.c 2.67 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 55 56 57

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
58 59 60 61 62
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
63 64 65 66 67 68 69
		inStream->error = errno;
		return -1;
	}

	return 0;
}

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

Avuton Olrich's avatar
Avuton Olrich committed
75 76 77 78 79 80
	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
81

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

	return readSize;
}

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

	return 0;
}

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

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

Avuton Olrich's avatar
Avuton Olrich committed
106
	return 0;
Warren Dukes's avatar
Warren Dukes committed
107
}
108

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