/** * scols_table_new_column: * @tb: table * @name: column header * @whint: column width hint (absolute width: N > 1; relative width: N < 1) * @flags: flags integer * * This is shortcut for * * cl = scols_new_column(); * scols_column_set_....(cl, ...); * scols_table_add_column(tb, cl); * * The column width is possible to define by three ways: * * @whint = 0..1 : relative width, percent of terminal width * * @whint = 1..N : absolute width, empty colum will be truncated to * the column header width * * @whint = 1..N * * The column is necessary to address by * sequential number. The first defined column has the colnum = 0. For example: * * scols_table_new_column(tab, "FOO", 0.5, 0); // colnum = 0 * scols_table_new_column(tab, "BAR", 0.5, 0); // colnum = 1 * . * . * scols_line_get_cell(line, 0); // FOO column * scols_line_get_cell(line, 1); // BAR column * * Returns: newly allocated column */ struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, double whint, int flags) { struct libscols_column *cl; struct libscols_cell *hr; assert (tb); if (!tb) return NULL; cl = scols_new_column(); if (!cl) return NULL; /* set column name */ hr = scols_column_get_header(cl); if (!hr) goto err; if (scols_cell_set_data(hr, name)) goto err; scols_column_set_whint(cl, whint); scols_column_set_flags(cl, flags); if (scols_table_add_column(tb, cl)) /* this increments column ref-counter */ goto err; scols_unref_column(cl); return cl; err: scols_unref_column(cl); return NULL; }
static struct libscols_column *parse_column(FILE *f) { size_t len = 0; char *line = NULL; int nlines = 0; struct libscols_column *cl = NULL; while (getline(&line, &len, f) != -1) { char *p = strrchr(line, '\n'); if (p) *p = '\0'; switch (nlines) { case 0: /* NAME */ { struct libscols_cell *hr; cl = scols_new_column(); if (!cl) goto fail; hr = scols_column_get_header(cl); if (!hr || scols_cell_set_data(hr, line)) goto fail; break; } case 1: /* WIDTH-HINT */ { double whint = strtod_or_err(line, "failed to parse column whint"); if (scols_column_set_whint(cl, whint)) goto fail; break; } case 2: /* FLAGS */ { int num_flags = parse_column_flags(line); if (scols_column_set_flags(cl, num_flags)) goto fail; if (strcmp(line, "wrapnl") == 0) { scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize, scols_wrapnl_nextchunk, NULL); scols_column_set_safechars(cl, "\n"); } break; } case 3: /* COLOR */ if (scols_column_set_color(cl, line)) goto fail; break; default: break; } nlines++; } free(line); return cl; fail: free(line); scols_unref_column(cl); return NULL; }