]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/line.c
6112090819a488600af2ce5c4887b99158aac5ff
2 * line.c - functions for table handling at the line level
4 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
5 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
14 * @short_description: line API
16 * An API to access and modify per-line data and information.
25 #include "smartcolsP.h"
30 * Note that the line is allocated without cells, the cells will be allocated
31 * later when you add the line to the table. If you want to use the line
32 * without table then you have to explicitly allocate the cells by
33 * scols_line_alloc_cells().
35 * Returns: a pointer to a new struct libscols_line instance.
37 struct libscols_line
*scols_new_line(void)
39 struct libscols_line
*ln
;
41 ln
= calloc(1, sizeof(*ln
));
45 INIT_LIST_HEAD(&ln
->ln_lines
);
46 INIT_LIST_HEAD(&ln
->ln_children
);
47 INIT_LIST_HEAD(&ln
->ln_branch
);
53 * @ln: a pointer to a struct libscols_line instance
55 * Increases the refcount of @ln.
57 void scols_ref_line(struct libscols_line
*ln
)
65 * @ln: a pointer to a struct libscols_line instance
67 * Decreases the refcount of @ln.
69 void scols_unref_line(struct libscols_line
*ln
)
72 if (ln
&& --ln
->refcount
<= 0) {
73 list_del(&ln
->ln_lines
);
74 list_del(&ln
->ln_children
);
76 scols_line_free_cells(ln
);
84 * scols_line_free_cells:
85 * @ln: a pointer to a struct libscols_line instance
87 * Frees the allocated cells referenced to by @ln.
89 void scols_line_free_cells(struct libscols_line
*ln
)
93 if (!ln
|| !ln
->cells
)
96 for (i
= 0; i
< ln
->ncells
; i
++)
97 scols_reset_cell(&ln
->cells
[i
]);
105 * scols_line_alloc_cells:
106 * @ln: a pointer to a struct libscols_line instance
107 * @n: the number of elements
109 * Allocates space for @n cells. This function is optional,
110 * and libsmartcols automatically allocates necessary cells
111 * according to number of columns in the table when you add
112 * the line to the table. See scols_table_add_line().
114 * Returns: 0, a negative value in case of an error.
116 int scols_line_alloc_cells(struct libscols_line
*ln
, size_t n
)
118 struct libscols_cell
*ce
;
128 scols_line_free_cells(ln
);
132 ce
= realloc(ln
->cells
, n
* sizeof(struct libscols_cell
));
137 memset(ce
+ ln
->ncells
, 0,
138 (n
- ln
->ncells
) * sizeof(struct libscols_cell
));
146 * scols_line_set_userdata:
147 * @ln: a pointer to a struct libscols_line instance
150 * Binds @data to @ln.
152 * Returns: 0, a negative value in case of an error.
154 int scols_line_set_userdata(struct libscols_line
*ln
, void *data
)
164 * scols_line_get_userdata:
165 * @ln: a pointer to a struct libscols_line instance
167 * Returns: 0, a negative value in case of an error.
169 void *scols_line_get_userdata(struct libscols_line
*ln
)
172 return ln
? ln
->userdata
: NULL
;
176 * scols_line_remove_child:
177 * @ln: a pointer to a struct libscols_line instance
178 * @child: a pointer to a struct libscols_line instance
180 * Removes @child as a child of @ln.
182 * Returns: 0, a negative value in case of an error.
184 int scols_line_remove_child(struct libscols_line
*ln
, struct libscols_line
*child
)
191 list_del_init(&child
->ln_children
);
192 scols_unref_line(child
);
194 child
->parent
= NULL
;
195 scols_unref_line(ln
);
200 * scols_line_add_child:
201 * @ln: a pointer to a struct libscols_line instance
202 * @child: a pointer to a struct libscols_line instance
204 * Sets @child as a child of @ln.
206 * Returns: 0, a negative value in case of an error.
208 int scols_line_add_child(struct libscols_line
*ln
, struct libscols_line
*child
)
216 /* unref old<->parent */
218 scols_line_remove_child(child
->parent
, child
);
220 /* new reference from parent to child */
221 list_add_tail(&child
->ln_children
, &ln
->ln_branch
);
222 scols_ref_line(child
);
224 /* new reference from child to parent */
232 * scols_line_get_parent:
233 * @ln: a pointer to a struct libscols_line instance
235 * Returns: a pointer to @ln's parent, NULL in case it has no parent or if there was an error.
237 struct libscols_line
*scols_line_get_parent(struct libscols_line
*ln
)
240 return ln
? ln
->parent
: NULL
;
244 * scols_line_has_children:
245 * @ln: a pointer to a struct libscols_line instance
247 * Returns: 1 if @ln has any children, otherwise 0.
249 int scols_line_has_children(struct libscols_line
*ln
)
252 return ln
? !list_empty(&ln
->ln_branch
) : 0;
256 * scols_line_next_child:
257 * @ln: a pointer to a struct libscols_line instance
258 * @itr: a pointer to a struct libscols_iter instance
259 * @chld: a pointer to a pointer to a struct libscols_line instance
261 * Finds the next child and returns a pointer to it via @chld.
263 * Returns: 0, a negative value in case of an error.
265 int scols_line_next_child(struct libscols_line
*ln
,
266 struct libscols_iter
*itr
,
267 struct libscols_line
**chld
)
271 if (!ln
|| !itr
|| !chld
)
276 SCOLS_ITER_INIT(itr
, &ln
->ln_branch
);
277 if (itr
->p
!= itr
->head
) {
278 SCOLS_ITER_ITERATE(itr
, *chld
, struct libscols_line
, ln_children
);
286 * scols_line_set_color:
287 * @ln: a pointer to a struct libscols_line instance
288 * @color: color name or ESC sequence
290 * Returns: 0, a negative value in case of an error.
292 int scols_line_set_color(struct libscols_line
*ln
, const char *color
)
300 if (isalnum(*color
)) {
301 color
= color_sequence_from_colorname(color
);
317 * scols_line_get_color:
318 * @ln: a pointer to a struct libscols_line instance
320 * Returns: @ln's color string, NULL in case of an error.
322 const char *scols_line_get_color(struct libscols_line
*ln
)
325 return ln
? ln
->color
: NULL
;
329 * scols_line_get_ncells:
330 * @ln: a pointer to a struct libscols_line instance
332 * Returns: @ln's number of cells
334 size_t scols_line_get_ncells(struct libscols_line
*ln
)
337 return ln
? ln
->ncells
: 0;
341 * scols_line_get_cell:
342 * @ln: a pointer to a struct libscols_line instance
343 * @n: cell number to retrieve
345 * Returns: the @n-th cell in @ln, NULL in case of an error.
347 struct libscols_cell
*scols_line_get_cell(struct libscols_line
*ln
,
352 if (!ln
|| n
>= ln
->ncells
)
354 return &ln
->cells
[n
];
358 * scols_line_get_column_cell:
359 * @ln: a pointer to a struct libscols_line instance
360 * @cl: pointer to cell
362 * Like scols_line_get_cell() by cell is referenced by column.
364 * Returns: the @n-th cell in @ln, NULL in case of an error.
366 struct libscols_cell
*scols_line_get_column_cell(
367 struct libscols_line
*ln
,
368 struct libscols_column
*cl
)
373 return scols_line_get_cell(ln
, cl
->seqnum
);
377 * scols_line_set_data:
378 * @ln: a pointer to a struct libscols_cell instance
379 * @n: number of the cell, whose data is to be set
380 * @data: actual data to set
382 * Returns: 0, a negative value in case of an error.
384 int scols_line_set_data(struct libscols_line
*ln
, size_t n
, const char *data
)
386 struct libscols_cell
*ce
= scols_line_get_cell(ln
, n
);
390 return scols_cell_set_data(ce
, data
);
394 * scols_line_refer_data:
395 * @ln: a pointer to a struct libscols_cell instance
396 * @n: number of the cell which will refer to @data
397 * @data: actual data to refer to
399 * Returns: 0, a negative value in case of an error.
401 int scols_line_refer_data(struct libscols_line
*ln
, size_t n
, char *data
)
403 struct libscols_cell
*ce
= scols_line_get_cell(ln
, n
);
407 return scols_cell_refer_data(ce
, data
);
412 * @ln: a pointer to a struct libscols_cell instance
414 * Returns: A newly allocated copy of @ln, NULL in case of an error.
416 struct libscols_line
*scols_copy_line(struct libscols_line
*ln
)
418 struct libscols_line
*ret
;
425 ret
= scols_new_line();
428 if (scols_line_set_color(ret
, ln
->color
))
430 if (scols_line_alloc_cells(ret
, ln
->ncells
))
433 ret
->userdata
= ln
->userdata
;
434 ret
->ncells
= ln
->ncells
;
435 ret
->seqnum
= ln
->seqnum
;
437 for (i
= 0; i
< ret
->ncells
; ++i
) {
438 if (scols_cell_copy_content(&ret
->cells
[i
], &ln
->cells
[i
]))
444 scols_unref_line(ret
);