2 This file is part of GNUnet.
3 Copyright (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
27 * @defgroup bio BIO library
28 * Buffered binary disk IO (with endianess conversion)
32 #ifndef GNUNET_BIO_LIB_H
33 #define GNUNET_BIO_LIB_H
35 #include "gnunet_container_lib.h"
40 #if 0 /* keep Emacsens' auto-indent happy */
47 * Handle for buffered reading.
49 struct GNUNET_BIO_ReadHandle;
53 * Open a file for reading.
55 * @param fn file name to be opened
56 * @return IO handle on success, NULL on error
58 struct GNUNET_BIO_ReadHandle *
59 GNUNET_BIO_read_open (const char *fn);
63 * Close an open file. Reports if any errors reading
64 * from the file were encountered.
66 * @param h file handle
67 * @param emsg set to the error message
68 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
71 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg);
75 * Read the contents of a binary file into a buffer.
77 * @param h handle to an open file
78 * @param what describes what is being read (for error message creation)
79 * @param result the buffer to write the result to
80 * @param len the number of bytes to read
81 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
84 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
85 void *result, size_t len);
89 * Read the contents of a binary file into a buffer.
91 * @param h handle to an open file
92 * @param file name of the source file
93 * @param line line number in the source file
94 * @param result the buffer to write the result to
95 * @param len the number of bytes to read
96 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
99 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
100 const char *file, int line,
101 void *result, size_t len);
104 * Read 0-terminated string from a file.
106 * @param h handle to an open file
107 * @param what describes what is being read (for error message creation)
108 * @param result the buffer to store a pointer to the (allocated) string to
109 * (note that *result could be set to NULL as well)
110 * @param max_length maximum allowed length for the string
111 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
114 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
115 char **result, size_t max_length);
119 * Read metadata container from a file.
121 * @param h handle to an open file
122 * @param what describes what is being read (for error message creation)
123 * @param result the buffer to store a pointer to the (allocated) metadata
124 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
127 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
128 struct GNUNET_CONTAINER_MetaData **result);
134 * @param h hande to open file
135 * @param f address of float to read
137 #define GNUNET_BIO_read_float(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(float)))
144 * @param h hande to open file
145 * @param f address of double to read
147 #define GNUNET_BIO_read_double(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(double)))
151 * Read an (u)int32_t.
153 * @param h hande to open file
154 * @param file name of the source file
155 * @param line line number in the code
156 * @param i address of 32-bit integer to read
157 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
160 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
161 int line, int32_t * i);
165 * Read an (u)int32_t.
167 * @param h hande to open file
168 * @param i address of 32-bit integer to read
170 #define GNUNET_BIO_read_int32(h, i) GNUNET_BIO_read_int32__ (h, __FILE__, __LINE__, (int32_t*) i)
174 * Read an (u)int64_t.
176 * @param h hande to open file
177 * @param file name of the source file
178 * @param line line number in the code
179 * @param i address of 64-bit integer to read
180 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
183 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
184 int line, int64_t * i);
188 * Read an (u)int64_t.
190 * @param h hande to open file
191 * @param i address of 64-bit integer to read
193 #define GNUNET_BIO_read_int64(h, i) GNUNET_BIO_read_int64__ (h, __FILE__, __LINE__, (int64_t*) i)
197 * Handle for buffered writing.
199 struct GNUNET_BIO_WriteHandle;
202 * Open a file for writing.
204 * @param fn file name to be opened
205 * @return IO handle on success, NULL on error
207 struct GNUNET_BIO_WriteHandle *
208 GNUNET_BIO_write_open (const char *fn);
212 * Close an open file for writing.
214 * @param h file handle
215 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
218 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
222 * Write a buffer to a file.
224 * @param h handle to open file
225 * @param buffer the data to write
226 * @param n number of bytes to write
227 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
230 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
235 * Force a buffered writer to flush its buffer
237 * @param h the writer handle
238 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
242 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h);
246 * Write a string to a file.
248 * @param h handle to open file
249 * @param s string to write (can be NULL)
250 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
253 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s);
257 * Write metadata container to a file.
259 * @param h handle to open file
260 * @param m metadata to write
261 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
264 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
265 const struct GNUNET_CONTAINER_MetaData *m);
272 * @param h hande to open file
273 * @param f float to write (must be a variable)
275 #define GNUNET_BIO_write_float(h, f) GNUNET_BIO_write (h, &f, sizeof(float))
282 * @param h hande to open file
283 * @param f double to write (must be a variable)
285 #define GNUNET_BIO_write_double(h, f) GNUNET_BIO_write (h, &f, sizeof(double))
289 * Write an (u)int32_t.
291 * @param h hande to open file
292 * @param i 32-bit integer to write
293 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
296 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i);
300 * Write an (u)int64_t.
302 * @param h hande to open file
303 * @param i 64-bit integer to write
304 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
307 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i);
310 #if 0 /* keep Emacsens' auto-indent happy */
317 /** @} */ /* end of group bio */
319 /* ifndef GNUNET_BIO_LIB_H */
321 /* end of gnunet_bio_lib.h */