-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
95 lines (87 loc) · 2.85 KB
/
main.c
File metadata and controls
95 lines (87 loc) · 2.85 KB
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
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
const char ALGORITHMS_FOLDER[] = "./algorithms/build/";
struct algorithm
{
int n;
char name[500];
char location[500];
};
int getNumberOfAlgorithms(DIR *algorithms)
{
int nb = 0;
struct dirent *dir; //une structure utilisée pour représenter les entrées de répertoire
// a comme propriété l file name wel type mte3ou
while ((dir = readdir(algorithms)) != NULL) //readdir pour lire le contenu d'une directory
{
if (dir->d_type != DT_DIR) //ken l type taa dir mahouch directory wa9tha ++nb_file
nb++;
}
return nb;
}
struct algorithm *getListOfSchedulingAlgorithms(int numberOfAlgorithms)
{
DIR *algorithms;
struct dirent *dir;
algorithms = opendir(ALGORITHMS_FOLDER); // pointer to files in src
printf("Choose a scheduling algorithm : \n");
struct algorithm *algorithmChoices;
// réserver la taille du algorithmChoices qui est un tableau d'algorithm
algorithmChoices = (struct algorithm *)malloc(numberOfAlgorithms * sizeof(struct algorithm));
if (algorithms)
{
int i = 1;
while ((dir = readdir(algorithms)) != NULL)
{
if (dir->d_type == DT_DIR)
continue;
struct algorithm algo;
algo.n = i;
strcpy(algo.name, dir->d_name); //copier une chaîne de caractères
char location[500] = "";
strcat(location, ALGORITHMS_FOLDER); //concaténation ./algorithms/build/
strcat(location, dir->d_name);
strcpy(algo.location, location);
algorithmChoices[i] = algo;
printf("\t%d - %s\n", i, dir->d_name); // printing the menu
i++;
}
closedir(algorithms);
}
return algorithmChoices;
}
int getUserChoice(int nbOfAlgorithms)
{
int choice;
while (1)
{
printf("Enter your choice : ");
scanf("%d", &choice);
if (choice > nbOfAlgorithms || choice < 1)
printf("Choose a number from the menu!\n");
else
break;
}
return choice;
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("Please enter processes file as an argument.\n");
exit(0);
}
DIR *algorithmsFiles = opendir(ALGORITHMS_FOLDER); // pointer on ./build directory
int nbOfAlgorithms = getNumberOfAlgorithms(algorithmsFiles);
struct algorithm *algorithmChoices = getListOfSchedulingAlgorithms(nbOfAlgorithms);
int choice = getUserChoice(nbOfAlgorithms);
char command[500] = "";
strcat(command, algorithmChoices[choice].location); //./algorithms/build/RR
char configFile[100] = " ";
strcat(configFile, argv[1]); // processes.txt
strcat(command, configFile); // ./algorithms/build/RR processes.txt
system(command); //execution
}