sbuf.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Buffered inputput from a VipsSource
  2. *
  3. * J.Cupitt, 18/11/19
  4. */
  5. /*
  6. This file is part of VIPS.
  7. VIPS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. 02110-1301 USA
  19. */
  20. /*
  21. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  22. */
  23. #ifndef VIPS_SBUF_H
  24. #define VIPS_SBUF_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /*__cplusplus*/
  28. #define VIPS_TYPE_SBUF (vips_sbuf_get_type())
  29. #define VIPS_SBUF( obj ) \
  30. (G_TYPE_CHECK_INSTANCE_CAST( (obj), \
  31. VIPS_TYPE_SBUF, VipsSbuf ))
  32. #define VIPS_SBUF_CLASS( klass ) \
  33. (G_TYPE_CHECK_CLASS_CAST( (klass), \
  34. VIPS_TYPE_SBUF, VipsSbufClass))
  35. #define VIPS_IS_SBUF( obj ) \
  36. (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_SBUF ))
  37. #define VIPS_IS_SBUF_CLASS( klass ) \
  38. (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_SBUF ))
  39. #define VIPS_SBUF_GET_CLASS( obj ) \
  40. (G_TYPE_INSTANCE_GET_CLASS( (obj), \
  41. VIPS_TYPE_SBUF, VipsSbufClass ))
  42. #define VIPS_SBUF_BUFFER_SIZE (4096)
  43. /* Layer over source: read with an input buffer.
  44. *
  45. * Libraries like libjpeg do their own input buffering and need raw IO, but
  46. * others, like radiance, need to parse the input into lines. A buffered read
  47. * class is very convenient.
  48. */
  49. typedef struct _VipsSbuf {
  50. VipsObject parent_object;
  51. /*< private >*/
  52. /* The VipsSource we wrap.
  53. */
  54. VipsSource *source;
  55. /* The +1 means there's always a \0 byte at the end.
  56. *
  57. * Unsigned char, since we don't want >127 to be -ve.
  58. *
  59. * chars_in_buffer is how many chars we have in input_buffer,
  60. * read_point is the current read position in that buffer.
  61. */
  62. unsigned char input_buffer[VIPS_SBUF_BUFFER_SIZE + 1];
  63. int chars_in_buffer;
  64. int read_point;
  65. /* Build lines of text here.
  66. */
  67. unsigned char line[VIPS_SBUF_BUFFER_SIZE + 1];
  68. } VipsSbuf;
  69. typedef struct _VipsSbufClass {
  70. VipsObjectClass parent_class;
  71. } VipsSbufClass;
  72. GType vips_sbuf_get_type( void );
  73. VipsSbuf *vips_sbuf_new_from_source( VipsSource *source );
  74. void vips_sbuf_unbuffer( VipsSbuf *sbuf );
  75. int vips_sbuf_getc( VipsSbuf *sbuf );
  76. #define VIPS_SBUF_GETC( S ) ( \
  77. (S)->read_point < (S)->chars_in_buffer ? \
  78. (S)->input_buffer[(S)->read_point++] : \
  79. vips_sbuf_getc( S ) \
  80. )
  81. void vips_sbuf_ungetc( VipsSbuf *sbuf );
  82. #define VIPS_SBUF_UNGETC( S ) { \
  83. if( (S)->read_point > 0 ) \
  84. (S)->read_point -= 1; \
  85. }
  86. int vips_sbuf_require( VipsSbuf *sbuf, int require );
  87. #define VIPS_SBUF_REQUIRE( S, R ) ( \
  88. (S)->read_point + (R) <= (S)->chars_in_buffer ? \
  89. 0 : \
  90. vips_sbuf_require( (S), (R) ) \
  91. )
  92. #define VIPS_SBUF_PEEK( S ) ((S)->input_buffer + (S)->read_point)
  93. #define VIPS_SBUF_FETCH( S ) ((S)->input_buffer[(S)->read_point++])
  94. const char *vips_sbuf_get_line( VipsSbuf *sbuf );
  95. char *vips_sbuf_get_line_copy( VipsSbuf *sbuf );
  96. const char *vips_sbuf_get_non_whitespace( VipsSbuf *sbuf );
  97. int vips_sbuf_skip_whitespace( VipsSbuf *sbuf );
  98. #ifdef __cplusplus
  99. }
  100. #endif /*__cplusplus*/
  101. #endif /*VIPS_SBUF_H*/