summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/table.c
blob: 6b4d9673eb4af0572daa10299537284ecd8720ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
 * table.c - functions handling the data at the table level
 *
 * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <ctype.h>

#include "nls.h"
#include "widechar.h"
#include "smartcolsP.h"

#ifdef HAVE_WIDECHAR
#define UTF_V	"\342\224\202"	/* U+2502, Vertical line drawing char */
#define UTF_VR	"\342\224\234"	/* U+251C, Vertical and right */
#define UTF_H	"\342\224\200"	/* U+2500, Horizontal */
#define UTF_UR	"\342\224\224"	/* U+2514, Up and right */
#endif /* !HAVE_WIDECHAR */

#define is_last_column(_tb, _cl) \
		list_entry_is_last(&(_cl)->cl_columns, &(_tb)->tb_columns)


/*
 * @syms: tree symbols or NULL for default
 *
 * Note that this function add a new reference to @syms
 *
 * Returns: newly allocated table
 */
struct libscols_table *scols_new_table(struct libscols_symbols *syms)
{
	struct libscols_table *tb;

	tb = calloc(1, sizeof(struct libscols_table));
	if (!tb)
		return NULL;

	tb->refcount = 1;
	tb->out = stdout;

	INIT_LIST_HEAD(&tb->tb_lines);
	INIT_LIST_HEAD(&tb->tb_columns);

	if (scols_table_set_symbols(tb, syms) == 0)
		return tb;

	scols_unref_table(tb);
	return NULL;
}

void scols_ref_table(struct libscols_table *tb)
{
	if (tb)
		tb->refcount++;
}

void scols_unref_table(struct libscols_table *tb)
{
	if (tb && (--tb->refcount <= 0)) {
		scols_table_remove_lines(tb);
		scols_table_remove_columns(tb);
		scols_unref_symbols(tb->symbols);
		free(tb);
	}
}

int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl, int flags)
{
	assert(tb);
	assert(cl);

	if (!tb || !cl || !list_empty(&tb->tb_lines))
		return -EINVAL;

	if (flags & SCOLS_FL_TREE)
		scols_table_set_tree(tb, 1);

	list_add_tail(&cl->cl_columns, &tb->tb_columns);
	cl->seqnum = tb->ncols++;
	scols_ref_column(cl);

	/* TODO:
	 *
	 * Currently it's possible to add/remove columns only if the table is
	 * empty (see list_empty(tb->tb_lines) above). It would be nice to
	 * enlarge/reduce lines cells[] always when we add/remove a new column.
	 */
	return 0;
}

int scols_table_remove_column(struct libscols_table *tb,
			      struct libscols_column *cl)
{
	assert(tb);
	assert(cl);

	if (!tb || !cl || !list_empty(&tb->tb_lines))
		return -EINVAL;

	list_del_init(&cl->cl_columns);
	tb->ncols--;
	scols_unref_column(cl);
	return 0;
}

int scols_table_remove_columns(struct libscols_table *tb)
{
	assert(tb);

	if (!tb || !list_empty(&tb->tb_lines))
		return -EINVAL;

	while (!list_empty(&tb->tb_columns)) {
		struct libscols_column *cl = list_entry(tb->tb_columns.next,
					struct libscols_column, cl_columns);
		scols_table_remove_column(tb, cl);
	}
	return 0;
}


/*
 * @tb: table
 * @name: column header
 * @whint: column width hint (absolute width: N > 1; relative width: N < 1)
 *
 * This is shortcut for
 *
 *   cl = scols_new_column();
 *   scols_column_set_....(cl, ...);
 *   scols_table_add_column(tb, cl);
 *
 * The column width is possible to define by three ways:
 *
 *  @whint = 0..1    : relative width, percent of terminal width
 *
 *  @whint = 1..N    : absolute width, empty colum will be truncated to
 *                     the column header width
 *
 *  @whint = 1..N
 *
 * The column is necessary to address (for example for scols_line_set_cell_data()) by
 * sequential number. The first defined column has the colnum = 0. For example:
 *
 *	scols_table_new_column(tab, "FOO", 0.5, 0);		// colnum = 0
 *	scols_table_new_column(tab, "BAR", 0.5, 0);		// colnum = 1
 *      .
 *      .
 *	scols_line_get_cell(line, 0);				// FOO column
 *	scols_line_get_cell(line, 1);				// BAR column
 *
 * Returns: newly allocated column
 */
