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