-
Notifications
You must be signed in to change notification settings - Fork 0
/
entab.c
70 lines (58 loc) · 1.64 KB
/
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
/* Replaces string of spaces with minimum number of tabs and spaces to achieve indentation */
#include <stdio.h>
#include <stdlib.h>
#define TABSTOP 4
#define MAXLINE 1000
int getline(char line[], int maxline);
int entab(char line[], int len, int tabstop);
main(int argc, char *argv[])
{
int len, tabstop;
char line[MAXLINE];
tabstop = 0;
if(argc == 2)
tabstop = atoi(argv[1]); /* assign the tabstop */
while((len = getline(line, MAXLINE)) > 0){
len = entab(line, len, tabstop);
printf("%s", line);
}
return 0;
}
/* getline: read a line, return length */
int getline(char line[], int maxline)
{
int c; /* character returned by getchar */
int i; /* array index*/
/* fill line until we reach maxline-1 length, EOF or newline */
for(i = 0; i < maxline-1 && (c=getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
/* add newline character and null termination character */
if(c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
int entab(char line[], int len, int tabstop)
{
int state, nspace;
int i, j;
if (!tabstop)
tabstop = TABSTOP;
nspace = 0;
for (i = 0; i < len; ++i) {
if (line[i] == ' ') {
++nspace;
if (nspace == tabstop) {
line[i-tabstop+1] = '\t'; /* replace space with tab */
for (j = i-tabstop+2; j < len; ++j) /* shift left by tabstop-1 */
line[j] = line[j+tabstop-1];
len -= tabstop - 1;
i -= tabstop - 1;
nspace = 0;
}
} else
nspace = 0;
}
}