working on revised pcb layout
[hw/telelco] / retab
1 #!/usr/bin/nickle
2
3 string[*][*]    lines;
4 int[*]          widths;
5
6 string[*] get_one(file in) {
7         string  l = File::fgets(in);
8         return String::wordsplit(l, "\t");
9 }
10
11 string[*][*] get_all(file in) {
12         string[...][*] l = {};
13         while (!File::end(in))
14                 l[dim(l)] = get_one(in);
15         return l;
16 }
17
18 int[*] find_widths(string[*][*] lines) {
19         int[...]        w = {};
20         for (int r = 0; r < dim(lines); r++) {
21                 for (int c = 0; c < dim(lines[r]); c++) {
22                         int     len = String::length(lines[r][c]);
23                         if (c >= dim(w))
24                                 w[c] = len;
25                         else
26                                 w[c] = max(w[c], len);
27                 }
28         }
29         return w;
30 }
31
32 void print_one(string s, int w) {
33         int     l = String::length(s);
34         printf ("%s ", s);
35         while (l < w) {
36                 putchar(' ');
37                 l++;
38         }
39 }
40
41 void print_line(string[*] line) {
42         for (int c = 0; c < dim(line); c++)
43                 print_one(line[c], widths[c]);
44         putchar('\n');
45 }
46
47 void print_all() {
48         for (int r = 0; r < dim(lines); r++)
49                 print_line(lines[r]);
50 }
51
52 void doit () {
53         lines = get_all(stdin);
54         widths = find_widths(lines);
55         print_all();
56 }
57
58 doit();