-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
169 lines (152 loc) · 3.31 KB
/
main.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
/* cplay
* By Michael Deakin
* CSC 460
* /home/deakin_mf/csc460/hw/cplay
* February 5th, 2013
* To build: make
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <unistd.h>
#include "heap.h"
#include "list.h"
typedef void (*CmdFunc)(heap *, bool);
void printmenu(void);
void cmdClear(heap *ops, bool print);
void cmdDelete(heap *ops, bool print);
void cmdInsert(heap *ops, bool print);
void cmdPeek(heap *ops, bool print);
void cmdQuit(heap *ops, bool print);
void cmdQuitNewline(heap *ops, bool print);
int runCommands(heap *hp, bool print);
int main(int argc, char **argv)
{
heap *hp = hpCreate((int (*)(const void *, const void *))strcmp);
if (isatty(fileno(stdin))) {
return runCommands(hp, true);
}
else {
return runCommands(hp, false);
}
}
CmdFunc *createCmdMap()
{
/* Because I am a lazy programmer and don't have
* a good map interface in C and like functional programming,
* I'll just do this terrible thing */
CmdFunc *commands = malloc(sizeof(CmdFunc[256]));
memset(commands, 0, sizeof(commands));
commands['c'] = cmdClear;
commands['d'] = cmdDelete;
commands['i'] = cmdInsert;
commands['q'] = cmdQuit;
commands[0] = cmdQuitNewline;
commands['p'] = cmdPeek;
return commands;
}
// Reads the next command from stdin.
char readCmd()
{
char buf = 0;
char cmd = 0;
/* Read the command and skip to the end */
size_t bytesRead = fread(&cmd, sizeof(cmd), 1, stdin);
if(bytesRead != 0) {
do {
bytesRead = fread(&buf, sizeof(buf), 1, stdin);
} while(buf != '\n' && bytesRead != 0);
}
else {
cmd = 0;
}
return tolower(cmd);
}
int runCommands(heap *hp, bool print)
{
CmdFunc *commands = createCmdMap();
if(print)
printmenu();
for(int i = 0;; i++) {
if(print)
printf(">> ");
char cmd = readCmd();
if(commands[(int)cmd])
commands[(int)cmd](hp, print);
}
free(commands);
}
void cmdClear(heap *ops, bool print)
{
while(hpSize(ops) > 0)
cmdDelete(ops, print);
}
void cmdDelete(heap *ops, bool print)
{
if(hpSize(ops) > 0) {
char *data = (char *)hpTop(ops);
printf("Top: %s\n", data);
free(data);
}
else if(print) {
printf("Heap is already empty\n");
}
}
void cmdPeek(heap *ops, bool print)
{
if(hpSize(ops) > 0) {
printf("Top: %s\n", (char *)hpPeek(ops));
}
else {
printf("Heap is empty\n");
}
}
void cmdInsert(heap *ops, bool print)
{
if(print)
printf("String: ");
char *buf = NULL;
list *strbuf = listCreate();
for(;;) {
buf = malloc(sizeof(*buf));
fread(buf, sizeof(*buf), 1, stdin);
if(*buf == '\n')
break;
listInsert(strbuf, buf);
}
free(buf);
int listsize = listSize(strbuf),
i;
buf = malloc(sizeof(char[listsize + 1]));
memset(buf, 0, sizeof(char[listsize + 1]));
for(i = 0, listMoveBack(strbuf);
i < listsize;
i++, listMoveBack(strbuf)) {
char *tmp = listGetCurrent(strbuf);
buf[i] = *tmp;
free(tmp);
}
listFree(strbuf);
hpAdd(ops, buf);
}
void cmdQuitNewline(heap *ops, bool print)
{
printf("\n");
cmdQuit(ops, print);
}
void cmdQuit(heap *ops, bool print)
{
cmdClear(ops, print);
hpFree(ops);
exit(0);
}
void printmenu(void)
{
puts("'c' to clear the heap,\n"
"'d' to delete the current string,\n"
"'i' to insert a new string into the heap (will need to prompt for string),\n"
"'p' to peek at the current string,\n"
"'q' to quit this program.\n");
}