forked from stengel/ecta2002
-
Notifications
You must be signed in to change notification settings - Fork 0
/
col.c
137 lines (125 loc) · 3.63 KB
/
col.c
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
/* col.c */
/* automatic pretty printing in columns */
/* 17 Apr 2000 */
/* Bernhard von Stengel stengel@maths.lse.ac.uk */
#include <stdio.h>
#include <stdlib.h>
/* malloc, calloc, atoi */
#include <stddef.h>
/* typedef unsigned size_t; */
#include <string.h>
/* strcmp, strlen */
#include "col.h"
/* contains in succession all strings generated by colpr,
* each terminated by '\0'
*/
static char buf[COLBUFSIZE];
static int posinbuf; /* first free byte is buf[posinbuf] */
static int ncols; /* number of columns */
static int *colwidth; /* [0..ncols-1] output widths */
static int *widthsign; /* -1 for left, 1 for right adjustment */
static int currlines; /* no of fully printed lines */
static int currcol; /* current column, modulo ncols */
/* --------- routines ----------------------------------------- */
void colset(int c)
{
int j;
if (c < 1)
{
fprintf(stderr, "colset requires positive no of cols, not %d\n", c);
fprintf(stderr, "Emergency stop.\n");
exit(1);
}
if (ncols) /* allocation has taken place before */
/* free space of previously allocated arrays */
{
free(colwidth);
free(widthsign);
}
colwidth = (int *) calloc(c, sizeof(int)); /* init to 0 */
widthsign = (int *) calloc(c, sizeof(int));
if (widthsign == NULL)
{
fprintf(stderr, "colset is out of memory for %d columns.\n", c);
fprintf(stderr, "Emergency stop.\n");
exit(1);
}
for (j=0; j < c; j++)
widthsign[j] = 1;
ncols = c;
posinbuf = 0;
currcol = 0;
currlines = 0;
}
void colipr(int i)
{
char s[ISTR];
sprintf(s, "%d", i);
colpr(s);
}
void colleft(int c)
/* making column c in 0..ncols-1 left-adjusted */
{
widthsign[c] = -1;
}
/* print the first cols strings starting at s
* followed by "\n". Print nothing if cols==0.
* return pointer to the next position.
*/
static char * prline (char *s, int cols)
{
int j;
for (j=0; j < cols; j++)
{
printf("%*s", colwidth[j] * widthsign[j], s);
s += strlen(s) + 1;
if (j < cols - 1) /* avoid trailing blanks in line */
printf(" "); /* more sophisticated spacing possible */
else
printf("\n"); /* in loop to prevent newline if 0 cols */
}
return s;
}
void colout(void)
{
int i;
char *s;
s = buf;
for (i=0; i < currlines; i++)
s = prline(s, ncols);
prline(s, currcol);
}
void colpr(const char *s)
{
int j;
int w = strlen(s);
if (posinbuf + w + 1 > COLBUFSIZE)
/* not enough space in current buffer, flush */
/* require w < COLBUFSIZE, otherwise undefined behavior */
{
int cc = currcol;
colout();
printf("\n-----output reset-----\n\n");
colset(ncols);
for (j=0; j < cc; j++)
/* print blank columns to continue in the correct column */
buf[posinbuf++] = '\0';
currcol = cc;
}
if (colwidth[currcol] < w)
colwidth[currcol] = w;
strcpy(&buf[posinbuf], s);
posinbuf += w + 1;
currcol++;
if (currcol==ncols) /* this requires ncols > 0 */
{
currcol = 0;
currlines++;
}
}
void colnl(void)
{
int j;
for (j=currcol; j < ncols; j++)
colpr("");
}