streams.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /****************************************************************************
  2. * include/nuttx/streams.h
  3. *
  4. * Copyright (C) 2009, 2011-2012, 2014-2016, 2019 Gregory Nutt. All
  5. * rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. #ifndef __INCLUDE_NUTTX_STREAMS_H
  37. #define __INCLUDE_NUTTX_STREAMS_H
  38. /****************************************************************************
  39. * Included Files
  40. ****************************************************************************/
  41. #include <nuttx/config.h>
  42. #include <stdio.h>
  43. /****************************************************************************
  44. * Public Types
  45. ****************************************************************************/
  46. /* These are the generic representations of a streams used by the NuttX */
  47. struct lib_instream_s;
  48. typedef CODE int (*lib_getc_t)(FAR struct lib_instream_s *this);
  49. struct lib_outstream_s;
  50. typedef CODE void (*lib_putc_t)(FAR struct lib_outstream_s *this, int ch);
  51. typedef CODE int (*lib_flush_t)(FAR struct lib_outstream_s *this);
  52. struct lib_instream_s
  53. {
  54. lib_getc_t get; /* Get one character from the instream */
  55. int nget; /* Total number of characters gotten. Written
  56. * by get method, readable by user */
  57. };
  58. struct lib_outstream_s
  59. {
  60. lib_putc_t put; /* Put one character to the outstream */
  61. lib_flush_t flush; /* Flush any buffered characters in the outstream */
  62. int nput; /* Total number of characters put. Written
  63. * by put method, readable by user */
  64. };
  65. /* Seek-able streams */
  66. struct lib_sistream_s;
  67. typedef CODE int (*lib_sigetc_t)(FAR struct lib_sistream_s *this);
  68. typedef CODE off_t (*lib_siseek_t)(FAR struct lib_sistream_s *this,
  69. off_t offset, int whence);
  70. struct lib_sostream_s;
  71. typedef CODE void (*lib_soputc_t)(FAR struct lib_sostream_s *this, int ch);
  72. typedef CODE int (*lib_soflush_t)(FAR struct lib_sostream_s *this);
  73. typedef CODE off_t (*lib_soseek_t)(FAR struct lib_sostream_s *this,
  74. off_t offset, int whence);
  75. struct lib_sistream_s
  76. {
  77. lib_sigetc_t get; /* Get one character from the instream */
  78. lib_siseek_t seek; /* Seek to a position in the instream */
  79. int nget; /* Total number of characters gotten. Written
  80. * by get method, readable by user */
  81. };
  82. struct lib_sostream_s
  83. {
  84. lib_soputc_t put; /* Put one character to the outstream */
  85. lib_soflush_t flush; /* Flush any buffered characters in the outstream */
  86. lib_soseek_t seek; /* Seek a position in the output stream */
  87. int nput; /* Total number of characters put. Written
  88. * by put method, readable by user */
  89. };
  90. /* These are streams that operate on a fixed-sized block of memory */
  91. struct lib_meminstream_s
  92. {
  93. struct lib_instream_s public;
  94. FAR const char *buffer; /* Address of first byte in the buffer */
  95. size_t buflen; /* Size of the buffer in bytes */
  96. };
  97. struct lib_memoutstream_s
  98. {
  99. struct lib_outstream_s public;
  100. FAR char *buffer; /* Address of first byte in the buffer */
  101. size_t buflen; /* Size of the buffer in bytes */
  102. };
  103. struct lib_memsistream_s
  104. {
  105. struct lib_sistream_s public;
  106. FAR const char *buffer; /* Address of first byte in the buffer */
  107. size_t offset; /* Current buffer offset in bytes */
  108. size_t buflen; /* Size of the buffer in bytes */
  109. };
  110. struct lib_memsostream_s
  111. {
  112. struct lib_sostream_s public;
  113. FAR char *buffer; /* Address of first byte in the buffer */
  114. size_t offset; /* Current buffer offset in bytes */
  115. size_t buflen; /* Size of the buffer in bytes */
  116. };
  117. /* These are streams that operate on a FILE */
  118. struct lib_stdinstream_s
  119. {
  120. struct lib_instream_s public;
  121. FAR FILE *stream;
  122. };
  123. struct lib_stdoutstream_s
  124. {
  125. struct lib_outstream_s public;
  126. FAR FILE *stream;
  127. };
  128. struct lib_stdsistream_s
  129. {
  130. struct lib_sistream_s public;
  131. FAR FILE *stream;
  132. };
  133. struct lib_stdsostream_s
  134. {
  135. struct lib_sostream_s public;
  136. FAR FILE *stream;
  137. };
  138. /* These are streams that operate on a file descriptor */
  139. struct lib_rawinstream_s
  140. {
  141. struct lib_instream_s public;
  142. int fd;
  143. };
  144. struct lib_rawoutstream_s
  145. {
  146. struct lib_outstream_s public;
  147. int fd;
  148. };
  149. struct lib_rawsistream_s
  150. {
  151. struct lib_sistream_s public;
  152. int fd;
  153. };
  154. struct lib_rawsostream_s
  155. {
  156. struct lib_sostream_s public;
  157. int fd;
  158. };
  159. /* This is a special stream that does buffered character I/O. NOTE that is
  160. * CONFIG_SYSLOG_BUFFER is not defined, it is the same as struct
  161. * lib_outstream_s
  162. */
  163. struct iob_s; /* Forward reference */
  164. struct lib_syslogstream_s
  165. {
  166. struct lib_outstream_s public;
  167. #ifdef CONFIG_SYSLOG_BUFFER
  168. FAR struct iob_s *iob;
  169. #endif
  170. };
  171. /****************************************************************************
  172. * Public Data
  173. ****************************************************************************/
  174. #undef EXTERN
  175. #if defined(__cplusplus)
  176. # define EXTERN extern "C"
  177. extern "C"
  178. {
  179. #else
  180. # define EXTERN extern
  181. #endif
  182. /****************************************************************************
  183. * Public Function Prototypes
  184. ****************************************************************************/
  185. /****************************************************************************
  186. * Name: lib_meminstream, lib_memoutstream, lib_memsistream, lib_memsostream
  187. *
  188. * Description:
  189. * Initializes a stream for use with a fixed-size memory buffer.
  190. * Defined in lib/stdio/lib_meminstream.c and lib/stdio/lib_memoutstream.c.
  191. * Seekable versions are defined in lib/stdio/lib_memsistream.c and
  192. * lib/stdio/lib_memsostream.c.
  193. *
  194. * Input Parameters:
  195. * memstream - User allocated, uninitialized instance of struct
  196. * lib_meminstream_s to be initialized.
  197. * memstream - User allocated, uninitialized instance of struct
  198. * lib_memoutstream_s to be initialized.
  199. * bufstart - Address of the beginning of the fixed-size memory buffer
  200. * buflen - Size of the fixed-sized memory buffer in bytes
  201. *
  202. * Returned Value:
  203. * None (User allocated instance initialized).
  204. *
  205. ****************************************************************************/
  206. void lib_meminstream(FAR struct lib_meminstream_s *instream,
  207. FAR const char *bufstart, int buflen);
  208. void lib_memoutstream(FAR struct lib_memoutstream_s *outstream,
  209. FAR char *bufstart, int buflen);
  210. void lib_memsistream(FAR struct lib_memsistream_s *instream,
  211. FAR const char *bufstart, int buflen);
  212. void lib_memsostream(FAR struct lib_memsostream_s *outstream,
  213. FAR char *bufstart, int buflen);
  214. /****************************************************************************
  215. * Name: lib_stdinstream, lib_stdoutstream
  216. *
  217. * Description:
  218. * Initializes a stream for use with a FILE instance.
  219. * Defined in lib/stdio/lib_stdinstream.c and lib/stdio/lib_stdoutstream.c
  220. *
  221. * Input Parameters:
  222. * instream - User allocated, uninitialized instance of struct
  223. * lib_stdinstream_s to be initialized.
  224. * outstream - User allocated, uninitialized instance of struct
  225. * lib_stdoutstream_s to be initialized.
  226. * stream - User provided stream instance (must have been opened for
  227. * the correct access).
  228. *
  229. * Returned Value:
  230. * None (User allocated instance initialized).
  231. *
  232. ****************************************************************************/
  233. void lib_stdinstream(FAR struct lib_stdinstream_s *instream,
  234. FAR FILE *stream);
  235. void lib_stdoutstream(FAR struct lib_stdoutstream_s *outstream,
  236. FAR FILE *stream);
  237. void lib_stdsistream(FAR struct lib_stdsistream_s *instream,
  238. FAR FILE *stream);
  239. void lib_stdsostream(FAR struct lib_stdsostream_s *outstream,
  240. FAR FILE *stream);
  241. /****************************************************************************
  242. * Name: lib_rawinstream, lib_rawoutstream, lib_rawsistream, and
  243. * lib_rawsostream,
  244. *
  245. * Description:
  246. * Initializes a stream for use with a file descriptor.
  247. * Defined in lib/stdio/lib_rawinstream.c and lib/stdio/lib_rawoutstream.c.
  248. * Seekable versions are defined in lib/stdio/lib_rawsistream.c and
  249. * lib/stdio/lib_rawsostream.c
  250. *
  251. * Input Parameters:
  252. * instream - User allocated, uninitialized instance of struct
  253. * lib_rawinstream_s to be initialized.
  254. * outstream - User allocated, uninitialized instance of struct
  255. * lib_rawoutstream_s to be initialized.
  256. * fd - User provided file/socket descriptor (must have been opened
  257. * for the correct access).
  258. *
  259. * Returned Value:
  260. * None (User allocated instance initialized).
  261. *
  262. ****************************************************************************/
  263. void lib_rawinstream(FAR struct lib_rawinstream_s *instream, int fd);
  264. void lib_rawoutstream(FAR struct lib_rawoutstream_s *outstream, int fd);
  265. void lib_rawsistream(FAR struct lib_rawsistream_s *instream, int fd);
  266. void lib_rawsostream(FAR struct lib_rawsostream_s *outstream, int fd);
  267. /****************************************************************************
  268. * Name: lib_lowoutstream
  269. *
  270. * Description:
  271. * Initializes a stream for use with low-level, architecture-specific output.
  272. * Defined in ib/stdio/lib_lowoutstream.c
  273. *
  274. * Input Parameters:
  275. * lowoutstream - User allocated, uninitialized instance of struct
  276. * lib_outstream_s to be initialized.
  277. *
  278. * Returned Value:
  279. * None (User allocated instance initialized).
  280. *
  281. ****************************************************************************/
  282. #ifdef CONFIG_ARCH_LOWPUTC
  283. void lib_lowoutstream(FAR struct lib_outstream_s *lowoutstream);
  284. #endif
  285. /****************************************************************************
  286. * Name: lib_zeroinstream, lib_nullinstream, lib_nulloutstream
  287. *
  288. * Description:
  289. * Initializes NULL streams:
  290. *
  291. * o The stream created by lib_zeroinstream will return an infinitely long
  292. * stream of zeroes. Defined in lib/stdio/lib_zeroinstream.c
  293. * o The stream created by lib_nullinstream will return only EOF.
  294. * Defined in lib/stdio/lib_nullinstream.c
  295. * o The stream created by lib_nulloutstream will write all data to the
  296. * bit-bucket. Defined in lib/stdio/lib_nulloutstream.c
  297. *
  298. * Input Parameters:
  299. * zeroinstream - User allocated, uninitialized instance of struct
  300. * lib_instream_s to be initialized.
  301. * nullinstream - User allocated, uninitialized instance of struct
  302. * lib_instream_s to be initialized.
  303. * nulloutstream - User allocated, uninitialized instance of struct
  304. * lib_outstream_s to be initialized.
  305. *
  306. * Returned Value:
  307. * None (User allocated instance initialized).
  308. *
  309. ****************************************************************************/
  310. void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream);
  311. void lib_nullinstream(FAR struct lib_instream_s *nullinstream);
  312. void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream);
  313. /****************************************************************************
  314. * Name: syslogstream_create
  315. *
  316. * Description:
  317. * Initializes a stream for use with the configured syslog interface.
  318. * Only accessible from with the OS SYSLOG logic.
  319. *
  320. * Input Parameters:
  321. * stream - User allocated, uninitialized instance of struct
  322. * lib_syslogstream_s to be initialized.
  323. *
  324. * Returned Value:
  325. * None (User allocated instance initialized).
  326. *
  327. ****************************************************************************/
  328. void syslogstream_create(FAR struct lib_syslogstream_s *stream);
  329. /****************************************************************************
  330. * Name: syslogstream_destroy
  331. *
  332. * Description:
  333. * Free resources held by the syslog stream.
  334. *
  335. * Input Parameters:
  336. * stream - User allocated, uninitialized instance of struct
  337. * lib_syslogstream_s to be initialized.
  338. *
  339. * Returned Value:
  340. * None (Resources freed).
  341. *
  342. ****************************************************************************/
  343. #ifdef CONFIG_SYSLOG_BUFFER
  344. void syslogstream_destroy(FAR struct lib_syslogstream_s *stream);
  345. #else
  346. # define syslogstream_destroy(s)
  347. #endif
  348. /****************************************************************************
  349. * Name: emergstream
  350. *
  351. * Description:
  352. * Initializes a stream for use with the configured emergency syslog
  353. * interface. Only accessible from with the OS SYSLOG logic.
  354. *
  355. * Input Parameters:
  356. * stream - User allocated, uninitialized instance of struct
  357. * lib_outstream_s to be initialized.
  358. *
  359. * Returned Value:
  360. * None (User allocated instance initialized).
  361. *
  362. ****************************************************************************/
  363. void emergstream(FAR struct lib_outstream_s *stream);
  364. /****************************************************************************
  365. * Name: lib_noflush
  366. *
  367. * Description:
  368. * lib_noflush() provides a common, dummy flush method for output streams
  369. * that are not flushable.
  370. *
  371. * Returned Value:
  372. * Always returns OK
  373. *
  374. ****************************************************************************/
  375. int lib_noflush(FAR struct lib_outstream_s *stream);
  376. /****************************************************************************
  377. * Name: lib_snoflush
  378. *
  379. * Description:
  380. * lib_snoflush() provides a common, dummy flush method for seekable output
  381. * streams that are not flushable.
  382. * is selected.
  383. *
  384. * Returned Value:
  385. * Always returns OK
  386. *
  387. ****************************************************************************/
  388. int lib_snoflush(FAR struct lib_sostream_s *this);
  389. /****************************************************************************
  390. * Name: lib_sprintf
  391. *
  392. * Description:
  393. * Stream-oriented implementation of sprintf. Used only by the SYSLOG.
  394. *
  395. ****************************************************************************/
  396. int lib_sprintf(FAR struct lib_outstream_s *obj,
  397. FAR const IPTR char *fmt, ...);
  398. /****************************************************************************
  399. * Name: lib_vsprintf
  400. *
  401. * Description:
  402. * Stream-oriented implementation that underlies printf family: printf,
  403. * fprint, sprint, etc.
  404. *
  405. ****************************************************************************/
  406. int lib_vsprintf(FAR struct lib_outstream_s *obj,
  407. FAR const IPTR char *src, va_list ap);
  408. /****************************************************************************
  409. * Name: lib_vscanf
  410. *
  411. * Description:
  412. * Stream-oriented implementation that underlies scanf family: scanf,
  413. * fscanf, vfscanf, sscanf, and vsscanf
  414. *
  415. ****************************************************************************/
  416. int lib_vscanf(FAR struct lib_instream_s *obj, FAR int *lastc,
  417. FAR const IPTR char *src, va_list ap);
  418. #undef EXTERN
  419. #if defined(__cplusplus)
  420. }
  421. #endif
  422. #endif /* __INCLUDE_NUTTX_STREAMS_H */