2003-03-24 19:01:09 +00:00
|
|
|
|
/*
|
2005-01-20 01:00:52 +00:00
|
|
|
|
$Id: _cdio_stream.c,v 1.2 2005/01/20 01:00:52 rocky Exp $
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
Copyright (C) 2000, 2004, 2005 Herbert Valerio Riedel <hvr@gnu.org>
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
|
# include "config.h"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
#include "cdio_assert.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* #define STREAM_DEBUG */
|
|
|
|
|
|
|
2003-04-22 12:09:08 +00:00
|
|
|
|
#include <cdio/logging.h>
|
|
|
|
|
|
#include <cdio/util.h>
|
2003-03-24 19:01:09 +00:00
|
|
|
|
#include "_cdio_stream.h"
|
|
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
static const char _rcsid[] = "$Id: _cdio_stream.c,v 1.2 2005/01/20 01:00:52 rocky Exp $";
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* DataSource implementations
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
struct _CdioDataSource {
|
|
|
|
|
|
void* user_data;
|
|
|
|
|
|
cdio_stream_io_functions op;
|
|
|
|
|
|
int is_open;
|
|
|
|
|
|
long position;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2003-04-06 23:40:21 +00:00
|
|
|
|
/*
|
|
|
|
|
|
Open if not already open.
|
|
|
|
|
|
Return false if we hit an error. Errno should be set for that error.
|
|
|
|
|
|
*/
|
2003-04-06 17:57:20 +00:00
|
|
|
|
static bool
|
2005-01-20 01:00:52 +00:00
|
|
|
|
_cdio_stream_open_if_necessary(CdioDataSource_t *p_obj)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
2004-10-24 23:42:39 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2004-10-24 23:42:39 +00:00
|
|
|
|
if (!p_obj->is_open) {
|
|
|
|
|
|
if (p_obj->op.open(p_obj->user_data)) {
|
2003-10-03 08:32:32 +00:00
|
|
|
|
cdio_warn ("could not open input stream...");
|
2003-04-06 17:57:20 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
} else {
|
2003-03-24 19:01:09 +00:00
|
|
|
|
cdio_debug ("opened source...");
|
2004-10-24 23:42:39 +00:00
|
|
|
|
p_obj->is_open = 1;
|
|
|
|
|
|
p_obj->position = 0;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2003-04-06 17:57:20 +00:00
|
|
|
|
return true;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-04-06 23:40:21 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Like 3 fseek and in fact may be the same.
|
|
|
|
|
|
|
|
|
|
|
|
This function sets the file position indicator for the stream
|
|
|
|
|
|
pointed to by stream. The new position, measured in bytes, is obtained
|
|
|
|
|
|
by adding offset bytes to the position specified by whence. If whence
|
|
|
|
|
|
is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
|
|
|
|
|
|
the start of the file, the current position indicator, or end-of-file,
|
|
|
|
|
|
respectively. A successful call to the fseek function clears the end-
|
|
|
|
|
|
of-file indicator for the stream and undoes any effects of the
|
|
|
|
|
|
ungetc(3) function on the same stream.
|
|
|
|
|
|
|
|
|
|
|
|
RETURN VALUE
|
|
|
|
|
|
Upon successful completion, return 0,
|
|
|
|
|
|
Otherwise, -1 is returned and the global variable errno is set to indi-
|
|
|
|
|
|
cate the error.
|
|
|
|
|
|
*/
|
2003-03-24 19:01:09 +00:00
|
|
|
|
long
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_seek(CdioDataSource_t* p_obj, long int offset, int whence)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
2004-10-24 23:42:39 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2004-10-24 23:42:39 +00:00
|
|
|
|
if (!_cdio_stream_open_if_necessary(p_obj))
|
2003-04-06 23:40:21 +00:00
|
|
|
|
/* errno is set by _cdio_stream_open_if necessary. */
|
|
|
|
|
|
return -1;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2004-10-24 23:42:39 +00:00
|
|
|
|
if (p_obj->position != offset) {
|
2003-03-24 19:01:09 +00:00
|
|
|
|
#ifdef STREAM_DEBUG
|
|
|
|
|
|
cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset);
|
|
|
|
|
|
#endif
|
2004-10-24 23:42:39 +00:00
|
|
|
|
p_obj->position = offset;
|
|
|
|
|
|
return p_obj->op.seek(p_obj->user_data, offset, whence);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
CdioDataSource_t *
|
2003-03-24 19:01:09 +00:00
|
|
|
|
cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs)
|
|
|
|
|
|
{
|
2005-01-20 01:00:52 +00:00
|
|
|
|
CdioDataSource_t *new_obj;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
new_obj = _cdio_malloc (sizeof (CdioDataSource_t));
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
new_obj->user_data = user_data;
|
|
|
|
|
|
memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions));
|
|
|
|
|
|
|
|
|
|
|
|
return new_obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-04-06 23:40:21 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Like fread(3) and in fact may be the same.
|
|
|
|
|
|
|
|
|
|
|
|
DESCRIPTION:
|
|
|
|
|
|
The function fread reads nmemb elements of data, each size bytes long,
|
|
|
|
|
|
from the stream pointed to by stream, storing them at the location
|
|
|
|
|
|
given by ptr.
|
|
|
|
|
|
|
|
|
|
|
|
RETURN VALUE:
|
|
|
|
|
|
return the number of items successfully read or written (i.e.,
|
|
|
|
|
|
not the number of characters). If an error occurs, or the
|
|
|
|
|
|
end-of-file is reached, the return value is a short item count
|
|
|
|
|
|
(or zero).
|
|
|
|
|
|
|
|
|
|
|
|
We do not distinguish between end-of-file and error, and callers
|
|
|
|
|
|
must use feof(3) and ferror(3) to determine which occurred.
|
|
|
|
|
|
*/
|
2003-03-24 19:01:09 +00:00
|
|
|
|
long
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long size, long nmemb)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
|
|
|
|
|
long read_bytes;
|
|
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
if (!_cdio_stream_open_if_necessary(p_obj)) return 0;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
read_bytes = p_obj->op.read(p_obj->user_data, ptr, size*nmemb);
|
|
|
|
|
|
p_obj->position += read_bytes;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
return read_bytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-04-06 23:40:21 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Return whatever size of stream reports, I guess unit size is bytes.
|
|
|
|
|
|
On error return -1;
|
|
|
|
|
|
*/
|
|
|
|
|
|
long int
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_stat(CdioDataSource_t *p_obj)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
if (!_cdio_stream_open_if_necessary(p_obj)) return -1;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
return p_obj->op.stat(p_obj->user_data);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_close(CdioDataSource_t *p_obj)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
if (p_obj->is_open) {
|
2003-03-24 19:01:09 +00:00
|
|
|
|
cdio_debug ("closed source...");
|
2005-01-20 01:00:52 +00:00
|
|
|
|
p_obj->op.close(p_obj->user_data);
|
|
|
|
|
|
p_obj->is_open = 0;
|
|
|
|
|
|
p_obj->position = 0;
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_destroy(CdioDataSource_t *p_obj)
|
2003-03-24 19:01:09 +00:00
|
|
|
|
{
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_assert (p_obj != NULL);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
cdio_stream_close(p_obj);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
p_obj->op.free(p_obj->user_data);
|
2004-02-07 18:53:02 +00:00
|
|
|
|
|
2005-01-20 01:00:52 +00:00
|
|
|
|
free(p_obj);
|
2003-03-24 19:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Local variables:
|
|
|
|
|
|
* c-file-style: "gnu"
|
|
|
|
|
|
* tab-width: 8
|
|
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
|
|
* End:
|
|
|
|
|
|
*/
|