struct libscols_column *scols_table_new_column(struct libscols_table *tb,
					       const char *name,
					       double whint,
					       int flags)
{
	struct libscols_column *cl;
	struct libscols_cell *hr;

	assert (tb);
	if (!tb)
		return NULL;
	cl = scols_new_column();
	if (!cl)
		return NULL;

	/* set column name */
	hr = scols_column_get_header(cl);
	if (!hr)
		goto err;
	if (scols_cell_set_data(hr, name))
		goto err;

	scols_column_set_whint(cl, whint);
	scols_column_set_flags(cl, flags);

	if (scols_table_add_column(tb, cl, flags))	/* this increments column ref-counter */
		goto err;

	scols_unref_column(cl);
	return cl;
err:
	scols_unref_column(cl);
	return NULL;
}

int scols_table_next_column(struct libscols_table *tb,
			    struct libscols_iter *itr,
			    struct libscols_column **cl)
{
	int rc = 1;

	if (!tb || !itr || !cl)
		return -EINVAL;
	*cl = NULL;

	if (!itr->head)
		SCOLS_ITER_INIT(itr, &tb->tb_columns);
	if (itr->p != itr->head) {
		SCOLS_ITER_ITERATE(itr, *cl, struct libscols_column, cl_columns);
		rc = 0;
	}

	return rc;
}


/*
 * @tb: table
 *
 * Returns: ncols integer
 */
int scols_table_get_ncols(struct libscols_table *tb)
{
	assert(tb);
	return tb ? tb->ncols : -EINVAL;
}

/*
 * @tb: table
 *
 * Returns: nlines integer
 */
int scols_table_get_nlines(struct libscols_table *tb)
{
	assert(tb);
	return tb ? tb->nlines : -EINVAL;
}

int scols_table_set_stream(struct libscols_table *tb, FILE *stream)
{
	assert(tb);
	if (!tb)
		return -EINVAL;

	tb->out = stream;
	return 0;
}

FILE *scols_table_get_stream(struct libscols_table *tb)
{
	assert(tb);
	return tb ? tb->out: NULL;
}

int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce)
{
	assert(tb);
	if (!tb)
		return -EINVAL;

	tb->termreduce = reduce;
	return 0;
}

/*
 * @tb: table
 * @: number of column (0..N)
 *
 * Returns: pointer to column or NULL
 */
struct libscols_column *scols_table_get_column(struct libscols_table *tb,
					       size_t n)
{
	struct libscols_iter itr;
	struct libscols_column *cl;

	assert(tb);
	if (!tb)
		return NULL;
	if (n >= tb->ncols)
		return NULL;

	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_column(tb, &itr, &cl) == 0) {
		if (cl->seqnum == n)
			return cl;
	}
	return NULL;
}

/*
 * Note that this functiion calls scols_line_alloc_cells() if number
 * of the cells in the line is too small for @tb.
 */
int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln)
{

	assert(tb);
	assert(ln);

	if (!tb || !ln)
		return -EINVAL;

	if (tb->ncols > ln->ncells) {
		int rc = scols_line_alloc_cells(ln, tb->ncols);
		if (rc)
			return rc;
	}

	list_add_tail(&ln->ln_lines, &tb->tb_lines);
	ln->seqnum = tb->nlines++;
	scols_ref_line(ln);
	return 0;
}

