-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentry.cpp
204 lines (184 loc) · 4.58 KB
/
entry.cpp
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
#include "entry.h"
Entry::Entry(){
for (int i=0;i<21;i++){
namaFile = namaFile + '\0';
}
fileAtt = '0';
time_t now = time(0);
tm *ltm = localtime(&now);
string year = DecToBin(ltm->tm_year-110);
string month = DecToBin(ltm->tm_mon+1);
string day = DecToBin(ltm->tm_mday);
string hour = DecToBin(ltm->tm_hour);
string min = DecToBin(ltm->tm_min);
string sec = DecToBin(ltm->tm_sec/2);
string clockRes = checkBin(hour,"hour") + checkBin(min,"minute") + checkBin(sec,"second");
string dateRes = checkBin(year,"year") + checkBin(month,"month") + checkBin(day,"day");
clock = BinToDec(clockRes);
date = BinToDec(dateRes);
dir = 0;
size = 0;
}
Entry::Entry(string name, char att, short _dir, int _size){
namaFile = name;
for (int i=name.length();i<21;i++){
namaFile = namaFile + '\0';
}
fileAtt = att;
time_t now = time(0);
tm *ltm = localtime(&now);
string year = DecToBin(ltm->tm_year-110);
string month = DecToBin(ltm->tm_mon+1);
string day = DecToBin(ltm->tm_mday);
string hour = DecToBin(ltm->tm_hour);
string min = DecToBin(ltm->tm_min);
string sec = DecToBin(ltm->tm_sec/2);
string clockRes = checkBin(hour,"hour") + checkBin(min,"minute") + checkBin(sec,"second");
string dateRes = checkBin(year,"year") + checkBin(month,"month") + checkBin(day,"day");
clock = BinToDec(clockRes);
date = BinToDec(dateRes);
dir = _dir;
size = _size;
}
void Entry::setAtt(char att){
fileAtt = att;
}
void Entry::setClock(){
time_t now = time(0);
tm *ltm = localtime(&now);
string hour = DecToBin(ltm->tm_hour);
string min = DecToBin(ltm->tm_min);
string sec = DecToBin(ltm->tm_sec/2);
string clockRes = checkBin(hour,"hour") + checkBin(min,"minute") + checkBin(sec,"second");
clock = BinToDec(clockRes);
}
void Entry::setDate(){
time_t now = time(0);
tm *ltm = localtime(&now);
string year = DecToBin(ltm->tm_year-110);
string month = DecToBin(ltm->tm_mon+1);
string day = DecToBin(ltm->tm_mday);
string dateRes = checkBin(year,"year") + checkBin(month,"month") + checkBin(day,"day");
date = BinToDec(dateRes);
}
void Entry::setDir(short _dir){
dir = _dir;
}
void Entry::setSize(int _size){
size = _size;
}
string Entry::DecToBin(int number)
{
if ( number == 0 ) return "0";
if ( number == 1 ) return "1";
if ( number % 2 == 0 )
return DecToBin(number / 2) + "0";
else
return DecToBin(number / 2) + "1";
}
int Entry::BinToDec(string number)
{
int result = 0, pow = 1;
for ( int i = number.length() - 1; i >= 0; --i, pow <<= 1 )
result += (number[i] - '0') * pow;
return result;
}
string Entry::checkBin(string binary,string status){
string zero = "0";
//cout << "Panjang : " << binary.length() << endl;
if (status=="hour"){
int delta = 5-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}else if (status=="minute"){
int delta = 6-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}else if (status=="second"){
int delta = 5-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}else if (status=="year"){
int delta = 7-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}else if (status=="month"){
int delta = 4-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}else if (status=="day"){
int delta = 5-binary.length();
if (delta>=0){
for(int i=0;i<delta;i++){
binary = zero + binary;
}
}
}
return binary;
}
string Entry::toString(){
//string namaFile;
//char fileAtt;
//unsigned short clock;
//unsigned short date;
//unsigned short dir;
//int size;
string str = namaFile;
while (str.length() < 21){
str = str + '\0';
}
str = str + fileAtt;
str = str + (char)(clock/256);
str = str + (char)(clock%256);
str = str + (char)(date/256);
str = str + (char)(date%256);
str = str + (char)(dir/256);
str = str + (char)(dir%256);
int a,b,c,d;
a = size/(256*256*256);
b = size/(256*256);
b%=256;
c = size/256;
c%=256;
d = size % 256;
str = str + (char)a;
str = str + (char)b;
str = str + (char)c;
str = str + (char)d;
//cout << str.length() << endl;
return str;
}
void Entry::load(string str){
int idx = 0;
for (int i=0;i<=21;i++){
if (str[i] == '\0')
continue;
else
namaFile = namaFile + str[i];
idx++;
}
fileAtt = str[idx];
idx++;
clock = str[idx]*256+str[idx+1];
idx+=2;
date = str[idx]*256+str[idx+1];
idx+=2;
dir = str[idx]*256+str[idx+1];
idx+=2;
size = str[idx]*256*256*256 + str[idx+1]*256*256 + str[idx+2]*256 + str[idx+3];
}