#include "smartcolsP.h"
+/*
+ * Note that the line is allocated without cells, the cells will be allocated
+ * later when you add the line to the table. If you want to use the line
+ * without table then you have to explicitly allocate the cells by
+ * scols_line_alloc_cells().
+ */
struct libscols_line *scols_new_line(void)
{
struct libscols_line *ln;
ln = calloc(1, sizeof(*ln));
if (!ln)
return NULL;
-
ln->refcount = 1;
INIT_LIST_HEAD(&ln->ln_lines);
INIT_LIST_HEAD(&ln->ln_children);
void scols_unref_line(struct libscols_line *ln)
{
- if (ln && --ln->refcount <= 0) {
- size_t i;
+ if (ln && --ln->refcount <= 0) {
list_del(&ln->ln_lines);
list_del(&ln->ln_children);
- for (i = 0; i <= ln->ncells; i++)
- scols_reset_cell(ln->cells[i]);
- free(ln->data);
+ scols_line_free_cells(ln);
free(ln->color);
free(ln);
+ return;
}
}
+void scols_line_free_cells(struct libscols_line *ln)
+{
+ size_t i;
+
+ if (!ln || !ln->cells)
+ return;
+
+ for (i = 0; i < ln->ncells; i++)
+ scols_reset_cell(&ln->cells[i]);
+
+ free(ln->cells);
+ ln->ncells = 0;
+ ln->cells = NULL;
+}
+
+
+int scols_line_alloc_cells(struct libscols_line *ln, size_t n)
+{
+ struct libscols_cell *ce;
+
+ assert(ln);
+
+ if (!ln)
+ return -EINVAL;
+ if (ln->ncells == n)
+ return 0;
+
+ if (!n) {
+ scols_line_free_cells(ln);
+ return 0;
+ }
+
+ ce = realloc(ln->cells, n * sizeof(struct libscols_cell));
+ if (!ce)
+ return -errno;
+
+ if (n > ln->ncells)
+ memset(ce + ln->ncells, 0,
+ (n - ln->ncells) * sizeof(struct libscols_cell));
+
+ ln->cells = ce;
+ ln->ncells = n;
+ return 0;
+}
+
int scols_line_set_userdata(struct libscols_line *ln, void *data)
{
assert(ln);
return ln ? ln->userdata : NULL;
}
-int scols_line_set_parent(struct libscols_line *ln, struct libscols_line *parent)
+int scols_line_remove_child(struct libscols_line *ln, struct libscols_line *child)
{
assert(ln);
- if (!ln)
+ assert(child);
+
+ if (!ln || !child)
+ return -EINVAL;
+ list_del_init(&child->ln_children);
+ scols_unref_line(child);
+
+ child->parent = NULL;
+ scols_unref_line(ln);
+ return 0;
+}
+
+int scols_line_add_child(struct libscols_line *ln, struct libscols_line *child)
+{
+ assert(ln);
+ assert(child);
+
+ if (!ln || !child)
return -EINVAL;
- ln->parent = parent;
+
+ /* unref old<->parent */
+ if (child->parent)
+ scols_line_remove_child(child->parent, child);
+
+ /* new reference from parent to child */
+ list_add_tail(&child->ln_children, &ln->ln_branch);
+ scols_ref_line(child);
+
+ /* new reference from child to parent */
+ child->parent = ln;
+ scols_ref_line(ln);
+
return 0;
}
return ln ? ln->color : NULL;
}
-size_t scols_line_get_datasize(struct libscols_line *ln)
-{
- assert(ln);
- return ln ? ln->data_sz : 0;
-}
-
size_t scols_line_get_ncells(struct libscols_line *ln)
{
assert(ln);
if (!ln || n >= ln->ncells)
return NULL;
- return ln->cells[n];
+ return &ln->cells[n];
+}
+
+/* just shortcut */
+int scols_line_set_data(struct libscols_line *ln, size_t n, const char *data)
+{
+ struct libscols_cell *ce = scols_line_get_cell(ln, n);
+
+ if (!ce)
+ return -EINVAL;
+ return scols_cell_set_data(ce, data);
+}
+
+/* just shortcut */
+int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data)
+{
+ struct libscols_cell *ce = scols_line_get_cell(ln, n);
+
+ if (!ce)
+ return -EINVAL;
+ return scols_cell_refer_data(ce, data);
}
struct libscols_line *scols_copy_line(struct libscols_line *ln)
{
struct libscols_line *ret;
+ size_t i;
assert (ln);
if (!ln)
return NULL;
if (scols_line_set_color(ret, ln->color))
goto err;
+ if (scols_line_alloc_cells(ret, ln->ncells))
+ goto err;
+
ret->userdata = ln->userdata;
- ret->parent = ln->parent;
- ret->data_sz = ln->data_sz;
ret->ncells = ln->ncells;
+ ret->seqnum = ln->seqnum;
- if (ln->ncells) {
- size_t i;
-
- ret->cells = calloc(ln->ncells, sizeof(struct libscols_cell));
- if (!ret->cells)
+ for (i = 0; i < ret->ncells; ++i) {
+ if (scols_cell_copy_content(&ret->cells[i], &ln->cells[i]))
goto err;
-
- for (i = 0; i < ret->ncells; ++i) {
- if (scols_cell_copy_content(ret->cells[i], ln->cells[i]))
- goto err;
- }
}
return ret;
# define assert(x)
#endif
+
/*
* Generic iterator
*/
* Tree symbols
*/
struct libscols_symbols {
- char *branch;
- char *vert;
- char *right;
+ int refcount;
+ char *branch;
+ char *vert;
+ char *right;
};
/*
struct libscols_cell {
char *data;
char *color;
+
+ unsigned int is_ref; /* data is reference to foreign pointer */
};
*/
struct libscols_line {
int refcount;
+ size_t seqnum;
+
void *userdata;
- size_t data_sz; /* strlen of all data */
char *color; /* default line color */
- struct libscols_cell **cells; /* array with data */
+ struct libscols_cell *cells; /* array with data */
size_t ncells; /* number of cells */
struct list_head ln_lines; /* table lines */
struct libscols_line *parent;
};
+/*
+ * The table
+ */
+struct libscols_table {
+ int refcount;
+ size_t ncols; /* number of columns */
+ size_t nlines; /* number of lines */
+ size_t termwidth; /* terminal width */
+ int is_term; /* is a tty? */
+ int flags;
+ int first_run;
+
+ struct list_head tb_columns;
+ struct list_head tb_lines;
+ struct libscols_symbols *symbols;
+};
+
+#define IS_ITER_FORWARD(_i) ((_i)->direction == SCOLS_ITER_FORWARD)
+#define IS_ITER_BACKWARD(_i) ((_i)->direction == SCOLS_ITER_BACKWARD)
+
+#define SCOLS_ITER_INIT(itr, list) \
+ do { \
+ (itr)->p = IS_ITER_FORWARD(itr) ? \
+ (list)->next : (list)->prev; \
+ (itr)->head = (list); \
+ } while(0)
+
+#define SCOLS_ITER_ITERATE(itr, res, restype, member) \
+ do { \
+ res = list_entry((itr)->p, restype, member); \
+ (itr)->p = IS_ITER_FORWARD(itr) ? \
+ (itr)->p->next : (itr)->p->prev; \
+ } while(0)
+
#endif /* _LIBSMARTCOLS_PRIVATE_H */