diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-08-24 09:26:47 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-08-24 09:26:47 +0000 |
commit | da7cc55488f90b3294dbb7aa186184e6e8501d7e (patch) | |
tree | 642f70be79ca8f2cbdbb3579d213086f3a25a4bf /src/include/gnunet_bio_lib.h | |
parent | 2518cfc0a86865ebe4d0550e0013ed52a494231b (diff) | |
download | gnunet-da7cc55488f90b3294dbb7aa186184e6e8501d7e.tar.gz gnunet-da7cc55488f90b3294dbb7aa186184e6e8501d7e.zip |
hxing
Diffstat (limited to 'src/include/gnunet_bio_lib.h')
-rw-r--r-- | src/include/gnunet_bio_lib.h | 281 |
1 files changed, 281 insertions, 0 deletions
diff --git a/src/include/gnunet_bio_lib.h b/src/include/gnunet_bio_lib.h new file mode 100644 index 000000000..02b04570e --- /dev/null +++ b/src/include/gnunet_bio_lib.h | |||
@@ -0,0 +1,281 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file include/gnunet_bio_lib.h | ||
23 | * @brief buffered IO API | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #ifndef GNUNET_BIO_LIB_H | ||
28 | #define GNUNET_BIO_LIB_H | ||
29 | |||
30 | #include "gnunet_container_lib.h" | ||
31 | |||
32 | #ifdef __cplusplus | ||
33 | extern "C" | ||
34 | { | ||
35 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
36 | } | ||
37 | #endif | ||
38 | #endif | ||
39 | |||
40 | /** | ||
41 | * Handle for buffered reading. | ||
42 | */ | ||
43 | struct GNUNET_BIO_ReadHandle; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Open a file for reading. | ||
48 | * | ||
49 | * @param fn file name to be opened | ||
50 | * @return IO handle on success, NULL on error | ||
51 | */ | ||
52 | struct GNUNET_BIO_ReadHandle *GNUNET_BIO_read_open (const char *fn); | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Close an open file. Reports if any errors reading | ||
57 | * from the file were encountered. | ||
58 | * | ||
59 | * @param h file handle | ||
60 | * @param emsg set to the error message | ||
61 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
62 | */ | ||
63 | int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, | ||
64 | char **emsg); | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Read the contents of a binary file into a buffer. | ||
69 | * | ||
70 | * @param h handle to an open file | ||
71 | * @param what describes what is being read (for error message creation) | ||
72 | * @param result the buffer to write the result to | ||
73 | * @param len the number of bytes to read | ||
74 | * @return len on success, GNUNET_SYSERR on failure | ||
75 | */ | ||
76 | ssize_t GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, | ||
77 | const char *what, | ||
78 | void *result, | ||
79 | size_t len); | ||
80 | |||
81 | /** | ||
82 | * Read 0-terminated string from a file. | ||
83 | * | ||
84 | * @param h handle to an open file | ||
85 | * @param what describes what is being read (for error message creation) | ||
86 | * @param result the buffer to store a pointer to the (allocated) string to | ||
87 | * (note that *result could be set to NULL as well) | ||
88 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | ||
89 | */ | ||
90 | int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, | ||
91 | const char *what, | ||
92 | char **result); | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Read metadata container from a file. | ||
97 | * | ||
98 | * @param h handle to an open file | ||
99 | * @param what describes what is being read (for error message creation) | ||
100 | * @param result the buffer to store a pointer to the (allocated) metadata | ||
101 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | ||
102 | */ | ||
103 | int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, | ||
104 | const char *what, | ||
105 | struct GNUNET_CONTAINER_MetaData **result); | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Read a float. | ||
110 | * | ||
111 | * @param h hande to open file | ||
112 | * @param f address of float to read | ||
113 | */ | ||
114 | #define GNUNET_BIO_read_float(h, f) (sizeof(float) == GNUNET_BIO_read (h, __FILE__ "##__LINE__##", f, sizeof(float))) | ||
115 | |||
116 | |||
117 | |||
118 | /** | ||
119 | * Read a double. | ||
120 | * | ||
121 | * @param h hande to open file | ||
122 | * @param f address of double to read | ||
123 | */ | ||
124 | #define GNUNET_BIO_read_double(h, f) (sizeof(double) == GNUNET_BIO_read (h, __FILE__ "##__LINE__##", f, sizeof(double))) | ||
125 | |||
126 | |||
127 | /** | ||
128 | * Read an (u)int32_t. | ||
129 | * | ||
130 | * @param h hande to open file | ||
131 | * @param what describes what is being read (for error message creation) | ||
132 | * @param i address of 32-bit integer to read | ||
133 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
134 | */ | ||
135 | int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, | ||
136 | const char *what, | ||
137 | int32_t *i); | ||
138 | |||
139 | |||
140 | /** | ||
141 | * Read an (u)int32_t. | ||
142 | * | ||
143 | * @param h hande to open file | ||
144 | * @param i address of 32-bit integer to read | ||
145 | */ | ||
146 | #define GNUNET_BIO_read_int32(h, i) GNUNET_BIO_read_int32__ (h, __FILE__ "##__LINE__##", (int32_t*) i) | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Read an (u)int64_t. | ||
151 | * | ||
152 | * @param h hande to open file | ||
153 | * @param what describes what is being read (for error message creation) | ||
154 | * @param i address of 64-bit integer to read | ||
155 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
156 | */ | ||
157 | int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, | ||
158 | const char *what, | ||
159 | int64_t *i); | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Read an (u)int64_t. | ||
164 | * | ||
165 | * @param h hande to open file | ||
166 | * @param i address of 64-bit integer to read | ||
167 | */ | ||
168 | #define GNUNET_BIO_read_int64(h, i) (sizeof(int64_t) == GNUNET_BIO_read (h, __FILE__ "##__LINE__##", (int64_t*) i, sizeof(int64_t))) | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Handle for buffered writing. | ||
173 | */ | ||
174 | struct GNUNET_BIO_WriteHandle; | ||
175 | /** | ||
176 | * Open a file for writing. | ||
177 | * | ||
178 | * @param fn file name to be opened | ||
179 | * @return IO handle on success, NULL on error | ||
180 | */ | ||
181 | struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn); | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Close an open file for writing. | ||
186 | * | ||
187 | * @param h file handle | ||
188 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
189 | */ | ||
190 | int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h); | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Write a buffer to a file. | ||
195 | * | ||
196 | * @param h handle to open file | ||
197 | * @param buffer the data to write | ||
198 | * @param n number of bytes to write | ||
199 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
200 | */ | ||
201 | ssize_t GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, | ||
202 | const void *buffer, | ||
203 | size_t n); | ||
204 | |||
205 | |||
206 | /** | ||
207 | * Write a string to a file. | ||
208 | * | ||
209 | * @param h handle to open file | ||
210 | * @param s string to write (can be NULL) | ||
211 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
212 | */ | ||
213 | int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, | ||
214 | const char *s); | ||
215 | |||
216 | |||
217 | |||
218 | |||
219 | /** | ||
220 | * Write metadata container to a file. | ||
221 | * | ||
222 | * @param h handle to open file | ||
223 | * @param m metadata to write | ||
224 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
225 | */ | ||
226 | int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, | ||
227 | const struct GNUNET_CONTAINER_MetaData *m); | ||
228 | |||
229 | |||
230 | |||
231 | /** | ||
232 | * Write a float. | ||
233 | * | ||
234 | * @param h hande to open file | ||
235 | * @param f float to write (must be a variable) | ||
236 | */ | ||
237 | #define GNUNET_BIO_write_float(h, f) (sizeof(float) == GNUNET_BIO_write (h, &f, sizeof(float))) | ||
238 | |||
239 | |||
240 | |||
241 | /** | ||
242 | * Write a double. | ||
243 | * | ||
244 | * @param h hande to open file | ||
245 | * @param f double to write (must be a variable) | ||
246 | */ | ||
247 | #define GNUNET_BIO_write_float(h, f) (sizeof(double) == GNUNET_BIO_write (h, &f, sizeof(double))) | ||
248 | |||
249 | |||
250 | /** | ||
251 | * Write an (u)int32_t. | ||
252 | * | ||
253 | * @param h hande to open file | ||
254 | * @param i address of 32-bit integer to write | ||
255 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
256 | */ | ||
257 | int GNUNET_BIO_write_int32 (struct GNUNET_BIO_ReadHandle *h, | ||
258 | int32_t i); | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Write an (u)int64_t. | ||
263 | * | ||
264 | * @param h hande to open file | ||
265 | * @param i address of 64-bit integer to write | ||
266 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
267 | */ | ||
268 | int GNUNET_BIO_write_int64 (struct GNUNET_BIO_ReadHandle *h, | ||
269 | int64_t i); | ||
270 | |||
271 | |||
272 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
273 | { | ||
274 | #endif | ||
275 | #ifdef __cplusplus | ||
276 | } | ||
277 | #endif | ||
278 | |||
279 | /* ifndef GNUNET_BIO_LIB_H */ | ||
280 | #endif | ||
281 | /* end of gnunet_bio_lib.h */ | ||