-
Notifications
You must be signed in to change notification settings - Fork 1
/
entab2.c
287 lines (260 loc) · 6.73 KB
/
entab2.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
// used for testing remove this, just debugging
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAXLINE 1000
#define DEFAULT_TABSTOP_SIZE 8
#define DEBUG 0
#define TEST 1
#define MAX_MANUAL_TABSTOPS 10
//#if DEBUG
//#define SPACE '-'
//#else
#define SPACE ' '
//#endif
int my_getline(char line[], int len);
int debug_getline(char s[], int max_len);
int use_tabs(char s[], int len);
int remove_chars(char s[], int len, int startIndex, int endIndex);
//TODO: we assume that the tab stops are > 0 and sorted lowest first
static int manual_tab_stops[MAX_MANUAL_TABSTOPS] = {0};
bool is_tab_stop(int index);
/* use strings of blanks by the minimum number of tabs
* and blanks to achieve the same spacing */
int main(int argc, char *argv[])
{
int len, newlen;
char line[MAXLINE];
if (argc > 1) {
//load args into manual tab stop list
for(int argindex = 1; argindex < argc; argindex++) {
manual_tab_stops[argindex - 1] = atoi(argv[argindex]);
}
} else if (argc > MAX_MANUAL_TABSTOPS) {
printf("Please enter less than %d tab stops.\n", MAX_MANUAL_TABSTOPS);
return -1;
}
#if TEST
char original_line[MAXLINE];
while ((len = debug_getline(line, MAXLINE)) > 0) {
memcpy(original_line, line, len);
#else
while ((len = my_getline(line, MAXLINE)) > 0) {
#endif
newlen = use_tabs(line, len);
#if TEST
printf("%s", original_line);
#endif
printf("%s", line);
printf("\noriginal: %d encoded: %d\n\n", len, newlen);
#if DEBUG
/* int z; */
/* for(z = 0; z < newlen; z++) { */
/* switch (line[z]) { */
/* case'\t': */
/* printf("%2d:\\t\n", z); */
/* break; */
/* case ' ': */
/* printf("%2d:_\n", z); */
/* break; */
/* default: */
/* printf("%2d:%c\n", z, line[z]); */
/* break; */
/* } */
/* } */
#endif
}
}
/* 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 use_tabs(char s[], int len)
{
int i, columnIndex,
inWord, indexOfWordEnd;
int numberOfSpaces = 0;
int newTabIndex;
i = 0;
columnIndex = 0;
inWord = 1;
indexOfWordEnd = -1;
while(i < len) {
#if DEBUG
printf("i: %d ci: %d iw: %d we: %d c: %c\n",
i, columnIndex, inWord, indexOfWordEnd, s[i]);
#endif
if (!inWord && is_tab_stop(columnIndex)) {
numberOfSpaces = i - indexOfWordEnd - 1;
// ...check for spaces to replace
#if DEBUG
printf("#sp: %d\n", numberOfSpaces);
#endif
if(numberOfSpaces > 1) {
newTabIndex = indexOfWordEnd + 1;
#if DEBUG
printf("need some tab action: startIndex %d endIndex: %d\n",
newTabIndex, i - 1);
#endif
s[newTabIndex] = '\t';
len = remove_chars(s, len, newTabIndex + 1, i - 1);
indexOfWordEnd = newTabIndex;
i = newTabIndex + 1;
#if DEBUG
printf("%s\n", s);
printf("reset i: %d\n", i);
#endif
} else {
//we can't go back over a tab stop
indexOfWordEnd = i;
}
}
if(s[i] == SPACE) {
if (inWord) {
indexOfWordEnd = i - 1;
inWord = 0;
}
} else {
if (!inWord) {
inWord = 1;
}
}
i++;
columnIndex++;
}
/*
if (i - indexOfWordEnd > 1
&& (columnIndex % DEFAULT_TABSTOP_SIZE) == 0) {
#if DEBUG
printf("_t:%d:%d\n", i, columnIndex);
#endif
s[indexOfWordEnd + 1] = '\t';
len = remove_chars(s, len, indexOfWordEnd + 2, i);
i = indexOfWordEnd + 1;
indexOfWordStart = i;
#if DEBUG
printf("i is now %d\n", i);
printf("columnIndex is now %d\n", columnIndex);
printf("len is now %d\n", len);
#endif
}
else {
#if DEBUG
printf("_:%d:%d\n", i, columnIndex);
#endif
indexOfWordStart = -1;
}
//TOO:magic number
} else if (indexOfWordStart == -1) {
#if DEBUG
printf("%c:%d:%d<\n", s[i], i, columnIndex);
#endif
indexOfWordStart = i;
} else {
#if DEBUG
printf("%c:%d:%d\n", s[i], i, columnIndex);
#endif
}
i++;
columnIndex++;
}
printf("column: %d\n", columnIndex);
return len;
*/
return len;
}
int remove_chars(char s[], int len, int startIndex, int endIndex)
{
int newlen, i;
newlen = startIndex;
i = endIndex + 1;
while(i < len) {
s[newlen] = s[i];
++newlen;
++i;
}
s[newlen] = '\0';
return newlen;
}
int debug_getline(char s[], int _)
{
static int numberOfCalls = 0;
int len;
switch(numberOfCalls++) {
case 0:
len = strlen("this is sparta\n") + 1;
memcpy(s, "this is sparta\n", len);
break;
case 1:
len = strlen("a sparta\n") + 1;
memcpy(s, "a sparta\n", len);
break;
case 2:
len = strlen("this is sparta\n") + 1;
memcpy(s, "this is sparta\n", len);
break;
case 3:
len = strlen("this is a thing ok y\n") + 1;
memcpy(s, "this is a thing ok y\n", len);
break;
case 4:
len = strlen("this thingosaurus rex is a lifestyle for me.\n") + 1;
memcpy(s, "this thingosaurus rex is a lifestyle for me.\n", len);
break;
case 5:
len = strlen("0123456789012345 gerrard lindsay\n") + 1;
memcpy(s, "0123456789012345 gerrard lindsay\n", len);
break;
case 6:
len = strlen("this e slfdk\n") + 1;
memcpy(s, "this e slfdk\n", len);
case 7:
len = strlen("01234567 90123 7\n") + 1;
memcpy(s, "01234567 90123 7\n", len);
break;
break;
default:
len = 0;
}
return len;
}
bool is_tab_stop(int index)
{
int *tab_stops = manual_tab_stops;
//TODO: support manual tab stops
//TODO: again, we assume manual tab stops > 0 throughout this fucn
if (*tab_stops) {
/* if (index == 16) printf("yeah man: %d\n", index); */
while(*tab_stops && *tab_stops != index) {
/* printf("test-> %d = %d\n", *tab_stops, index); */
tab_stops++;
}
/* printf("so? %d\n", *tab_stops); */
// if we hit a zero tab stop, we never found a match
return (*tab_stops);
}
// default dynamic tab stops
if (index % DEFAULT_TABSTOP_SIZE == 0) printf("yeah: %d\n", index);
return (index % DEFAULT_TABSTOP_SIZE == 0);
}