-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
132 lines (104 loc) · 3.38 KB
/
queue.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
FILE *fptr;
/*void error_old(const char* message, const int code){
printf("%s\n", message);
// Close the file
if (fptr != NULL) {
fclose(fptr);
}
exit(code);
}*/
void error(const char* message){
printf("%s\n", message);
// Close the file
if (fptr != NULL) {
fclose(fptr);
}
// exit(code);
exit(EXIT_FAILURE);
}
void syntax(){
printf("ADDING AN ELEMENT TO A QUEUE\n\tqueue add <element> <queue name> (if the queue does not exists it will be created\n\nSHOW A QUEUE\n\tqueue show <queue name>\n\nDELETE A QUEUE\n\tqueue delete <queue name>\n\nLIST ALL QUEUE\n\tqueue list\n\n");
}
char* queue_name(const char* name){
//char* qn_ = malloc(strlen(name) * 2); // Allocate memory
char* qn_ = (char*)name;
strcat(qn_, ".queue");
return qn_;
}
int main(int argc, char const *argv[]) {
// vector add <element> <file>
// vector show <file>
// vector delete <file>
if (argc < 2) {
// error("Insufficient arguments.", 1);
syntax();
return EXIT_FAILURE;
}
const char* command = argv[1];
if (strcmp(command, "add") == 0) {
// Check if all arguments are present
if (argc < 4) error("Insufficient arguments");
// Create the file if does not exists
fptr = fopen(queue_name(argv[3]), "a");
// Add the element
fprintf(fptr, "%s\n", argv[2]);
} else if (strcmp(command, "show") == 0) {
// Check if all arguments are present
if (argc < 3) error("Insufficient arguments");
// Check if the file exists
fptr = fopen(queue_name(argv[2]), "r");
if (fptr != NULL) {
// TODO Print all elements
char currentline[100];
while (fgets(currentline, sizeof(currentline), fptr) != NULL) {
printf("%s", currentline);
}
} else {
error("Queue does not exists");
}
} else if (strcmp(command, "delete") == 0) {
// Check if all arguments are present
if (argc < 3) error("Insufficient arguments");
// Delete the file
if (remove(queue_name(argv[2])) != 0) error("Error while deleting");
} else if (strcmp(command, "list") == 0) {
// Check if all arguments are present
if (argc < 2) error("Insufficient arguments");
// Retrive all .queue files
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL) {
//printf("%s\n", dir->d_name);
char* qne[2];
char* token = strtok(dir->d_name, ".");
for (size_t i = 0; i < 2; i++) {
qne[i] = token;
token = strtok(NULL, " ");
}
//printf("%s.%s\n", qne[0], qne[1]);
if (qne[1] != NULL) {
// printf("%s\n", qne[1]);
if (strcmp(qne[1], "queue") == 0) {
printf("%s\n", qne[0]);
}
}
}
closedir(d);
}
} else if (strcmp(command, "help") == 0) {
syntax();
} else {
error("Invalid command");
}
// Close the file
if (fptr != NULL) {
fclose(fptr);
}
return EXIT_SUCCESS;
}