]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/cell.c
2 * cell.c - functions for table handling at the cell 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: container for your data
16 * An API to access and modify per-cell data and information. Note that cell is
17 * always part of the line. If you destroy (un-reference) a line than it
18 * destroys all line cells too.
27 #include "smartcolsP.h"
30 * The cell has no ref-counting, free() and new() functions. All is
31 * handled by libscols_line.
36 * @ce: pointer to a struct libscols_cell instance
38 * Frees the cell's internal data and resets its status.
40 * Returns: 0, a negative value in case of an error.
42 int scols_reset_cell(struct libscols_cell
*ce
)
47 /*DBG(CELL, ul_debugobj(ce, "reset"));*/
50 memset(ce
, 0, sizeof(*ce
));
55 * scols_cell_set_data:
56 * @ce: a pointer to a struct libscols_cell instance
57 * @str: data (used for scols_print_table())
59 * Stores a copy of the @str in @ce.
61 * Returns: 0, a negative value in case of an error.
63 int scols_cell_set_data(struct libscols_cell
*ce
, const char *str
)
80 * scols_cell_refer_data:
81 * @ce: a pointer to a struct libscols_cell instance
82 * @str: data (used for scols_print_table())
84 * Adds a reference to @str to @ce. The pointer is deallocated by
85 * scols_reset_cell() or scols_unref_line(). This function is mostly designed
86 * for situations when the data for the cell are already composed in allocated
87 * memory (e.g. asprintf()) to avoid extra unnecessary strdup().
89 * Returns: 0, a negative value in case of an error.
91 int scols_cell_refer_data(struct libscols_cell
*ce
, char *str
)
101 * scols_cell_get_data:
102 * @ce: a pointer to a struct libscols_cell instance
104 * Returns: data in @ce or NULL.
106 const char *scols_cell_get_data(const struct libscols_cell
*ce
)
108 return ce
? ce
->data
: NULL
;
112 * scols_cell_set_userdata:
113 * @ce: a pointer to a struct libscols_cell instance
114 * @data: private user data
116 * Returns: 0, a negative value in case of an error.
118 int scols_cell_set_userdata(struct libscols_cell
*ce
, void *data
)
127 * scols_cell_get_userdata
128 * @ce: a pointer to a struct libscols_cell instance
132 void *scols_cell_get_userdata(struct libscols_cell
*ce
)
134 return ce
? ce
->userdata
: NULL
;
138 * scols_cmpstr_cells:
139 * @a: pointer to cell
140 * @b: pointer to cell
141 * @data: unused pointer to private data (defined by API)
143 * Compares cells data by strcmp(). The function is designed for
144 * scols_column_set_cmpfunc() and scols_sort_table().
146 * Returns: follows strcmp() return values.
148 int scols_cmpstr_cells(struct libscols_cell
*a
,
149 struct libscols_cell
*b
,
150 __attribute__((__unused__
)) void *data
)
152 const char *adata
, *bdata
;
157 adata
= scols_cell_get_data(a
);
158 bdata
= scols_cell_get_data(b
);
160 if (adata
== NULL
&& bdata
== NULL
)
166 return strcmp(adata
, bdata
);
170 * scols_cell_set_color:
171 * @ce: a pointer to a struct libscols_cell instance
172 * @color: color name or ESC sequence
174 * Set the color of @ce to @color.
176 * Returns: 0, a negative value in case of an error.
178 int scols_cell_set_color(struct libscols_cell
*ce
, const char *color
)
185 if (isalpha(*color
)) {
186 color
= color_sequence_from_colorname(color
);
201 * scols_cell_get_color:
202 * @ce: a pointer to a struct libscols_cell instance
204 * Returns: the current color of @ce.
206 const char *scols_cell_get_color(const struct libscols_cell
*ce
)
208 return ce
? ce
->color
: NULL
;
212 * scols_cell_set_flags:
213 * @ce: a pointer to a struct libscols_cell instance
214 * @flags: SCOLS_CELL_FL_* flags
216 * Note that cells in the table are always aligned by column flags. The cell
217 * flags are used for table title only (now).
219 * Returns: 0, a negative value in case of an error.
221 int scols_cell_set_flags(struct libscols_cell
*ce
, int flags
)
230 * scols_cell_get_flags:
231 * @ce: a pointer to a struct libscols_cell instance
233 * Returns: the current flags or -1 in case of an error.
235 int scols_cell_get_flags(const struct libscols_cell
*ce
)
237 return ce
? ce
->flags
: -1;
241 * scols_cell_copy_content:
242 * @dest: a pointer to a struct libscols_cell instance
243 * @src: a pointer to an immutable struct libscols_cell instance
245 * Copy the contents of @src into @dest.
247 * Returns: 0, a negative value in case of an error.
249 int scols_cell_copy_content(struct libscols_cell
*dest
,
250 const struct libscols_cell
*src
)
254 rc
= scols_cell_set_data(dest
, scols_cell_get_data(src
));
256 rc
= scols_cell_set_color(dest
, scols_cell_get_color(src
));
258 dest
->userdata
= src
->userdata
;
260 DBG(CELL
, ul_debugobj((void *) src
, "copy into %p", dest
));