buf_reader.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright 2020 NXP
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * 'Software'), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sub license, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject
  14. * to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the
  17. * next paragraph) shall be included in all copies or substantial
  18. * portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  23. * IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
  24. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. *****************************************************************************/
  29. #include "buf_reader.h"
  30. #include "stdio.h"
  31. #include "string.h"
  32. #define DBG_TRACE(x) printf x
  33. static int _is_buffered_handle_valid(bufferred_reader_t *fd)
  34. {
  35. if (fd == NULL)
  36. return E_BUF_INVALID_HANDLE;
  37. if ( fd->data_buf == NULL )
  38. return E_BUF_INVALID_HANDLE;
  39. if ( fd->size < 0 )
  40. return E_BUF_INVALID_HANDLE;
  41. if ( fd->index > fd->size )
  42. return E_BUF_INVALID_HANDLE;
  43. return 0;
  44. }
  45. /* Write buffered IO operations */
  46. int bufferred_fopen(bufferred_reader_t *fd, char *buf, int size)
  47. {
  48. if (fd == NULL)
  49. return E_BUF_INVALID_HANDLE;
  50. fd->data_buf = buf;
  51. fd->size = size;
  52. fd->index = 0;
  53. if ( _is_buffered_handle_valid(fd)) {
  54. return E_BUF_INVALID_HANDLE;
  55. }
  56. fd->is_valid = 1;
  57. return 0;
  58. }
  59. int bufferred_ftell(bufferred_reader_t *fd)
  60. {
  61. if ( _is_buffered_handle_valid(fd)) {
  62. return E_BUF_INVALID_HANDLE;
  63. }
  64. return fd->index;
  65. }
  66. int bufferred_fread(
  67. void *ptr,
  68. int size,
  69. int nmemb,
  70. bufferred_reader_t *fd
  71. )
  72. {
  73. int to_copy = 0;
  74. char *data_ptr = NULL;
  75. char *buff = (char *)ptr;
  76. int byte_to_read = size * nmemb;
  77. if ( _is_buffered_handle_valid(fd)) {
  78. return E_BUF_INVALID_HANDLE;
  79. }
  80. if ( buff == NULL ) {
  81. return E_BUF_IO_INVALID_PARAMETERS;
  82. }
  83. if ( byte_to_read < 0 ) {
  84. return E_BUF_IO_INVALID_PARAMETERS;
  85. }
  86. to_copy = (fd->size - fd->index);
  87. data_ptr = fd->data_buf + fd->index;
  88. if ( to_copy > byte_to_read )
  89. to_copy = byte_to_read;
  90. if (to_copy <= 0)
  91. return -1; //EOF
  92. memcpy(buff, data_ptr, to_copy);
  93. fd->index += to_copy;
  94. return 0;
  95. }
  96. int bufferred_fseek(
  97. bufferred_reader_t *fd,
  98. int offset,
  99. int direction
  100. )
  101. {
  102. if ( _is_buffered_handle_valid(fd)) {
  103. return E_BUF_INVALID_HANDLE;
  104. }
  105. switch(direction)
  106. {
  107. case SEEK_SET:
  108. fd->index = offset;
  109. break;
  110. case SEEK_CUR:
  111. fd->index += offset;
  112. break;
  113. case SEEK_END:
  114. fd->index = fd->size - offset;
  115. break;
  116. }
  117. /* Clamp current offset */
  118. if ( fd->index > fd->size ) {
  119. DBG_TRACE(("WARNING: seeking beyond buffer size\n"));
  120. fd->index = fd->size;
  121. }
  122. if ( fd->index < 0 ) {
  123. DBG_TRACE(("WARNING: seeking beyond buffer size\n"));
  124. fd->index = 0;
  125. }
  126. return 0;
  127. }
  128. int bufferred_fclose(bufferred_reader_t *fd)
  129. {
  130. if ( _is_buffered_handle_valid(fd)) {
  131. return E_BUF_INVALID_HANDLE;
  132. }
  133. fd->size = -1;
  134. fd->is_valid = 0;
  135. return 0;
  136. }
  137. char *bufferred_fgets(char* buff, int len, bufferred_reader_t *fd)
  138. {
  139. char *ptr;
  140. int i, j, valid_bytes;
  141. if ( buff == NULL ) {
  142. return NULL;
  143. }
  144. if ( len <= 0 ) {
  145. return NULL;
  146. }
  147. if ( _is_buffered_handle_valid(fd)) {
  148. return NULL;
  149. }
  150. /* Check how many bytes are available to read */
  151. valid_bytes = fd->size - fd->index;
  152. if ( len > 0 )
  153. len -=1; /* fgets read one character less than buffer length */
  154. if ( len > valid_bytes )
  155. len = valid_bytes;
  156. if ( valid_bytes <= 0 )
  157. return NULL;
  158. ptr = fd->data_buf + fd->index;
  159. for(i=0; ptr[i] != '\0' && i < len; i++)
  160. {
  161. if ( ptr[i] == '\n' )
  162. break;
  163. if ( ptr[i] == '\r' )
  164. break;
  165. buff[i] = ptr[i];
  166. }
  167. buff[i] = '\0';
  168. j = i;
  169. /* skip trailing newline from file buffer */
  170. if ( ptr[i] == '\r' )
  171. i++;
  172. if ( ptr[i] == '\n' )
  173. i++;
  174. fd->index += i;
  175. /* Trim trailing spaces from output buffer */
  176. for (i = j-1 ; i>0; i--) {
  177. if (buff[i] == ' ')
  178. buff[i] = '\0';
  179. else
  180. break;
  181. }
  182. return ptr;
  183. }