summaryrefslogtreecommitdiff
path: root/src/config/config_collapse.c
blob: ca0c863ea55248dae60a183e212635106aabfcc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_config_collapse --
 *	Given a NULL-terminated list of configuration strings, where the first
 *	one contains all the defaults, collapse them into a newly allocated
 *	buffer.
 */
int
__wt_config_collapse(WT_SESSION_IMPL *session,
    const char **cfg, const char **config_ret)
{
	WT_CONFIG cparser;
	WT_CONFIG_ITEM k, v;
	WT_ITEM buf;
	int ret;

	WT_CLEAR(buf);

	WT_RET(__wt_config_init(session, &cparser, cfg[0]));
	while ((ret = __wt_config_next(&cparser, &k, &v)) == 0) {
		if (k.type != ITEM_STRING && k.type != ITEM_ID)
			WT_RET_MSG(session, EINVAL,
			    "Invalid configuration key found: '%s'\n", k.str);
		WT_ERR(__wt_config_get(session, cfg, &k, &v));
		/* Include the quotes around string keys/values. */
		if (k.type == ITEM_STRING) {
			--k.str;
			k.len += 2;
		}
		if (v.type == ITEM_STRING) {
			--v.str;
			v.len += 2;
		}
		WT_ERR(__wt_buf_catfmt(session, &buf, "%.*s=%.*s,",
		    (int)k.len, k.str, (int)v.len, v.str));
	}

	if (ret != WT_NOTFOUND)
		goto err;

	/*
	 * If the caller passes us no valid configuration strings, we end up
	 * here with no allocated memory to return.  Check the final buffer
	 * size: empty configuration strings are possible, and paranoia is
	 * good.
	 */
	if (buf.size == 0)
		WT_RET(__wt_buf_initsize(session, &buf, 1));

	/* Strip the trailing comma and NUL-terminate */
	((char *)buf.data)[buf.size - 1] = '\0';

	*config_ret = buf.data;
	return (0);

err:	__wt_buf_free(session, &buf);
	return (ret);
}