forked from michiguel/Ordo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
justify.c
139 lines (114 loc) · 2.62 KB
/
justify.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
138
/*
ordoprep is program for preparing/compacting input for Ordo
Copyright 2016 Miguel A. Ballicora
This file is part of ordoprep.
ordoprep is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ordoprep is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ordoprep. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stddef.h>
#include "justify.h"
static bool_t
iswordbreak (int c)
{
return c == '\0' || isspace(c);
}
static const char *
goto_nextstop(const char *p, ptrdiff_t rem)
{
const char *q = p;
while (q < p + rem && !iswordbreak(*q)) {
q++;
}
return q;
}
static const char *
print_stretch(FILE *f, const char *sta, const char *end)
{
while (*sta && sta < end) {
fprintf(f,"%c", *sta++);
}
return sta;
}
static bool_t
fit_horizontally(const char *p, ptrdiff_t HOR)
{
ptrdiff_t i;
bool_t found = FALSE;
for (i = 0; !found && i < (HOR+1); i++) {
found = iswordbreak(p[i]);
}
return found;
}
static void
carriage(FILE *f, int m)
{
while (m-->0) fprintf(f," ");
}
static void
carriage_return(FILE *f, int m)
{
fprintf (f,"\n");
carriage(f, m);
}
//----------------------------------------------------
void
fprint_justified (FILE *f, const char *p, int blankheader, int margin, int hor)
{
const char *q;
ptrdiff_t rem, HOR;
assert (margin >= 0);
assert (hor > 0);
HOR = (ptrdiff_t)hor;
rem = HOR;
carriage(f,blankheader);
while (*p != '\0') {
if (rem == HOR && isspace(*p)) {
p++;
continue;
}
if (rem > 0 && isspace(*p)) {
fprintf(f,"%s", *p=='\n'?"\n":" ");
p++;
rem--;
continue;
}
if (rem == 0) {
carriage_return(f,margin);
rem = HOR;
continue;
}
q = goto_nextstop(p, rem);
if (iswordbreak(*q)) {
const char *x;
x = print_stretch (f, p, q);
rem -= q - p;
p = x;
} else {
if (fit_horizontally(p,HOR)) {
carriage_return(f,margin);
rem = HOR;
} else {
print_stretch (f, p, p + rem);
p += rem;
rem = HOR;
carriage_return(f, margin);
}
}
} //loop
} //function
void
print_justified (const char *p, int blankheader, int margin, int hor)
{
fprint_justified (stdout, p, blankheader, margin, hor);
}