gnunet_bio_lib.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * Buffered IO library
  22. *
  23. * @defgroup bio BIO library
  24. * Buffered binary disk IO (with endianess conversion)
  25. * @{
  26. */
  27. #ifndef GNUNET_BIO_LIB_H
  28. #define GNUNET_BIO_LIB_H
  29. #include "gnunet_container_lib.h"
  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #if 0 /* keep Emacsens' auto-indent happy */
  34. }
  35. #endif
  36. #endif
  37. /**
  38. * Handle for buffered reading.
  39. */
  40. struct GNUNET_BIO_ReadHandle;
  41. /**
  42. * Open a file for reading.
  43. *
  44. * @param fn file name to be opened
  45. * @return IO handle on success, NULL on error
  46. */
  47. struct GNUNET_BIO_ReadHandle *
  48. GNUNET_BIO_read_open (const char *fn);
  49. /**
  50. * Close an open file. Reports if any errors reading
  51. * from the file were encountered.
  52. *
  53. * @param h file handle
  54. * @param emsg set to the error message
  55. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  56. */
  57. int
  58. GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg);
  59. /**
  60. * Read the contents of a binary file into a buffer.
  61. *
  62. * @param h handle to an open file
  63. * @param what describes what is being read (for error message creation)
  64. * @param result the buffer to write the result to
  65. * @param len the number of bytes to read
  66. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  67. */
  68. int
  69. GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
  70. void *result, size_t len);
  71. /**
  72. * Read the contents of a binary file into a buffer.
  73. *
  74. * @param h handle to an open file
  75. * @param file name of the source file
  76. * @param line line number in the source file
  77. * @param result the buffer to write the result to
  78. * @param len the number of bytes to read
  79. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  80. */
  81. int
  82. GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
  83. const char *file, int line,
  84. void *result, size_t len);
  85. /**
  86. * Read 0-terminated string from a file.
  87. *
  88. * @param h handle to an open file
  89. * @param what describes what is being read (for error message creation)
  90. * @param result the buffer to store a pointer to the (allocated) string to
  91. * (note that *result could be set to NULL as well)
  92. * @param max_length maximum allowed length for the string
  93. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  94. */
  95. int
  96. GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what,
  97. char **result, size_t max_length);
  98. /**
  99. * Read metadata container from a file.
  100. *
  101. * @param h handle to an open file
  102. * @param what describes what is being read (for error message creation)
  103. * @param result the buffer to store a pointer to the (allocated) metadata
  104. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  105. */
  106. int
  107. GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
  108. struct GNUNET_CONTAINER_MetaData **result);
  109. /**
  110. * Read a float.
  111. *
  112. * @param h hande to open file
  113. * @param f address of float to read
  114. */
  115. #define GNUNET_BIO_read_float(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(float)))
  116. /**
  117. * Read a double.
  118. *
  119. * @param h hande to open file
  120. * @param f address of double to read
  121. */
  122. #define GNUNET_BIO_read_double(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, f, sizeof(double)))
  123. /**
  124. * Read an (u)int32_t.
  125. *
  126. * @param h hande to open file
  127. * @param file name of the source file
  128. * @param line line number in the code
  129. * @param i address of 32-bit integer to read
  130. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  131. */
  132. int
  133. GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
  134. int line, int32_t * i);
  135. /**
  136. * Read an (u)int32_t.
  137. *
  138. * @param h hande to open file
  139. * @param i address of 32-bit integer to read
  140. */
  141. #define GNUNET_BIO_read_int32(h, i) GNUNET_BIO_read_int32__ (h, __FILE__, __LINE__, (int32_t*) i)
  142. /**
  143. * Read an (u)int64_t.
  144. *
  145. * @param h hande to open file
  146. * @param file name of the source file
  147. * @param line line number in the code
  148. * @param i address of 64-bit integer to read
  149. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  150. */
  151. int
  152. GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
  153. int line, int64_t * i);
  154. /**
  155. * Read an (u)int64_t.
  156. *
  157. * @param h hande to open file
  158. * @param i address of 64-bit integer to read
  159. */
  160. #define GNUNET_BIO_read_int64(h, i) GNUNET_BIO_read_int64__ (h, __FILE__, __LINE__, (int64_t*) i)
  161. /**
  162. * Handle for buffered writing.
  163. */
  164. struct GNUNET_BIO_WriteHandle;
  165. /**
  166. * Open a file for writing.
  167. *
  168. * @param fn file name to be opened
  169. * @return IO handle on success, NULL on error
  170. */
  171. struct GNUNET_BIO_WriteHandle *
  172. GNUNET_BIO_write_open (const char *fn);
  173. /**
  174. * Close an open file for writing.
  175. *
  176. * @param h file handle
  177. * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
  178. */
  179. int
  180. GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
  181. /**
  182. * Write a buffer to a file.
  183. *
  184. * @param h handle to open file
  185. * @param buffer the data to write
  186. * @param n number of bytes to write
  187. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  188. */
  189. int
  190. GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
  191. size_t n);
  192. /**
  193. * Force a buffered writer to flush its buffer
  194. *
  195. * @param h the writer handle
  196. * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
  197. * the file is closed
  198. */
  199. int
  200. GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h);
  201. /**
  202. * Write a string to a file.
  203. *
  204. * @param h handle to open file
  205. * @param s string to write (can be NULL)
  206. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  207. */
  208. int
  209. GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s);
  210. /**
  211. * Write metadata container to a file.
  212. *
  213. * @param h handle to open file
  214. * @param m metadata to write
  215. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  216. */
  217. int
  218. GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
  219. const struct GNUNET_CONTAINER_MetaData *m);
  220. /**
  221. * Write a float.
  222. *
  223. * @param h hande to open file
  224. * @param f float to write (must be a variable)
  225. */
  226. #define GNUNET_BIO_write_float(h, f) GNUNET_BIO_write (h, &f, sizeof(float))
  227. /**
  228. * Write a double.
  229. *
  230. * @param h hande to open file
  231. * @param f double to write (must be a variable)
  232. */
  233. #define GNUNET_BIO_write_double(h, f) GNUNET_BIO_write (h, &f, sizeof(double))
  234. /**
  235. * Write an (u)int32_t.
  236. *
  237. * @param h hande to open file
  238. * @param i 32-bit integer to write
  239. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  240. */
  241. int
  242. GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i);
  243. /**
  244. * Write an (u)int64_t.
  245. *
  246. * @param h hande to open file
  247. * @param i 64-bit integer to write
  248. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  249. */
  250. int
  251. GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i);
  252. #if 0 /* keep Emacsens' auto-indent happy */
  253. {
  254. #endif
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. /* ifndef GNUNET_BIO_LIB_H */
  259. #endif
  260. /** @} */ /* end of group bio */
  261. /* end of gnunet_bio_lib.h */