]>
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"));
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
;
113 ret
->is_groups
= cl
->is_groups
;
117 scols_unref_column(ret
);
122 * scols_column_set_whint:
123 * @cl: a pointer to a struct libscols_column instance
124 * @whint: a width hint
126 * Sets the width hint of column @cl to @whint. See scols_table_new_column().
128 * Returns: 0, a negative value in case of an error.
130 int scols_column_set_whint(struct libscols_column
*cl
, double whint
)
135 cl
->width_hint
= whint
;
140 * scols_column_get_whint:
141 * @cl: a pointer to a struct libscols_column instance
143 * Returns: The width hint of column @cl, a negative value in case of an error.
145 double scols_column_get_whint(const struct libscols_column
*cl
)
147 return cl
->width_hint
;
151 * scols_column_set_flags:
152 * @cl: a pointer to a struct libscols_column instance
153 * @flags: a flag mask
155 * Sets the flags of @cl to @flags.
157 * Returns: 0, a negative value in case of an error.
159 int scols_column_set_flags(struct libscols_column
*cl
, int flags
)
165 if (!(cl
->flags
& SCOLS_FL_TREE
) && (flags
& SCOLS_FL_TREE
))
166 cl
->table
->ntreecols
++;
167 else if ((cl
->flags
& SCOLS_FL_TREE
) && !(flags
& SCOLS_FL_TREE
))
168 cl
->table
->ntreecols
--;
171 DBG(COL
, ul_debugobj(cl
, "setting flags from 0%x to 0%x", cl
->flags
, flags
));
177 * scols_column_set_json_type:
178 * @cl: a pointer to a struct libscols_column instance
179 * @type: SCOLS_JSON_* type
181 * Sets the type used for JSON formatting, the default is SCOLS_JSON_STRING.
183 * Returns: 0, a negative value in case of an error.
187 int scols_column_set_json_type(struct libscols_column
*cl
, int type
)
192 cl
->json_type
= type
;
198 * scols_column_get_json_type:
199 * @cl: a pointer to a struct libscols_column instance
201 * Note that SCOLS_JSON_BOOLEAN interprets NULL, empty strings, '0', 'N' and
202 * 'n' as "false"; and everything else as "true".
204 * Returns: JSON type used for formatting or a negative value in case of an error.
208 int scols_column_get_json_type(const struct libscols_column
*cl
)
210 return cl
? cl
->json_type
: -EINVAL
;
215 * scols_column_get_table:
216 * @cl: a pointer to a struct libscols_column instance
218 * Returns: pointer to the table where columns is used
220 struct libscols_table
*scols_column_get_table(const struct libscols_column
*cl
)
226 * scols_column_get_flags:
227 * @cl: a pointer to a struct libscols_column instance
229 * Returns: The flag mask of @cl, a negative value in case of an error.
231 int scols_column_get_flags(const struct libscols_column
*cl
)
237 * scols_column_get_header:
238 * @cl: a pointer to a struct libscols_column instance
240 * Returns: A pointer to a struct libscols_cell instance, representing the
241 * header info of column @cl or NULL in case of an error.
243 struct libscols_cell
*scols_column_get_header(struct libscols_column
*cl
)
249 * scols_column_set_color:
250 * @cl: a pointer to a struct libscols_column instance
251 * @color: color name or ESC sequence
253 * The default color for data cells and column header.
255 * If you want to set header specific color then use scols_column_get_header()
256 * and scols_cell_set_color().
258 * If you want to set data cell specific color the use scols_line_get_cell() +
259 * scols_cell_set_color().
261 * Returns: 0, a negative value in case of an error.
263 int scols_column_set_color(struct libscols_column
*cl
, const char *color
)
265 if (color
&& isalpha(*color
)) {
266 color
= color_sequence_from_colorname(color
);
270 return strdup_to_struct_member(cl
, color
, color
);
274 * scols_column_get_color:
275 * @cl: a pointer to a struct libscols_column instance
277 * Returns: The current color setting of the column @cl.
279 const char *scols_column_get_color(const struct libscols_column
*cl
)
285 * scols_wrapnl_nextchunk:
286 * @cl: a pointer to a struct libscols_column instance
288 * @userdata: callback private data
290 * This is built-in function for scols_column_set_wrapfunc(). This function
291 * terminates the current chunk by \0 and returns pointer to the begin of
292 * the next chunk. The chunks are based on \n.
294 * For example for data "AAA\nBBB\nCCC" the next chunk is "BBB".
296 * Returns: next chunk
300 char *scols_wrapnl_nextchunk(const struct libscols_column
*cl
__attribute__((unused
)),
302 void *userdata
__attribute__((unused
)))
304 char *p
= data
? strchr(data
, '\n') : NULL
;
314 * scols_wrapnl_chunksize:
315 * @cl: a pointer to a struct libscols_column instance
317 * @userdata: callback private data
319 * Analyzes @data and returns size of the largest chunk. The chunks are based
320 * on \n. For example for data "AAA\nBBB\nCCCC" the largest chunk size is 4.
322 * Note that the size has to be based on number of terminal cells rather than
323 * bytes to support multu-byte output.
325 * Returns: size of the largest chunk.
329 size_t scols_wrapnl_chunksize(const struct libscols_column
*cl
__attribute__((unused
)),
331 void *userdata
__attribute__((unused
)))
335 while (data
&& *data
) {
339 p
= strchr(data
, '\n');
341 sz
= cl
->table
&& scols_table_is_noencoding(cl
->table
) ?
342 mbs_nwidth(data
, p
- data
) :
343 mbs_safe_nwidth(data
, p
- data
, NULL
);
346 sz
= cl
->table
&& scols_table_is_noencoding(cl
->table
) ?
348 mbs_safe_width(data
);
358 * scols_column_set_cmpfunc:
360 * @cmp: pointer to compare function
361 * @data: private data for cmp function
363 * Returns: 0, a negative value in case of an error.
365 int scols_column_set_cmpfunc(struct libscols_column
*cl
,
366 int (*cmp
)(struct libscols_cell
*,
367 struct libscols_cell
*,
375 cl
->cmpfunc_data
= data
;
380 * scols_column_set_wrapfunc:
381 * @cl: a pointer to a struct libscols_column instance
382 * @wrap_chunksize: function to return size of the largest chink of data
383 * @wrap_nextchunk: function to return next zero terminated data
384 * @userdata: optional stuff for callbacks
386 * Extends SCOLS_FL_WRAP and allows to set custom wrap function. The default
387 * is to wrap by column size, but you can create functions to wrap for example
388 * after \n or after words, etc.
390 * Returns: 0, a negative value in case of an error.
394 int scols_column_set_wrapfunc(struct libscols_column
*cl
,
395 size_t (*wrap_chunksize
)(const struct libscols_column
*,
398 char * (*wrap_nextchunk
)(const struct libscols_column
*,
406 cl
->wrap_nextchunk
= wrap_nextchunk
;
407 cl
->wrap_chunksize
= wrap_chunksize
;
408 cl
->wrapfunc_data
= userdata
;
413 * scols_column_set_safechars:
414 * @cl: a pointer to a struct libscols_column instance
415 * @safe: safe characters (e.g. "\n\t")
417 * Use for bytes you don't want to encode on output. This is for example
418 * necessary if you want to use custom wrap function based on \n, in this case
419 * you have to set "\n" as a safe char.
421 * Returns: 0, a negative value in case of an error.
425 int scols_column_set_safechars(struct libscols_column
*cl
, const char *safe
)
427 return strdup_to_struct_member(cl
, safechars
, safe
);
431 * scols_column_get_safechars:
432 * @cl: a pointer to a struct libscols_column instance
434 * Returns: safe chars
438 const char *scols_column_get_safechars(const struct libscols_column
*cl
)
440 return cl
->safechars
;
444 * scols_column_get_width:
445 * @cl: a pointer to a struct libscols_column instance
447 * Important note: the column width is unknown until library starts printing
448 * (width is calculated before printing). The function is usable for example in
449 * nextchunk() callback specified by scols_column_set_wrapfunc().
451 * See also scols_column_get_whint(), it returns wanted size (!= final size).
453 * Returns: column width
457 size_t scols_column_get_width(const struct libscols_column
*cl
)
463 * scols_column_is_hidden:
464 * @cl: a pointer to a struct libscols_column instance
466 * Gets the value of @cl's flag hidden.
472 int scols_column_is_hidden(const struct libscols_column
*cl
)
474 return cl
->flags
& SCOLS_FL_HIDDEN
? 1 : 0;
478 * scols_column_is_trunc:
479 * @cl: a pointer to a struct libscols_column instance
481 * Gets the value of @cl's flag trunc.
485 int scols_column_is_trunc(const struct libscols_column
*cl
)
487 return cl
->flags
& SCOLS_FL_TRUNC
? 1 : 0;
490 * scols_column_is_tree:
491 * @cl: a pointer to a struct libscols_column instance
493 * Gets the value of @cl's flag tree.
497 int scols_column_is_tree(const struct libscols_column
*cl
)
499 return cl
->flags
& SCOLS_FL_TREE
? 1 : 0;
502 * scols_column_is_right:
503 * @cl: a pointer to a struct libscols_column instance
505 * Gets the value of @cl's flag right.
509 int scols_column_is_right(const struct libscols_column
*cl
)
511 return cl
->flags
& SCOLS_FL_RIGHT
? 1 : 0;
514 * scols_column_is_strict_width:
515 * @cl: a pointer to a struct libscols_column instance
517 * Gets the value of @cl's flag strict_width.
521 int scols_column_is_strict_width(const struct libscols_column
*cl
)
523 return cl
->flags
& SCOLS_FL_STRICTWIDTH
? 1 : 0;
526 * scols_column_is_noextremes:
527 * @cl: a pointer to a struct libscols_column instance
529 * Gets the value of @cl's flag no_extremes.
533 int scols_column_is_noextremes(const struct libscols_column
*cl
)
535 return cl
->flags
& SCOLS_FL_NOEXTREMES
? 1 : 0;
538 * scols_column_is_wrap:
539 * @cl: a pointer to a struct libscols_column instance
541 * Gets the value of @cl's flag wrap.
547 int scols_column_is_wrap(const struct libscols_column
*cl
)
549 return cl
->flags
& SCOLS_FL_WRAP
? 1 : 0;
552 * scols_column_is_customwrap:
553 * @cl: a pointer to a struct libscols_column instance
559 int scols_column_is_customwrap(const struct libscols_column
*cl
)
561 return (cl
->flags
& SCOLS_FL_WRAP
)
562 && cl
->wrap_chunksize
563 && cl
->wrap_nextchunk
? 1 : 0;