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