Esempio n. 1
0
static int _write_value(struct output_line *outline, const struct config_value *v)
{
	char *buf;

	switch (v->type) {
	case CFG_STRING:
		if (!(buf = alloca(escaped_len(v->v.str)))) {
			log_error("temporary stack allocation for a config "
				  "string failed");
			return 0;
		}
		line_append("\"%s\"", escape_double_quotes(buf, v->v.str));
		break;

	case CFG_FLOAT:
		line_append("%f", v->v.r);
		break;

	case CFG_INT:
		line_append("%" PRId64, v->v.i);
		break;

	case CFG_EMPTY_ARRAY:
		line_append("[]");
		break;

	default:
		log_error("_write_value: Unknown value type: %d", v->type);

	}

	return 1;
}
Esempio n. 2
0
static int _write_value(struct config_output *out, const struct dm_config_value *v)
{
	char *buf;

	switch (v->type) {
	case DM_CFG_STRING:
		buf = alloca(dm_escaped_len(v->v.str));
		line_append("\"%s\"", dm_escape_double_quotes(buf, v->v.str));
		break;

	case DM_CFG_FLOAT:
		line_append("%f", v->v.f);
		break;

	case DM_CFG_INT:
		line_append("%" PRId64, v->v.i);
		break;

	case DM_CFG_EMPTY_ARRAY:
		line_append("[]");
		break;

	default:
		log_error("_write_value: Unknown value type: %d", v->type);

	}

	return 1;
}
Esempio n. 3
0
static int _write_config(const struct dm_config_node *n, int only_one,
			 struct config_output *out, int level)
{
	char space[MAX_INDENT + 1];
	int l = (level < MAX_INDENT) ? level : MAX_INDENT;
	int i;

	if (!n)
		return 1;

	for (i = 0; i < l; i++)
		space[i] = '\t';
	space[i] = '\0';

	do {
		if (out->spec && out->spec->prefix_fn)
			out->spec->prefix_fn(n, space, out->baton);

		if (!_line_start(out))
			return_0;
		line_append("%s%s", space, n->key);
		if (!n->v) {
			/* it's a sub section */
			line_append(" {");
			if (!_line_end(n, out))
				return_0;
			_write_config(n->child, 0, out, level + 1);
			if (!_line_start(out))
				return_0;
			line_append("%s}", space);
		} else {
			/* it's a value */
			const struct dm_config_value *v = n->v;
			line_append("=");
			if (v->next) {
				line_append("[");
				while (v && v->type != DM_CFG_EMPTY_ARRAY) {
					if (!_write_value(out, v))
						return_0;
					v = v->next;
					if (v && v->type != DM_CFG_EMPTY_ARRAY)
						line_append(", ");
				}
				line_append("]");
			} else
				if (!_write_value(out, v))
					return_0;
		}
		if (!_line_end(n, out))
			return_0;

		if (out->spec && out->spec->suffix_fn)
			out->spec->suffix_fn(n, space, out->baton);

		n = n->sib;
	} while (n && !only_one);
	/* FIXME: add error checking */
	return 1;
}