File: stream.c

package info (click to toggle)
libmirage 3.2.10-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,540 kB
  • sloc: ansic: 25,454; xml: 108; makefile: 11
file content (205 lines) | stat: -rw-r--r-- 6,991 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  libMirage: stream interface
 *  Copyright (C) 2014 Rok Mandeljc
 *
 *  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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * SECTION: mirage-stream
 * @title: MirageStream
 * @short_description: Interface for I/O streams.
 * @see_also: #MirageFileStream, #MirageFilterStream
 * @include: mirage-stream.h
 *
 * #MirageStream is a basic unit of file access abstraction used in
 * libMirage. It supports basic I/O operations, such as read, write,
 * seek and tell.
 *
 * Streams in libMirage are designed around the idea of filter stream
 * chains, where several filter streams (#MirageFilterStream) can be
 * chained on top of a stream that abstracts direct access to the file
 * (#MirageFileStream).
 */

#include "mirage/config.h"
#include "mirage/mirage.h"
#include "mirage/compat-input-stream.h"

#include <glib/gi18n-lib.h>


/**********************************************************************\
 *                        Stream information                          *
\**********************************************************************/
/**
 * mirage_stream_get_filename:
 * @self: a #MirageFileStream
 *
 * Retrieves the name to file on which the stream is opened. If @self is
 * a filter stream in the filter stream chain, the filename is obtained from
 * the stream at the bottom of the chain.
 *
 * Returns: (transfer none): pointer to a buffer containing the filename.
 * The buffer belongs to the stream object and should not be modified.
 */
const gchar *mirage_stream_get_filename (MirageStream *self)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->get_filename(self);
}

/**
 * mirage_stream_is_writable:
 * @self: a #MirageFileStream
 *
 * Queries the stream (chain) for write support. For the stream to be
 * writable, the stream object implementation itself must support write
 * operations, and any stream objects below it in the stream chain must
 * also be writable.
 *
 * Returns: %TRUE if the stream (chain) is writable, %FALSE if it is not.
 */
gboolean mirage_stream_is_writable (MirageStream *self)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->is_writable(self);
}


/**********************************************************************\
 *                           I/O functions                            *
\**********************************************************************/
/**
 * mirage_stream_read:
 * @self: a #MirageFileStream
 * @buffer: (in): a buffer to read data into
 * @count: (in): number of bytes to read from stream
 * @error: (out) (allow-none): location to store error, or %NULL
 *
 * Attempts to read @count bytes from stream into the buffer starting at
 * @buffer. Will block during the operation.
 *
 * Returns: number of bytes read, or -1 on error, or 0 on end of file.
 */
gssize mirage_stream_read (MirageStream *self, void *buffer, gsize count, GError **error)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->read(self, buffer, count, error);
}

/**
 * mirage_stream_write:
 * @self: a #MirageFileStream
 * @buffer: (in): a buffer to write data from
 * @count: (in): number of bytes to write to stream
 * @error: (out) (allow-none): location to store error, or %NULL
 *
 * Attempts to write @count bytes to stream from the buffer starting at
 * @buffer. Will block during the operation.
 *
 * Returns: number of bytes written, or -1 on error.
 */
gssize mirage_stream_write (MirageStream *self, const void *buffer, gsize count, GError **error)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->write(self, buffer, count, error);
}

/**
 * mirage_stream_seek:
 * @self: a #MirageFileStream
 * @offset: (in): offset to seek
 * @type: (in): seek type
 * @error: (out) (allow-none): location to store error, or %NULL
 *
 * Seeks in the stream by the given @offset, modified by @type.
 *
 * Returns: %TRUE on success, %FALSE on failure.
 */
gboolean mirage_stream_seek (MirageStream *self, goffset offset, GSeekType type, GError **error)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->seek(self, offset, type, error);
}

/**
 * mirage_stream_tell:
 * @self: a #MirageFileStream
 *
 * Retrieves the current position within the stream.
 *
 * Returns: the offset from the beginning of the stream.
 */
goffset mirage_stream_tell (MirageStream *self)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->tell(self);
}


/**
 * mirage_stream_move_file:
 * @self: a #MirageFileStream
 * @new_filename: (in): the new filename
 * @error: (out) (allow-none): location to store error, or %NULL
 *
 * Attempts to move the file on top of which the stream (chain) is opened
 * to @new_filename. If supported, native move operations are used,
 * otherwise a copy + delete fallback is used.
 *
 * Returns: %TRUE on success, %FALSE on failure.
 */
gboolean mirage_stream_move_file (MirageStream *self, const gchar *new_filename, GError **error)
{
    return MIRAGE_STREAM_GET_INTERFACE(self)->move_file(self, new_filename, error);
}


/**
 * mirage_stream_get_g_input_stream:
 * @self: a #MirageFileStream
 *
 * Constructs and returns a compatibility object inheriting a #GInputStream.
 * This is to allow regular GIO stream objects (for example, a
 * #GDataInputStream) to be chained on top of our filter stream chain.
 *
 * Returns: (transfer full): a #GInputStream. The reference should be
 * released using g_object_unref() when no longer needed.
 */
GInputStream *mirage_stream_get_g_input_stream (MirageStream *self)
{
    return g_object_new(MIRAGE_TYPE_COMPAT_INPUT_STREAM, "stream", self, NULL);
}


/**********************************************************************\
 *                           Interface init                           *
\**********************************************************************/
GType mirage_stream_get_type (void) {
    static GType iface_type = 0;
    if (iface_type == 0) {
        static const GTypeInfo info = {
            sizeof(MirageStreamInterface),
            NULL,   /* base_init */
            NULL,   /* base_finalize */
            NULL,   /* class_init */
            NULL,   /* class_finalize */
            NULL,   /* class_data */
            0,
            0,      /* n_preallocs */
            NULL,   /* instance_init */
            NULL    /* value_table */
        };

        iface_type = g_type_register_static(G_TYPE_INTERFACE, "MirageStream", &info, 0);
    }

    return iface_type;
}