1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/* $NetBSD: citrus_memstream.c,v 1.4 2009/02/03 05:02:12 lukem Exp $ */
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2003 Citrus Project,
6219019Sgabor * All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor *
17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219019Sgabor * SUCH DAMAGE.
28219019Sgabor */
29219019Sgabor
30219019Sgabor#include <sys/cdefs.h>
31219019Sgabor
32219019Sgabor#include <assert.h>
33219019Sgabor#include <stdio.h>
34219019Sgabor#include <stdlib.h>
35219019Sgabor#include <string.h>
36219019Sgabor
37219019Sgabor#include "citrus_namespace.h"
38219019Sgabor#include "citrus_region.h"
39219019Sgabor#include "citrus_memstream.h"
40219019Sgabor#include "citrus_bcs.h"
41219019Sgabor
42219019Sgaborconst char *
43219019Sgabor_citrus_memory_stream_getln(struct _citrus_memory_stream * __restrict ms,
44219019Sgabor    size_t * __restrict rlen)
45219019Sgabor{
46219019Sgabor	const uint8_t *h, *p;
47219019Sgabor	size_t ret;
48219019Sgabor	int i;
49219019Sgabor
50219019Sgabor	if (ms->ms_pos>=_region_size(&ms->ms_region))
51219019Sgabor		return (NULL);
52219019Sgabor
53219019Sgabor	h = p = (uint8_t *)_region_offset(&ms->ms_region, ms->ms_pos);
54219019Sgabor	ret = 0;
55219019Sgabor	for (i = _region_size(&ms->ms_region) - ms->ms_pos; i > 0; i--) {
56219019Sgabor		ret++;
57219019Sgabor		if (_bcs_iseol(*p))
58219019Sgabor			break;
59219019Sgabor		p++;
60219019Sgabor	}
61219019Sgabor
62219019Sgabor	ms->ms_pos += ret;
63219019Sgabor	*rlen = ret;
64219019Sgabor	return ((const char *)h);
65219019Sgabor}
66219019Sgabor
67219019Sgabor#define T_COMM	'#'
68219019Sgabor
69219019Sgaborconst char *
70219019Sgabor_citrus_memory_stream_matchline(struct _citrus_memory_stream * __restrict ms,
71219019Sgabor    const char * __restrict key, size_t * __restrict rlen, int iscasesensitive)
72219019Sgabor{
73219019Sgabor	const char *p, *q;
74219019Sgabor	size_t keylen, len;
75219019Sgabor
76219019Sgabor	keylen = strlen(key);
77219019Sgabor	for(;;) {
78219019Sgabor		p = _citrus_memory_stream_getln(ms, &len);
79219019Sgabor		if (p == NULL)
80219019Sgabor			return (NULL);
81219019Sgabor
82219019Sgabor		/* ignore comment */
83219019Sgabor		q = memchr(p, T_COMM, len);
84219019Sgabor		if (q) {
85219019Sgabor			len = q - p;
86219019Sgabor		}
87219019Sgabor		/* ignore trailing white space and newline */
88219019Sgabor		_bcs_trunc_rws_len(p, &len);
89219019Sgabor		if (len == 0)
90219019Sgabor			continue; /* ignore null line */
91219019Sgabor
92219019Sgabor		/* skip white spaces at the head of the line */
93219019Sgabor		p = _bcs_skip_ws_len(p, &len);
94219019Sgabor		q = _bcs_skip_nonws_len(p, &len);
95219019Sgabor
96219019Sgabor		if ((size_t)(q - p) == keylen) {
97219019Sgabor			if (iscasesensitive) {
98219019Sgabor				if (memcmp(key, p, keylen) == 0)
99219019Sgabor					break; /* match */
100219019Sgabor			} else {
101219019Sgabor				if (_bcs_strncasecmp(key, p, keylen) == 0)
102219019Sgabor					break; /* match */
103219019Sgabor			}
104219019Sgabor		}
105219019Sgabor	}
106219019Sgabor
107219019Sgabor	p = _bcs_skip_ws_len(q, &len);
108219019Sgabor	*rlen = len;
109219019Sgabor
110219019Sgabor	return (p);
111219019Sgabor}
112219019Sgabor
113219019Sgaborvoid *
114219019Sgabor_citrus_memory_stream_chr(struct _citrus_memory_stream *ms,
115219019Sgabor    struct _citrus_region *r, char ch)
116219019Sgabor{
117219019Sgabor	void *chr, *head;
118219019Sgabor	size_t sz;
119219019Sgabor
120219019Sgabor	if (ms->ms_pos >= _region_size(&ms->ms_region))
121219019Sgabor		return (NULL);
122219019Sgabor
123219019Sgabor	head = _region_offset(&ms->ms_region, ms->ms_pos);
124219019Sgabor	chr = memchr(head, ch, _memstream_remainder(ms));
125219019Sgabor	if (chr == NULL) {
126219019Sgabor		_region_init(r, head, _memstream_remainder(ms));
127219019Sgabor		ms->ms_pos = _region_size(&ms->ms_region);
128219019Sgabor		return (NULL);
129219019Sgabor	}
130219019Sgabor	sz = (char *)chr - (char *)head;
131219019Sgabor
132219019Sgabor	_region_init(r, head, sz);
133219019Sgabor	ms->ms_pos += sz + 1;
134219019Sgabor
135219019Sgabor	return (chr);
136219019Sgabor}
137219019Sgabor
138219019Sgaborvoid
139219019Sgabor_citrus_memory_stream_skip_ws(struct _citrus_memory_stream *ms)
140219019Sgabor{
141219019Sgabor	int ch;
142219019Sgabor
143219019Sgabor	while ((ch = _memstream_peek(ms)) != EOF) {
144219019Sgabor		if (!_bcs_isspace(ch))
145219019Sgabor			break;
146219019Sgabor		_memstream_getc(ms);
147219019Sgabor	}
148219019Sgabor}
149