aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
blob: 5e94f35e854b15d70fbb644263e329aa51e36a52 (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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <bh/internal/io.h>
#include <stdlib.h>

/**
 * \defgroup io Input/Output
 *
 * Input/output device abstraction layer.
 * \{
 */

/**
 * Creates the new io object with specified \a table and \a size.
 *
 * \param table Pointer to the io table
 * \param size  Size of the io object
 *
 * \return On success, returns new semi-initialized io object.
 * \return On failure, returns null pointer.
 *
 * \warning This function should be used in context of implementing child
 *          io objects (files, sockets, streaming compression, etc).
 */
bh_io_t *bh_io_new(bh_io_table_t *table,
                   size_t size)
{
    bh_io_t *result;

    result = malloc(size);
    if (result)
        bh_io_init(result, table);

    return result;
}

/**
 * Frees the \a io object.
 *
 * \param io    Pointer to the io object to be freed
 */
void bh_io_free(bh_io_t *io)
{
    bh_io_destroy(io);
    free(io);
}

/**
 * Initializes the \a io object with specified \a table.
 *
 * \param io    Pointer to the io object to be initialized
 * \param table Pointer to the io table
 */
void bh_io_init(bh_io_t *io,
                const bh_io_table_t *table)
{
    io->table = table;
    io->flags = 0;
}

/**
 * Destroys the \a io object.
 *
 * \param io    Pointer to the io object to be destroyed
 */
void bh_io_destroy(bh_io_t *io)
{
    io->table->destroy(io);
}

/**
 * Opens the \a io object for specified \a mode of operation.
 *
 * \param io    Pointer to the io object
 * \param mode  Bitmask determining access mode
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int bh_io_open(bh_io_t *io,
               int mode)
{
    return io->table->open(io, mode);
}

/**
 * Closes the \a io object.
 *
 * \param io    Pointer to the io object
 */
void bh_io_close(bh_io_t *io)
{
    io->table->close(io);
}

/**
 * Checks if the \a io is open.
 *
 * \param io    Pointer to the io object
 *
 * \return If io object is open - returns non-zero.
 * \return If io object is closed - returns zero.
 */
int bh_io_is_open(bh_io_t *io)
{
    return io->table->is_open(io);
}

/**
 * Reads up to \a size amount of bytes from the \a io object into memory buffer
 * pointed by \a data pointer.
 *
 * \param io    Pointer to the io object
 * \param data  Pointer to the memory buffer
 * \param size  Maximum number of bytes to be read
 *
 * \return On success, returns number of bytes successfuly read.
 * \return On failure, returns zero.
 *
 * \note To check for end-of-file or error see bh_io_eof and bh_io_error.
 */
size_t bh_io_read(bh_io_t *io,
                  char *data,
                  size_t size)
{
    return io->table->read(io, data, size);
}

/**
 * Writes up to \a size amount of bytes to the \a io object from memory buffer
 * pointed by \a data pointer.
 *
 * \param io    Pointer to the io object
 * \param data  Pointer to the memory buffer
 * \param size  Maximum number of bytes to be read
 *
 * \return On success, returns number of bytes successfuly written.
 * \return On failure, returns zero.
 *
 * \note To check for error see bh_io_error.
 */
size_t bh_io_write(bh_io_t *io,
                   const char* data,
                   size_t size)
{
    return io->table->write(io, data, size);
}

/**
 * Synchronizes the \a io object (if possible).
 *
 * In most cases, this function causes any unwritten/buffered data to be
 * written.
 *
 * \param io    Pointer to the io object
 */
void bh_io_flush(bh_io_t *io)
{
    io->table->flush(io);
}

/**
 * Seeks the \a io object in the specified direction \a dir and \a offset (if
 * possible).
 *
 * \param io        Pointer to the io object
 * \param offset    Number of bytes to seek in specified direciton
 * \param dir       Seeking direction
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int bh_io_seek(bh_io_t *io,
               bh_off_t offset,
               int dir)
{
    return io->table->seek(io, offset, dir);
}

/**
 * Returns total size of the \a io object (if possible)
 *
 * \param io    Pointer to the io object
 *
 * \return On success, returns total size of the io object.
 * \return On failure, returns -1.
 */
bh_off_t bh_io_size(bh_io_t *io)
{
    return io->table->size(io);
}

/**
 * Returns current position in the \a io object (if possible).
 *
 * \param io    Pointer to the io object
 *
 * \return On success, returns current position in the io object.
 * \return On failure, returns -1.
 */
bh_off_t bh_io_tell(bh_io_t *io)
{
    return io->table->tell(io);
}

/**
 * Returns available number of bytes to be read from the \a io object.
 *
 * \param io    Pointer to the io object
 *
 * \return On success, returns number of available bytes to be read.
 * \return On failure, returns zero.
 */
bh_off_t bh_io_available(bh_io_t *io)
{
    return io->table->available(io);
}

/**
 * Checks error flag of the \a io object.
 *
 * \param io    Pointer to the io object
 *
 * \return If error flag is set, returns non-zero.
 * \return If error flag is not set, returns zero.
 */
int bh_io_error(bh_io_t *io)
{
    return (io->flags & BH_IO_ERROR) != 0;
}

/**
 * Checks end-of-file flag of the \a io object.
 *
 * \param io    Pointer to the io object
 *
 * \return If end-of-file flag is set, returns non-zero.
 * \return If end-of-file flag is not set, returns zero.
 */
int bh_io_eof(bh_io_t *io)
{
    return (io->flags & BH_IO_EOF) != 0;
}

/**
 * Clears error of the \a io object.
 *
 * \param io    Pointer to the io object
 */
void bh_io_clear(bh_io_t *io)
{
    io->table->clear(io);
}

/**
 * \}
 */