summaryrefslogtreecommitdiffstats
path: root/utils/libsmartcols/src/symbols.c
blob: 2fadfc7ad246553e89037566e0d6b349088a0e41 (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
/*
 * symbols.c - routines for symbol handling
 *
 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
 * Copyright (C) 2016 Igor Gnatenko <i.gnatenko.brain@gmail.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

/**
 * SECTION: symbols
 * @title: Symbols
 * @short_description: can be used to overwrite default output chars (for ascii art)
 *
 * An API to access and modify data and information per symbol/symbol group.
 */


#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "smartcolsP.h"

/**
 * scols_new_symbols:
 *
 * Returns: a pointer to a newly allocated struct libscols_symbols instance.
 */
struct libscols_symbols *scols_new_symbols(void)
{
	struct libscols_symbols *sy = calloc(1, sizeof(struct libscols_symbols));

	if (!sy)
		return NULL;
	sy->refcount = 1;
	return sy;
}

/**
 * scols_ref_symbols:
 * @sy: a pointer to a struct libscols_symbols instance
 *
 * Increases the refcount of @sy.
 */
void scols_ref_symbols(struct libscols_symbols *sy)
{
	if (sy)
		sy->refcount++;
}

/**
 * scols_unref_symbols:
 * @sy: a pointer to a struct libscols_symbols instance
 *
 * Decreases the refcount of @sy.
 */
void scols_unref_symbols(struct libscols_symbols *sy)
{
	if (sy && --sy->refcount <= 0) {
		free(sy->tree_branch);
		free(sy->tree_vert);
		free(sy->tree_right);
		free(sy->group_last_member);
		free(sy->group_middle_member);
		free(sy->group_first_member);
		free(sy->group_vert);
		free(sy->group_horz);
		free(sy->group_last_child);
		free(sy->group_middle_child);
		free(sy->title_padding);
		free(sy->cell_padding);
		free(sy);
	}
}

/**
 * scols_symbols_set_branch:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the branch part of a tree output
 *
 * Returns: 0, a negative value in case of an error.
 */
int scols_symbols_set_branch(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, tree_branch, str);
}

/**
 * scols_symbols_set_vertical:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the vertical part of a tree output
 *
 * Returns: 0, a negative value in case of an error.
 */
int scols_symbols_set_vertical(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, tree_vert, str);
}

/**
 * scols_symbols_set_right:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the right part of a tree output
 *
 * Returns: 0, a negative value in case of an error.
 */
int scols_symbols_set_right(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, tree_right, str);
}

/**
 * scols_symbols_set_title_padding:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the symbols which fill title output
 *
 * The current implementation uses only the first byte from the padding string.
 * A multibyte chars are not supported yet.
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.28
 */
int scols_symbols_set_title_padding(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, title_padding, str);
}

/**
 * scols_symbols_set_cell_padding:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the symbols which fill cells
 *
 * The padding char has to take up just one cell on the terminal.
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.29
 */
int scols_symbols_set_cell_padding(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, cell_padding, str);
}


/**
 * scols_symbols_set_group_vertical:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the vertival line
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_vertical(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_vert, str);
}

/**
 * scols_symbols_set_group_horizontal:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent the horizontal line
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_horizontal(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_horz, str);
}

/**
 * scols_symbols_set_group_first_member:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent first member
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_first_member(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_first_member, str);
}

/**
 * scols_symbols_set_group_last_member:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent last member
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_last_member(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_last_member, str);
}

/**
 * scols_symbols_set_group_middle:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent middle member
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_middle_member(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_middle_member, str);
}

/**
 * scols_symbols_set_group_last_child:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent last child
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_last_child(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_last_child, str);
}

/**
 * scols_symbols_set_group_middle_child:
 * @sy: a pointer to a struct libscols_symbols instance
 * @str: a string which will represent last child
 *
 * Returns: 0, a negative value in case of an error.
 *
 * Since: 2.34
 */
int scols_symbols_set_group_middle_child(struct libscols_symbols *sy, const char *str)
{
	return strdup_to_struct_member(sy, group_middle_child, str);
}

/**
 * scols_copy_symbols:
 * @sy: a pointer to a struct libscols_symbols instance
 *
 * Returns: a newly allocated copy of the @sy symbol group or NULL in case of an error.
 */
struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sy)
{
	struct libscols_symbols *ret;
	int rc;

	assert(sy);
	if (!sy)
		return NULL;

	ret = scols_new_symbols();
	if (!ret)
		return NULL;

	rc = scols_symbols_set_branch(ret, sy->tree_branch);
	if (!rc)
		rc = scols_symbols_set_vertical(ret, sy->tree_vert);
	if (!rc)
		rc = scols_symbols_set_right(ret, sy->tree_right);
	if (!rc)
		rc = scols_symbols_set_group_vertical(ret, sy->group_vert);
	if (!rc)
		rc = scols_symbols_set_group_horizontal(ret, sy->group_horz);
	if (!rc)
		rc = scols_symbols_set_group_first_member(ret, sy->group_first_member);
	if (!rc)
		rc = scols_symbols_set_group_last_member(ret, sy->group_last_member);
	if (!rc)
		rc = scols_symbols_set_group_middle_member(ret, sy->group_middle_member);
	if (!rc)
		rc = scols_symbols_set_group_middle_child(ret, sy->group_middle_child);
	if (!rc)
		rc = scols_symbols_set_group_last_child(ret, sy->group_last_child);
	if (!rc)
		rc = scols_symbols_set_title_padding(ret, sy->title_padding);
	if (!rc)
		rc = scols_symbols_set_cell_padding(ret, sy->cell_padding);
	if (!rc)
		return ret;

	scols_unref_symbols(ret);
	return NULL;
}