-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletion.cpp
More file actions
63 lines (59 loc) · 1.8 KB
/
deletion.cpp
File metadata and controls
63 lines (59 loc) · 1.8 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
#include "file_manager.h"
#include "errors.h"
#include <iostream>
#include <limits.h>
#include <fstream>
#include <string>
ifstream infile;
FileManager fm;
FileHandler fh1;
PageHandler ph1, ph2;
int k = PAGE_CONTENT_SIZE/sizeof(int);
void output_close(int size) {
while(k < PAGE_CONTENT_SIZE/sizeof(int)) {
((int *)ph2.GetData())[k++] = INT_MIN;
} fh1.MarkDirty(ph2.GetPageNum());
fh1.FlushPage(ph2.GetPageNum());
for(int i = ph2.GetPageNum()+1; i < size; i++) {
fh1.DisposePage(i);
fh1.FlushPage(i);
}
}
void output_write(int value) {
if(k == PAGE_CONTENT_SIZE/sizeof(int)) {
fh1.MarkDirty(ph2.GetPageNum());
fh1.FlushPage(ph2.GetPageNum());
ph2 = fh1.PageAt(ph2.GetPageNum()+1);
k = 0;
} ((int *)ph2.GetData())[k++] = value;
}
int main(int argc, const char* argv[]) {
fh1 = fm.OpenFile(argv[1]);
infile.open(argv[2]);
string str;
int num;
while(infile >> str) {
infile >> num;
bool found = false;
int size = fh1.LastPage().GetPageNum()+1;
fh1.FlushPages();
for(int i = 0; i < size; i++) {
ph1 = fh1.PageAt(i);
for(int j = 0; j < PAGE_CONTENT_SIZE/sizeof(int); j++) {
if(((int *)ph1.GetData())[j] == INT_MIN) {
break;
} else if((!found) && (((int *)ph1.GetData())[j] == num)) {
found = true;
ph2 = fh1.PageAt(i);
k = j;
} else if((found) && (((int *)ph1.GetData())[j] != num)) {
output_write(((int *)ph1.GetData())[j]);
}
} if(ph2.GetPageNum() != i) {
fh1.FlushPage(i);
}
} output_close(size);
} fm.CloseFile(fh1);
infile.close();
return 0;
}