Example #1
0
static int
convert_line(struct _citrus_db_factory *df, const char *line, size_t len)
{
	const char *p;
	char data[LINE_MAX], key[LINE_MAX];

	/* cut off trailing comment */
	p = memchr(line, T_COMM, len);
	if (p)
		len = p - line;

	/* key */
	line = _bcs_skip_ws_len(line, &len);
	if (len == 0)
		return (0);
	p = _bcs_skip_nonws_len(line, &len);
	if (p == line)
		return (0);
	snprintf(key, sizeof(key), "%.*s", (int)(p-line), line);
	_bcs_convert_to_lower(key);

	/* data */
	line = _bcs_skip_ws_len(p, &len);
	_bcs_trunc_rws_len(line, &len);
	snprintf(data, sizeof(data), "%.*s", (int)len, line);

	return (_db_factory_addstr_by_s(df, key, data));
}
static int
convert_line(struct src_head *sh, const char *line, size_t len)
{
	struct src_entry *se;
	const char *p;
	char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX];
	char *ep;
	uint32_t val;
	int ret;

	se = NULL;

	/* cut off trailing comment */
	p = memchr(line, T_COMM, len);
	if (p)
		len = p - line;

	/* key1 */
	line = _bcs_skip_ws_len(line, &len);
	if (len == 0)
		return (0);
	p = _bcs_skip_nonws_len(line, &len);
	if (p == line)
		return (0);
	snprintf(key1, sizeof(key1), "%.*s", (int)(p - line), line);

	/* key2 */
	line = _bcs_skip_ws_len(p, &len);
	if (len == 0)
		return (0);
	p = _bcs_skip_nonws_len(line, &len);
	if (p == line)
		return (0);
	snprintf(key2, sizeof(key2), "%.*s", (int)(p - line), line);

	/* data */
	line = _bcs_skip_ws_len(p, &len);
	_bcs_trunc_rws_len(line, &len);
	snprintf(data, sizeof(data), "%.*s", (int)len, line);
	val = strtoul(data, &ep, 0);
	if (*ep != '\0')
		return (EFTYPE);

	/* insert to DB */
	ret = find_src(sh, &se, key1);
	if (ret)
		return (ret);

	return (_db_factory_add32_by_s(se->se_df, key2, val));
}
Example #3
0
static int
seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
	       struct _region *data)
{
	const char *p, *q;
	size_t len;

	if (cl->cl_rewind)
		_memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
	cl->cl_rewind = 0;

retry:
	p = _memstream_getln(&cl->cl_plainms, &len);
	if (p == NULL)
		return ENOENT;
	/* ignore comment */
	q = memchr(p, T_COMM, len);
	if (q) {
		len = q-p;
	}
	/* ignore trailing spaces */
	_bcs_trunc_rws_len(p, &len);
	p = _bcs_skip_ws_len(p, &len);
	q = _bcs_skip_nonws_len(p, &len);
	if (p==q)
		goto retry;
	if (cl->cl_key && ((size_t)(q-p) != cl->cl_keylen ||
			   memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
		goto retry;

	/* found a entry */
	if (key)
		_region_init(key, __UNCONST(p), (size_t)(q-p));
	p = _bcs_skip_ws_len(q, &len);
	if (data)
		_region_init(data, len ? __UNCONST(p) : NULL, len);

	return 0;
}