toolbox

Check-in [80511aebc8]
Login

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

Overview
Comment:retab: adapt to new jtlst structure
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 80511aebc8526dea20612b6fc2b2f03879d8e8be54e42c0db2382a8951e098b6
User & Date: jef 2020-07-17 19:00:10
Context
2020-07-17
19:29
retab: use generic struct jtlst_str to store columns check-in: 8db1f2b13e user: jef tags: trunk
19:00
retab: adapt to new jtlst structure check-in: 80511aebc8 user: jef tags: trunk
18:59
lib: redesign jtlst check-in: 93148cea01 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/retab/retab.c.

4
5
6
7
8
9
10

















11
12
13
14
15
16
17
..
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
#include <string.h>
#include <getopt.h>
#include <sys/queue.h>
#include "jt.h"
#include "utf8.h"

#define PROGNAME "retab"


















void
usage()
{
	fprintf(stderr,
		"Retabulate a markdown table\n"
		"Usage:\n"
................................................................................
		return 0;
	if (str[strlen(str) - 1] != '|')
		return 0;

	return 1;
}

int
read_line(char **line)
{
	char *data = NULL;
	size_t linecap = 0;
	ssize_t res;

	res = getline(&data, &linecap, stdin);
	if (res == -1) {
		xfree(data);
		return 0;
	}

	str_trim(data);
	if (is_line(data) || is_row(data)) {
		*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 *rows_maxs, jtlst *row)
{
	void *data;




	if (jtlst_size(rows_maxs) != jtlst_size(row))

		die("rows with different number of fields");

	jtlstnode *iter1 = jtlst_iter(rows_maxs);

	jtlstnode *iter2 = jtlst_iter(row);
	while (iter1 && iter2) {
		iter1->data = (size_t *) max((size_t) iter1->data, (size_t) iter2->data);
		jtlst_next(&iter1, &data);
		jtlst_next(&iter2, &data);
	}



	return rows_maxs;
}

jtlst *
table_widths(jtlst *rows)
{
	jtlst *res = NULL;

	struct row *row;



	JTLST_FOREACH(row, rows) {
		if (row->is_line)
			continue;
		else if (res == NULL) {
			res = jtlst_clone(row->fields_widths);







			continue;
		}
		else
			res = max_widths(res, row->fields_widths);
	}

	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);
	}

	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;







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







 







|
|



<

|
<

|



|
<
|
|
<

|


>
|
<
<
<
<
|
|
<
<
<
|
>



|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<








>

|
<
|
<







>
>




|
|
>
>





|


|
|

>
>
>

<
>
|


|





|
|
|






|
|









<
>
|

|
>
>
>

<
>


<
>
|
<
<
<
<
|
>
>
|
<
|
<
|
|

<
>

>
>

|


<
|
>
>
>
>
>
>
>



|


|





|
|
|


|
|
|
>



|



|




<







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
..
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
#include <string.h>
#include <getopt.h>
#include <sys/queue.h>
#include "jt.h"
#include "utf8.h"

#define PROGNAME "retab"

struct col {
	struct jtlst_node node;
	char *str;
	int width;
};

struct row {
	struct jtlst_node node;
	struct jtlst cols;
	int is_line;
};

struct colwidth {
	struct jtlst_node node;
	size_t width;
};

void
usage()
{
	fprintf(stderr,
		"Retabulate a markdown table\n"
		"Usage:\n"
................................................................................
		return 0;
	if (str[strlen(str) - 1] != '|')
		return 0;

	return 1;
}

char *
read_line()
{
	char *data = NULL;
	size_t linecap = 0;


	if (getline(&data, &linecap, stdin) == -1) {

		xfree(data);
		return NULL;
	}

	str_trim(data);
	if (is_line(data) || is_row(data))

		return data;


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

void
row_parse_cols(struct row *row, char *str)




{
	struct jtlst fields;



	struct jtlst_str *field;
	struct col *col;

	str_del_prefix(str, "|");
	str_del_suffix(str, "|");
	jtlst_init(&fields, struct jtlst_str, node);
	str_split(str, "|", &fields);

	jtlst_foreach(field, &fields) {
		str_trim(field->str);
		col = xcalloc(1, sizeof(struct col));
		col->str = field->str;
		col->width = utf8len(col->str);
		jtlst_append(&row->cols, &col->node);
	}

	while ((field = jtlst_pop(&fields)))
		xfree(field);





}

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

	row = xcalloc(1, sizeof(struct row));
	jtlst_init(&(row->cols), struct col, node);
	row->is_line = is_line(str);
	if (! row->is_line)

		row_parse_cols(row, str);


	return row;
}

void
row_free(struct row *row)
{
	struct col *col;

	if (row == NULL)
		return;

	if (! row->is_line) {
		while ((col = jtlst_pop(&row->cols))) {
			xfree(col->str);
			xfree(col);
		}
	}
	xfree(row);
}

void
row_render(struct row *row, struct jtlst *maxwidths)
{
	int first = 1;
	size_t nchars, wsum = 0;
	char *space, *line;
	jtbuf buf;
	struct jtlst_iter i1, i2;
	struct colwidth *colwidth;
	struct col *col;


	jtlst_foreach(colwidth, maxwidths)
		wsum += (size_t) colwidth->width;

	if (row->is_line) {
		nchars = wsum + (jtlst_size(maxwidths) -1) * 3 + 4;
		line = str_repeat("-", nchars);
		puts(line);
		xfree(line);
	} else {
		buf = jtbuf_new();
		jtlst_iter(maxwidths, &i1);
		jtlst_iter(&row->cols, &i2);
		while ((colwidth = jtlst_iter_next(&i1)) && (col = jtlst_iter_next(&i2))) {
			if (first) {
				buf = jtbuf_append_str(buf, "| ");
				first = 0;
			} else {
				buf = jtbuf_append_str(buf, " | ");
			}
			buf = jtbuf_append_str(buf, col->str);
			space = str_repeat(" ", (size_t) colwidth->width - utf8len(col->str));
			buf = jtbuf_append_str(buf, space);
			xfree(space);
		}
		buf = jtbuf_append_str(buf, " |");
		puts(buf);
		jtbuf_free(buf);
	}
}


void
max_widths(struct jtlst *maxwidths, struct jtlst *cols)
{
	struct jtlst_iter i1;
	struct jtlst_iter i2;
	struct colwidth *colwidth;
	struct col *col;


	if (jtlst_size(maxwidths) != jtlst_size(cols))
		die("rows with different number of fields");


	jtlst_iter(maxwidths, &i1);
	jtlst_iter(cols, &i2);





	while ((colwidth = jtlst_iter_next(&i1)) && (col = jtlst_iter_next(&i2)))
		colwidth->width = max(colwidth->width, col->width);
}



struct jtlst *
table_widths(struct jtlst *rows)
{

	struct jtlst *maxwidths = NULL;
	struct row *row;
	struct col *col;
	struct colwidth *colwidth;

	jtlst_foreach(row, rows) {
		if (row->is_line)
			continue;

		else if (maxwidths == NULL) {
			maxwidths = xcalloc(1, sizeof(struct jtlst));
			jtlst_init(maxwidths, struct colwidth, node);
			jtlst_foreach(col, &row->cols) {
				colwidth = xcalloc(1, sizeof(struct colwidth));
				colwidth->width = col->width;
				jtlst_append(maxwidths, &colwidth->node);
			}
			continue;
		}
		else
			max_widths(maxwidths, &row->cols);
	}

	return maxwidths;
}

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

	jtlst_init(&rows, struct row, node);
	while ((line = read_line())) {
		row = row_new(line);
		jtlst_append(&rows, &row->node);
		xfree(line);
	}

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

	while ((row = jtlst_pop(&rows))) {
		row_render(row, widths);
		row_free(row);
	}


	jtlst_free(widths);
}

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