esl_buffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2010-2012, Anthony Minessale II
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of the original author; nor the names of any contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "esl.h"
  34. #ifndef ESL_BUFFER_H
  35. #define ESL_BUFFER_H
  36. /**
  37. * @defgroup esl_buffer Buffer Routines
  38. * @ingroup buffer
  39. * The purpose of this module is to make a plain buffering interface that can be used for read/write buffers
  40. * throughout the application.
  41. * @{
  42. */
  43. struct esl_buffer;
  44. typedef struct esl_buffer esl_buffer_t;
  45. /*! \brief Allocate a new dynamic esl_buffer
  46. * \param buffer returned pointer to the new buffer
  47. * \param blocksize length to realloc by as data is added
  48. * \param start_len ammount of memory to reserve initially
  49. * \param max_len length the buffer is allowed to grow to
  50. * \return status
  51. */
  52. ESL_DECLARE(esl_status_t) esl_buffer_create(esl_buffer_t **buffer, esl_size_t blocksize, esl_size_t start_len, esl_size_t max_len);
  53. /*! \brief Get the length of a esl_buffer_t
  54. * \param buffer any buffer of type esl_buffer_t
  55. * \return int size of the buffer.
  56. */
  57. ESL_DECLARE(esl_size_t) esl_buffer_len(esl_buffer_t *buffer);
  58. /*! \brief Get the freespace of a esl_buffer_t
  59. * \param buffer any buffer of type esl_buffer_t
  60. * \return int freespace in the buffer.
  61. */
  62. ESL_DECLARE(esl_size_t) esl_buffer_freespace(esl_buffer_t *buffer);
  63. /*! \brief Get the in use amount of a esl_buffer_t
  64. * \param buffer any buffer of type esl_buffer_t
  65. * \return int ammount of buffer curently in use
  66. */
  67. ESL_DECLARE(esl_size_t) esl_buffer_inuse(esl_buffer_t *buffer);
  68. /*! \brief Read data from a esl_buffer_t up to the ammount of datalen if it is available. Remove read data from buffer.
  69. * \param buffer any buffer of type esl_buffer_t
  70. * \param data pointer to the read data to be returned
  71. * \param datalen amount of data to be returned
  72. * \return int ammount of data actually read
  73. */
  74. ESL_DECLARE(esl_size_t) esl_buffer_read(esl_buffer_t *buffer, void *data, esl_size_t datalen);
  75. ESL_DECLARE(esl_size_t) esl_buffer_read_packet(esl_buffer_t *buffer, void *data, esl_size_t maxlen);
  76. ESL_DECLARE(esl_size_t) esl_buffer_packet_count(esl_buffer_t *buffer);
  77. /*! \brief Read data endlessly from a esl_buffer_t
  78. * \param buffer any buffer of type esl_buffer_t
  79. * \param data pointer to the read data to be returned
  80. * \param datalen amount of data to be returned
  81. * \return int ammount of data actually read
  82. * \note Once you have read all the data from the buffer it will loop around.
  83. */
  84. ESL_DECLARE(esl_size_t) esl_buffer_read_loop(esl_buffer_t *buffer, void *data, esl_size_t datalen);
  85. /*! \brief Assign a number of loops to read
  86. * \param buffer any buffer of type esl_buffer_t
  87. * \param loops the number of loops (-1 for infinite)
  88. */
  89. ESL_DECLARE(void) esl_buffer_set_loops(esl_buffer_t *buffer, int32_t loops);
  90. /*! \brief Write data into a esl_buffer_t up to the length of datalen
  91. * \param buffer any buffer of type esl_buffer_t
  92. * \param data pointer to the data to be written
  93. * \param datalen amount of data to be written
  94. * \return int amount of buffer used after the write, or 0 if no space available
  95. */
  96. ESL_DECLARE(esl_size_t) esl_buffer_write(esl_buffer_t *buffer, const void *data, esl_size_t datalen);
  97. /*! \brief Remove data from the buffer
  98. * \param buffer any buffer of type esl_buffer_t
  99. * \param datalen amount of data to be removed
  100. * \return int size of buffer, or 0 if unable to toss that much data
  101. */
  102. ESL_DECLARE(esl_size_t) esl_buffer_toss(esl_buffer_t *buffer, esl_size_t datalen);
  103. /*! \brief Remove all data from the buffer
  104. * \param buffer any buffer of type esl_buffer_t
  105. */
  106. ESL_DECLARE(void) esl_buffer_zero(esl_buffer_t *buffer);
  107. /*! \brief Destroy the buffer
  108. * \param buffer buffer to destroy
  109. * \note only neccessary on dynamic buffers (noop on pooled ones)
  110. */
  111. ESL_DECLARE(void) esl_buffer_destroy(esl_buffer_t **buffer);
  112. /*! \brief Seek to offset from the beginning of the buffer
  113. * \param buffer buffer to seek
  114. * \param datalen offset in bytes
  115. * \return new position
  116. */
  117. ESL_DECLARE(esl_size_t) esl_buffer_seek(esl_buffer_t *buffer, esl_size_t datalen);
  118. /** @} */
  119. ESL_DECLARE(esl_size_t) esl_buffer_zwrite(esl_buffer_t *buffer, const void *data, esl_size_t datalen);
  120. #endif
  121. /* For Emacs:
  122. * Local Variables:
  123. * mode:c
  124. * indent-tabs-mode:t
  125. * tab-width:4
  126. * c-basic-offset:4
  127. * End:
  128. * For VIM:
  129. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  130. */