]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/column.c
2 * column.c - functions for table handling at the column level
4 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
14 * @short_description: defines output columns formats, headers, etc.
16 * An API to access and modify per-column data and information.
25 #include "smartcolsP.h"
30 * Allocates space for a new column.
32 * Returns: a pointer to a new struct libscols_cell instance, NULL in case of an ENOMEM error.
34 struct libscols_column
*scols_new_column(void)
36 struct libscols_column
*cl
;
38 cl
= calloc(1, sizeof(*cl
));
41 DBG(COL
, ul_debugobj(cl
, "alloc"));
43 INIT_LIST_HEAD(&cl
->cl_columns
);
49 * @cl: a pointer to a struct libscols_column instance
51 * Increases the refcount of @cl.
53 void scols_ref_column(struct libscols_column
*cl
)
61 * @cl: a pointer to a struct libscols_column instance
63 * Decreases the refcount of @cl. When the count falls to zero, the instance
64 * is automatically deallocated.
66 void scols_unref_column(struct libscols_column
*cl
)
68 if (cl
&& --cl
->refcount
<= 0) {
69 DBG(COL
, ul_debugobj(cl
, "dealloc"));
70 list_del(&cl
->cl_columns
);
71 scols_reset_cell(&cl
->header
);
79 * @cl: a pointer to a struct libscols_column instance
81 * Creates a new column and copies @cl's data over to it.
83 * Returns: a pointer to a new struct libscols_column instance.
85 struct libscols_column
*scols_copy_column(const struct libscols_column
*cl
)
87 struct libscols_column
*ret
;
91 ret
= scols_new_column();
95 DBG(COL
, ul_debugobj((void *) cl
, "copy to %p", ret
));
97 if (scols_column_set_color(ret
, cl
->color
))
99 if (scols_cell_copy_content(&ret
->header
, &cl
->header
))
102 ret
->width
= cl
->width
;
103 ret
->width_min
= cl
->width_min
;
104 ret
->width_max
= cl
->width_max
;
105 ret
->width_avg
= cl
->width_avg
;
106 ret
->width_hint
= cl
->width_hint
;
107 ret
->flags
= cl
->flags
;
108 ret
->is_extreme
= cl
->is_extreme
;
112 scols_unref_column(ret
);
117 * scols_column_set_whint:
118 * @cl: a pointer to a struct libscols_column instance
119 * @whint: a width hint
121 * Sets the width hint of column @cl to @whint.
123 * Returns: 0, a negative value in case of an error.
125 int scols_column_set_whint(struct libscols_column
*cl
, double whint
)
130 cl
->width_hint
= whint
;
135 * scols_column_get_whint:
136 * @cl: a pointer to a struct libscols_column instance
138 * Returns: The width hint of column @cl, a negative value in case of an error.
140 double scols_column_get_whint(struct libscols_column
*cl
)
143 return cl
? cl
->width_hint
: -EINVAL
;
147 * scols_column_set_flags:
148 * @cl: a pointer to a struct libscols_column instance
149 * @flags: a flag mask
151 * Sets the flags of @cl to @flags.
153 * Returns: 0, a negative value in case of an error.
155 int scols_column_set_flags(struct libscols_column
*cl
, int flags
)
161 if (!(cl
->flags
& SCOLS_FL_TREE
) && (flags
& SCOLS_FL_TREE
))
162 cl
->table
->ntreecols
++;
163 else if ((cl
->flags
& SCOLS_FL_TREE
) && !(flags
& SCOLS_FL_TREE
))
164 cl
->table
->ntreecols
--;
172 * scols_column_get_flags:
173 * @cl: a pointer to a struct libscols_column instance
175 * Returns: The flag mask of @cl, a negative value in case of an error.
177 int scols_column_get_flags(struct libscols_column
*cl
)
180 return cl
? cl
->flags
: -EINVAL
;
184 * scols_column_get_header:
185 * @cl: a pointer to a struct libscols_column instance
187 * Returns: A pointer to a struct libscols_cell instance, representing the
188 * header info of column @cl or NULL in case of an error.
190 struct libscols_cell
*scols_column_get_header(struct libscols_column
*cl
)
192 return cl
? &cl
->header
: NULL
;
196 * scols_column_set_color:
197 * @cl: a pointer to a struct libscols_column instance
198 * @color: color name or ESC sequence
200 * The default color for data cells and column header.
202 * If you want to set header specific color then use scols_column_get_header()
203 * and scols_cell_set_color().
205 * If you want to set data cell specific color the use scols_line_get_cell() +
206 * scols_cell_set_color().
208 * Returns: 0, a negative value in case of an error.
210 int scols_column_set_color(struct libscols_column
*cl
, const char *color
)
217 if (isalpha(*color
)) {
218 color
= color_sequence_from_colorname(color
);
234 * scols_column_get_color:
235 * @cl: a pointer to a struct libscols_column instance
237 * Returns: The current color setting of the column @cl.
239 const char *scols_column_get_color(struct libscols_column
*cl
)
242 return cl
? cl
->color
: NULL
;
247 * scols_column_set_cmpfunc:
249 * @cmp: pointer to compare function
250 * @data: private data for cmp function
252 * Returns: 0, a negative value in case of an error.
254 int scols_column_set_cmpfunc(struct libscols_column
*cl
,
255 int (*cmp
)(struct libscols_cell
*,
256 struct libscols_cell
*,
264 cl
->cmpfunc_data
= data
;
269 * scols_column_is_hidden:
270 * @cl: a pointer to a struct libscols_column instance
272 * Gets the value of @cl's flag hidden.
274 * Returns: hidden flag value, negative value in case of an error.
278 int scols_column_is_hidden(struct libscols_column
*cl
)
282 return cl
->flags
& SCOLS_FL_HIDDEN
;
286 * scols_column_is_trunc:
287 * @cl: a pointer to a struct libscols_column instance
289 * Gets the value of @cl's flag trunc.
291 * Returns: trunc flag value, negative value in case of an error.
293 int scols_column_is_trunc(struct libscols_column
*cl
)
297 return cl
->flags
& SCOLS_FL_TRUNC
;
300 * scols_column_is_tree:
301 * @cl: a pointer to a struct libscols_column instance
303 * Gets the value of @cl's flag tree.
305 * Returns: tree flag value, negative value in case of an error.
307 int scols_column_is_tree(struct libscols_column
*cl
)
311 return cl
->flags
& SCOLS_FL_TREE
;
314 * scols_column_is_right:
315 * @cl: a pointer to a struct libscols_column instance
317 * Gets the value of @cl's flag right.
319 * Returns: right flag value, negative value in case of an error.
321 int scols_column_is_right(struct libscols_column
*cl
)
325 return cl
->flags
& SCOLS_FL_RIGHT
;
328 * scols_column_is_strict_width:
329 * @cl: a pointer to a struct libscols_column instance
331 * Gets the value of @cl's flag strict_width.
333 * Returns: strict_width flag value, negative value in case of an error.
335 int scols_column_is_strict_width(struct libscols_column
*cl
)
339 return cl
->flags
& SCOLS_FL_STRICTWIDTH
;
342 * scols_column_is_noextremes:
343 * @cl: a pointer to a struct libscols_column instance
345 * Gets the value of @cl's flag no_extremes.
347 * Returns: no_extremes flag value, negative value in case of an error.
349 int scols_column_is_noextremes(struct libscols_column
*cl
)
353 return cl
->flags
& SCOLS_FL_NOEXTREMES
;
356 * scols_column_is_wrap:
357 * @cl: a pointer to a struct libscols_column instance
359 * Gets the value of @cl's flag wrap.
361 * Returns: wrap flag value, negative value in case of an error.
363 int scols_column_is_wrap(struct libscols_column
*cl
)
367 return cl
->flags
& SCOLS_FL_WRAP
;