| 1 |
/* |
| 2 |
* cell.c - functions for table handling at the cell level |
| 3 |
* |
| 4 |
* Copyright (C) 2014 Ondrej Oprala <[email protected]> |
| 5 |
* Copyright (C) 2014 Karel Zak <[email protected]> |
| 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 |
free(ce->uri); |
| 51 |
memset(ce, 0, sizeof(*ce)); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* scols_cell_set_data: |
| 57 |
* @ce: a pointer to a struct libscols_cell instance |
| 58 |
* @data: data (used for scols_print_table()) |
| 59 |
* |
| 60 |
* Stores a copy of the @data in @ce, the old data are deallocated by free(). |
| 61 |
* |
| 62 |
* Returns: 0, a negative value in case of an error. |
| 63 |
*/ |
| 64 |
int scols_cell_set_data(struct libscols_cell *ce, const char *data) |
| 65 |
{ |
| 66 |
int rc; |
| 67 |
|
| 68 |
if (!ce) |
| 69 |
return -EINVAL; |
| 70 |
|
| 71 |
ce->is_filled = 1; |
| 72 |
rc = strdup_to_struct_member(ce, data, data); |
| 73 |
ce->datasiz = ce->data && *ce->data ? strlen(ce->data) + 1: 0; |
| 74 |
return rc; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* scols_cell_refer_data: |
| 79 |
* @ce: a pointer to a struct libscols_cell instance |
| 80 |
* @data: string (used for scols_print_table()) |
| 81 |
* |
| 82 |
* Adds a reference to @data to @ce. The pointer is deallocated by |
| 83 |
* scols_reset_cell() or scols_unref_line() by free(). This function is mostly |
| 84 |
* designed for situations when the data for the cell are already composed in |
| 85 |
* allocated memory (e.g. asprintf()) to avoid extra unnecessary strdup(). |
| 86 |
* |
| 87 |
* Returns: 0, a negative value in case of an error. |
| 88 |
*/ |
| 89 |
int scols_cell_refer_data(struct libscols_cell *ce, char *data) |
| 90 |
{ |
| 91 |
if (!ce) |
| 92 |
return -EINVAL; |
| 93 |
free(ce->data); |
| 94 |
ce->data = data; |
| 95 |
ce->datasiz = ce->data && *ce->data ? strlen(ce->data) + 1: 0; |
| 96 |
ce->is_filled = 1; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* scols_cell_refer_memory: |
| 102 |
* @ce: a pointer to a struct libscols_cell instance |
| 103 |
* @data: data |
| 104 |
* @datasiz: size of the data |
| 105 |
* |
| 106 |
* Same like scols_cell_refer_data, but @data does not have to be zero terminated. |
| 107 |
* The pointer is deallocated by scols_reset_cell() or scols_unref_line() by free(). |
| 108 |
* |
| 109 |
* The column (for the cell) has to define wrap function which converts the |
| 110 |
* data to zero terminated string, otherwise library will work with the data as |
| 111 |
* with string! |
| 112 |
* |
| 113 |
* Returns: 0, a negative value in case of an error. |
| 114 |
* |
| 115 |
* Since: 2.40 |
| 116 |
*/ |
| 117 |
int scols_cell_refer_memory(struct libscols_cell *ce, char *data, size_t datasiz) |
| 118 |
{ |
| 119 |
if (!ce) |
| 120 |
return -EINVAL; |
| 121 |
free(ce->data); |
| 122 |
ce->data = data; |
| 123 |
ce->datasiz = datasiz; |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* scols_cell_get_datasiz: |
| 129 |
* @ce: a pointer to a struct libscols_cell instance |
| 130 |
* |
| 131 |
* Returns: the current set data size. |
| 132 |
* |
| 133 |
* Since: 2.40 |
| 134 |
*/ |
| 135 |
size_t scols_cell_get_datasiz(struct libscols_cell *ce) |
| 136 |
{ |
| 137 |
return ce ? ce->datasiz : 0; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* scols_cell_get_data: |
| 142 |
* @ce: a pointer to a struct libscols_cell instance |
| 143 |
* |
| 144 |
* Returns: data in @ce or NULL. |
| 145 |
*/ |
| 146 |
const char *scols_cell_get_data(const struct libscols_cell *ce) |
| 147 |
{ |
| 148 |
return ce ? ce->data : NULL; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* scols_cell_set_userdata: |
| 153 |
* @ce: a pointer to a struct libscols_cell instance |
| 154 |
* @data: private user data |
| 155 |
* |
| 156 |
* Returns: 0, a negative value in case of an error. |
| 157 |
*/ |
| 158 |
int scols_cell_set_userdata(struct libscols_cell *ce, void *data) |
| 159 |
{ |
| 160 |
if (!ce) |
| 161 |
return -EINVAL; |
| 162 |
ce->userdata = data; |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* scols_cell_get_userdata |
| 168 |
* @ce: a pointer to a struct libscols_cell instance |
| 169 |
* |
| 170 |
* Returns: user data |
| 171 |
*/ |
| 172 |
void *scols_cell_get_userdata(struct libscols_cell *ce) |
| 173 |
{ |
| 174 |
return ce ? ce->userdata : NULL; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* scols_cmpstr_cells: |
| 179 |
* @a: pointer to cell |
| 180 |
* @b: pointer to cell |
| 181 |
* @data: unused pointer to private data (defined by API) |
| 182 |
* |
| 183 |
* Compares cells data by strcoll(). The function is designed for |
| 184 |
* scols_column_set_cmpfunc() and scols_sort_table(). |
| 185 |
* |
| 186 |
* Returns: follows strcoll() return values. |
| 187 |
*/ |
| 188 |
int scols_cmpstr_cells(struct libscols_cell *a, |
| 189 |
struct libscols_cell *b, |
| 190 |
__attribute__((__unused__)) void *data) |
| 191 |
{ |
| 192 |
const char *adata, *bdata; |
| 193 |
|
| 194 |
if (a == b) |
| 195 |
return 0; |
| 196 |
|
| 197 |
adata = scols_cell_get_data(a); |
| 198 |
bdata = scols_cell_get_data(b); |
| 199 |
|
| 200 |
if (adata == NULL && bdata == NULL) |
| 201 |
return 0; |
| 202 |
if (adata == NULL) |
| 203 |
return -1; |
| 204 |
if (bdata == NULL) |
| 205 |
return 1; |
| 206 |
return strcoll(adata, bdata); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* scols_cell_set_color: |
| 211 |
* @ce: a pointer to a struct libscols_cell instance |
| 212 |
* @color: color name or ESC sequence |
| 213 |
* |
| 214 |
* Set the color of @ce to @color. |
| 215 |
* |
| 216 |
* Returns: 0, a negative value in case of an error. |
| 217 |
*/ |
| 218 |
int scols_cell_set_color(struct libscols_cell *ce, const char *color) |
| 219 |
{ |
| 220 |
if (!ce) |
| 221 |
return -EINVAL; |
| 222 |
|
| 223 |
if (color && !color_is_sequence(color)) { |
| 224 |
char *seq = color_get_sequence(color); |
| 225 |
if (!seq) |
| 226 |
return -EINVAL; |
| 227 |
free(ce->color); |
| 228 |
ce->color = seq; |
| 229 |
return 0; |
| 230 |
} |
| 231 |
return strdup_to_struct_member(ce, color, color); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* scols_cell_get_color: |
| 236 |
* @ce: a pointer to a struct libscols_cell instance |
| 237 |
* |
| 238 |
* Returns: the current color of @ce. |
| 239 |
*/ |
| 240 |
const char *scols_cell_get_color(const struct libscols_cell *ce) |
| 241 |
{ |
| 242 |
if (!ce) |
| 243 |
return NULL; |
| 244 |
|
| 245 |
return ce->color; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* scols_cell_set_uri: |
| 250 |
* @ce: a pointer to a struct libscols_cell instance |
| 251 |
* @uri: URI string |
| 252 |
* |
| 253 |
* Set the URI of @ce to @uri. |
| 254 |
* |
| 255 |
* Returns: 0, a negative value in case of an error. |
| 256 |
* |
| 257 |
* Since: 2.41 |
| 258 |
*/ |
| 259 |
int scols_cell_set_uri(struct libscols_cell *ce, const char *uri) |
| 260 |
{ |
| 261 |
if (!ce) |
| 262 |
return -EINVAL; |
| 263 |
|
| 264 |
return strdup_to_struct_member(ce, uri, uri); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* scols_cell_get_uri: |
| 269 |
* @ce: a pointer to a struct libscols_cell instance |
| 270 |
* |
| 271 |
* The function returns the URI setting, but it may not necessarily be the final |
| 272 |
* URI used in the output. This is because the column may define a URI prefix or |
| 273 |
* the cell content may be used as part of the URI. |
| 274 |
* |
| 275 |
* Returns: the current URI of @ce. |
| 276 |
* |
| 277 |
* Since: 2.41 |
| 278 |
*/ |
| 279 |
const char *scols_cell_get_uri(const struct libscols_cell *ce) |
| 280 |
{ |
| 281 |
if (!ce) |
| 282 |
return NULL; |
| 283 |
|
| 284 |
return ce->uri; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* scols_cell_disable_uri: |
| 289 |
* @ce: a pointer to a struct libscols_cell instance |
| 290 |
* @disable: 1 or 0 |
| 291 |
* |
| 292 |
* Force the library to ignore the cell and column URI setting and print the |
| 293 |
* content as a regular string. |
| 294 |
* |
| 295 |
* Returns: 0, a negative value in case of an error. |
| 296 |
* |
| 297 |
* Since: 2.41 |
| 298 |
*/ |
| 299 |
int scols_cell_disable_uri(struct libscols_cell *ce, int disable) |
| 300 |
{ |
| 301 |
if (!ce) |
| 302 |
return -EINVAL; |
| 303 |
ce->no_uri = disable ? 1 : 0; |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* scols_cell_set_flags: |
| 309 |
* @ce: a pointer to a struct libscols_cell instance |
| 310 |
* @flags: SCOLS_CELL_FL_* flags |
| 311 |
* |
| 312 |
* Note that cells in the table are always aligned by column flags. The cell |
| 313 |
* flags are used for table title only (now). |
| 314 |
* |
| 315 |
* Returns: 0, a negative value in case of an error. |
| 316 |
*/ |
| 317 |
int scols_cell_set_flags(struct libscols_cell *ce, int flags) |
| 318 |
{ |
| 319 |
if (!ce) |
| 320 |
return -EINVAL; |
| 321 |
ce->flags = flags; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* scols_cell_get_flags: |
| 327 |
* @ce: a pointer to a struct libscols_cell instance |
| 328 |
* |
| 329 |
* Returns: the current flags |
| 330 |
*/ |
| 331 |
int scols_cell_get_flags(const struct libscols_cell *ce) |
| 332 |
{ |
| 333 |
return ce ? ce->flags : 0; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* scols_cell_get_alignment: |
| 338 |
* @ce: a pointer to a struct libscols_cell instance |
| 339 |
* |
| 340 |
* Since: 2.30 |
| 341 |
* |
| 342 |
* Returns: SCOLS_CELL_FL_{RIGHT,CELNTER,LEFT} |
| 343 |
*/ |
| 344 |
int scols_cell_get_alignment(const struct libscols_cell *ce) |
| 345 |
{ |
| 346 |
int flags = scols_cell_get_flags(ce); |
| 347 |
|
| 348 |
if (flags & SCOLS_CELL_FL_RIGHT) |
| 349 |
return SCOLS_CELL_FL_RIGHT; |
| 350 |
if (flags & SCOLS_CELL_FL_CENTER) |
| 351 |
return SCOLS_CELL_FL_CENTER; |
| 352 |
|
| 353 |
return SCOLS_CELL_FL_LEFT; /* default */ |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* scols_cell_copy_content: |
| 358 |
* @dest: a pointer to a struct libscols_cell instance |
| 359 |
* @src: a pointer to an immutable struct libscols_cell instance |
| 360 |
* |
| 361 |
* Copy the contents (data, usewrdata, colors) of @src into @dest. |
| 362 |
* |
| 363 |
* Returns: 0, a negative value in case of an error. |
| 364 |
*/ |
| 365 |
int scols_cell_copy_content(struct libscols_cell *dest, |
| 366 |
const struct libscols_cell *src) |
| 367 |
{ |
| 368 |
int rc; |
| 369 |
char *data = NULL; |
| 370 |
|
| 371 |
if (!dest || !src) |
| 372 |
return -EINVAL; |
| 373 |
|
| 374 |
if (src->datasiz) { |
| 375 |
data = malloc(src->datasiz); |
| 376 |
if (!data) |
| 377 |
return -ENOMEM; |
| 378 |
memcpy(data, src->data, src->datasiz); |
| 379 |
} |
| 380 |
|
| 381 |
rc = scols_cell_refer_memory(dest, data, src->datasiz); |
| 382 |
if (!rc) |
| 383 |
rc = scols_cell_set_color(dest, scols_cell_get_color(src)); |
| 384 |
if (!rc) |
| 385 |
rc = scols_cell_set_uri(dest, scols_cell_get_uri(src)); |
| 386 |
if (!rc) |
| 387 |
dest->userdata = src->userdata; |
| 388 |
|
| 389 |
DBG(CELL, ul_debugobj(src, "copy")); |
| 390 |
return rc; |
| 391 |
} |