]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/cell.c
783bac2ebbfe61274cb4e13704b6cd72a058aa6e
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 * @data: data (used for scols_print_table())
59 * Stores a copy of the @str in @ce, the old data are deallocated by free().
61 * Returns: 0, a negative value in case of an error.
63 int scols_cell_set_data(struct libscols_cell
*ce
, const char *data
)
65 return strdup_to_struct_member(ce
, data
, data
);
69 * scols_cell_refer_data:
70 * @ce: a pointer to a struct libscols_cell instance
71 * @data: data (used for scols_print_table())
73 * Adds a reference to @str to @ce. The pointer is deallocated by
74 * scols_reset_cell() or scols_unref_line(). This function is mostly designed
75 * for situations when the data for the cell are already composed in allocated
76 * memory (e.g. asprintf()) to avoid extra unnecessary strdup().
78 * Returns: 0, a negative value in case of an error.
80 int scols_cell_refer_data(struct libscols_cell
*ce
, char *data
)
90 * scols_cell_get_data:
91 * @ce: a pointer to a struct libscols_cell instance
93 * Returns: data in @ce or NULL.
95 const char *scols_cell_get_data(const struct libscols_cell
*ce
)
97 return ce
? ce
->data
: NULL
;
101 * scols_cell_set_userdata:
102 * @ce: a pointer to a struct libscols_cell instance
103 * @data: private user data
105 * Returns: 0, a negative value in case of an error.
107 int scols_cell_set_userdata(struct libscols_cell
*ce
, void *data
)
116 * scols_cell_get_userdata
117 * @ce: a pointer to a struct libscols_cell instance
121 void *scols_cell_get_userdata(struct libscols_cell
*ce
)
127 * scols_cmpstr_cells:
128 * @a: pointer to cell
129 * @b: pointer to cell
130 * @data: unused pointer to private data (defined by API)
132 * Compares cells data by strcmp(). The function is designed for
133 * scols_column_set_cmpfunc() and scols_sort_table().
135 * Returns: follows strcmp() return values.
137 int scols_cmpstr_cells(struct libscols_cell
*a
,
138 struct libscols_cell
*b
,
139 __attribute__((__unused__
)) void *data
)
141 const char *adata
, *bdata
;
146 adata
= scols_cell_get_data(a
);
147 bdata
= scols_cell_get_data(b
);
149 if (adata
== NULL
&& bdata
== NULL
)
155 return strcmp(adata
, bdata
);
159 * scols_cell_set_color:
160 * @ce: a pointer to a struct libscols_cell instance
161 * @color: color name or ESC sequence
163 * Set the color of @ce to @color.
165 * Returns: 0, a negative value in case of an error.
167 int scols_cell_set_color(struct libscols_cell
*ce
, const char *color
)
169 if (color
&& isalpha(*color
)) {
170 color
= color_sequence_from_colorname(color
);
174 return strdup_to_struct_member(ce
, color
, color
);
178 * scols_cell_get_color:
179 * @ce: a pointer to a struct libscols_cell instance
181 * Returns: the current color of @ce.
183 const char *scols_cell_get_color(const struct libscols_cell
*ce
)
189 * scols_cell_set_flags:
190 * @ce: a pointer to a struct libscols_cell instance
191 * @flags: SCOLS_CELL_FL_* flags
193 * Note that cells in the table are always aligned by column flags. The cell
194 * flags are used for table title only (now).
196 * Returns: 0, a negative value in case of an error.
198 int scols_cell_set_flags(struct libscols_cell
*ce
, int flags
)
207 * scols_cell_get_flags:
208 * @ce: a pointer to a struct libscols_cell instance
210 * Returns: the current flags
212 int scols_cell_get_flags(const struct libscols_cell
*ce
)
218 * scols_cell_get_alignment:
219 * @ce: a pointer to a struct libscols_cell instance
223 * Returns: SCOLS_CELL_FL_{RIGHT,CELNTER,LEFT}
225 int scols_cell_get_alignment(const struct libscols_cell
*ce
)
227 if (ce
->flags
& SCOLS_CELL_FL_RIGHT
)
228 return SCOLS_CELL_FL_RIGHT
;
229 else if (ce
->flags
& SCOLS_CELL_FL_CENTER
)
230 return SCOLS_CELL_FL_CENTER
;
232 return SCOLS_CELL_FL_LEFT
; /* default */
236 * scols_cell_copy_content:
237 * @dest: a pointer to a struct libscols_cell instance
238 * @src: a pointer to an immutable struct libscols_cell instance
240 * Copy the contents of @src into @dest.
242 * Returns: 0, a negative value in case of an error.
244 int scols_cell_copy_content(struct libscols_cell
*dest
,
245 const struct libscols_cell
*src
)
249 rc
= scols_cell_set_data(dest
, scols_cell_get_data(src
));
251 rc
= scols_cell_set_color(dest
, scols_cell_get_color(src
));
253 dest
->userdata
= src
->userdata
;
255 DBG(CELL
, ul_debugobj(src
, "copy into %p", dest
));