]>
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.
27 #include "smartcolsP.h"
32 * Allocates space for a new column.
34 * Returns: a pointer to a new struct libscols_column instance, NULL in case of an ENOMEM error.
36 struct libscols_column
*scols_new_column(void)
38 struct libscols_column
*cl
;
40 cl
= calloc(1, sizeof(*cl
));
43 DBG(COL
, ul_debugobj(cl
, "alloc"));
45 INIT_LIST_HEAD(&cl
->cl_columns
);
51 * @cl: a pointer to a struct libscols_column instance
53 * Increases the refcount of @cl.
55 void scols_ref_column(struct libscols_column
*cl
)
63 * @cl: a pointer to a struct libscols_column instance
65 * Decreases the refcount of @cl. When the count falls to zero, the instance
66 * is automatically deallocated.
68 void scols_unref_column(struct libscols_column
*cl
)
70 if (cl
&& --cl
->refcount
<= 0) {
71 DBG(COL
, ul_debugobj(cl
, "dealloc"));
72 list_del(&cl
->cl_columns
);
73 scols_reset_cell(&cl
->header
);
76 free(cl
->pending_data_buf
);
83 * @cl: a pointer to a struct libscols_column instance
85 * Creates a new column and copies @cl's data over to it.
87 * Returns: a pointer to a new struct libscols_column instance.
89 struct libscols_column
*scols_copy_column(const struct libscols_column
*cl
)
91 struct libscols_column
*ret
;
95 ret
= scols_new_column();
99 DBG(COL
, ul_debugobj(cl
, "copy to %p", ret
));
101 if (scols_column_set_color(ret
, cl
->color
))
103 if (scols_cell_copy_content(&ret
->header
, &cl
->header
))
106 ret
->width
= cl
->width
;
107 ret
->width_min
= cl
->width_min
;
108 ret
->width_max
= cl
->width_max
;
109 ret
->width_avg
= cl
->width_avg
;
110 ret
->width_hint
= cl
->width_hint
;
111 ret
->flags
= cl
->flags
;
112 ret
->is_extreme
= cl
->is_extreme
;
116 scols_unref_column(ret
);
121 * scols_column_set_whint:
122 * @cl: a pointer to a struct libscols_column instance
123 * @whint: a width hint
125 * Sets the width hint of column @cl to @whint. See scols_table_new_column().
127 * Returns: 0, a negative value in case of an error.
129 int scols_column_set_whint(struct libscols_column
*cl
, double whint
)
134 cl
->width_hint
= whint
;
139 * scols_column_get_whint:
140 * @cl: a pointer to a struct libscols_column instance
142 * Returns: The width hint of column @cl, a negative value in case of an error.
144 double scols_column_get_whint(const struct libscols_column
*cl
)
146 return cl
->width_hint
;
150 * scols_column_set_flags:
151 * @cl: a pointer to a struct libscols_column instance
152 * @flags: a flag mask
154 * Sets the flags of @cl to @flags.
156 * Returns: 0, a negative value in case of an error.
158 int scols_column_set_flags(struct libscols_column
*cl
, int flags
)
164 if (!(cl
->flags
& SCOLS_FL_TREE
) && (flags
& SCOLS_FL_TREE
))
165 cl
->table
->ntreecols
++;
166 else if ((cl
->flags
& SCOLS_FL_TREE
) && !(flags
& SCOLS_FL_TREE
))
167 cl
->table
->ntreecols
--;
175 * scols_column_get_table:
176 * @cl: a pointer to a struct libscols_column instance
178 * Returns: pointer to the table where columns is used
180 struct libscols_table
*scols_column_get_table(const struct libscols_column
*cl
)
186 * scols_column_get_flags:
187 * @cl: a pointer to a struct libscols_column instance
189 * Returns: The flag mask of @cl, a negative value in case of an error.
191 int scols_column_get_flags(const struct libscols_column
*cl
)
197 * scols_column_get_header:
198 * @cl: a pointer to a struct libscols_column instance
200 * Returns: A pointer to a struct libscols_cell instance, representing the
201 * header info of column @cl or NULL in case of an error.
203 struct libscols_cell
*scols_column_get_header(struct libscols_column
*cl
)
209 * scols_column_set_color:
210 * @cl: a pointer to a struct libscols_column instance
211 * @color: color name or ESC sequence
213 * The default color for data cells and column header.
215 * If you want to set header specific color then use scols_column_get_header()
216 * and scols_cell_set_color().
218 * If you want to set data cell specific color the use scols_line_get_cell() +
219 * scols_cell_set_color().
221 * Returns: 0, a negative value in case of an error.
223 int scols_column_set_color(struct libscols_column
*cl
, const char *color
)
225 if (color
&& isalpha(*color
)) {
226 color
= color_sequence_from_colorname(color
);
230 return strdup_to_struct_member(cl
, color
, 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(const struct libscols_column
*cl
)
245 * scols_wrapnl_nextchunk:
246 * @cl: a pointer to a struct libscols_column instance
248 * @userdata: callback private data
250 * This is built-in function for scols_column_set_wrapfunc(). This function
251 * terminates the current chunk by \0 and returns pointer to the begin of
252 * the next chunk. The chunks are based on \n.
254 * For example for data "AAA\nBBB\nCCC" the next chunk is "BBB".
256 * Returns: next chunk
260 char *scols_wrapnl_nextchunk(const struct libscols_column
*cl
__attribute__((unused
)),
262 void *userdata
__attribute__((unused
)))
264 char *p
= data
? strchr(data
, '\n') : NULL
;
274 * scols_wrapnl_chunksize:
275 * @cl: a pointer to a struct libscols_column instance
277 * @userdata: callback private data
279 * Analyzes @data and returns size of the largest chunk. The chunks are based
280 * on \n. For example for data "AAA\nBBB\nCCCC" the largest chunk size is 4.
282 * Note that the size has to be based on number of terminal cells rather than
283 * bytes to support multu-byte output.
285 * Returns: size of the largest chunk.
289 size_t scols_wrapnl_chunksize(const struct libscols_column
*cl
__attribute__((unused
)),
291 void *userdata
__attribute__((unused
)))
295 while (data
&& *data
) {
299 p
= strchr(data
, '\n');
301 sz
= mbs_safe_nwidth(data
, p
- data
, NULL
);
304 sz
= mbs_safe_width(data
);
314 * scols_column_set_cmpfunc:
316 * @cmp: pointer to compare function
317 * @data: private data for cmp function
319 * Returns: 0, a negative value in case of an error.
321 int scols_column_set_cmpfunc(struct libscols_column
*cl
,
322 int (*cmp
)(struct libscols_cell
*,
323 struct libscols_cell
*,
331 cl
->cmpfunc_data
= data
;
336 * scols_column_set_wrapfunc:
337 * @cl: a pointer to a struct libscols_column instance
338 * @wrap_chunksize: function to return size of the largest chink of data
339 * @wrap_nextchunk: function to return next zero terminated data
340 * @userdata: optional stuff for callbacks
342 * Extends SCOLS_FL_WRAP and allows to set custom wrap function. The default
343 * is to wrap by column size, but you can create functions to wrap for example
344 * after \n or after words, etc.
346 * Returns: 0, a negative value in case of an error.
350 int scols_column_set_wrapfunc(struct libscols_column
*cl
,
351 size_t (*wrap_chunksize
)(const struct libscols_column
*,
354 char * (*wrap_nextchunk
)(const struct libscols_column
*,
362 cl
->wrap_nextchunk
= wrap_nextchunk
;
363 cl
->wrap_chunksize
= wrap_chunksize
;
364 cl
->wrapfunc_data
= userdata
;
369 * scols_column_set_safechars:
370 * @cl: a pointer to a struct libscols_column instance
371 * @safe: safe characters (e.g. "\n\t")
373 * Use for bytes you don't want to encode on output. This is for example
374 * necessary if you want to use custom wrap function based on \n, in this case
375 * you have to set "\n" as a safe char.
377 * Returns: 0, a negative value in case of an error.
381 int scols_column_set_safechars(struct libscols_column
*cl
, const char *safe
)
383 return strdup_to_struct_member(cl
, safechars
, safe
);
387 * scols_column_get_safechars:
388 * @cl: a pointer to a struct libscols_column instance
390 * Returns: safe chars
394 const char *scols_column_get_safechars(const struct libscols_column
*cl
)
396 return cl
->safechars
;
400 * scols_column_get_width:
401 * @cl: a pointer to a struct libscols_column instance
403 * Important note: the column width is unknown until library starts printing
404 * (width is calculated before printing). The function is usable for example in
405 * nextchunk() callback specified by scols_column_set_wrapfunc().
407 * See also scols_column_get_whint(), it returns wanted size (!= final size).
409 * Returns: column width
413 size_t scols_column_get_width(const struct libscols_column
*cl
)
419 * scols_column_is_hidden:
420 * @cl: a pointer to a struct libscols_column instance
422 * Gets the value of @cl's flag hidden.
428 int scols_column_is_hidden(const struct libscols_column
*cl
)
430 return cl
->flags
& SCOLS_FL_HIDDEN
? 1 : 0;
434 * scols_column_is_trunc:
435 * @cl: a pointer to a struct libscols_column instance
437 * Gets the value of @cl's flag trunc.
441 int scols_column_is_trunc(const struct libscols_column
*cl
)
443 return cl
->flags
& SCOLS_FL_TRUNC
? 1 : 0;
446 * scols_column_is_tree:
447 * @cl: a pointer to a struct libscols_column instance
449 * Gets the value of @cl's flag tree.
453 int scols_column_is_tree(const struct libscols_column
*cl
)
455 return cl
->flags
& SCOLS_FL_TREE
? 1 : 0;
458 * scols_column_is_right:
459 * @cl: a pointer to a struct libscols_column instance
461 * Gets the value of @cl's flag right.
465 int scols_column_is_right(const struct libscols_column
*cl
)
467 return cl
->flags
& SCOLS_FL_RIGHT
? 1 : 0;
470 * scols_column_is_strict_width:
471 * @cl: a pointer to a struct libscols_column instance
473 * Gets the value of @cl's flag strict_width.
477 int scols_column_is_strict_width(const struct libscols_column
*cl
)
479 return cl
->flags
& SCOLS_FL_STRICTWIDTH
? 1 : 0;
482 * scols_column_is_noextremes:
483 * @cl: a pointer to a struct libscols_column instance
485 * Gets the value of @cl's flag no_extremes.
489 int scols_column_is_noextremes(const struct libscols_column
*cl
)
491 return cl
->flags
& SCOLS_FL_NOEXTREMES
? 1 : 0;
494 * scols_column_is_wrap:
495 * @cl: a pointer to a struct libscols_column instance
497 * Gets the value of @cl's flag wrap.
503 int scols_column_is_wrap(const struct libscols_column
*cl
)
505 return cl
->flags
& SCOLS_FL_WRAP
? 1 : 0;
508 * scols_column_is_customwrap:
509 * @cl: a pointer to a struct libscols_column instance
515 int scols_column_is_customwrap(const struct libscols_column
*cl
)
517 return (cl
->flags
& SCOLS_FL_WRAP
)
518 && cl
->wrap_chunksize
519 && cl
->wrap_nextchunk
? 1 : 0;