2 This file is part of GNUnet.
3 (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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_bio_lib.h
23 * @brief buffered IO API
24 * @author Christian Grothoff
27 #ifndef GNUNET_BIO_LIB_H
28 #define GNUNET_BIO_LIB_H
30 #include "gnunet_container_lib.h"
35 #if 0 /* keep Emacsens' auto-indent happy */
41 * Handle for buffered reading.
43 struct GNUNET_BIO_ReadHandle;
47 * Open a file for reading.
49 * @param fn file name to be opened
50 * @return IO handle on success, NULL on error
52 struct GNUNET_BIO_ReadHandle *
53 GNUNET_BIO_read_open (const char *fn);
57 * Close an open file. Reports if any errors reading
58 * from the file were encountered.
60 * @param h file handle
61 * @param emsg set to the error message
62 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
65 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg);
69 * Read the contents of a binary file into a buffer.
71 * @param h handle to an open file
72 * @param what describes what is being read (for error message creation)
73 * @param result the buffer to write the result to
74 * @param len the number of bytes to read
75 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
78 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
79 void *result, size_t len);
83 * Read the contents of a binary file into a buffer.
85 * @param h handle to an open file
86 * @param file name of the source file
87 * @param line line number in the source file
88 * @param result the buffer to write the result to
89 * @param len the number of bytes to read
90 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
93 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line,
94 void *result, size_t len);
97 * Read 0-terminated string from a file.
99 * @param h handle to an open file
100 * @param what describes what is being read (for error message creation)
101 * @param result the buffer to store a pointer to the (allocated) string to
102 * (note that *result could be set to NULL as well)
103 * @param maxLen maximum allowed length for the string
104 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
107 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
108 char **result, size_t maxLen);
112 * Read metadata container from a file.
114 * @param h handle to an open file
115 * @param what describes what is being read (for error message creation)
116 * @param result the buffer to store a pointer to the (allocated) metadata
117 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
120 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
121 struct GNUNET_CONTAINER_MetaData **result);
127 * @param h hande to open file
128 * @param f address of float to read
130 #define GNUNET_BIO_read_float(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(float)))
137 * @param h hande to open file
138 * @param f address of double to read
140 #define GNUNET_BIO_read_double(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(double)))
144 * Read an (u)int32_t.
146 * @param h hande to open file
147 * @param file name of the source file
148 * @param line line number in the code
149 * @param i address of 32-bit integer to read
150 * @return GNUNET_OK on success, GNUNET_SYSERR on error
153 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
154 int line, int32_t * i);
158 * Read an (u)int32_t.
160 * @param h hande to open file
161 * @param i address of 32-bit integer to read
163 #define GNUNET_BIO_read_int32(h, i) GNUNET_BIO_read_int32__ (h, __FILE__, __LINE__, (int32_t*) i)
167 * Read an (u)int64_t.
169 * @param h hande to open file
170 * @param file name of the source file
171 * @param line line number in the code
172 * @param i address of 64-bit integer to read
173 * @return GNUNET_OK on success, GNUNET_SYSERR on error
176 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
177 int line, int64_t * i);
181 * Read an (u)int64_t.
183 * @param h hande to open file
184 * @param i address of 64-bit integer to read
186 #define GNUNET_BIO_read_int64(h, i) GNUNET_BIO_read_int64__ (h, __FILE__, __LINE__, (int64_t*) i)
190 * Handle for buffered writing.
192 struct GNUNET_BIO_WriteHandle;
195 * Open a file for writing.
197 * @param fn file name to be opened
198 * @return IO handle on success, NULL on error
200 struct GNUNET_BIO_WriteHandle *
201 GNUNET_BIO_write_open (const char *fn);
205 * Close an open file for writing.
207 * @param h file handle
208 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
211 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
215 * Write a buffer to a file.
217 * @param h handle to open file
218 * @param buffer the data to write
219 * @param n number of bytes to write
220 * @return GNUNET_OK on success, GNUNET_SYSERR on error
223 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
228 * Write a string to a file.
230 * @param h handle to open file
231 * @param s string to write (can be NULL)
232 * @return GNUNET_OK on success, GNUNET_SYSERR on error
235 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s);
241 * Write metadata container to a file.
243 * @param h handle to open file
244 * @param m metadata to write
245 * @return GNUNET_OK on success, GNUNET_SYSERR on error
248 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
249 const struct GNUNET_CONTAINER_MetaData *m);
256 * @param h hande to open file
257 * @param f float to write (must be a variable)
259 #define GNUNET_BIO_write_float(h, f) GNUNET_BIO_write (h, &f, sizeof(float))
266 * @param h hande to open file
267 * @param f double to write (must be a variable)
269 #define GNUNET_BIO_write_double(h, f) GNUNET_BIO_write (h, &f, sizeof(double))
273 * Write an (u)int32_t.
275 * @param h hande to open file
276 * @param i address of 32-bit integer to write
277 * @return GNUNET_OK on success, GNUNET_SYSERR on error
280 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i);
284 * Write an (u)int64_t.
286 * @param h hande to open file
287 * @param i address of 64-bit integer to write
288 * @return GNUNET_OK on success, GNUNET_SYSERR on error
291 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i);
294 #if 0 /* keep Emacsens' auto-indent happy */
301 /* ifndef GNUNET_BIO_LIB_H */
303 /* end of gnunet_bio_lib.h */