| 1 |
/* |
| 2 |
* column.c - functions for table handling at the column 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: column |
| 13 |
* @title: Column |
| 14 |
* @short_description: defines output columns formats, headers, etc. |
| 15 |
* |
| 16 |
* An API to access and modify per-column data and information. |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <string.h> |
| 23 |
#include <ctype.h> |
| 24 |
|
| 25 |
#include "mbsalign.h" |
| 26 |
#include "strutils.h" |
| 27 |
#include "smartcolsP.h" |
| 28 |
|
| 29 |
/** |
| 30 |
* scols_new_column: |
| 31 |
* |
| 32 |
* Allocates space for a new column. |
| 33 |
* |
| 34 |
* Returns: a pointer to a new struct libscols_column instance, NULL in case of an ENOMEM error. |
| 35 |
*/ |
| 36 |
struct libscols_column *scols_new_column(void) |
| 37 |
{ |
| 38 |
struct libscols_column *cl; |
| 39 |
|
| 40 |
cl = calloc(1, sizeof(*cl)); |
| 41 |
if (!cl) |
| 42 |
return NULL; |
| 43 |
DBG(COL, ul_debugobj(cl, "alloc")); |
| 44 |
cl->refcount = 1; |
| 45 |
INIT_LIST_HEAD(&cl->cl_columns); |
| 46 |
return cl; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* scols_ref_column: |
| 51 |
* @cl: a pointer to a struct libscols_column instance |
| 52 |
* |
| 53 |
* Increases the refcount of @cl. |
| 54 |
*/ |
| 55 |
void scols_ref_column(struct libscols_column *cl) |
| 56 |
{ |
| 57 |
if (cl) |
| 58 |
cl->refcount++; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* scols_unref_column: |
| 63 |
* @cl: a pointer to a struct libscols_column instance |
| 64 |
* |
| 65 |
* Decreases the refcount of @cl. When the count falls to zero, the instance |
| 66 |
* is automatically deallocated. |
| 67 |
*/ |
| 68 |
void scols_unref_column(struct libscols_column *cl) |
| 69 |
{ |
| 70 |
if (cl && --cl->refcount <= 0) { |
| 71 |
DBG(COL, ul_debugobj(cl, "dealloc")); |
| 72 |
list_del(&cl->cl_columns); |
| 73 |
scols_reset_cell(&cl->header); |
| 74 |
free(cl->color); |
| 75 |
free(cl->uri); |
| 76 |
ul_buffer_free_data(&cl->uri_buf); |
| 77 |
free(cl->safechars); |
| 78 |
free(cl->wrap_data); |
| 79 |
free(cl->shellvar); |
| 80 |
free(cl); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* scols_copy_column: |
| 86 |
* @cl: a pointer to a struct libscols_column instance |
| 87 |
* |
| 88 |
* Creates a new column and copies @cl's data over to it. |
| 89 |
* |
| 90 |
* Returns: a pointer to a new struct libscols_column instance. |
| 91 |
*/ |
| 92 |
struct libscols_column *scols_copy_column(const struct libscols_column *cl) |
| 93 |
{ |
| 94 |
struct libscols_column *ret; |
| 95 |
|
| 96 |
if (!cl) |
| 97 |
return NULL; |
| 98 |
ret = scols_new_column(); |
| 99 |
if (!ret) |
| 100 |
return NULL; |
| 101 |
|
| 102 |
DBG(COL, ul_debugobj(cl, "copy")); |
| 103 |
|
| 104 |
if (scols_column_set_color(ret, cl->color)) |
| 105 |
goto err; |
| 106 |
if (scols_column_set_uri(ret, cl->uri)) |
| 107 |
goto err; |
| 108 |
if (scols_cell_copy_content(&ret->header, &cl->header)) |
| 109 |
goto err; |
| 110 |
if (scols_column_refer_annotation(ret, cl->annotation)) |
| 111 |
goto err; |
| 112 |
|
| 113 |
ret->width = cl->width; |
| 114 |
ret->width_hint = cl->width_hint; |
| 115 |
ret->flags = cl->flags; |
| 116 |
ret->is_groups = cl->is_groups; |
| 117 |
|
| 118 |
memcpy(&ret->wstat, &cl->wstat, sizeof(cl->wstat)); |
| 119 |
|
| 120 |
return ret; |
| 121 |
err: |
| 122 |
scols_unref_column(ret); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* scols_column_set_whint: |
| 128 |
* @cl: a pointer to a struct libscols_column instance |
| 129 |
* @whint: a width hint |
| 130 |
* |
| 131 |
* Sets the width hint of column @cl to @whint. See scols_table_new_column(). |
| 132 |
* |
| 133 |
* Returns: 0, a negative value in case of an error. |
| 134 |
*/ |
| 135 |
int scols_column_set_whint(struct libscols_column *cl, double whint) |
| 136 |
{ |
| 137 |
if (!cl) |
| 138 |
return -EINVAL; |
| 139 |
|
| 140 |
cl->width_hint = whint; |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* scols_column_get_whint: |
| 146 |
* @cl: a pointer to a struct libscols_column instance |
| 147 |
* |
| 148 |
* Returns: The width hint of column @cl, a negative value in case of an error. |
| 149 |
*/ |
| 150 |
double scols_column_get_whint(const struct libscols_column *cl) |
| 151 |
{ |
| 152 |
return cl->width_hint; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* scols_column_set_flags: |
| 157 |
* @cl: a pointer to a struct libscols_column instance |
| 158 |
* @flags: a flag mask |
| 159 |
* |
| 160 |
* Sets the flags of @cl to @flags. |
| 161 |
* |
| 162 |
* Returns: 0, a negative value in case of an error. |
| 163 |
*/ |
| 164 |
int scols_column_set_flags(struct libscols_column *cl, int flags) |
| 165 |
{ |
| 166 |
if (!cl) |
| 167 |
return -EINVAL; |
| 168 |
|
| 169 |
if (cl->table) { |
| 170 |
if (!(cl->flags & SCOLS_FL_TREE) && (flags & SCOLS_FL_TREE)) |
| 171 |
cl->table->ntreecols++; |
| 172 |
else if ((cl->flags & SCOLS_FL_TREE) && !(flags & SCOLS_FL_TREE)) |
| 173 |
cl->table->ntreecols--; |
| 174 |
} |
| 175 |
|
| 176 |
DBG(COL, ul_debugobj(cl, "setting flags from 0x%04x to 0x%04x", cl->flags, flags)); |
| 177 |
cl->flags = flags; |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* scols_column_set_json_type: |
| 183 |
* @cl: a pointer to a struct libscols_column instance |
| 184 |
* @type: SCOLS_JSON_* type |
| 185 |
* |
| 186 |
* Sets the type used for JSON formatting, the default is SCOLS_JSON_STRING. |
| 187 |
* |
| 188 |
* Returns: 0, a negative value in case of an error. |
| 189 |
* |
| 190 |
* Since: 2.33 |
| 191 |
*/ |
| 192 |
int scols_column_set_json_type(struct libscols_column *cl, int type) |
| 193 |
{ |
| 194 |
if (!cl) |
| 195 |
return -EINVAL; |
| 196 |
|
| 197 |
cl->json_type = type; |
| 198 |
return 0; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* scols_column_get_json_type: |
| 204 |
* @cl: a pointer to a struct libscols_column instance |
| 205 |
* |
| 206 |
* Note that SCOLS_JSON_BOOLEAN interprets NULL, empty strings, '0', 'N' and |
| 207 |
* 'n' as "false"; and everything else as "true". |
| 208 |
* |
| 209 |
* Returns: JSON type used for formatting or a negative value in case of an error. |
| 210 |
* |
| 211 |
* Since: 2.33 |
| 212 |
*/ |
| 213 |
int scols_column_get_json_type(const struct libscols_column *cl) |
| 214 |
{ |
| 215 |
return cl ? cl->json_type : -EINVAL; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* scols_column_set_data_type: |
| 221 |
* @cl: a pointer to a struct libscols_column instance |
| 222 |
* @type: SCOLS_DATA_* |
| 223 |
* |
| 224 |
* The table always keep data in strings in form that is printed on output, but |
| 225 |
* for some internal operations (like filters or counters) it needs to convert |
| 226 |
* the strings to usable data format. This data format is possible to specify, |
| 227 |
* by this function. If the format is not specified then filter and counters |
| 228 |
* try to use SCOLS_JSON_* types, if also not define than defaults to string. |
| 229 |
* |
| 230 |
* If a simple string conversion is not possible then application (which want |
| 231 |
* to use filters and counters) needs to define data function to do the |
| 232 |
* conversion. See scols_column_set_data_func(). |
| 233 |
* |
| 234 |
* Returns: 0, a negative value in case of an error. |
| 235 |
* |
| 236 |
* Since: 2.40 |
| 237 |
*/ |
| 238 |
int scols_column_set_data_type(struct libscols_column *cl, int type) |
| 239 |
{ |
| 240 |
return cl->data_type = type; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* scols_column_get_data_type: |
| 245 |
* @cl: a pointer to a struct libscols_column instance |
| 246 |
* |
| 247 |
* Returns: The current datatype setting of the column @cl. |
| 248 |
* |
| 249 |
* Since: 2.40 |
| 250 |
*/ |
| 251 |
int scols_column_get_data_type(const struct libscols_column *cl) |
| 252 |
{ |
| 253 |
return cl->data_type; |
| 254 |
} |
| 255 |
/** |
| 256 |
* scols_column_get_table: |
| 257 |
* @cl: a pointer to a struct libscols_column instance |
| 258 |
* |
| 259 |
* Returns: pointer to the table where columns is used |
| 260 |
*/ |
| 261 |
struct libscols_table *scols_column_get_table(const struct libscols_column *cl) |
| 262 |
{ |
| 263 |
return cl->table; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* scols_column_get_flags: |
| 268 |
* @cl: a pointer to a struct libscols_column instance |
| 269 |
* |
| 270 |
* Returns: The flag mask of @cl, a negative value in case of an error. |
| 271 |
*/ |
| 272 |
int scols_column_get_flags(const struct libscols_column *cl) |
| 273 |
{ |
| 274 |
return cl->flags; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* scols_column_get_header: |
| 279 |
* @cl: a pointer to a struct libscols_column instance |
| 280 |
* |
| 281 |
* Returns: A pointer to a struct libscols_cell instance, representing the |
| 282 |
* header info of column @cl or NULL in case of an error. |
| 283 |
*/ |
| 284 |
struct libscols_cell *scols_column_get_header(struct libscols_column *cl) |
| 285 |
{ |
| 286 |
return &cl->header; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* scols_column_set_name: |
| 291 |
* @cl: a pointer to a struct libscols_column instance |
| 292 |
* @name: column name |
| 293 |
* |
| 294 |
* Returns: 0, a negative value in case of an error. |
| 295 |
* |
| 296 |
* Since: 2.38 |
| 297 |
*/ |
| 298 |
int scols_column_set_name(struct libscols_column *cl, const char *name) |
| 299 |
{ |
| 300 |
struct libscols_cell *hr = scols_column_get_header(cl); |
| 301 |
|
| 302 |
if (!hr) |
| 303 |
return -EINVAL; |
| 304 |
|
| 305 |
free(cl->shellvar); |
| 306 |
cl->shellvar = NULL; |
| 307 |
|
| 308 |
return scols_cell_set_data(hr, name); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* scols_column_get_name: |
| 313 |
* @cl: a pointer to a struct libscols_column instance |
| 314 |
* |
| 315 |
* Returns: A pointer to a column name, which is stored in column header |
| 316 |
* |
| 317 |
* Since: 2.38 |
| 318 |
*/ |
| 319 |
const char *scols_column_get_name(struct libscols_column *cl) |
| 320 |
{ |
| 321 |
return scols_cell_get_data(&cl->header); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* scols_column_refer_annotation: |
| 326 |
* @cl: a pointer to a struct libscols_column instance |
| 327 |
* @annotation: column annotation |
| 328 |
* |
| 329 |
* Returns: 0, a negative value in case of an error. |
| 330 |
* |
| 331 |
* Since: 2.42 |
| 332 |
*/ |
| 333 |
int scols_column_refer_annotation(struct libscols_column *cl, const char *annotation) |
| 334 |
{ |
| 335 |
if (!annotation) |
| 336 |
return -1; |
| 337 |
|
| 338 |
cl->annotation = (char *) annotation; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* scols_column_get_annotation: |
| 344 |
* @cl: a pointer to a struct libscols_column instance |
| 345 |
* |
| 346 |
* Returns: A pointer to a column annotation, which is stored in column annotation |
| 347 |
* |
| 348 |
* Since: 2.42 |
| 349 |
*/ |
| 350 |
const char *scols_column_get_annotation(struct libscols_column *cl) |
| 351 |
{ |
| 352 |
return cl->annotation; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* scols_column_set_headercolor: |
| 357 |
* @cl: a pointer to a struct libscols_column instance |
| 358 |
* @color: color name or ESC sequence |
| 359 |
* |
| 360 |
* The set header color. |
| 361 |
* |
| 362 |
* Returns: 0, a negative value in case of an error. |
| 363 |
* |
| 364 |
* Since: 2.42 |
| 365 |
*/ |
| 366 |
int scols_column_set_headercolor(struct libscols_column *cl, const char *color) |
| 367 |
{ |
| 368 |
struct libscols_cell *hdr = scols_column_get_header(cl); |
| 369 |
|
| 370 |
if (!hdr) |
| 371 |
return -EINVAL; |
| 372 |
|
| 373 |
return scols_cell_set_color(hdr, color); |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* scols_column_get_headercolor: |
| 378 |
* @cl: a pointer to a struct libscols_column instance |
| 379 |
* |
| 380 |
* Returns: The current color setting of the column header or NULL. |
| 381 |
* |
| 382 |
* Since: 2.42 |
| 383 |
*/ |
| 384 |
const char *scols_column_get_headercolor(struct libscols_column *cl) |
| 385 |
{ |
| 386 |
struct libscols_cell *hdr = scols_column_get_header(cl); |
| 387 |
|
| 388 |
if (hdr) |
| 389 |
return scols_cell_get_color(hdr); |
| 390 |
return NULL; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* scols_shellvar_name: |
| 395 |
* @name: raw (column) name |
| 396 |
* @buf: buffer to returns normalized name |
| 397 |
* @bufsz: size of the buffer |
| 398 |
* |
| 399 |
* Converts @name to a name compatible with shell. The buffer is reallocated if |
| 400 |
* not large enough. |
| 401 |
* |
| 402 |
* Returns: 0 in case of conversion, 1 if conversion unnecessary, <0 on error. |
| 403 |
* |
| 404 |
* Since: 2.40 |
| 405 |
*/ |
| 406 |
int scols_shellvar_name(const char *name, char **buf, size_t *bufsz) |
| 407 |
{ |
| 408 |
char *p; |
| 409 |
const char *s; |
| 410 |
size_t sz; |
| 411 |
|
| 412 |
if (!name || !*name || !buf || !bufsz) |
| 413 |
return -EINVAL; |
| 414 |
|
| 415 |
/* size to convert "1FOO%" --> "_1FOO_PCT */ |
| 416 |
sz = strlen(name) + 1 + 3; |
| 417 |
if (sz + 1 > *bufsz) { |
| 418 |
char *tmp; |
| 419 |
|
| 420 |
*bufsz = sz + 1; |
| 421 |
tmp = realloc(*buf, *bufsz); |
| 422 |
if (!tmp) |
| 423 |
return -ENOMEM; |
| 424 |
*buf = tmp; |
| 425 |
} |
| 426 |
memset(*buf, 0, *bufsz); |
| 427 |
p = *buf; |
| 428 |
|
| 429 |
/* convert "1FOO" to "_1FOO" */ |
| 430 |
if (!isalpha(*name)) |
| 431 |
*p++ = '_'; |
| 432 |
|
| 433 |
/* replace all "bad" chars with "_" */ |
| 434 |
for (s = name; *s; s++) |
| 435 |
*p++ = !isalnum(*s) ? '_' : *s; |
| 436 |
|
| 437 |
if (!*s && *(s - 1) == '%') { |
| 438 |
*p++ = 'P'; |
| 439 |
*p++ = 'C'; |
| 440 |
*p++ = 'T'; |
| 441 |
} |
| 442 |
|
| 443 |
return strcmp(name, *buf) == 0; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* scols_column_get_name_as_shellvar |
| 448 |
* @cl: a pointer to a struct libscols_column instance |
| 449 |
* |
| 450 |
* Like scols_column_get_name(), but column name is modified to be compatible with shells |
| 451 |
* requirements for variable names. |
| 452 |
* |
| 453 |
* Since: 2.38 |
| 454 |
*/ |
| 455 |
const char *scols_column_get_name_as_shellvar(struct libscols_column *cl) |
| 456 |
{ |
| 457 |
if (!cl->shellvar) { |
| 458 |
const char *name = scols_column_get_name(cl); |
| 459 |
size_t sz = 0; |
| 460 |
|
| 461 |
if (!name || !*name) |
| 462 |
return NULL; |
| 463 |
if (scols_shellvar_name(name, &cl->shellvar, &sz) < 0) |
| 464 |
return NULL; |
| 465 |
} |
| 466 |
return cl->shellvar; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
/** |
| 471 |
* scols_column_set_color: |
| 472 |
* @cl: a pointer to a struct libscols_column instance |
| 473 |
* @color: color name or ESC sequence |
| 474 |
* |
| 475 |
* The default color for data cells and column header. |
| 476 |
* |
| 477 |
* If you want to set header specific color then use scols_column_get_header() |
| 478 |
* and scols_cell_set_color() or scols_column_set_headercolor() since v2.42. |
| 479 |
* |
| 480 |
* If you want to set data cell specific color the use scols_line_get_cell() + |
| 481 |
* scols_cell_set_color(). |
| 482 |
* |
| 483 |
* Returns: 0, a negative value in case of an error. |
| 484 |
*/ |
| 485 |
int scols_column_set_color(struct libscols_column *cl, const char *color) |
| 486 |
{ |
| 487 |
if (color && !color_is_sequence(color)) { |
| 488 |
char *seq = color_get_sequence(color); |
| 489 |
if (!seq) |
| 490 |
return -EINVAL; |
| 491 |
free(cl->color); |
| 492 |
cl->color = seq; |
| 493 |
return 0; |
| 494 |
} |
| 495 |
return strdup_to_struct_member(cl, color, color); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* scols_column_get_color: |
| 500 |
* @cl: a pointer to a struct libscols_column instance |
| 501 |
* |
| 502 |
* Returns: The current color setting of the column @cl. |
| 503 |
*/ |
| 504 |
const char *scols_column_get_color(const struct libscols_column *cl) |
| 505 |
{ |
| 506 |
return cl->color; |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* scols_column_set_uri: |
| 511 |
* @cl: a pointer to a struct libscols_column instance |
| 512 |
* @uri: URI string |
| 513 |
* |
| 514 |
* The default URI prefix for cells is used when creating hyperlinks. However, |
| 515 |
* it can still be disabled for selected cells using scols_cell_disable_uri(). |
| 516 |
* See also scols_cell_set_uri(). |
| 517 |
* |
| 518 |
* The final cell URI is composed of the column-uri, cell-uri, and cell-data. |
| 519 |
* The column-uri and/or cell-uri must be set for this feature to be enabled. |
| 520 |
* |
| 521 |
* <informalexample> |
| 522 |
* <programlisting> |
| 523 |
* column-uri cell-uri cell-data final-URI link |
| 524 |
* -------------------------------------------------------------------------------------------- |
| 525 |
* file://host/path/foo.txt foo file://host/path/foo.txt foo |
| 526 |
* file://host /path/foo.txt foo file://host/path/foo.txt foo |
| 527 |
* file://host /path/foo.txt file://host/path/foo.txt /path/foo.txt |
| 528 |
* </programlisting> |
| 529 |
* </informalexample> |
| 530 |
* |
| 531 |
* Returns: 0, a negative value in case of an error. |
| 532 |
*/ |
| 533 |
int scols_column_set_uri(struct libscols_column *cl, const char *uri) |
| 534 |
{ |
| 535 |
return strdup_to_struct_member(cl, uri, uri); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* scols_column_get_uri: |
| 540 |
* @cl: a pointer to a struct libscols_column instance |
| 541 |
* |
| 542 |
* Returns: The current URI setting of the column @cl. |
| 543 |
*/ |
| 544 |
const char *scols_column_get_uri(const struct libscols_column *cl) |
| 545 |
{ |
| 546 |
return cl->uri; |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* scols_wrapnl_nextchunk: |
| 551 |
* @cl: a pointer to a struct libscols_column instance |
| 552 |
* @data: string |
| 553 |
* @userdata: callback private data |
| 554 |
* |
| 555 |
* This is built-in function for scols_column_set_wrapfunc(). This function |
| 556 |
* terminates the current chunk by \0 and returns pointer to the begin of |
| 557 |
* the next chunk. The chunks are based on \n. |
| 558 |
* |
| 559 |
* For example for data "AAA\nBBB\nCCC" the next chunk is "BBB". |
| 560 |
* |
| 561 |
* Returns: next chunk |
| 562 |
* |
| 563 |
* Since: 2.29 |
| 564 |
*/ |
| 565 |
char *scols_wrapnl_nextchunk(const struct libscols_column *cl __attribute__((unused)), |
| 566 |
char *data, |
| 567 |
void *userdata __attribute__((unused))) |
| 568 |
{ |
| 569 |
char *p = data ? strchr(data, '\n') : NULL; |
| 570 |
|
| 571 |
if (p) { |
| 572 |
*p = '\0'; |
| 573 |
return p + 1; |
| 574 |
} |
| 575 |
return NULL; |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* scols_wrapzero_nextchunk: |
| 580 |
* @cl: a pointer to a struct libscols_column instance |
| 581 |
* @data: string |
| 582 |
* @userdata: callback private data |
| 583 |
* |
| 584 |
* This is built-in function for scols_column_set_wrapfunc(). This function |
| 585 |
* walk string separated by \0. |
| 586 |
* |
| 587 |
* For example for data "AAA\0BBB\0CCC\0" the next chunk is "BBB". |
| 588 |
* |
| 589 |
* Returns: next chunk |
| 590 |
* |
| 591 |
* Since: 2.40 |
| 592 |
*/ |
| 593 |
char *scols_wrapzero_nextchunk(const struct libscols_column *cl, |
| 594 |
char *data, |
| 595 |
void *userdata __attribute__((unused))) |
| 596 |
{ |
| 597 |
char *start = NULL; |
| 598 |
size_t sz = 0; |
| 599 |
|
| 600 |
if (!data) |
| 601 |
return NULL; |
| 602 |
scols_column_get_wrap_data(cl, &start, &sz, NULL, NULL); |
| 603 |
if (!start || !sz) |
| 604 |
return NULL; |
| 605 |
return ul_next_string(data, start + sz); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
/** |
| 610 |
* scols_wrapnl_chunksize: |
| 611 |
* @cl: a pointer to a struct libscols_column instance |
| 612 |
* @data: string |
| 613 |
* @userdata: callback private data |
| 614 |
* |
| 615 |
* Analyzes @data and returns size of the largest chunk. The chunks are based |
| 616 |
* on \n. For example for data "AAA\nBBB\nCCCC" the largest chunk size is 4. |
| 617 |
* |
| 618 |
* Note that the size has to be based on number of terminal cells rather than |
| 619 |
* bytes to support multu-byte output. |
| 620 |
* |
| 621 |
* Deprecated since 2.40. |
| 622 |
* |
| 623 |
* Returns: size of the largest chunk. |
| 624 |
* |
| 625 |
* Since: 2.29 |
| 626 |
*/ |
| 627 |
size_t scols_wrapnl_chunksize(const struct libscols_column *cl __attribute__((unused)), |
| 628 |
const char *data, |
| 629 |
void *userdata __attribute__((unused))) |
| 630 |
{ |
| 631 |
size_t sum = 0; |
| 632 |
|
| 633 |
while (data && *data) { |
| 634 |
const char *p; |
| 635 |
size_t sz; |
| 636 |
|
| 637 |
p = strchr(data, '\n'); |
| 638 |
if (p) { |
| 639 |
sz = cl->table && scols_table_is_noencoding(cl->table) ? |
| 640 |
mbs_nwidth(data, p - data) : |
| 641 |
mbs_safe_nwidth(data, p - data, NULL); |
| 642 |
p++; |
| 643 |
} else { |
| 644 |
sz = cl->table && scols_table_is_noencoding(cl->table) ? |
| 645 |
mbs_width(data) : |
| 646 |
mbs_safe_width(data); |
| 647 |
} |
| 648 |
sum = max(sum, sz); |
| 649 |
data = p; |
| 650 |
} |
| 651 |
|
| 652 |
return sum; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* scols_column_set_cmpfunc: |
| 657 |
* @cl: column |
| 658 |
* @cmp: pointer to compare function |
| 659 |
* @data: private data for cmp function |
| 660 |
* |
| 661 |
* Returns: 0, a negative value in case of an error. |
| 662 |
*/ |
| 663 |
int scols_column_set_cmpfunc(struct libscols_column *cl, |
| 664 |
int (*cmp)(struct libscols_cell *, |
| 665 |
struct libscols_cell *, |
| 666 |
void *), |
| 667 |
void *data) |
| 668 |
{ |
| 669 |
if (!cl) |
| 670 |
return -EINVAL; |
| 671 |
|
| 672 |
cl->cmpfunc = cmp; |
| 673 |
cl->cmpfunc_data = data; |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* scols_column_set_wrapfunc: |
| 679 |
* @cl: a pointer to a struct libscols_column instance |
| 680 |
* @wrap_chunksize: function to return size of the largest chink of data (deprecated) |
| 681 |
* @wrap_nextchunk: function to return next zero terminated data |
| 682 |
* @userdata: optional stuff for callbacks |
| 683 |
* |
| 684 |
* Extends SCOLS_FL_WRAP and can be used to set custom wrap function. The default |
| 685 |
* is to wrap by column size, but you can create functions to wrap for example |
| 686 |
* after \n or after words, etc. |
| 687 |
* |
| 688 |
* Note that since 2.40 the @wrap_chunksize is unnecessary. The library calculates |
| 689 |
* the size itself. |
| 690 |
* |
| 691 |
* The wrap functions do not work directly with cell data, but with buffer used |
| 692 |
* by library to compose output data. The wrap_nextchunk() function can access |
| 693 |
* additional details about wrap data by scols_column_get_wrap_data(). |
| 694 |
* |
| 695 |
* Returns: 0, a negative value in case of an error. |
| 696 |
* |
| 697 |
* Since: 2.29 |
| 698 |
*/ |
| 699 |
int scols_column_set_wrapfunc(struct libscols_column *cl, |
| 700 |
size_t (*wrap_chunksize)(const struct libscols_column *, |
| 701 |
const char *, |
| 702 |
void *) __attribute__((__unused__)), |
| 703 |
char * (*wrap_nextchunk)(const struct libscols_column *, |
| 704 |
char *, |
| 705 |
void *), |
| 706 |
void *userdata) |
| 707 |
{ |
| 708 |
if (!cl) |
| 709 |
return -EINVAL; |
| 710 |
|
| 711 |
cl->wrap_nextchunk = wrap_nextchunk; |
| 712 |
cl->wrapfunc_data = userdata; |
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* scols_column_get_wrap_data: |
| 718 |
* @cl: column |
| 719 |
* @data: return wrap data |
| 720 |
* @datasiz: return wrap buffer size |
| 721 |
* @cur: the current pozition in the buffer |
| 722 |
* @next: the next pozition |
| 723 |
* |
| 724 |
* This function returns the current status of wrapping cell data (for multi-line cells). |
| 725 |
* |
| 726 |
* Returns: 0, a negative value in case of an error. |
| 727 |
* |
| 728 |
* Since: 2.40 |
| 729 |
*/ |
| 730 |
int scols_column_get_wrap_data(const struct libscols_column *cl, |
| 731 |
char **data, size_t *datasiz, char **cur, char **next) |
| 732 |
{ |
| 733 |
if (!cl) |
| 734 |
return -EINVAL; |
| 735 |
if (data) |
| 736 |
*data = cl->wrap_data; |
| 737 |
if (datasiz) |
| 738 |
*datasiz = cl->wrap_datasz; |
| 739 |
if (cur) |
| 740 |
*cur = cl->wrap_cur; |
| 741 |
if (next) |
| 742 |
*next = cl->wrap_next; |
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* scols_column_set_data_func: |
| 748 |
* @cl: a pointer to a struct libscols_column instance |
| 749 |
* @datafunc: function to return data |
| 750 |
* @userdata: optional stuff for callbacks |
| 751 |
* |
| 752 |
* The table always keep data in strings in form that is printed on output, but |
| 753 |
* for some internal operations (like filters or counters) it needs to convert |
| 754 |
* the strings to usable data format. If this conversion is not possible then |
| 755 |
* application can define datafunc() callback to provide data for filters and counters. |
| 756 |
* |
| 757 |
* The callback needs to return the data as pointer to void, and the data type |
| 758 |
* is defined by scols_column_set_data_type(). |
| 759 |
* |
| 760 |
* Returns: 0, a negative value in case of an error. |
| 761 |
* |
| 762 |
* Since: 2.40 |
| 763 |
*/ |
| 764 |
int scols_column_set_data_func(struct libscols_column *cl, |
| 765 |
void *(*datafunc)(const struct libscols_column *, |
| 766 |
struct libscols_cell *, |
| 767 |
void *), |
| 768 |
void *userdata) |
| 769 |
{ |
| 770 |
if (!cl) |
| 771 |
return -EINVAL; |
| 772 |
|
| 773 |
cl->datafunc = datafunc; |
| 774 |
cl->datafunc_data = userdata; |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* scols_column_has_data_func: |
| 780 |
* @cl: a pointer to a struct libscols_column instance |
| 781 |
* |
| 782 |
* See scols_column_set_data_func() for more details. |
| 783 |
* |
| 784 |
* Returns: 1 if data function defined, or 0 |
| 785 |
* |
| 786 |
* Since: 2.40 |
| 787 |
*/ |
| 788 |
int scols_column_has_data_func(struct libscols_column *cl) |
| 789 |
{ |
| 790 |
return cl && cl->datafunc != NULL ? 1 : 0; |
| 791 |
} |
| 792 |
|
| 793 |
/** |
| 794 |
* scols_column_set_safechars: |
| 795 |
* @cl: a pointer to a struct libscols_column instance |
| 796 |
* @safe: safe characters (e.g. "\n\t") |
| 797 |
* |
| 798 |
* Use for bytes you don't want to encode on output. This is for example |
| 799 |
* necessary if you want to use custom wrap function based on \n, in this case |
| 800 |
* you have to set "\n" as a safe char. |
| 801 |
* |
| 802 |
* Returns: 0, a negative value in case of an error. |
| 803 |
* |
| 804 |
* Since: 2.29 |
| 805 |
*/ |
| 806 |
int scols_column_set_safechars(struct libscols_column *cl, const char *safe) |
| 807 |
{ |
| 808 |
return strdup_to_struct_member(cl, safechars, safe); |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* scols_column_get_safechars: |
| 813 |
* @cl: a pointer to a struct libscols_column instance |
| 814 |
* |
| 815 |
* Returns: safe chars |
| 816 |
* |
| 817 |
* Since: 2.29 |
| 818 |
*/ |
| 819 |
const char *scols_column_get_safechars(const struct libscols_column *cl) |
| 820 |
{ |
| 821 |
return cl->safechars; |
| 822 |
} |
| 823 |
|
| 824 |
/** |
| 825 |
* scols_column_get_width: |
| 826 |
* @cl: a pointer to a struct libscols_column instance |
| 827 |
* |
| 828 |
* Important note: the column width is unknown until library starts printing |
| 829 |
* (width is calculated before printing). The function is usable for example in |
| 830 |
* nextchunk() callback specified by scols_column_set_wrapfunc(). |
| 831 |
* |
| 832 |
* See also scols_column_get_whint(), it returns wanted size (!= final size). |
| 833 |
* |
| 834 |
* Returns: column width |
| 835 |
* |
| 836 |
* Since: 2.29 |
| 837 |
*/ |
| 838 |
size_t scols_column_get_width(const struct libscols_column *cl) |
| 839 |
{ |
| 840 |
return cl->width; |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* scols_column_is_hidden: |
| 845 |
* @cl: a pointer to a struct libscols_column instance |
| 846 |
* |
| 847 |
* Gets the value of @cl's flag hidden. |
| 848 |
* |
| 849 |
* Returns: 0 or 1 |
| 850 |
* |
| 851 |
* Since: 2.27 |
| 852 |
*/ |
| 853 |
int scols_column_is_hidden(const struct libscols_column *cl) |
| 854 |
{ |
| 855 |
return cl->flags & SCOLS_FL_HIDDEN ? 1 : 0; |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
* scols_column_is_trunc: |
| 860 |
* @cl: a pointer to a struct libscols_column instance |
| 861 |
* |
| 862 |
* Gets the value of @cl's flag trunc. |
| 863 |
* |
| 864 |
* Returns: 0 or 1 |
| 865 |
*/ |
| 866 |
int scols_column_is_trunc(const struct libscols_column *cl) |
| 867 |
{ |
| 868 |
return cl->flags & SCOLS_FL_TRUNC ? 1 : 0; |
| 869 |
} |
| 870 |
/** |
| 871 |
* scols_column_is_tree: |
| 872 |
* @cl: a pointer to a struct libscols_column instance |
| 873 |
* |
| 874 |
* Gets the value of @cl's flag tree. |
| 875 |
* |
| 876 |
* Returns: 0 or 1 |
| 877 |
*/ |
| 878 |
int scols_column_is_tree(const struct libscols_column *cl) |
| 879 |
{ |
| 880 |
return cl->flags & SCOLS_FL_TREE ? 1 : 0; |
| 881 |
} |
| 882 |
/** |
| 883 |
* scols_column_is_right: |
| 884 |
* @cl: a pointer to a struct libscols_column instance |
| 885 |
* |
| 886 |
* Gets the value of @cl's flag right. |
| 887 |
* |
| 888 |
* Returns: 0 or 1 |
| 889 |
*/ |
| 890 |
int scols_column_is_right(const struct libscols_column *cl) |
| 891 |
{ |
| 892 |
return cl->flags & SCOLS_FL_RIGHT ? 1 : 0; |
| 893 |
} |
| 894 |
/** |
| 895 |
* scols_column_is_strict_width: |
| 896 |
* @cl: a pointer to a struct libscols_column instance |
| 897 |
* |
| 898 |
* Gets the value of @cl's flag strict_width. |
| 899 |
* |
| 900 |
* Returns: 0 or 1 |
| 901 |
*/ |
| 902 |
int scols_column_is_strict_width(const struct libscols_column *cl) |
| 903 |
{ |
| 904 |
return cl->flags & SCOLS_FL_STRICTWIDTH ? 1 : 0; |
| 905 |
} |
| 906 |
/** |
| 907 |
* scols_column_is_noextremes: |
| 908 |
* @cl: a pointer to a struct libscols_column instance |
| 909 |
* |
| 910 |
* Gets the value of @cl's flag no_extremes. |
| 911 |
* |
| 912 |
* Returns: 0 or 1 |
| 913 |
*/ |
| 914 |
int scols_column_is_noextremes(const struct libscols_column *cl) |
| 915 |
{ |
| 916 |
return cl->flags & SCOLS_FL_NOEXTREMES ? 1 : 0; |
| 917 |
} |
| 918 |
/** |
| 919 |
* scols_column_is_wrap: |
| 920 |
* @cl: a pointer to a struct libscols_column instance |
| 921 |
* |
| 922 |
* Gets the value of @cl's flag wrap. |
| 923 |
* |
| 924 |
* Returns: 0 or 1 |
| 925 |
* |
| 926 |
* Since: 2.28 |
| 927 |
*/ |
| 928 |
int scols_column_is_wrap(const struct libscols_column *cl) |
| 929 |
{ |
| 930 |
return cl->flags & SCOLS_FL_WRAP ? 1 : 0; |
| 931 |
} |
| 932 |
/** |
| 933 |
* scols_column_is_customwrap: |
| 934 |
* @cl: a pointer to a struct libscols_column instance |
| 935 |
* |
| 936 |
* Returns: 0 or 1 |
| 937 |
* |
| 938 |
* Since: 2.29 |
| 939 |
*/ |
| 940 |
int scols_column_is_customwrap(const struct libscols_column *cl) |
| 941 |
{ |
| 942 |
return (cl->flags & SCOLS_FL_WRAP) |
| 943 |
&& cl->wrap_nextchunk ? 1 : 0; |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
* scols_column_set_properties: |
| 948 |
* @cl: a pointer to a struct libscols_column instance |
| 949 |
* @opts: options string |
| 950 |
* |
| 951 |
* Set properties from string, the string is comma separated list. Supported |
| 952 |
* properties are: trunc, tree, right, strictwidth, noextremes, hidden, wrap, |
| 953 |
* wrapnl, wrapzero,json={string,number,float,array-string,array-number,boolean}, |
| 954 |
* width=, color=, headercolor= and name=. |
| 955 |
* |
| 956 |
* Returns: 0 on success, <0 on error |
| 957 |
* |
| 958 |
* Since: 2.39 |
| 959 |
*/ |
| 960 |
int scols_column_set_properties(struct libscols_column *cl, const char *opts) |
| 961 |
{ |
| 962 |
char *str = (char *) opts; |
| 963 |
char *name, *value; |
| 964 |
size_t namesz, valuesz; |
| 965 |
unsigned int flags = 0; |
| 966 |
int rc = 0; |
| 967 |
|
| 968 |
DBG(COL, ul_debugobj(cl, "apply properties '%s'", opts)); |
| 969 |
|
| 970 |
while (rc == 0 |
| 971 |
&& !ul_optstr_next(&str, &name, &namesz, &value, &valuesz)) { |
| 972 |
|
| 973 |
if (strncmp(name, "trunc", namesz) == 0) |
| 974 |
flags |= SCOLS_FL_TRUNC; |
| 975 |
|
| 976 |
else if (strncmp(name, "tree", namesz) == 0) |
| 977 |
flags |= SCOLS_FL_TREE; |
| 978 |
|
| 979 |
else if (strncmp(name, "right", namesz) == 0) |
| 980 |
flags |= SCOLS_FL_RIGHT; |
| 981 |
|
| 982 |
else if (strncmp(name, "strictwidth", namesz) == 0) |
| 983 |
flags |= SCOLS_FL_STRICTWIDTH; |
| 984 |
|
| 985 |
else if (strncmp(name, "noextremes", namesz) == 0) |
| 986 |
flags |= SCOLS_FL_NOEXTREMES; |
| 987 |
|
| 988 |
else if (strncmp(name, "hidden", namesz) == 0) |
| 989 |
flags |= SCOLS_FL_HIDDEN; |
| 990 |
|
| 991 |
else if (strncmp(name, "wrap", namesz) == 0) |
| 992 |
flags |= SCOLS_FL_WRAP; |
| 993 |
|
| 994 |
else if (strncmp(name, "wrapnl", namesz) == 0) { |
| 995 |
flags |= SCOLS_FL_WRAP; |
| 996 |
scols_column_set_wrapfunc(cl, |
| 997 |
NULL, |
| 998 |
scols_wrapnl_nextchunk, |
| 999 |
NULL); |
| 1000 |
scols_column_set_safechars(cl, "\n"); |
| 1001 |
|
| 1002 |
} else if (strncmp(name, "wrapzero", namesz) == 0) { |
| 1003 |
flags |= SCOLS_FL_WRAP; |
| 1004 |
scols_column_set_wrapfunc(cl, |
| 1005 |
NULL, |
| 1006 |
scols_wrapzero_nextchunk, |
| 1007 |
NULL); |
| 1008 |
|
| 1009 |
} else if (value && strncmp(name, "json", namesz) == 0) { |
| 1010 |
|
| 1011 |
if (strncmp(value, "string", valuesz) == 0) |
| 1012 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_STRING); |
| 1013 |
else if (strncmp(value, "number", valuesz) == 0) |
| 1014 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_NUMBER); |
| 1015 |
else if (strncmp(value, "float", valuesz) == 0) |
| 1016 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_FLOAT); |
| 1017 |
else if (strncmp(value, "array-string", valuesz) == 0) |
| 1018 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_STRING); |
| 1019 |
else if (strncmp(value, "array-number", valuesz) == 0) |
| 1020 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_NUMBER); |
| 1021 |
else if (strncmp(value, "boolean", valuesz) == 0) |
| 1022 |
rc = scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN); |
| 1023 |
|
| 1024 |
} else if (value && strncmp(name, "width", namesz) == 0) { |
| 1025 |
|
| 1026 |
char *end = NULL; |
| 1027 |
double x; |
| 1028 |
|
| 1029 |
errno = 0; |
| 1030 |
x = strtod(value, &end); |
| 1031 |
if (errno || value == end) |
| 1032 |
return -EINVAL; |
| 1033 |
rc = scols_column_set_whint(cl, x); |
| 1034 |
|
| 1035 |
} else if (value && strncmp(name, "color", namesz) == 0) { |
| 1036 |
|
| 1037 |
char *x = strndup(value, valuesz); |
| 1038 |
if (x) { |
| 1039 |
scols_column_set_color(cl, x); |
| 1040 |
free(x); |
| 1041 |
} |
| 1042 |
|
| 1043 |
} else if (value && strncmp(name, "headercolor", namesz) == 0) { |
| 1044 |
|
| 1045 |
char *x = strndup(value, valuesz); |
| 1046 |
if (x) { |
| 1047 |
scols_column_set_headercolor(cl, x); |
| 1048 |
free(x); |
| 1049 |
} |
| 1050 |
|
| 1051 |
} else if (value && strncmp(name, "name", namesz) == 0) { |
| 1052 |
|
| 1053 |
char *x = strndup(value, valuesz); |
| 1054 |
if (x) { |
| 1055 |
scols_column_set_name(cl, x); |
| 1056 |
free(x); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (!rc && flags) |
| 1062 |
rc = scols_column_set_flags(cl, flags); |
| 1063 |
|
| 1064 |
return rc; |
| 1065 |
} |
| 1066 |
|
| 1067 |
void scols_column_reset_wrap(struct libscols_column *cl) |
| 1068 |
{ |
| 1069 |
if (!cl) |
| 1070 |
return; |
| 1071 |
|
| 1072 |
if (cl->wrap_data) |
| 1073 |
memset(cl->wrap_data, 0, cl->wrap_datamax); |
| 1074 |
cl->wrap_cell = NULL; |
| 1075 |
cl->wrap_datasz = 0; |
| 1076 |
cl->wrap_cur = NULL; |
| 1077 |
cl->wrap_next = NULL; |
| 1078 |
} |
| 1079 |
|
| 1080 |
static int scols_column_init_wrap( |
| 1081 |
struct libscols_column *cl, |
| 1082 |
struct libscols_cell *ce) |
| 1083 |
{ |
| 1084 |
const char *data = scols_cell_get_data(ce); |
| 1085 |
|
| 1086 |
if (!cl || !ce) |
| 1087 |
return -EINVAL; |
| 1088 |
|
| 1089 |
assert(cl->table->cur_column == cl); |
| 1090 |
assert(cl->table->cur_cell == ce); |
| 1091 |
|
| 1092 |
scols_column_reset_wrap(cl); |
| 1093 |
|
| 1094 |
cl->wrap_cell = ce; |
| 1095 |
if (data) { |
| 1096 |
void *tmp; |
| 1097 |
cl->wrap_datasz = scols_cell_get_datasiz(ce); |
| 1098 |
|
| 1099 |
if (cl->wrap_datasz > cl->wrap_datamax) { |
| 1100 |
cl->wrap_datamax = cl->wrap_datasz; |
| 1101 |
tmp = realloc(cl->wrap_data, cl->wrap_datamax); |
| 1102 |
if (!tmp) |
| 1103 |
return -ENOMEM; |
| 1104 |
cl->wrap_data = tmp; |
| 1105 |
} |
| 1106 |
memcpy(cl->wrap_data, data, cl->wrap_datasz); |
| 1107 |
cl->wrap_cur = cl->wrap_data; |
| 1108 |
cl->wrap_next = NULL; |
| 1109 |
} |
| 1110 |
|
| 1111 |
return 0; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Returns the next chunk of cell data in multi-line cells */ |
| 1115 |
int scols_column_next_wrap( |
| 1116 |
struct libscols_column *cl, |
| 1117 |
struct libscols_cell *ce, |
| 1118 |
char **data) |
| 1119 |
{ |
| 1120 |
if (!cl || !data || (!cl->wrap_cell && !ce)) |
| 1121 |
return -EINVAL; |
| 1122 |
|
| 1123 |
*data = NULL; |
| 1124 |
|
| 1125 |
if (ce && cl->wrap_cell != ce) |
| 1126 |
scols_column_init_wrap(cl, ce); /* init */ |
| 1127 |
else { |
| 1128 |
cl->wrap_cur = cl->wrap_next; /* next step */ |
| 1129 |
cl->wrap_next = NULL; |
| 1130 |
} |
| 1131 |
|
| 1132 |
if (!cl->wrap_cur) |
| 1133 |
return 1; /* no more data */ |
| 1134 |
if (scols_column_is_customwrap(cl)) |
| 1135 |
cl->wrap_next = cl->wrap_nextchunk(cl, cl->wrap_cur, cl->wrapfunc_data); |
| 1136 |
|
| 1137 |
*data = cl->wrap_cur; |
| 1138 |
return 0; |
| 1139 |
} |
| 1140 |
|
| 1141 |
int scols_column_greatest_wrap( |
| 1142 |
struct libscols_column *cl, |
| 1143 |
struct libscols_cell *ce, |
| 1144 |
char **data) |
| 1145 |
{ |
| 1146 |
size_t maxsz = 0; |
| 1147 |
char *res = NULL; |
| 1148 |
|
| 1149 |
if (!scols_column_is_customwrap(cl)) |
| 1150 |
return scols_column_next_wrap(cl, ce, data); |
| 1151 |
|
| 1152 |
while (scols_column_next_wrap(cl, ce, data) == 0) { |
| 1153 |
size_t sz = strlen(*data); |
| 1154 |
|
| 1155 |
maxsz = max(maxsz, sz); |
| 1156 |
if (maxsz == sz) |
| 1157 |
res = *data; |
| 1158 |
} |
| 1159 |
|
| 1160 |
*data = res; |
| 1161 |
return 0; |
| 1162 |
} |
| 1163 |
|
| 1164 |
/* Set the "next" chunk in multi-line cell to offset specified by @bytes. |
| 1165 |
* Don't use it for columns with custom wrapfunc(). |
| 1166 |
*/ |
| 1167 |
int scols_column_move_wrap(struct libscols_column *cl, size_t bytes) |
| 1168 |
{ |
| 1169 |
size_t x; /* remaining bytes */ |
| 1170 |
|
| 1171 |
if (!cl->wrap_cur) |
| 1172 |
return -EINVAL; /* scols_column_init_wrap() not called */ |
| 1173 |
|
| 1174 |
x = cl->wrap_datasz - (cl->wrap_cur - cl->wrap_data); |
| 1175 |
if (bytes >= x) |
| 1176 |
cl->wrap_next = NULL; /* done */ |
| 1177 |
else |
| 1178 |
cl->wrap_next = cl->wrap_cur + bytes; |
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
|
| 1182 |
int scols_column_has_pending_wrap(struct libscols_column *cl) |
| 1183 |
{ |
| 1184 |
return cl && cl->wrap_next; |
| 1185 |
} |