-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileHandler.c
106 lines (94 loc) · 4.01 KB
/
fileHandler.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
#include "fileHandler.h"
#include "globalDefs.h"
op_status readFile(string file_name, JobsDeps* jobDeps, Jobs* jobs, JobsForThreads* jobsForThreads, int* threadCount, int* jobsCount) {
FILE* hFile;
hFile = fopen(file_name, "r");
int i=0, j = 0;
char* temp = 0;
if (hFile == NULL) {
return OP_FAIL;
} else {
string c;
char* p;
char oneLine[LINE_LENGTH];
//reading thread count=======================
c = fgets(oneLine, LINE_LENGTH, hFile); /* get one line from the file */
*threadCount = readThreadAmount(oneLine);
//===========================================
//checking empty lines=======================
GET_NEXT_LINE(oneLine, hFile);
//===========================================
//reading jobs count=========================
*jobsCount = readJobsAmount(oneLine);
//initializing Jobs dataStracture
if(!*jobs) *jobs = calloc(*jobsCount, sizeof (JobState));
for (i=0; i < *jobsCount; i++)
(*jobs)[i] = NotStarted;
//===========================================
//initializing jobDeps DataStracture=========
if(!*jobDeps) *jobDeps = calloc(*jobsCount, sizeof (boolean*));
for (i = 0; i < *jobsCount; i++) {
if(!((*jobDeps)[i])) (*jobDeps)[i] = calloc(*jobsCount, sizeof (boolean));
for (j = 0; j<*jobsCount; j++)
(*jobDeps)[i][j] = false;
}
int col = 0;
for (i = 0; i <= *jobsCount - 1; i++)//reading input file rows
{
GET_NEXT_LINE(oneLine, hFile);
char* currChar = oneLine;
for (col = 0; col <= *jobsCount - 1; col++)//reading input file colums
{
while (*currChar == ' ' || *currChar == '\t' || *currChar == '\r' || *currChar == '\n')
currChar++;
int temp = 0;
sscanf(currChar, "%d", &temp);
(*jobDeps)[i][col] = temp;
currChar++;
}
}
//===========================================
//Initializing Jobs for thread stracture=====
if(!*jobsForThreads) *jobsForThreads = calloc(*threadCount, sizeof (ThreadJobs));
for (i = 0; i<*threadCount; i++) {
GET_NEXT_LINE(oneLine, hFile);
if (*oneLine != 0) {
int currentThread, currentJob, counter = 0;
char* currChar = oneLine;
while (*currChar == ' ' || *currChar == '\t')
currChar++;
sscanf(currChar, "%d:%s", ¤tThread,currChar);
if(!(*jobsForThreads)[currentThread - 1].jobs) (*jobsForThreads)[currentThread - 1].jobs = calloc(*jobsCount, sizeof (int));
(*jobsForThreads)[currentThread - 1].threadID = currentThread;
//currChar = currChar + 2;
while (*currChar != '\r' && *currChar != '\n') {
while (*currChar == ' ' || *currChar == '\t' || *currChar == '\0')
currChar++;
if (*currChar != '\n' && *currChar != '\r') {
if(sscanf(currChar, "%d,", ¤tJob)==0)
sscanf(currChar, "%d", ¤tJob);
while (*currChar != '\r' && *currChar != '\n' && *currChar != ',')
currChar++;
if (*currChar == ',')
currChar++;
(*jobsForThreads)[currentThread - 1].jobs[counter++] = currentJob;
}
}
(*jobsForThreads)[currentThread - 1].jobsAmount = counter;
}
}
//===========================================
fclose(hFile);
}
return OP_SUCCESS;
}
int readThreadAmount(string line) {
int toReturn = -1;
sscanf(line, "k = %d", &toReturn);
return toReturn;
}
int readJobsAmount(string line) {
int toReturn = -1;
sscanf(line, "n = %d", &toReturn);
return toReturn;
}