toolbox

Check-in [7fcee8800c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:retab: use struct to rearrange the code
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7fcee8800cf00e29cc54a7d4c4d954d7e19251deeb4a8173f102f635d81c3142
User & Date: jef 2020-05-01 08:42:59
Context
2020-05-01
11:44
retab: fix memory leak check-in: 87d8a0bbf2 user: jef tags: trunk
08:42
retab: use struct to rearrange the code check-in: 7fcee8800c user: jef tags: trunk
2020-04-29
13:11
retab: cosmetic changes check-in: 040954acea user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/retab/retab.c.

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
...
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
		*line = data;
		return 1;
	}

	die("invalid line '%s'", data);
	return 0;
}







jtlst *
line_row(char *line)

{
	jtlst *row;
	char *field;

	str_del_prefix(line, "|");
	str_del_suffix(line, "|");
	row = str_split(line, "|");
	JTLST_FOREACH(field, row)
		str_trim(field);

	return row;
}

jtlst *
row_widths(jtlst *row)
{
	jtlst *widths;
	char *field;

	widths = jtlst_new();
	JTLST_FOREACH(field, row)
		jtlst_append(widths, (void *)utf8len(field));

	return widths;
}



































































jtlst *
max_widths(jtlst *r1, jtlst *r2)
{
	jtlst *res;
	size_t *v1, *v2;

................................................................................
	while (jtlst_next(&iter1, (void **) &v1) && jtlst_next(&iter2, (void **) &v2))
		jtlst_append(res, (size_t *) max((size_t) v1, (size_t) v2));

	return res;
}

jtlst *
table_widths(jtlst *lines)
{
	jtlst *widths, *width, *row, *res, *tmp;
	char *line;


	widths = jtlst_new();
	JTLST_FOREACH(line, lines) {
		if (is_line(line))

			continue;
		row = line_row(line);
		jtlst_append(widths, row_widths(row));
		jtlst_free_all(row);
	}

	if (jtlst_size(widths) == 0) {
		jtlst_free(widths);
		return NULL;
	}

	jtlst_pop(widths, (void **) &res);
	while (jtlst_pop(widths, (void **) &width)) {
		tmp = max_widths(res, width);

		jtlst_free(res);
		jtlst_free(width);


		res = tmp;
	}

	jtlst_free(widths);

	return res;
}

jtlst *
table_rows(jtlst *lines)
{
	jtlst *rows;
	char *line;

	rows = jtlst_new();
	JTLST_FOREACH(line, lines)
		jtlst_append(rows, line_row(line));

	return rows;
}

void
line_render(char *line, jtlst *widths)
{
	int first = 1;
	size_t *width, nchars, wsum = 0;
	char *field, *space;
	jtlst *fields;
	jtbuf buf;

	JTLST_FOREACH(width, widths)
		wsum += (size_t) width;

	if (is_line(line)) {
		nchars = wsum + (jtlst_size(widths) -1) * 3 + 4;
		line = str_repeat("-", nchars);
		puts(line);
		xfree(line);
	} else {
		buf = jtbuf_new();
		fields = line_row(line);
		jtlstnode *f = jtlst_iter(fields);
		jtlstnode *w = jtlst_iter(widths);
		while (jtlst_next(&f, (void **) &field) && jtlst_next(&w, (void **) &width)) {
			if (first) {
				buf = jtbuf_append_str(buf, "| ");
				first = 0;
			} else {
				buf = jtbuf_append_str(buf, " | ");
			}
			buf = jtbuf_append_str(buf, field);
			space = str_repeat(" ", (size_t) width - utf8len(field));
			buf = jtbuf_append_str(buf, space);
			xfree(space);
		}
		buf = jtbuf_append_str(buf, " |");
		puts(buf);
		jtlst_free_all(fields);
		jtbuf_free(buf);
	}
}

void
retab()
{
	char *line = NULL;

	jtlst *lines, *widths;


	lines = jtlst_new();
	while (read_line(&line))
		jtlst_append(lines, line);


	if (jtlst_size(lines) == 0)
		return;

	widths = table_widths(lines);
	if (widths == NULL)
		die("abort, table without rows");

	JTLST_FOREACH(line, lines)
		line_render(line, widths);



	jtlst_free_all(lines);
	jtlst_free(widths);
}

