aboutsummaryrefslogtreecommitdiff
path: root/lib/stream/open.c
blob: db09f2a61b4547a86d3b593f96e93750a6ef2029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ****************************************************************************
 * stream/open.c -- open a stream.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "stream.h"

/**
 *	casio_open_stream:
 *	Open a libcasio stream.
 *
 *	@arg	pstream		the stream to open.
 *	@arg	mode		the open mode.
 *	@arg	cookie		the stream cookie.
 *	@arg	callbacks	the stream callbacks.
 *	@arg	ioff		the initial offset.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_open_stream(casio_stream_t **pstream,
	casio_openmode_t mode, void *cookie,
	const casio_streamfuncs_t *callbacks,
	casio_off_t ioff)
{
	int err; casio_stream_t *stream = NULL;
	casio_streamfuncs_t *c;

	/* Allocate the stream. */

	*pstream = casio_alloc(1, sizeof(casio_stream_t));
	if (!(stream = *pstream)) {
		err = casio_error_alloc;
		goto fail;
	}

	/* Initialize the stream callbacks. */

	stream->casio_stream_mode = 0;
	c = &stream->casio_stream_callbacks;
	memset(c, 0, sizeof(casio_streamfuncs_t));
	c->casio_streamfuncs_close = callbacks->casio_streamfuncs_close;
	c->casio_streamfuncs_settm = callbacks->casio_streamfuncs_settm;
	if ((mode & CASIO_OPENMODE_READ) && callbacks->casio_streamfuncs_read) {
		stream->casio_stream_mode |= CASIO_OPENMODE_READ;
		c->casio_streamfuncs_read  = callbacks->casio_streamfuncs_read;
	}
	if ((mode & CASIO_OPENMODE_WRITE) && callbacks->casio_streamfuncs_write) {
		stream->casio_stream_mode |= CASIO_OPENMODE_WRITE;
		c->casio_streamfuncs_write = callbacks->casio_streamfuncs_write;
	}
	if (mode & (CASIO_OPENMODE_READ | CASIO_OPENMODE_WRITE))
		c->casio_streamfuncs_seek  = callbacks->casio_streamfuncs_seek;

	if ((mode & CASIO_OPENMODE_SERIAL)
	 && callbacks->casio_streamfuncs_setattrs) {
		stream->casio_stream_mode |= CASIO_OPENMODE_SERIAL;
		c->casio_streamfuncs_setattrs = callbacks->casio_streamfuncs_setattrs;
	}
	if ((mode & CASIO_OPENMODE_SCSI) && callbacks->casio_streamfuncs_scsi) {
		stream->casio_stream_mode |= CASIO_OPENMODE_SCSI;
		c->casio_streamfuncs_scsi = callbacks->casio_streamfuncs_scsi;
	}

	/* Initialize the stream properties. */

	stream->casio_stream_cookie = cookie;
	stream->casio_stream_offset = ioff;
	stream->casio_stream_lasterr = 0;
	casio_init_attrs(stream);
	casio_init_timeouts(stream);

	err = 0;
fail:
	if (err) {
		casio_free(stream);
		(*callbacks->casio_streamfuncs_close)(cookie);
	}
	return (err);
}

/**
 *	casio_close:
 *	Close a libcasio stream.
 *
 *	@arg	stream		the stream to close.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_close(casio_stream_t *stream)
{
	casio_stream_close_t *c;

	if (!stream) return (0);
	c = getcb(stream, close);
	if (c) (*c)(stream->casio_stream_cookie);
	casio_free(stream);
	return (0);
}