-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamp.c
206 lines (177 loc) · 3.6 KB
/
examp.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
#include "vstr.h"
#include <stdio.h>
#include <ctype.h>
int i;
void eatnl() {
register char ch;
register int foundnum;
while ((ch = getchar()) != '\n')
if (isdigit(ch) || ch == '-') {
ungetc(ch, stdin);
scanf("%d", &i);
foundnum = 1;
}
if (!foundnum)
i = 0;
}
int main() {
register p_vstr testv = 0;
register char fname[40];
char ch;
struct cursor save;
int going = 1, mode = FWD, cnt, chr;
register FILE *fp;
while (going) {
printf("Command> ");
ch = getchar();
if (ch == '\n') {
eatnl();
continue;
}
eatnl();
switch (ch) {
case 'p':
while ((ch = getchar()) != '~') {
v_put(testv, ch, mode);
if (mode == HERE)
break;
}
eatnl();
break;
case 'g':
if (mode == INS) {
printf("Now in FWD mode\n");
mode = FWD;
}
while ((ch = v_get(testv, mode)) !=
(mode == FWD ? ATEND : ATBEG)) {
putchar(ch);
if (mode == HERE)
break;
}
printf("*END*\n");
break;
case 's':
v_savecursor(testv, &save);
printf("saved\n");
break;
case 'r':
v_setcursor(testv, &save);
printf("restored\n");
break;
case 'S':
v_show(testv);
break;
case 'm':
if (i < 0)
i = v_move(testv, BACK, -i);
else
i = v_move(testv, FWD, i);
printf("Moved %d positions\n", i);
break;
case 'd':
printf("Deleted %d chars\n", v_delete(testv, i));
break;
case 'q':
going = 0;
break;
case 'f':
v_free(testv);
testv = 0;
break;
case 'n':
testv = new_vstr(i);
printf("Address is %p\n", testv);
break;
case 'c':
v_clear(testv, END);
break;
case 'R':
v_rewind(testv);
printf("Rewound\n");
break;
case 'A':
v_append(testv);
printf("Appended\n");
break;
case 'M':
switch(i) {
case 0:
mode = FWD;
printf("Forward\n");
break;
case 1:
mode = BACK;
printf("Back\n");
break;
case 2:
mode = HERE;
printf("Here\n");
break;
case 3:
mode = INS;
printf("Insert\n");
break;
default:
printf("Unknown mode. Set to forward\n");
mode = FWD;
break;
}
break;
case '>':
cnt = 0;
printf("To file name: ");
scanf("%s", fname);
eatnl();
fp = fopen(fname, "w");
if (!fp) {
printf("Can't open %s for writing\n", fname);
continue;
}
while ((ch = v_get(testv, mode)) !=
(mode == FWD ? ATEND : ATBEG)) {
fputc(ch, fp);
cnt++;
if (mode == HERE)
break;
}
printf("%d chars written\n", cnt);
fclose(fp);
break;
case '<':
cnt = 0;
printf("From file name: ");
scanf("%s", fname);
eatnl();
fp = fopen(fname, "r");
if (!fp) {
printf("Can't open %s for writing\n", fname);
continue;
}
while((chr = fgetc(fp)) != EOF) {
cnt++;
v_put(testv, chr, FWD);
}
fclose(fp);
printf("%d chars read, length of vstr is now %d\n",
cnt, v_length(testv, BEG));
break;
case '/':
/* look ma, no statement */
{
register p_cursor cp;
printf("Search for char: ");
scanf("%c", &ch);
if ((cp = v_find(testv, ch)) != NULL) {
printf("Found.\n");
v_setcursor(testv, cp);
}
} /* yuk. So I'm too lazy to declare my locals earlier.
this is just a lousy test program.
What do you want from me? Style? */
break;
default:
printf("Unknown command: %c.\n", ch);
}
}
}