/* Note that this function does not destroy parent->child relation between lines.
 * You have to call scols_line_remove_child()
 */
int scols_table_remove_line(struct libscols_table *tb,
			    struct libscols_line *ln)
{
	assert(tb);
	assert(ln);

	if (!tb || !ln)
		return -EINVAL;

	list_del_init(&ln->ln_lines);
	tb->nlines--;
	scols_unref_line(ln);
	return 0;
}

/* This make the table empty and also destroy all parent<->child relations */
void scols_table_remove_lines(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return;

	while (!list_empty(&tb->tb_lines)) {
		struct libscols_line *ln = list_entry(tb->tb_lines.next,
						struct libscols_line, ln_lines);
		if (ln->parent)
			scols_line_remove_child(ln->parent, ln);
		scols_table_remove_line(tb, ln);
	}
}

int scols_table_next_line(struct libscols_table *tb,
			  struct libscols_iter *itr,
			  struct libscols_line **ln)
{
	int rc = 1;

	if (!tb || !itr || !ln)
		return -EINVAL;
	*ln = NULL;

	if (!itr->head)
		SCOLS_ITER_INIT(itr, &tb->tb_lines);
	if (itr->p != itr->head) {
		SCOLS_ITER_ITERATE(itr, *ln, struct libscols_line, ln_lines);
		rc = 0;
	}

	return rc;
}

/*
 * @tb: table
 * @parent: parental line or NULL
 *
 * This is shortcut for
 *
 *   ln = scols_new_linen();
 *   scols_line_set_....(cl, ...);
 *   scols_table_add_line(tb, ln);

 *
 * Returns: newly allocate line
 */
struct libscols_line *scols_table_new_line(struct libscols_table *tb,
					   struct libscols_line *parent)
{
	struct libscols_line *ln;

	assert(tb);
	assert(tb->ncols);

	if (!tb || !tb->ncols)
		return NULL;
	ln = scols_new_line();
	if (!ln)
		return NULL;

	if (scols_table_add_line(tb, ln))
		goto err;
	if (parent)
		scols_line_add_child(parent, ln);

	scols_unref_line(ln);	/* ref-counter incremented by scols_table_add_line() */
	return ln;
err:
	scols_unref_line(ln);
	return NULL;
}


struct libscols_line *scols_table_get_line(struct libscols_table *tb,
					   size_t n)
{
	struct libscols_iter itr;
	struct libscols_line *ln;

	assert(tb);
	if (!tb)
		return NULL;
	if (n >= tb->nlines)
		return NULL;

	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_line(tb, &itr, &ln) == 0) {
		if (ln->seqnum == n)
			return ln;
	}
	return NULL;
}

/*
 * Creates a new independent table copy, except struct libscols_symbols that
 * are shared between the tables.
 */
struct libscols_table *scols_copy_table(struct libscols_table *tb)
{
	struct libscols_table *ret;
	struct libscols_line *ln;
	struct libscols_column *cl;
	struct libscols_iter itr;

	assert(tb);
	if (!tb)
		return NULL;
	ret = scols_new_table(tb->symbols);
	if (!ret)
		return NULL;

	/* columns */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_column(tb, &itr, &cl) == 0) {
		cl = scols_copy_column(cl);
		if (!cl)
			goto err;
		if (scols_table_add_column(ret, cl, tb->tree ? SCOLS_FL_TREE : 0))
			goto err;
		scols_unref_column(cl);
	}

	/* lines */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_line(tb, &itr, &ln) == 0) {
		struct libscols_line *newln = scols_copy_line(ln);
		if (!newln)
			goto err;
		if (scols_table_add_line(ret, newln))
			goto err;
		if (ln->parent) {
			struct libscols_line *p =
				scols_table_get_line(ret, ln->parent->seqnum);
			if (p)
				scols_line_add_child(p, newln);
		}
		scols_unref_line(newln);
	}

	return ret;
