summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/column.c
blob: 8e9b11fdd2495bdb33189db8d33640ff85d79ab4 (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
/*
 * column.c - functions for table handling at the column level
 *
 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
 * Copyright (C) 2014 Karel Zak <kzak@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 <ctype.h>

#include "smartcolsP.h"

struct libscols_column *scols_new_column(void)
{
	struct libscols_column *cl;

	cl = calloc(1, sizeof(*cl));
	if (!cl)
		return NULL;

	cl->refcount = 1;
	INIT_LIST_HEAD(&cl->cl_columns);
	return cl;
}

void scols_ref_column(struct libscols_column *cl)
{
	if (cl)
		cl->refcount++;
}

void scols_unref_column(struct libscols_column *cl)
{
	if (cl && --cl->refcount <= 0) {
		list_del(&cl->cl_columns);
		scols_reset_cell(&cl->header);
		free(cl->color);
		free(cl);
	}
}

struct libscols_column *scols_copy_column(const struct libscols_column *cl)
{
	struct libscols_column *ret;

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

	if (scols_column_set_color(ret, cl->color))
		goto err;
	if (scols_cell_copy_content(&ret->header, &cl->header))
		goto err;

	ret->width	= cl->width;
	ret->width_min	= cl->width_min;
	ret->width_max	= cl->width_max;
	ret->width_avg	= cl->width_avg;
	ret->width_hint	= cl->width_hint;
	ret->flags	= cl->flags;
	ret->is_extreme = cl->is_extreme;

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

int scols_column_set_whint(struct libscols_column *cl, double whint)
{
	assert(cl);

	if (!cl)
		return -EINVAL;

	cl->width_hint = whint;
	return 0;
}

double scols_column_get_whint(struct libscols_column *cl)
{
	assert(cl);
	return cl ? cl->width_hint : -EINVAL;
}

int scols_column_set_flags(struct libscols_column *cl, int flags)
{
	assert(cl);

	if (!cl)
		return -EINVAL;

	cl->flags = flags;
	return 0;
}

int scols_column_get_flags(struct libscols_column *cl)
{
	assert(cl);
	return cl ? cl->flags : -EINVAL;
}

struct libscols_cell *scols_column_get_header(struct libscols_column *cl)
{
	assert(cl);
	return cl ? &cl->header : NULL;
}

/*
 * The default color for data cells and column header.
 *
 * If you want to set header specific color then use scols_column_get_header()
 * and scols_cell_set_color().
 *
 * If you want to set data cell specific color the use scols_line_get_cell() +
 * scols_cell_set_color().
 */
int scols_column_set_color(struct libscols_column *cl, const char *color)
{
	char *p = NULL;

	assert(cl);
	if (!cl)
		return -EINVAL;
	if (color) {
		if (isalnum(*color)) {
			color = colorscheme_from_string(color);

			if (!color)
				return -EINVAL;
		}
		p = strdup(color);
		if (!p)
			return -ENOMEM;
	}

	free(cl->color);
	cl->color = p;
	return 0;
}

const char *scols_column_get_color(struct libscols_column *cl)
{
	assert(cl);
	return cl ? cl->color : NULL;
}

/**
 * scols_column_is_trunc:
 * @cl: column
 *
 * Get the value of trunc
 *
 * Returns: trunc flag value, negative value in case of an error.
 */
int scols_column_is_trunc(struct libscols_column *cl)
{
	assert(cl);
	if (!cl)
		return -EINVAL;
	return cl->flags & SCOLS_FL_TRUNC;
}
/**
 * scols_column_is_tree:
 * @cl: column
 *
 * Get the value of tree
 *
 * Returns: tree flag value, negative value in case of an error.
 */
int scols_column_is_tree(struct libscols_column *cl)
{
	assert(cl);
	if (!cl)
		return -EINVAL;
	return cl->flags & SCOLS_FL_TREE;
}
/**
 * scols_column_is_right:
 * @cl: column
 *
 * Get the value of right
 *
 * Returns: right flag value, negative value in case of an error.
 */
int scols_column_is_right(struct libscols_column *cl)
{
	assert(cl);
	if (!cl)
		return -EINVAL;
	return cl->flags & SCOLS_FL_RIGHT;
}
/**
 * scols_column_is_strict_width:
 * @cl: column
 *
 * Get the value of strict_width
 *
 * Returns: strict_width flag value, negative value in case of an error.
 */
int scols_column_is_strict_width(struct libscols_column *cl)
{
	assert(cl);
	if (!cl)
		return -EINVAL;
	return cl->flags & SCOLS_FL_STRICTWIDTH;
}
/**
 * scols_column_is_no_extremes:
 * @cl: column
 *
 * Get the value of no_extremes
 *
 * Returns: no_extremes flag value, negative value in case of an error.
 */
int scols_column_is_no_extremes(struct libscols_column *cl)
{
	assert(cl);
	if (!cl)
		return -EINVAL;
	return cl->flags & SCOLS_FL_NOEXTREMES;
}