Beispiel #1
0
static int parse_column_data(FILE *f, struct libscols_table *tb, int col)
{
	size_t len = 0, nlines = 0;
	int i;
	char *str = NULL;

	while ((i = getline(&str, &len, f)) != -1) {

		struct libscols_line *ln;
		char *p = strrchr(str, '\n');
		if (p)
			*p = '\0';

		while ((p = strrchr(str, '\\')) && *(p + 1) == 'n') {
			*p = '\n';
			memmove(p + 1, p + 2, i - (p + 2 - str));
		}

		ln = scols_table_get_line(tb, nlines++);
		if (!ln)
			break;

		scols_line_set_data(ln, col, str);
	}

	free(str);
	return 0;

}
Beispiel #2
0
int main(int argc, char *argv[])
{
	struct libscols_table *tb;
	int c, notree = 0, nstart = -1, nend = -1;


	static const struct option longopts[] = {
		{ "ascii",	0, 0, 'i' },
		{ "csv",        0, 0, 'c' },
		{ "list",       0, 0, 'l' },
		{ "noheadings",	0, 0, 'n' },
		{ "pairs",      0, 0, 'p' },
		{ "json",       0, 0, 'J' },
		{ "raw",        0, 0, 'r' },
		{ "range-start",1, 0, 'S' },
		{ "range-end",  1, 0, 'E' },
		{ NULL, 0, 0, 0 },
	};

	setlocale(LC_ALL, "");	/* just to have enable UTF8 chars */

	scols_init_debug(0);

	tb = scols_new_table();
	if (!tb)
		err(EXIT_FAILURE, "failed to create output table");

	while((c = getopt_long(argc, argv, "ciJlnprS:E:", longopts, NULL)) != -1) {
		switch(c) {
		case 'c':
			scols_table_set_column_separator(tb, ",");
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'i':
			scols_table_enable_ascii(tb, 1);
			break;
		case 'J':
			scols_table_set_name(tb, "scolstest");
			scols_table_enable_json(tb, 1);
			break;
		case 'l':
			notree = 1;
			break;
		case 'n':
			scols_table_enable_noheadings(tb, 1);
			break;
		case 'p':
			scols_table_enable_export(tb, 1);
			notree = 1;
			break;
		case 'r':
			scols_table_enable_raw(tb, 1);
			notree = 1;
			break;
		case 'S':
			nstart = strtos32_or_err(optarg, "failed to parse range start") - 1;
			break;
		case 'E':
			nend = strtos32_or_err(optarg, "failed to parse range end") - 1;
			break;
		default:
			usage(stderr);
		}
	}

	scols_table_enable_colors(tb, 1);
	setup_columns(tb, notree);

	if (optind == argc)
		add_lines(tb, ".");
	else while (optind < argc)
		add_lines(tb, argv[optind++]);

	if (nstart >= 0 || nend >= 0) {
		/* print subset */
		struct libscols_line *start = NULL, *end = NULL;

		if (nstart >= 0)
			start = scols_table_get_line(tb, nstart);
		if (nend >= 0)
			end = scols_table_get_line(tb, nend);

		if (start || end)
			scols_table_print_range(tb, start, end);
	} else
		/* print all table */
		scols_print_table(tb);

	scols_unref_table(tb);
	return EXIT_SUCCESS;
}