]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libsmartcols/src/cell.c
04e9bfc203eae9992f6b5c83cbe4aa3c4e71e65d
[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 * @data: data (used for scols_print_table())
58 *
59 * Stores a copy of the @str in @ce, the old data are deallocated by free().
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 *data)
64 {
65 return strdup_to_struct_member(ce, data, data);
66 }
67
68 /**
69 * scols_cell_refer_data:
70 * @ce: a pointer to a struct libscols_cell instance
71 * @data: data (used for scols_print_table())
72 *
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().
77 *
78 * Returns: 0, a negative value in case of an error.
79 */
80 int scols_cell_refer_data(struct libscols_cell *ce, char *data)
81 {
82 if (!ce)
83 return -EINVAL;
84 free(ce->data);
85 ce->data = data;
86 return 0;
87 }
88
89 /**
90 * scols_cell_get_data:
91 * @ce: a pointer to a struct libscols_cell instance
92 *
93 * Returns: data in @ce or NULL.
94 */
95 const char *scols_cell_get_data(const struct libscols_cell *ce)
96 {
97 return ce ? ce->data : NULL;
98 }
99
100 /**
101 * scols_cell_set_userdata:
102 * @ce: a pointer to a struct libscols_cell instance
103 * @data: private user data
104 *
105 * Returns: 0, a negative value in case of an error.
106 */
107 int scols_cell_set_userdata(struct libscols_cell *ce, void *data)
108 {
109 if (!ce)
110 return -EINVAL;
111 ce->userdata = data;
112 return 0;
113 }
114
115 /**
116 * scols_cell_get_userdata
117 * @ce: a pointer to a struct libscols_cell instance
118 *
119 * Returns: user data
120 */
121 void *scols_cell_get_userdata(struct libscols_cell *ce)
122 {
123 return ce->userdata;
124 }
125
126 /**
127 * scols_cmpstr_cells:
128 * @a: pointer to cell
129 * @b: pointer to cell
130 * @data: unused pointer to private data (defined by API)
131 *
132 * Compares cells data by strcmp(). The function is designed for
133 * scols_column_set_cmpfunc() and scols_sort_table().
134 *
135 * Returns: follows strcmp() return values.
136 */
137 int scols_cmpstr_cells(struct libscols_cell *a,
138 struct libscols_cell *b,
139 __attribute__((__unused__)) void *data)
140 {
141 const char *adata, *bdata;
142
143 if (a == b)
144 return 0;
145
146 adata = scols_cell_get_data(a);
147 bdata = scols_cell_get_data(b);
148
149 if (adata == NULL && bdata == NULL)
150 return 0;
151 if (adata == NULL)
152 return -1;
153 if (bdata == NULL)
154 return 1;
155 return strcmp(adata, bdata);
156 }
157
158 /**
159 * scols_cell_set_color:
160 * @ce: a pointer to a struct libscols_cell instance
161 * @color: color name or ESC sequence
162 *
163 * Set the color of @ce to @color.
164 *
165 * Returns: 0, a negative value in case of an error.
166 */
167 int scols_cell_set_color(struct libscols_cell *ce, const char *color)
168 {
169 if (color && isalpha(*color)) {
170 color = color_sequence_from_colorname(color);
171 if (!color)
172 return -EINVAL;
173 }
174 return strdup_to_struct_member(ce, color, color);
175 }
176
177 /**
178 * scols_cell_get_color:
179 * @ce: a pointer to a struct libscols_cell instance
180 *
181 * Returns: the current color of @ce.
182 */
183 const char *scols_cell_get_color(const struct libscols_cell *ce)
184 {
185 return ce->color;
186 }
187
188 /**
189 * scols_cell_set_flags:
190 * @ce: a pointer to a struct libscols_cell instance
191 * @flags: SCOLS_CELL_FL_* flags
192 *
193 * Note that cells in the table are always aligned by column flags. The cell
194 * flags are used for table title only (now).
195 *
196 * Returns: 0, a negative value in case of an error.
197 */
198 int scols_cell_set_flags(struct libscols_cell *ce, int flags)
199 {
200 if (!ce)
201 return -EINVAL;
202 ce->flags = flags;
203 return 0;
204 }
205
206 /**
207 * scols_cell_get_flags:
208 * @ce: a pointer to a struct libscols_cell instance
209 *
210 * Returns: the current flags
211 */
212 int scols_cell_get_flags(const struct libscols_cell *ce)
213 {
214 return ce->flags;
215 }
216
217 /**
218 * scols_cell_get_alignment:
219 * @ce: a pointer to a struct libscols_cell instance
220 *
221 * Returns: SCOLS_CELL_FL_{RIGHT,CELNTER,LEFT}
222 */
223 int scols_cell_get_alignment(const struct libscols_cell *ce)
224 {
225 if (ce->flags & SCOLS_CELL_FL_RIGHT)
226 return SCOLS_CELL_FL_RIGHT;
227 else if (ce->flags & SCOLS_CELL_FL_CENTER)
228 return SCOLS_CELL_FL_CENTER;
229
230 return SCOLS_CELL_FL_LEFT; /* default */
231 }
232
233 /**
234 * scols_cell_copy_content:
235 * @dest: a pointer to a struct libscols_cell instance
236 * @src: a pointer to an immutable struct libscols_cell instance
237 *
238 * Copy the contents of @src into @dest.
239 *
240 * Returns: 0, a negative value in case of an error.
241 */
242 int scols_cell_copy_content(struct libscols_cell *dest,
243 const struct libscols_cell *src)
244 {
245 int rc;
246
247 rc = scols_cell_set_data(dest, scols_cell_get_data(src));
248 if (!rc)
249 rc = scols_cell_set_color(dest, scols_cell_get_color(src));
250 if (!rc)
251 dest->userdata = src->userdata;
252
253 DBG(CELL, ul_debugobj(src, "copy into %p", dest));
254 return rc;
255 }