aboutsummaryrefslogtreecommitdiff
path: root/src/IO.c
blob: c32752d337fbca6a0bde2ee8ff20acb505f46523 (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
#include <BH/IO.h>
#include <malloc.h>


#define BUFFER_SIZE (sizeof(char *))

struct BH_IO
{
    BH_IOCallback cb;
};


BH_IO *BH_IONew(BH_IOCallback cb,
                void *data)
{
    size_t requested;
    BH_IO *io;

    /* Get information about IO device size */
    if (cb(NULL, BH_IO_INFO_CB, &requested, NULL))
        return NULL;

    /* Allocate space for the IO device */
    io = malloc(sizeof(*io) + requested);
    if (!io)
        return NULL;

    /* Initialize IO device */
    io->cb = cb;
    if (cb(io + 1, BH_IO_INIT_CB, data, NULL))
    {
        free(io);
        return NULL;
    }

    return io;
}


void BH_IOFree(BH_IO *io)
{
    /* Prevent working with NULL io */
    if (!io)
        return;

    /* Call the IO device destruction handler */
    io->cb(io + 1, BH_IO_DESTROY_CB, NULL, NULL);

    /* Deallocate object */
    free(io);
}


const char *BH_IOClassname(BH_IO *io)
{
    const char *name;

    if (!io)
        goto error;

    if (io->cb(io + 1, BH_IO_INFO_CB, NULL, &name) != BH_OK)
        goto error;

    return name;

error:
    return NULL;
}


int BH_IOOpen(BH_IO *io,
              int mode)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device open handler with specified mode */
    return io->cb(io + 1, BH_IO_OPEN_CB, &mode, NULL);
}


int BH_IOClose(BH_IO *io)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device close handler */
    return io->cb(io + 1, BH_IO_CLOSE_CB, NULL, NULL);
}


int BH_IORead(BH_IO *io,
              char *buffer,
              size_t size,
              size_t *actual)
{
    int code;

    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device read handler */
    code = io->cb(io + 1, BH_IO_READ_CB, buffer, &size);

    /* If caller wants to know actual read size - report it back */
    if (actual)
        *actual = size;

    return code;
}


int BH_IOWrite(BH_IO *io,
               const char *buffer,
               size_t size,
               size_t *actual)
{
    int code;

    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device write handler */
    code = io->cb(io + 1, BH_IO_WRITE_CB, (void *)buffer, &size);

    /* If caller wants to know actual written size - report it back */
    if (actual)
        *actual = size;

    return code;
}

int BH_IOPeek(BH_IO *io,
              char *buffer,
              size_t size,
              size_t *actual)
{
    int code;

    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device peek handler */
    code = io->cb(io + 1, BH_IO_PEEK_CB, (void *)buffer, &size);

    /* If caller wants to know actual written size - report it back */
    if (actual)
        *actual = size;

    return code;
}


int BH_IOTell(BH_IO *io,
              int64_t *position)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device tell handler */
    return io->cb(io + 1, BH_IO_TELL_CB, position, NULL);
}


int BH_IOSeek(BH_IO *io,
              int64_t position,
              int direction)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device seek handler */
    return io->cb(io + 1, BH_IO_SEEK_CB, &position, &direction);
}


int BH_IOFlush(BH_IO *io)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device flush handler */
    return io->cb(io + 1, BH_IO_FLUSH_CB, NULL, NULL);
}


int BH_IOSize(BH_IO *io,
              int64_t *size)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_ERROR;

    /* Call the IO device size handler */
    return io->cb(io + 1, BH_IO_SIZE_CB, size, NULL);
}


int BH_IOFlags(BH_IO *io)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_IO_FLAG_ERROR;

    /* Call the IO device flags handler */
    return io->cb(io + 1, BH_IO_FLAGS_CB, NULL, NULL);
}


int BH_IOClear(BH_IO *io)
{
    /* Prevent working with NULL io */
    if (!io)
        return BH_OK;

    /* Call the IO device clear error handler */
    return io->cb(io + 1, BH_IO_CLEAR_CB, NULL, NULL);
}



BH_IO *BH_BufferNew(BH_IO *io)
{
    (void)io;
    return NULL;
}