Exemple #1
0
	void CSVParser::_post_escape_on()
	{
		if (_curr_char() == ',')
		{
			_back_quote();
			_field_end();
		}
		else if (_curr_char() == '"')
		{
			_escape_off();
		}
		else if (_curr_char() == '\n')
		{
			_back_quote();
			_field_end();
			_line_end();
		}
		else
		{
			throw std::runtime_error("Syntax error: quote symbol in front of character which is not quote symbol.");
		}
	}
Exemple #2
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;
	char *escaped_key = NULL;

	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;
		if (strchr(n->key, '#') || strchr(n->key, '"') || strchr(n->key, '!')) {
			escaped_key = alloca(dm_escaped_len(n->key) + 2);
			*escaped_key = '"';
			dm_escape_double_quotes(escaped_key + 1, n->key);
			strcat(escaped_key, "\"");
		}
		line_append("%s%s", space, escaped_key ? escaped_key : n->key);
		escaped_key = NULL;
		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;
}