-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfold-long-lines.c
165 lines (143 loc) · 3.5 KB
/
fold-long-lines.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
// used for testing remove this, just debugging
#include <string.h>
#define MAXLINE 1000
#define LAST_DISPLAY_COLUMN 15
#define START_AT_TEST 0
#define STOP_AT_TEST 6 /* 5 */
#define DEBUG 0
int my_getline(char line[], int len);
int debug_getline(char s[], int max_len);
int find_last_whitespace(char s[], int len);
int insert_char(char newChar, char s[], int index, int len);
int main()
{
int len, remaining_length, breakIndex, whitespace_index;
char line[MAXLINE];
#if STOP_AT_TEST
while ((len = debug_getline(line, MAXLINE)) > 0) {
printf("%s\n", line);
#else
while ((len = my_getline(line, MAXLINE)) > 0) {
#endif
// ignore the ending newline
len--;
breakIndex = 0;
while ((remaining_length = len - breakIndex) > LAST_DISPLAY_COLUMN) {
breakIndex += LAST_DISPLAY_COLUMN;
#if DEBUG
printf("remaining length: %d\n", remaining_length);
printf("break %c [%d]: \n", line[breakIndex], breakIndex);
#endif
if (len > LAST_DISPLAY_COLUMN) {
whitespace_index =
find_last_whitespace(line, breakIndex);
if (whitespace_index > 0) {
#if DEBUG
printf("last WS charachter: %d\n",
whitespace_index);
#endif
line[whitespace_index] = '\n';
breakIndex = whitespace_index + 1;
#if DEBUG
printf("%s\n", line);
#endif
} else {
#if DEBUG
printf("have to use a hyphen\n");
#endif
len = insert_char('-', line, breakIndex - 1, len);
len = insert_char('\n', line, breakIndex, len);
breakIndex += 2;;
}
}
}
printf("%s\n", line);
printf("----------\n");
}
}
int find_last_whitespace(char s[], int startIndex)
{
int i;
i= startIndex;
while (i > 0 && s[i] != '\n') {
if (s[i] == ' ' || s[i] == '\t') {
return i;
}
i--;
}
return -1;
}
/* my_getline: read a line into s, return length */
int my_getline(char s[], int len)
{
int c, i;
for(i=0; (c=getchar())!=EOF && c!='\n'; ++i) {
if(i<len-1) {
s[i] = c;
}
}
if(c == '\n') {
s[i] = c;
++i;
}
// do we need an ellipse?
if (i > len - 1) {
s[len - 4] = '.';
s[len - 3] = '.';
s[len - 2] = '.';
s[len - 1] = '\0';
} else {
s[i] = '\0';
}
return i;
}
int insert_char(char newChar, char s[], int index, int len)
{
int i;
char last, next;
last = newChar;
for(i = index; i <= len; i++) {
next = s[i];
s[i] = last;
last = next;
}
s[++len] = '\0';
return len;
}
int debug_getline(char s[], int _)
{
static int numberOfCalls = START_AT_TEST;
int len;
if (numberOfCalls >= STOP_AT_TEST)
return 0;
switch(numberOfCalls++) {
case 0:
len = strlen("this is a long line\n") + 1;
memcpy(s, "this is a long line\n", len);
break;
case 1:
len = strlen("this is already too long\n") + 1;
memcpy(s, "this is already too long\n", len);
break;
case 2:
len = strlen("this is already too long it really is\n") + 1;
memcpy(s, "this is already too long it really is\n", len);
break;
case 3:
len = strlen("123456789012345\n") + 1;
memcpy(s, "123456789012345\n", len);
break;
case 4:
len = strlen("0123456789012345\n") + 1;
memcpy(s, "0123456789012345\n", len);
break;
case 5:
len = strlen("this is a lesson in speed and effectiveness\n") + 1;
memcpy(s, "this is a lesson in speed and effectiveness\n", len);
break;
default:
len = 0;
}
return len;
}