err:
	scols_unref_table(ret);
	return NULL;
}

int scols_table_set_symbols(struct libscols_table *tb,
			    struct libscols_symbols *sy)
{
	assert(tb);

	if (!tb)
		return -EINVAL;

	if (tb->symbols)				/* unref old */
		scols_unref_symbols(tb->symbols);
	if (sy) {					/* ref user defined */
		tb->symbols = sy;
		scols_ref_symbols(sy);
	} else {					/* default symbols */
		tb->symbols = scols_new_symbols();
		if (!tb->symbols)
			return -ENOMEM;
#if defined(HAVE_WIDECHAR)
		if (!scols_table_is_ascii(tb) &&
		    !strcmp(nl_langinfo(CODESET), "UTF-8")) {
			scols_symbols_set_branch(tb->symbols, UTF_VR UTF_H);
			scols_symbols_set_vertical(tb->symbols, UTF_V " ");
			scols_symbols_set_right(tb->symbols, UTF_UR UTF_H);
		} else
#endif
		{
			scols_symbols_set_branch(tb->symbols, "|-");
			scols_symbols_set_vertical(tb->symbols, "| ");
			scols_symbols_set_right(tb->symbols, "`-");
		}
	}

	return 0;
}
/**
 * scols_table_enable_colors:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable colors
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_enable_colors(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->colors_wanted = enable;
	return 0;
}
/**
 * scols_table_set_raw:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable raw
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_raw(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->raw = enable;
	return 0;
}
/**
 * scols_table_set_ascii:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable ascii
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_ascii(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->ascii = enable;
	return 0;
}
/**
 * scols_table_set_no_headings:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable no_headings
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_no_headings(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->no_headings = enable;
	return 0;
}
/**
 * scols_table_set_export:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable export
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_export(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->export = enable;
	return 0;
}
/**
 * scols_table_set_max:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable max
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_max(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->max = enable;
	return 0;
}

/**
 * scols_table_set_tree:
 * @tb: table
 * @enable: 1 or 0
 *
 * Enable/disable tree
 *
 * Returns: 0 on success, negative number in case of an error.
 */
int scols_table_set_tree(struct libscols_table *tb, int enable)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	tb->tree = enable;
	return 0;
}
/**
 * scols_table_colors_wanted:
 * @tb: table
 *
 * Get the value of colors_wanted
 *
 * Returns: colors_wanted flag value, negative value in case of an error.
 */
int scols_table_colors_wanted(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->colors_wanted;
}
/**
 * scols_table_is_raw:
 * @tb: table
 *
 * Get the value of raw
 *
 * Returns: raw flag value, negative value in case of an error.
 */
int scols_table_is_raw(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->raw;
}
/**
 * scols_table_is_ascii:
 * @tb: table
 *
 * Get the value of ascii
 *
 * Returns: ascii flag value, negative value in case of an error.
 */
int scols_table_is_ascii(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->ascii;
}
/**
 * scols_table_is_no_headings:
 * @tb: table
 *
 * Get the value of no_headings
 *
 * Returns: no_headings flag value, negative value in case of an error.
 */
int scols_table_is_no_headings(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->no_headings;
}
/**
 * scols_table_is_export:
 * @tb: table
 *
 * Get the value of export
 *
 * Returns: export flag value, negative value in case of an error.
 */
int scols_table_is_export(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->export;
}
/**
 * scols_table_is_max:
 * @tb: table
 *
 * Get the value of max
 *
 * Returns: max flag value, negative value in case of an error.
 */
int scols_table_is_max(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->max;
}
/**
 * scols_table_is_tree:
 * @tb: table
 *
 * Get the value of tree
 *
 * Returns: tree flag value, negative value in case of an error.
 */
int scols_table_is_tree(struct libscols_table *tb)
{
	assert(tb);
	if (!tb)
		return -EINVAL;
	return tb->tree;
}