int
main(int argc, char *argv[])
{
	int ch;








>
>
>
>
>
>

<
>

|


|
|
|
|


|



|





|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|

|
|
>

|
|
<
>

<
|
<


|
|



|
|

>
|
<
>
>



|




<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



>
|
>

|

|
>

|


|



|
|
>
|
>
|







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
...
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
		*line = data;
		return 1;
	}

	die("invalid line '%s'", data);
	return 0;
}

struct row {
	int is_line;
	jtlst *fields;
	jtlst *fields_widths;
};

jtlst *

row_fields(char *str)
{
	jtlst *fields;
	char *field;

	str_del_prefix(str, "|");
	str_del_suffix(str, "|");
	fields = str_split(str, "|");
	JTLST_FOREACH(field, fields)
		str_trim(field);

	return fields;
}

jtlst *
fields_widths(jtlst *fields)
{
	jtlst *widths;
	char *field;

	widths = jtlst_new();
	JTLST_FOREACH(field, fields)
		jtlst_append(widths, (void *)utf8len(field));

	return widths;
}

struct row *
row_new(char *str)
{
	struct row *row;

	row = xcalloc(1, sizeof(struct row));
	row->is_line = is_line(str);
	if (! row->is_line) {
		row->fields = row_fields(str);
		row->fields_widths = fields_widths(row->fields);
	}

	return row;
}

void
row_free(struct row *row)
{
	if (row == NULL)
		return;

	if (! row->is_line) {
		jtlst_free_all(row->fields);
		jtlst_free(row->fields_widths);
	}
	xfree(row);
}

void
row_render(struct row *row, jtlst *widths)
{
	int first = 1;
	size_t *width, nchars, wsum = 0;
	char *field, *space, *line;
	jtbuf buf;

	JTLST_FOREACH(width, widths)
		wsum += (size_t) width;

	if (row->is_line) {
		nchars = wsum + (jtlst_size(widths) -1) * 3 + 4;
		line = str_repeat("-", nchars);
		puts(line);
		xfree(line);
	} else {
		buf = jtbuf_new();
		jtlstnode *f = jtlst_iter(row->fields);
		jtlstnode *w = jtlst_iter(widths);
		while (jtlst_next(&f, (void **) &field) && jtlst_next(&w, (void **) &width)) {
			if (first) {
				buf = jtbuf_append_str(buf, "| ");
				first = 0;
			} else {
				buf = jtbuf_append_str(buf, " | ");
			}
			buf = jtbuf_append_str(buf, field);
			space = str_repeat(" ", (size_t) width - utf8len(field));
			buf = jtbuf_append_str(buf, space);
			xfree(space);
		}
		buf = jtbuf_append_str(buf, " |");
		puts(buf);
		jtbuf_free(buf);
	}
}

jtlst *
max_widths(jtlst *r1, jtlst *r2)
{
	jtlst *res;
	size_t *v1, *v2;

................................................................................
	while (jtlst_next(&iter1, (void **) &v1) && jtlst_next(&iter2, (void **) &v2))
		jtlst_append(res, (size_t *) max((size_t) v1, (size_t) v2));

	return res;
}

jtlst *
table_widths(jtlst *rows)
{
	jtlst *allwidths, *width, *res, *tmp;
	struct row *row;
	int first = 1;

	allwidths = jtlst_new();
	JTLST_FOREACH(row, rows) {

		if (row->is_line)
			continue;

		jtlst_append(allwidths, row->fields_widths);

	}

	if (jtlst_size(allwidths) == 0) {
		jtlst_free(allwidths);
		return NULL;
	}

	jtlst_pop(allwidths, (void **) &res);
	while (jtlst_pop(allwidths, (void **) &width)) {
		tmp = max_widths(res, width);
		if (! first) {
			jtlst_free(res);

		}
		first = 0;
		res = tmp;
	}

	jtlst_free(allwidths);

	return res;
}














void









































retab()
{
	char *line = NULL;
	jtlst *rows;
	jtlst *widths;
	struct row *row;

	rows = jtlst_new();
	while (read_line(&line))
		jtlst_append(rows, row_new(line));
	xfree(line);

	if (jtlst_size(rows) == 0)
		return;

	widths = table_widths(rows);
	if (widths == NULL)
		die("abort, table without rows");

	JTLST_FOREACH(row, rows) {
		row_render(row, widths);
		row_free(row);
	}

	jtlst_free(rows);
	jtlst_free(widths);
}

int
main(int argc, char *argv[])
{
	int ch;