-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (142 loc) · 4.4 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
170
171
#include "functions.h"
int main() {
bmp_file *bmp = malloc(sizeof(bmp_file));
check_alloc(bmp, "main");
set *drawset = malloc(sizeof(set));
check_alloc(drawset, "main");
drawset->l_w = 1;
drawset->draw_clr.blue = 0;
drawset->draw_clr.green = 0;
drawset->draw_clr.red = 0;
parser(bmp, drawset);
free(bmp);
free(drawset);
return 0;
}
void parser(bmp_file *bmp, set *drawset) {
char *command = malloc(MAX_CHARS * sizeof(char));
check_alloc(command, "parser");
char *op = malloc(MAX_CHARS_OP * sizeof(char));
check_alloc(op, "parser");
do {
scanf("%[^\n]%*c", command);
int i = 0;
while (command[i] != ' ' && command[i] != '\0') {
op[i] = command[i];
i++;
}
op[i] = '\0';
menu(op, command, bmp, drawset);
} while (strcmp(op, "quit") != 0);
free(command);
free(op);
}
int op_to_int(char *operation) {
if (strcmp(operation, "save") == 0) {
return SAVE;
} else {
if (strcmp(operation, "edit") == 0) {
return EDIT;
} else {
if (strcmp(operation, "insert") == 0) {
return INSERT;
} else {
if (strcmp(operation, "set") == 0) {
return SET;
} else {
if (strcmp(operation, "draw") == 0) {
return DRAW;
} else {
if (strcmp(operation, "fill") == 0) {
return FILL;
} else {
if (strcmp(operation, "quit") == 0) {
return QUIT;
} else {
return OTHER;
}
}
}
}
}
}
}
}
void menu(char *operation, char *command, bmp_file *bmp, set *drawset) {
int i = 0;
char *ptr = NULL;
char *path = NULL;
switch (op_to_int(operation)) {
case SAVE:
ptr = strtok(command, " ");
while (ptr != NULL) {
if (i > 0)
path = ptr;
ptr = strtok(NULL, " ");
i++;
}
printf("Image saved at path %s...\n", path);
save(path, bmp);
break;
case EDIT:
ptr = strtok(command, " ");
while (ptr != NULL) {
if (i > 0)
path = ptr;
ptr = strtok(NULL, " ");
i++;
}
printf("Start editing file %s...\n", path);
edit(path, bmp);
break;
case INSERT:
ptr = strtok(command, " ");
unsigned int y = 0, x = 0;
while (ptr != NULL) {
if (i == 1) {
path = ptr;
} else {
if (i == 2) {
y = atoi(ptr);
} else {
if (i == 3)
x = atoi(ptr);
}
}
ptr = strtok(NULL, " ");
i++;
}
printf("Inserting image %s at point (%d,%d)...\n", path, x, y);
insert(path, bmp, y, x);
break;
case SET:
set_f(command, drawset);
break;
case DRAW:
draw(command, bmp, drawset);
break;
case FILL:
ptr = strtok(command + strlen("fill") + 1, " ");
int Y = 0, X = 0;
while (ptr != NULL) {
if (i == 0) {
Y = atoi(ptr);
} else {
if (i == 1)
X = atoi(ptr);
}
i++;
ptr = strtok(NULL, " ");
}
printf("Filling from point (%d,%d) ...\n", Y, X);
fill(bmp, drawset, bmp->pic.map[X][Y], Y, X);
break;
case OTHER:
printf("Try with a valid command... \n\n");
break;
case QUIT:
printf("Exiting the program... Thank you!\n");
break;
default: break;
}
}