toolbox

Check-in [8db1f2b13e]
Login

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

Overview
Comment:retab: use generic struct jtlst_str to store columns
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8db1f2b13e6c1aa8cc63c46d4663d272533241088bf694d8ef223f859e982561
User & Date: jef 2020-07-17 19:29:32
Context
2020-07-18
08:46
lib: rework jtlst_iter check-in: 8b222eb2dc user: jef tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/retab/retab.c.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
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
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
...
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
#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 {
................................................................................
	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);
................................................................................
{
	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);
................................................................................

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







<
<
<
<
<
<







 







<
|
<



|
|

|
|
<
<
<
<
<
<
<
<








|










|







 







|







 







|








|







|










|







5
6
7
8
9
10
11






12
13
14
15
16
17
18
..
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
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
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
#include <getopt.h>
#include <sys/queue.h>
#include "jt.h"
#include "utf8.h"

#define PROGNAME "retab"







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

struct colwidth {
................................................................................
	die("invalid line '%s'", data);
	return NULL;
}

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

	struct jtlst_str *col;


	str_del_prefix(str, "|");
	str_del_suffix(str, "|");
	jtlst_init(&row->cols, struct jtlst_str, node);
	str_split(str, "|", &row->cols);

	jtlst_foreach(col, &row->cols)
		str_trim(col->str);








}

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

	row = xcalloc(1, sizeof(struct row));
	jtlst_init(&row->cols, struct jtlst_str, 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 jtlst_str *col;

	if (row == NULL)
		return;

	if (! row->is_line) {
		while ((col = jtlst_pop(&row->cols))) {
			xfree(col->str);
................................................................................
{
	int first = 1;
	size_t nchars, wsum = 0;
	char *space, *line;
	jtbuf buf;
	struct jtlst_iter i1, i2;
	struct colwidth *colwidth;
	struct jtlst_str *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);
................................................................................

void
max_widths(struct jtlst *maxwidths, struct jtlst *cols)
{
	struct jtlst_iter i1;
	struct jtlst_iter i2;
	struct colwidth *colwidth;
	struct jtlst_str *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, utf8len(col->str));
}

struct jtlst *
table_widths(struct jtlst *rows)
{
	struct jtlst *maxwidths = NULL;
	struct row *row;
	struct jtlst_str *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 = utf8len(col->str);
				jtlst_append(maxwidths, &colwidth->node);
			}
			continue;
		}
		else
			max_widths(maxwidths, &row->cols);
	}