aboutsummaryrefslogtreecommitdiff
path: root/1-22_wordwrap.c
blob: 83acfd6259c3aaecacd5c704f9dd8e5938c32961 (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
73
74
75
#include <stdio.h>

/* The C Programming Language: 2nd Edition
 * Exercise 1-22:
 * "Write a program to 'fold' long input lines into two or more shorter lines
 * after the last non-blank character that occurs before the n-th column of
 * input. Make sure your program does something intelligent with very long
 * lines, and if there are no blanks or tabs before the specified column."
 *
 * So... Quite a hefty requirement. In a nutshell, our goal is to create sane
 * hard-wrapping. This is a common function in text editors, and it's
 * important to get it right or the results are wonky.
 *
 *
 * TODO: get_line() is not fully correct. When it hits a \t, it counts it as
 * one character and has no concept of display count. I'll fix this later on.
 */

// For tradition's sake, let's wrap at 80 columns
#define MAXLEN 80

char data[MAXLEN];
int i, j, k;

int get_line(char s[], int lim) {
	/* Put as much as possible into a temp string, and count its length */
	int c, i;

	for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i) {
		s[i] = c;
	}
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\0';
	return i;
}

/* Find the first blank character, starting from the end of the string. Returns
 * the position of the blank, or -1 if one wasn't found.
 */
int b_find_blank(char s[], int lim) {
	// Start at the end of the string and go backwards.
	for (i = lim; i >= 0; i--) {
		// Simply replace the first blank with a newline.
		if (s[i] == ' ' || s[i] == '\t') {
			return i;
		}
	}
	return -1;
}

int main() {
	while (j = get_line(data, MAXLEN)) {
		if (j == 80) {
			// We know it's a long line now. Let's make sure we're breaking in
			// the right place
			k = b_find_blank(data, MAXLEN);
			//printf("%d\n", k);
			if (k > -1) {
				data[k] = '\n';
				data[MAXLEN] = '\0';
				printf("%s", data);
				continue;
			} else {
				data[MAXLEN] = '\0';
				printf("%s\n", data);
				continue;
			}
		}
		printf("%s", data);
	}

}