-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cpp
More file actions
222 lines (210 loc) · 7.57 KB
/
Copy pathLoader.cpp
File metadata and controls
222 lines (210 loc) · 7.57 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
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
//
// Created by Eshin Griffith on 9/26/17.
//
#include "Loader.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include "Log.h"
#include "Hex_Util.h"
Loader::Loader() {
current_data = std::vector<std::string>(4);
}
void Loader::init(MMU &mmu, M_priority_queue<PCB *> &pcbs) {
std::vector<std::string> page = std::vector<std::string>(4);
int current_MMU_page = 0;
int current_pcb_page = 0;
int word_count = 0;
std::ifstream input("../Program.txt");
std::string temp = "";
PCB *pcb;
// pcb = new PCB();
while (getline(input, temp)) {
if (temp.length() != 0) {
// For MetaData
if (temp.at(0) == '/' && temp != "// END" && temp != "//END") {
pcb = strip_meta_data(temp, pcb);
} else if (temp.at(0) == '0') {
updatePCBData(&mmu, pcb, current_MMU_page, current_pcb_page, word_count, temp);
} else {
addPCB(&mmu,pcbs,pcb, current_MMU_page, current_pcb_page, word_count);
}
}
}
mmu.print_disk_map(false);
}
PCB* Loader::strip_meta_data(std::string temp_data, PCB *pcb) {
PCB *p;
if (temp_data.at(3) == 'J') {
p = new PCB();
strip_job_meta_data(p, temp_data);
} else {
strip_data_meta_data(pcb, temp_data);
p = pcb;
}
return p;
}
PCB* Loader::strip_job_meta_data(PCB *pcb, std::string temp_data) {
std::string buildString = "";
temp_data = temp_data.substr(7, std::string::npos);
std::vector<int> data = Hex_Util::parseIntsFromString(temp_data);
pcb->job_id = data[0];
pcb->job_size = data[1];
pcb->job_pri = data[2];
return pcb;
}
PCB* Loader::strip_data_meta_data(PCB *pcb, std::string temp_data) {
std::string buildString = "";
temp_data = temp_data.substr(7, std::string::npos);
std::vector<int> data = Hex_Util::parseIntsFromString(temp_data);
pcb->in_buf_size = data[0];
pcb->out_buf_size = data[1];
pcb->temp_buf_size = data[2];
pcb->total_size = pcb->job_size + pcb->in_buf_size + pcb->out_buf_size + pcb->temp_buf_size;
return pcb;
}
void Loader::updatePCBData(MMU *mmu, PCB *pcb, int ¤t_MMU_page, int ¤t_pcb_page, int &word_count, std::string temp_data) {
int b = current_pcb_page;
temp_data = temp_data.substr(2, std::string::npos);
if(word_count == 4){
pcb->page_table[current_pcb_page] = (std::make_tuple(current_MMU_page,-1,false));
mmu->add_page_to_disk(current_data,current_MMU_page);
current_MMU_page++;
current_pcb_page++;
for(int i = 0; i < 4; i++)
current_data[i] = "";
word_count = 0;
}
current_data[word_count] = temp_data;
word_count++;
}
void Loader::addPCB(MMU *mmu ,M_priority_queue<PCB *> &pcbs, PCB *pcb, int ¤t_MMU_page, int ¤t_pcb_page, int &word_count) {
if(word_count != 0){
mmu->add_page_to_disk(current_data,current_MMU_page);
pcb->page_table[current_pcb_page] = (std::make_tuple(current_MMU_page,-1,false));
current_MMU_page++;
current_pcb_page++;//ds
}
current_pcb_page = 0;
word_count = 0;
pcbs.push(pcb);
}
//void Loader::init(MMU &mmu, M_priority_queue<PCB *> &pcbs) {
//
// // Variables for init function
// int data[10];
// std::vector<std::string> page = std::vector<std::string>(4);
// int wordCount = 0;
// int currentPagesForJob= 0;
// int currentMMUPage = 0;
// std::ifstream input("../Program.txt");
// std::string temp;
// std::string store;
// std::string build_string = "";
// int counter = 0;
// int current_position = 0;
// int lines = 0;
// PCB *p = new PCB();
// // Read the file
// while (getline(input, temp)) {
// lines++;
// build_string = "";
// if (temp.length() == 0)
// break;
// if (temp.at(0) == '/' && temp != "// END" && temp != "//END") { // If the line is PCB Data
// if (temp.at(3) == 'J') {// Do this if the line is a PCB Job line
// //create new PCB with registers and address initialized
//
//
//
// temp = temp.substr(7, std::string::npos);//drop the characters before index 7
//
// //store the values from the line in data
// for (int i = 0; i < temp.length(); i++) {
// if (temp.at(i) != ' ')
// build_string += temp.at(i);
// else {
// if (build_string.length() != 0) {
// data[counter] = Hex_Util::hex_to_decimal(build_string);
// counter++;
// build_string = "";
// }
// }
// }
//
// // Last build string must be added
// data[counter] = Hex_Util::hex_to_decimal(build_string);
// counter++;
// build_string = "";
//
// } else { //if the line is a data info line
// int innerIterations = 0;
//
// temp = temp.substr(8, std::string::npos);
//
// //store the values from the line in data
// for (int i = 0; i < temp.length(); i++) {
// innerIterations++;
// if (temp.at(i) != ' ')
// build_string += temp.at(i);
// else {
// // Last buildstring must be added
// data[counter] = Hex_Util::hex_to_decimal(build_string);
// counter++;
// build_string = "";
// }
// }
//
// data[counter] = Hex_Util::hex_to_decimal(build_string);
// counter++;
// build_string = "";
//
// // Once a PCB Data Line information is recorded, store that into a pcb
// p->job_id = data[0];
// p->job_size = data[1];
// p->job_pri = data[2];
// p->in_buf_size = data[3];
// p->out_buf_size = data[4];
// p->temp_buf_size = data[5];
// p->total_size = p->job_size + p->in_buf_size + p->out_buf_size + p->temp_buf_size;
// p->state = PCB::PROCESS_STATUS::NEW;
// p->prgm_counter = 0;
//
// mmu.add_page_to_disk(page, currentMMUPage);
// p->page_table[currentPagesForJob] = std::make_tuple(currentMMUPage,-1,true,false);
// currentPagesForJob = 0;
// currentMMUPage++;
// wordCount = 0;
// for(int j = 0; j < 4; j++)
// page[j] = "";
//
// p = new PCB();
// for (int &j : p->registers) {
// j = -1;
// }
//
// pcbs.push(p);
// counter = 0;
//
// }
// }
// // If the data is just raw hexcode, strip off the first few characters and store it as a string
// else if (temp.at(0) != '/') {
// temp = temp.substr(2, std::string::npos);
// if(wordCount == 4)
// {
// mmu.add_page_to_disk(page, currentMMUPage);
// wordCount = 0;
// p->page_table[currentPagesForJob] = std::make_tuple(currentMMUPage,-1,true,false);
// currentPagesForJob++;
// currentMMUPage++;
// for(int j = 0; j < 4; j++)
// page[j] = "";
// }
// page[wordCount] = temp; //xdf
// wordCount++;
// }
// }
// input.close();
// }