aboutsummaryrefslogtreecommitdiff
path: root/include/bh/io.h
blob: cdf442a2a4c91e16af82abb9b2f80982e25ee117 (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
#ifndef BHLIB_IO_H
#define BHLIB_IO_H

#include "bh.h"

#define BH_IO_ERROR (1 << 0)
#define BH_IO_EOF   (1 << 1)

#define BH_IO_CAST(x) \
    ((bh_io_t *)(x))

struct bh_io_s;

typedef struct bh_io_table_s
{
    size_t (*read)(struct bh_io_s *io,
                   char *data,
                   size_t size);

    size_t (*write)(struct bh_io_s *io,
                    const char *data,
                    size_t size);

    void (*flush)(struct bh_io_s *io);

    void (*seek)(struct bh_io_s *io,
                 bh_off_t offset,
                 int dir);

    bh_off_t (*tell)(struct bh_io_s *io);

    bh_off_t (*available)(struct bh_io_s *io);

    void (*clear)(struct bh_io_s *io);

    void (*destroy)(struct bh_io_s *io);
} bh_io_table_t;

typedef struct bh_io_s
{
    bh_io_table_t *table;
    int flags;
} bh_io_t;

/**
 * @brief   Create new IO object.
 *
 * @param   table   Valid pointer to the IO table.
 * @param   size    Size of the IO object.
 *
 * @return  If the function succeeds, the return value is a pointer to the
 *          new, semi-initalized, IO object.
 * @return  If the function fails, the return value is NULL.
 *
 * @warning This function should be used in context of implementing child
 *          IO objects (files, sockets, streaming compression, etc).
 *
 * @sa      bh_io_free
 */
bh_io_t *bh_io_new(bh_io_table_t *table,
                   size_t size);

/**
 * @brief   Free IO object.
 *
 * @param   io      Valid pointer to the IO object.
 */
void bh_io_free(bh_io_t *io);

/**
 * @brief   Read data from IO.
 *
 * @param   io      Valid pointer to the IO object.
 * @param   data    Valid pointer to the buffer.
 * @param   size    Size of the buffer.
 *
 * @return  If the function succeeds, the return value is the amount
 *          of bytes read from the IO object.
 * @return  If the function fails, the return value is zero and error
 *          flag is set.
 *
 * @sa      bh_io_write, bh_io_eof, bh_io_error
 */
size_t bh_io_read(bh_io_t *io,
                  char *data,
                  size_t size);

/**
 * @brief   Write data to IO.
 *
 * @param   io      Valid pointer to the IO object.
 * @param   data    Valid pointer to the buffer.
 * @param   size    Size of the buffer.
 *
 * @return  If the function succeeds, the return value is the amount
 *          of bytes written to the IO object.
 * @return  If the function fails, the return value is zero and error
 *          flag is set.
 *
 * @sa      bh_io_read, bh_io_error, bh_io_flush
 */
size_t bh_io_write(bh_io_t *io,
                   const char* data,
                   size_t size);

/**
 * @brief   Writes any uncommited changes (if possible).
 *
 * @param   io      Valid pointer to the IO object.
 */
void bh_io_flush(bh_io_t *io);

/**
 * @brief   Seeks IO object to the specified position (if possible).
 *
 * @param   io      Valid pointer to the IO object.
 * @param   offset  Position
 * @param   dir     Direction
 *
 * @sa      bh_io_tell
 */
void bh_io_seek(bh_io_t *io,
                bh_off_t offset,
                int dir);

/**
 * @brief   Return current position in IO.
 *
 * @param   io      Valid pointer to the IO object.
 *
 * @return  If the function succeeds, the return value is current 
 *          position in the IO.
 * @return  If the function fails, the return value is -1.
 * 
 * @sa      bh_io_seek
 */
bh_off_t bh_io_tell(bh_io_t *io);

/**
 * @brief   Return available bytes in the IO.
 *
 * @param   io      Valid pointer to the IO object.
 *
 * @return  If the function succeeds, the return value is the amount
 *          of the available bytes for reading.
 * @return  If the function fails, the return value is zero.
 */
bh_off_t bh_io_available(bh_io_t *io);

/**
 * @brief   Return error flag of the IO.
 *
 * @param   io      Valid pointer to the IO object.
 * 
 * @return  The return value is error flag.
 *
 * @sa      bh_io_eof, bh_io_clear
 */
int bh_io_error(bh_io_t *io);

/**
 * @brief   Return end-of-file flag of the IO.
 *
 * @param   io      Valid pointer to the IO object.
 *
 * @return  The return value is end-of-file flag.
 *
 * @sa      bh_io_error, bh_io_clear
 */
int bh_io_eof(bh_io_t *io);

/**
 * @brief   Crear IO object state.
 *
 * @param   io      Valid pointer to the IO object.
 *
 * @sa      bh_io_error, bh_io_eof
 */
void bh_io_clear(bh_io_t *io);


#endif /* BHLIB_IO_H */