-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtema1.cpp
348 lines (292 loc) · 7.71 KB
/
tema1.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"compare.h"
#define BUFFER_SIZE 20000
#define BUFFER_READ 2
/*Queue operation code*/
#define START_OP_CODE 0
#define INSERT_OP_CODE (START_OP_CODE+1)
#define TOP_OP_CODE (START_OP_CODE+2)
#define POP_OP_CODE (START_OP_CODE+3)
#define UNDEFINED_OP_CODE 9999
#define MMIN_PRIORITY 1
#define ERROR_ALLOCATION "Error Allocation"
#define ERROR_OPENING_FILE -1
#define ERROR_MALLOC 12
//TO DO: Define delimiers
static const char tokens[] = " ";
/*Define data structure for priority queue*/
typedef struct _priority_queue {
/*String should be dynamically allocate*/
char *String;
int priority;
struct _priority_queue *next;
} priority_queue;
/*define first and only elemnt of queue*/
static priority_queue *root;
/*TO DO3: Create proper functions for allocate/deallocate*/
/*queue objects and perform a clean exit, in case malloc*/
/*cannot allocate elemets. */
/*Forward definiton of function specialised in deallocate memory*/
void clean_memory(void);
/*TO DO4: Create a function to return exit code 12*/
/*Represend malloc failure exit code*/
void Safe_Exit(void *obj, int rCode)
{
/*before deallocation check valability*/
if (obj == NULL) {
/* before exiting, deallocate heap memory used;*/
clean_memory();
exit(rCode);
}
};
/*TO DO4: Allocate space for string*/
/*and check for value returned*/
void queue_alloc(priority_queue **bufer, int string_size)
{
*bufer = (priority_queue *)malloc(sizeof(priority_queue));
Safe_Exit(*bufer, ERROR_MALLOC);
(*bufer)->String = (char *)malloc(BUFFER_SIZE * sizeof(char));
Safe_Exit((*bufer)->String, ERROR_MALLOC);
(*bufer)->next = NULL;
(*bufer)->priority = MMIN_PRIORITY;
};
/*TO DO5: Deallocate elements*/
void my_free(priority_queue **buffer)
{
/*Check before deallocate memory*/
if ((*buffer)->String != NULL) {
free((*buffer)->String);
(*buffer)->String = NULL;
}
if ((*buffer) != NULL) {
free(*buffer);
(*buffer) = NULL;
}
};
/* #pragma endregion*/
/* #pragma region COADA_PRIORITATI*/
/*test if any operations is valid and return code*/
int get_operation_code(char *command)
{
char *command2 = (char *)malloc(BUFFER_SIZE * sizeof(char));
Safe_Exit(command2, ERROR_MALLOC);
strcpy(command2, command);
/*define and initialised returned code*/
int cod = UNDEFINED_OP_CODE;
char *operation = strtok(command2, tokens);
char *check = strtok(NULL, tokens);
/*check if is a string and it is not empty*/
if (operation != NULL) {
if (strcmp("insert", operation) == 0)
cod = INSERT_OP_CODE;
if (strcmp("top", operation) == 0 && check == NULL)
cod = TOP_OP_CODE;
if (strcmp("pop", operation) == 0 && check == NULL)
cod = POP_OP_CODE;
}
/*Deallocation*/
free(command2);
command2 = NULL;
return cod;
};
/*TO DO:Implement a copy function for queue element*/
void duplicate_element(priority_queue **destination,
const priority_queue *source)
{
/*Copy element from destination to source. Be careful of pointers.*/
strcpy((*destination)->String, source->String);
(*destination)->priority = source->priority;
};
/*TO DO:Make space for new element with big priority*/
void move_to_right(priority_queue **Start, char *name, int priority)
{
priority_queue *tmp, *tmp_next;
/*Make space for intermediate element neccesarly for swap*/
queue_alloc(&tmp, BUFFER_SIZE);
queue_alloc(&tmp_next, BUFFER_SIZE);
duplicate_element(&tmp, *Start);
priority_queue *pMove = *Start;
strcpy(pMove->String, name);
pMove->priority = priority;
int change = 0;
/*Find firt element with less priority*/
while (pMove->next != NULL) {
change = 1;
/*Save element for swap*/
duplicate_element(&tmp_next, pMove->next);
pMove = pMove->next;
duplicate_element(&pMove, tmp);
duplicate_element(&tmp, tmp_next);
}
/*Initialised next element */
queue_alloc(&pMove->next, BUFFER_SIZE);
pMove = pMove->next;
pMove->next = NULL;
if (!change)
duplicate_element(&tmp_next, tmp);
/*Insert new element and shit the rest to right*/
duplicate_element(&pMove, tmp_next);
my_free(&tmp_next);
my_free(&tmp);
};
/*DO TO: Insert name and priority*/
void insert(char *name, int priority)
{
/*Create fisrt element */
if (root == NULL) {
queue_alloc(&root, BUFFER_SIZE);
strcpy(root->String, name);
root->priority = priority;
} else {
priority_queue *pMove = root;
/*Find the element with low priority*/
while (pMove->next != NULL &&
compare(pMove->priority, priority) > 0)
pMove = pMove->next;
if (compare(pMove->priority, priority) > 0) {
/*If new element has the lowest priority, insert at end:*/
queue_alloc(&pMove->next, BUFFER_SIZE);
pMove = pMove->next;
strcpy(pMove->String, name);
pMove->priority = priority;
pMove->next = NULL;
} else
move_to_right(&pMove, name, priority);
}
};
/*TO DO: pop, Eliminate first element*/
void pop(void)
{ if (root != NULL) {
priority_queue *remove = root;
/*Move to next element */
root = root->next;
my_free(&remove);
}
};
/*TO DO: Implement top function*/
char *top(void)
{
size_t Size = 0;
char *rBuff = (char *)malloc(2 * sizeof(char) * BUFFER_SIZE);
Safe_Exit(rBuff, ERROR_MALLOC);
Size = sizeof(rBuff);
memset(rBuff, 0, Size);
/*Check if queue is empty*/
if (root != NULL)
strcpy(rBuff, root->String);
else
strcpy(rBuff, "");
return rBuff;
};
int prepare_insert(const char *command)
{
char *command2 = (char *)malloc(BUFFER_SIZE * sizeof(char));
Safe_Exit(command2, ERROR_MALLOC);
strcpy(command2, command);
strtok(command2, tokens);
/*Break string for checking operation valability*/
char *name = strtok(NULL, tokens);
char *s_priority = strtok(NULL, tokens);
char *end = strtok(NULL, tokens);
int priority = MMIN_PRIORITY;
if (s_priority != NULL)
priority = atoi(s_priority);
else
end = "skip";
/*If operation is insert check if is something after priority*/
if (end == NULL)
insert(name, priority);
/*Deallocate memory*/
free(command2);
return 0;
};
/*Checking for operation*/
void operation_priority_queue(char *command)
{
unsigned int op_code = get_operation_code(command);
char *tmp = NULL;
/*Based on code returned there are 3 cases:*/
switch (op_code) {
case INSERT_OP_CODE:
prepare_insert(command);
break;
case TOP_OP_CODE:
tmp = top();
printf("%s\n", tmp);
/*Deallocate memory beacuse type of function top(char*) */
free(tmp);
tmp = NULL;
break;
case POP_OP_CODE:
pop();
break;
default:
break;
}
};
void clean_memory(void)
{ /*Deallocate string and element*/
if (root != NULL) {
while (root->next != NULL) {
priority_queue *tmp = root;
root = root->next;
my_free(&tmp);
}
my_free(&root);
}
};
/*#pragma endregion*/
void read_command(FILE *fin)
{
size_t Size = 0;
char separators[] = "\n";
char *outBuffer = (char *)malloc(sizeof(char) * BUFFER_READ);
Safe_Exit(outBuffer, ERROR_MALLOC);
char *command = (char *)malloc(2 * BUFFER_SIZE * sizeof(char));
Safe_Exit(command, ERROR_MALLOC);
Size = sizeof(command);
memset(command, 0, Size);
do {
char c = fgetc(fin);
/*Read chactacter with chactacter and make command*/
outBuffer[0] = c;
outBuffer[1] = '\0';
if (strchr(separators, outBuffer[strlen(outBuffer) - 1]) != 0) {
operation_priority_queue(command);
Size = sizeof(command);
memset(command, 0, Size);
} else
strcat(command, outBuffer);
if (c == EOF)
break;
} while (!feof(fin));
/*Deallocation*/
free(command);
command = NULL;
/*Deallocation*/
free(outBuffer);
outBuffer = NULL;
};
int OpenFiles(char *fileIn)
{ /*Open file */
FILE *fin = fopen(fileIn, "r");
/*check if file exist*/
if (fin == NULL)
return ERROR_OPENING_FILE;
read_command(fin);
/*Close file*/
fclose(fin);
return 0;
};
int main(int argc, char *argv[])
{
if (argc > 1)
for (int i = 1; i < argc; i++)
OpenFiles(argv[i]);
else
read_command(stdin);
clean_memory();
return 0;
}