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