-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_11_entab.c
85 lines (72 loc) · 1.35 KB
/
ex5_11_entab.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
#include <stdio.h>
#include <stdlib.h>
#define IOMAXSIZE 100
#define TABSMAX 20
#define TABWIDTH 8
int my_getline(char s[], int lim)
{
int c, i;
i = 0;
while ((c = getchar()) != EOF && c != '\n' && i < lim) {
s[i++] = c;
}
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
void entab(char *output, char *input, int *tabstops, int nts)
{
int i, j, t, o, spaces;
i = o = t = spaces = 0;
while (*(input + i) != '\0') {
if (*(input + i) != ' ' || i == t) {
if (spaces && i == t) {
spaces = 0;
*(output + o++) = '\t';
}
else if (spaces && i != t) {
spaces = 0;
if (t == -1) {
for (j = 0; j < i/TABWIDTH; ++j) {
*(output + o++) = '\t';
}
}
while (o < i) {
*(output + o++) = ' ';
}
*(output + o++) = *(input + i);
}
else {
*(output + o++) = *(input + i);
}
}
else {
spaces = 1;
}
++i;
if (*(tabstops + t) < i && t < nts) {
++t;
if (t == nts) {
t = -1;
}
}
}
*(output + o) = '\0';
}
int main(int argc, char *argv[])
{
char input[IOMAXSIZE];
char output[IOMAXSIZE];
int tabstops[TABSMAX];
int nts = argc - 1;
int t = 0;
while (--argc > 0) {
tabstops[t++] = atoi(*++argv);
}
while (my_getline(input, IOMAXSIZE) > 0) {
entab(output, input, tabstops, nts);
printf("%s", output);
}
return 0;
}