1 /* $NetBSD: citrus_memstream.h,v 1.3 2005/05/14 17:55:42 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_memstream.h,v 1.3 2008/04/10 10:21:01 hasso Exp $ */
3 
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #ifndef _CITRUS_MEMSTREAM_H_
32 #define _CITRUS_MEMSTREAM_H_
33 
34 struct _citrus_memory_stream {
35 	struct _citrus_region	ms_region;
36 	size_t			ms_pos;
37 };
38 
39 __BEGIN_DECLS
40 const char *	_citrus_memory_stream_getln(
41 	struct _citrus_memory_stream * __restrict, size_t * __restrict);
42 const char *	_citrus_memory_stream_matchline(
43 	struct _citrus_memory_stream * __restrict, const char * __restrict,
44 	size_t * __restrict, int);
45 void *		_citrus_memory_stream_chr(struct _citrus_memory_stream *,
46 					  struct _citrus_region *, char);
47 void		_citrus_memory_stream_skip_ws(struct _citrus_memory_stream *);
48 __END_DECLS
49 
50 static __inline int
51 _citrus_memory_stream_iseof(struct _citrus_memory_stream *ms)
52 {
53 	return ms->ms_pos >= _citrus_region_size(&ms->ms_region);
54 }
55 
56 static __inline void
57 _citrus_memory_stream_bind(struct _citrus_memory_stream * __restrict ms,
58 			   const struct _citrus_region * __restrict r)
59 {
60 	ms->ms_region = *r;
61 	ms->ms_pos = 0;
62 }
63 
64 static __inline void
65 _citrus_memory_stream_bind_ptr(struct _citrus_memory_stream * __restrict ms,
66 			       void *ptr, size_t sz)
67 {
68 	struct _citrus_region r;
69 
70 	_citrus_region_init(&r, ptr, sz);
71 	_citrus_memory_stream_bind(ms, &r);
72 }
73 
74 static __inline void
75 _citrus_memory_stream_rewind(struct _citrus_memory_stream *ms)
76 {
77 	ms->ms_pos = 0;
78 }
79 
80 static __inline size_t
81 _citrus_memory_stream_tell(struct _citrus_memory_stream *ms)
82 {
83 	return ms->ms_pos;
84 }
85 
86 static __inline size_t
87 _citrus_memory_stream_remainder(struct _citrus_memory_stream *ms)
88 {
89 	size_t sz;
90 	sz = _citrus_region_size(&ms->ms_region);
91 	if (ms->ms_pos>sz)
92 		return 0;
93 	return sz-ms->ms_pos;
94 }
95 
96 static __inline int
97 _citrus_memory_stream_seek(struct _citrus_memory_stream *ms, size_t pos, int w)
98 {
99 	size_t sz = _citrus_region_size(&ms->ms_region);
100 	switch (w) {
101 	case SEEK_SET:
102 		if (pos>=sz)
103 			return -1;
104 		ms->ms_pos = pos;
105 		break;
106 	case SEEK_CUR:
107 		pos += (ssize_t)ms->ms_pos;
108 		if (pos>=sz)
109 			return -1;
110 		ms->ms_pos = pos;
111 		break;
112 	case SEEK_END:
113 		if (sz<pos)
114 			return -1;
115 		ms->ms_pos = sz - pos;
116 		break;
117 	}
118 	return 0;
119 }
120 
121 static __inline int
122 _citrus_memory_stream_getc(struct _citrus_memory_stream *ms)
123 {
124 	if (_citrus_memory_stream_iseof(ms))
125 		return (EOF);
126 	return _citrus_region_peek8(&ms->ms_region, ms->ms_pos++);
127 }
128 
129 static __inline void
130 _citrus_memory_stream_ungetc(struct _citrus_memory_stream *ms, int ch)
131 {
132 	if (ch != EOF && ms->ms_pos > 0)
133 		ms->ms_pos--;
134 }
135 
136 static __inline int
137 _citrus_memory_stream_peek(struct _citrus_memory_stream *ms)
138 {
139 	if (_citrus_memory_stream_iseof(ms))
140 		return (EOF);
141 	return _citrus_region_peek8(&ms->ms_region, ms->ms_pos);
142 }
143 
144 static __inline void *
145 _citrus_memory_stream_getregion(struct _citrus_memory_stream *ms,
146 				struct _citrus_region *r, size_t sz)
147 {
148 	void *ret;
149 
150 	if (ms->ms_pos + sz > _citrus_region_size(&ms->ms_region))
151 		return NULL;
152 
153 	ret = _citrus_region_offset(&ms->ms_region, ms->ms_pos);
154 	ms->ms_pos += sz;
155 	if (r)
156 		_citrus_region_init(r, ret, sz);
157 
158 	return ret;
159 }
160 
161 static __inline int
162 _citrus_memory_stream_get8(struct _citrus_memory_stream *ms, uint8_t *rval)
163 {
164 
165 	if (ms->ms_pos + 1 > _citrus_region_size(&ms->ms_region))
166 		return -1;
167 
168 	*rval = _citrus_region_peek8(&ms->ms_region, ms->ms_pos);
169 	ms->ms_pos += 2;
170 
171 	return 0;
172 }
173 
174 static __inline int
175 _citrus_memory_stream_get16(struct _citrus_memory_stream *ms, uint16_t *rval)
176 {
177 
178 	if (ms->ms_pos + 2 > _citrus_region_size(&ms->ms_region))
179 		return -1;
180 
181 	*rval = _citrus_region_peek16(&ms->ms_region, ms->ms_pos);
182 	ms->ms_pos += 2;
183 
184 	return 0;
185 }
186 
187 static __inline int
188 _citrus_memory_stream_get32(struct _citrus_memory_stream *ms, uint32_t *rval)
189 {
190 
191 	if (ms->ms_pos + 4 > _citrus_region_size(&ms->ms_region))
192 		return -1;
193 
194 	*rval = _citrus_region_peek32(&ms->ms_region, ms->ms_pos);
195 	ms->ms_pos += 4;
196 
197 	return 0;
198 }
199 
200 static __inline int
201 _citrus_memory_stream_getln_region(struct _citrus_memory_stream *ms,
202 				   struct _citrus_region *r)
203 {
204 	const char *ptr;
205 	size_t sz;
206 
207 	ptr = _citrus_memory_stream_getln(ms, &sz);
208 	if (ptr)
209 		_citrus_region_init(r, __DECONST(char *, ptr), sz);
210 
211 	return ptr == NULL;
212 }
213 
214 #endif
215