aboutsummaryrefslogtreecommitdiff
path: root/ch5/5-11_entab-remixed.c
blob: d5173656f586dbc8611954e82fe5c187d7a3ee14 (plain)
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
#include <stdio.h>
#include <stdlib.h>

/* The C Programming Language: 2nd Edition
 *
 * Exercise 5-11: Modify the programs entab and detab (written as exercises in
 * Chapter 1) to accept a list of tab stops as arguments. Use the default tab
 * settings if there are no arguments.
 */

/* The directions for this exercise aren't terribly clear. The way I see it,
 * each argument is a character column that all tabs should align to. For the
 * entab portion, I should count 2+ spaces forward and, if I reach a tabstop,
 * insert a tab. Then move onto the next argument. If 2+ spaces are present,
 * but don't reach the tabstop threshold, the spaces are left alone. This is
 * the best I could manage with the ambiguous instructions.
 */

#define TABWIDTH 8

int main(int argc, char *argv[]) {
	int column, c, spaces, tabnum, stop;
	spaces = column = 0;
	if (argc > 1) {
		tabnum = 1;
		stop = atoi(argv[tabnum]);
	} else {
		tabnum = 0;
	}
	while ((c = getchar()) != EOF) {
		column++;
		if (c == ' ') {
			spaces++;
			if (argc > 1) {
				if (column == stop) {
					if (tabnum < (argc - 1)) {
						stop = atoi(argv[++tabnum]);
					}
					if (spaces > 1) {
						putchar('\t');
						spaces = 0;
					}
				} else if (column > stop && tabnum < (argc - 1)) {
					stop = atoi(argv[++tabnum]);
				}
			} else {
				// we need to do default tabstopping
				if (column % TABWIDTH == 0 && spaces > 1) {
					putchar('\t');
					spaces = 0;
				}
			}
		} else {
			// output all extra spaces first
			while (spaces > 0) {
				putchar(' ');
				spaces--;
			}
			// reset the counts and argument position on a newline
			if (c == '\n') {
				column = 0;
				spaces = 0;
				if (tabnum > 0) {
					tabnum = 1;
					stop = atoi(argv[tabnum]);
				}
			}
			putchar(c);
		}
	}
	return 0;
}