1219019Sgabor/* $FreeBSD$ */
2263986Stijl/*	$NetBSD: citrus_memstream.c,v 1.5 2012/03/13 21:13:31 christos 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;
47263986Stijl	size_t i, ret;
48219019Sgabor
49219019Sgabor	if (ms->ms_pos>=_region_size(&ms->ms_region))
50219019Sgabor		return (NULL);
51219019Sgabor
52219019Sgabor	h = p = (uint8_t *)_region_offset(&ms->ms_region, ms->ms_pos);
53219019Sgabor	ret = 0;
54219019Sgabor	for (i = _region_size(&ms->ms_region) - ms->ms_pos; i > 0; i--) {
55219019Sgabor		ret++;
56219019Sgabor		if (_bcs_iseol(*p))
57219019Sgabor			break;
58219019Sgabor		p++;
59219019Sgabor	}
60219019Sgabor
61219019Sgabor	ms->ms_pos += ret;
62219019Sgabor	*rlen = ret;
63219019Sgabor	return ((const char *)h);
64219019Sgabor}
65219019Sgabor
66219019Sgabor#define T_COMM	'#'
67219019Sgabor
68219019Sgaborconst char *
69219019Sgabor_citrus_memory_stream_matchline(struct _citrus_memory_stream * __restrict ms,
70219019Sgabor    const char * __restrict key, size_t * __restrict rlen, int iscasesensitive)
71219019Sgabor{
72219019Sgabor	const char *p, *q;
73219019Sgabor	size_t keylen, len;
74219019Sgabor
75219019Sgabor	keylen = strlen(key);
76219019Sgabor	for(;;) {
77219019Sgabor		p = _citrus_memory_stream_getln(ms, &len);
78219019Sgabor		if (p == NULL)
79219019Sgabor			return (NULL);
80219019Sgabor
81219019Sgabor		/* ignore comment */
82219019Sgabor		q = memchr(p, T_COMM, len);
83219019Sgabor		if (q) {
84219019Sgabor			len = q - p;
85219019Sgabor		}
86219019Sgabor		/* ignore trailing white space and newline */
87219019Sgabor		_bcs_trunc_rws_len(p, &len);
88219019Sgabor		if (len == 0)
89219019Sgabor			continue; /* ignore null line */
90219019Sgabor
91219019Sgabor		/* skip white spaces at the head of the line */
92219019Sgabor		p = _bcs_skip_ws_len(p, &len);
93219019Sgabor		q = _bcs_skip_nonws_len(p, &len);
94219019Sgabor
95219019Sgabor		if ((size_t)(q - p) == keylen) {
96219019Sgabor			if (iscasesensitive) {
97219019Sgabor				if (memcmp(key, p, keylen) == 0)
98219019Sgabor					break; /* match */
99219019Sgabor			} else {
100219019Sgabor				if (_bcs_strncasecmp(key, p, keylen) == 0)
101219019Sgabor					break; /* match */
102219019Sgabor			}
103219019Sgabor		}
104219019Sgabor	}
105219019Sgabor
106219019Sgabor	p = _bcs_skip_ws_len(q, &len);
107219019Sgabor	*rlen = len;
108219019Sgabor
109219019Sgabor	return (p);
110219019Sgabor}
111219019Sgabor
112219019Sgaborvoid *
113219019Sgabor_citrus_memory_stream_chr(struct _citrus_memory_stream *ms,
114219019Sgabor    struct _citrus_region *r, char ch)
115219019Sgabor{
116219019Sgabor	void *chr, *head;
117219019Sgabor	size_t sz;
118219019Sgabor
119219019Sgabor	if (ms->ms_pos >= _region_size(&ms->ms_region))
120219019Sgabor		return (NULL);
121219019Sgabor
122219019Sgabor	head = _region_offset(&ms->ms_region, ms->ms_pos);
123219019Sgabor	chr = memchr(head, ch, _memstream_remainder(ms));
124219019Sgabor	if (chr == NULL) {
125219019Sgabor		_region_init(r, head, _memstream_remainder(ms));
126219019Sgabor		ms->ms_pos = _region_size(&ms->ms_region);
127219019Sgabor		return (NULL);
128219019Sgabor	}
129219019Sgabor	sz = (char *)chr - (char *)head;
130219019Sgabor
131219019Sgabor	_region_init(r, head, sz);
132219019Sgabor	ms->ms_pos += sz + 1;
133219019Sgabor
134219019Sgabor	return (chr);
135219019Sgabor}
136219019Sgabor
137219019Sgaborvoid
138219019Sgabor_citrus_memory_stream_skip_ws(struct _citrus_memory_stream *ms)
139219019Sgabor{
140219019Sgabor	int ch;
141219019Sgabor
142219019Sgabor	while ((ch = _memstream_peek(ms)) != EOF) {
143219019Sgabor		if (!_bcs_isspace(ch))
144219019Sgabor			break;
145219019Sgabor		_memstream_getc(ms);
146219019Sgabor	}
147219019Sgabor}
148