]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/cell.c
libsmartcols: use libscols_cell for title
[thirdparty/util-linux.git] / libsmartcols / src / cell.c
1 /*
2 * cell.c - functions for table handling at the cell level
3 *
4 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
6 *
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 */
10
11 /**
12 * SECTION: cell
13 * @title: Cell
14 * @short_description: container for your data
15 *
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.
19 */
20
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #include "smartcolsP.h"
28
29 /*
30 * The cell has no ref-counting, free() and new() functions. All is
31 * handled by libscols_line.
32 */
33
34 /**
35 * scols_reset_cell:
36 * @ce: pointer to a struct libscols_cell instance
37 *
38 * Frees the cell's internal data and resets its status.
39 *
40 * Returns: 0, a negative value in case of an error.
41 */
42 int scols_reset_cell(struct libscols_cell *ce)
43 {
44 if (!ce)
45 return -EINVAL;
46
47 /*DBG(CELL, ul_debugobj(ce, "reset"));*/
48 free(ce->data);
49 free(ce->color);
50 memset(ce, 0, sizeof(*ce));
51 return 0;
52 }
53
54 /**
55 * scols_cell_set_data:
56 * @ce: a pointer to a struct libscols_cell instance
57 * @str: data (used for scols_print_table())
58 *
59 * Stores a copy of the @str in @ce.
60 *
61 * Returns: 0, a negative value in case of an error.
62 */
63 int scols_cell_set_data(struct libscols_cell *ce, const char *str)
64 {
65 char *p = NULL;
66
67 if (!ce)
68 return -EINVAL;
69 if (str) {
70 p = strdup(str);
71 if (!p)
72 return -ENOMEM;
73 }
74 free(ce->data);
75 ce->data = p;
76 return 0;
77 }
78
79 /**
80 * scols_cell_refer_data:
81 * @ce: a pointer to a struct libscols_cell instance
82 * @str: data (used for scols_print_table())
83 *
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().
88 *
89 * Returns: 0, a negative value in case of an error.
90 */
91 int scols_cell_refer_data(struct libscols_cell *ce, char *str)
92 {
93 if (!ce)
94 return -EINVAL;
95 free(ce->data);
96 ce->data = str;
97 return 0;
98 }
99
100 /**
101 * scols_cell_get_data:
102 * @ce: a pointer to a struct libscols_cell instance
103 *
104 * Returns: data in @ce or NULL.
105 */
106 const char *scols_cell_get_data(const struct libscols_cell *ce)
107 {
108 return ce ? ce->data : NULL;
109 }
110
111 /**
112 * scols_cell_set_userdata:
113 * @ce: a pointer to a struct libscols_cell instance
114 * @data: private user data
115 *
116 * Returns: 0, a negative value in case of an error.
117 */
118 int scols_cell_set_userdata(struct libscols_cell *ce, void *data)
119 {
120 if (!ce)
121 return -EINVAL;
122 ce->userdata = data;
123 return 0;
124 }
125
126 /**
127 * scols_cell_get_userdata
128 * @ce: a pointer to a struct libscols_cell instance
129 *
130 * Returns: user data
131 */
132 void *scols_cell_get_userdata(struct libscols_cell *ce)
133 {
134 return ce ? ce->userdata : NULL;
135 }
136
137 /**
138 * scols_cmpstr_cells:
139 * @a: pointer to cell
140 * @b: pointer to cell
141 * @data: unused pointer to private data (defined by API)
142 *
143 * Compares cells data by strcmp(). The function is designed for
144 * scols_column_set_cmpfunc() and scols_sort_table().
145 *
146 * Returns: follows strcmp() return values.
147 */
148 int scols_cmpstr_cells(struct libscols_cell *a,
149 struct libscols_cell *b,
150 __attribute__((__unused__)) void *data)
151 {
152 const char *adata, *bdata;
153
154 if (a == b)
155 return 0;
156
157 adata = scols_cell_get_data(a);
158 bdata = scols_cell_get_data(b);
159
160 if (adata == NULL && bdata == NULL)
161 return 0;
162 if (adata == NULL)
163 return -1;
164 if (bdata == NULL)
165 return 1;
166 return strcmp(adata, bdata);
167 }
168
169 /**
170 * scols_cell_set_color:
171 * @ce: a pointer to a struct libscols_cell instance
172 * @color: color name or ESC sequence
173 *
174 * Set the color of @ce to @color.
175 *
176 * Returns: 0, a negative value in case of an error.
177 */
178 int scols_cell_set_color(struct libscols_cell *ce, const char *color)
179 {
180 char *p = NULL;
181
182 if (!ce)
183 return -EINVAL;
184 if (color) {
185 if (isalpha(*color)) {
186 color = color_sequence_from_colorname(color);
187
188 if (!color)
189 return -EINVAL;
190 }
191 p = strdup(color);
192 if (!p)
193 return -ENOMEM;
194 }
195 free(ce->color);
196 ce->color = p;
197 return 0;
198 }
199
200 /**
201 * scols_cell_get_color:
202 * @ce: a pointer to a struct libscols_cell instance
203 *
204 * Returns: the current color of @ce.
205 */
206 const char *scols_cell_get_color(const struct libscols_cell *ce)
207 {
208 return ce ? ce->color : NULL;
209 }
210
211 /**
212 * scols_cell_set_flags:
213 * @ce: a pointer to a struct libscols_cell instance
214 * @flags: SCOLS_CELL_FL_* flags
215 *
216 * Note that cells in the table are always aligned by column flags. The cell
217 * flags are used for table title only (now).
218 *
219 * Returns: 0, a negative value in case of an error.
220 */
221 int scols_cell_set_flags(struct libscols_cell *ce, int flags)
222 {
223 if (!ce)
224 return -EINVAL;
225 ce->flags = flags;
226 return 0;
227 }
228
229 /**
230 * scols_cell_get_flags:
231 * @ce: a pointer to a struct libscols_cell instance
232 *
233 * Returns: the current flags or -1 in case of an error.
234 */
235 int scols_cell_get_flags(const struct libscols_cell *ce)
236 {
237 return ce ? ce->flags : -1;
238 }
239
240 /**
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
244 *
245 * Copy the contents of @src into @dest.
246 *
247 * Returns: 0, a negative value in case of an error.
248 */
249 int scols_cell_copy_content(struct libscols_cell *dest,
250 const struct libscols_cell *src)
251 {
252 int rc;
253
254 rc = scols_cell_set_data(dest, scols_cell_get_data(src));
255 if (!rc)
256 rc = scols_cell_set_color(dest, scols_cell_get_color(src));
257 if (!rc)
258 dest->userdata = src->userdata;
259
260 DBG(CELL, ul_debugobj((void *) src, "copy into %p", dest));
261 return rc;
262 }