-
Notifications
You must be signed in to change notification settings - Fork 5
/
classes.cpp
231 lines (179 loc) · 4.79 KB
/
classes.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Flopgen: a tool for automatic creation of FAT-formatted floppy disk images
Copyright (C) 2020 Maksymilian Graczyk.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "classes.hpp"
#include "image.hpp"
#include <iostream>
using namespace std;
/*** File class begins here ***/
File::File(filesystem::path path, bool image, bool init_stream) {
if (init_stream) {
if (image) {
stream = new fstream(path,
ios::in | ios::out | ios::binary | ios::trunc);
} else {
stream = new fstream(path, ios::in | ios::binary);
}
} else {
stream = NULL;
}
this->path = path;
}
File::~File() {
if (stream != NULL) {
stream->close();
delete stream;
}
}
File *File::get_instance(string path) {
filesystem::path fs_path(path);
File *instance;
if (!filesystem::exists(fs_path)) {
cerr << fs_path << " does not exist, ignoring." << endl;
return NULL;
}
if (filesystem::is_directory(fs_path)) {
instance = new Directory(fs_path);
} else if (filesystem::is_regular_file(fs_path)) {
instance = new File(fs_path, false, true);
} else {
cerr << fs_path.generic_string();
cerr << " is neither a directory nor a regular file, ignoring." << endl;
return NULL;
}
if (instance == NULL) {
cerr << "Could not allocate memory for " << path << ", ignoring." << endl;
}
return instance;
}
fstream *File::get_stream() {
return stream;
}
bool File::is_directory() {
return false;
}
string File::get_path_str(bool with_dirs) {
if (with_dirs) {
return path.generic_string();
} else {
return path.filename().generic_string();
}
}
int File::get_size() {
return filesystem::file_size(path);
}
/*** File class ends here ***/
/*** Directory class begins here ***/
Directory::Directory(filesystem::path path) : File(path, false, false) {
filesystem::directory_iterator dir(this->path);
total_size = 0;
for (const filesystem::directory_entry &entry : dir) {
if (entry.is_regular_file()) {
File *file = new File(entry.path(), false);
files.push_back(file);
total_size += file->get_size();
} else if (entry.is_directory()) {
File *directory = new Directory(entry.path());
files.push_back(directory);
total_size += directory->get_size();
} else {
cerr << entry.path().generic_string()
<< " is neither a directory nor a regular file, ignoring." << endl;
}
}
}
Directory::~Directory() {
for (File *file : files) {
delete file;
}
}
fstream *Directory::get_stream() {
return NULL;
}
bool Directory::is_directory() {
return true;
}
int Directory::get_size() {
return total_size;
}
File *Directory::operator[](int index) {
return files[index];
}
int Directory::get_file_count() {
return files.size();
}
/*** Directory class ends here ***/
/*** Floppy class begins here ***/
Floppy::Floppy(FloppySize size, int code_page) {
this->size = size;
this->code_page = code_page;
switch (size) {
case _360K:
case _720K:
case _2880K:
cluster_size = 1024;
break;
default:
cluster_size = 512;
break;
}
free_space = (size * BYTES_IN_KB) / cluster_size - 2;
}
bool Floppy::fit(File *file, int *cl_size) {
int file_size = file->get_size();
int c_size = file_size / cluster_size
+ ((file_size % cluster_size) > 0);
if (c_size > free_space) {
return false;
}
if (cl_size != NULL) {
*cl_size = c_size;
}
return true;
}
bool Floppy::fit_capacity(File *file) {
int file_size = file->get_size();
int c_size = file_size / cluster_size
+ ((file_size % cluster_size) > 0);
if (c_size > (size * BYTES_IN_KB) / cluster_size - 2) {
return false;
}
return true;
}
bool Floppy::add_file(File *file) {
int cl_size;
if (!fit(file, &cl_size)) {
return false;
}
files.push_back(file);
free_space -= cl_size;
return true;
}
int Floppy::get_size() {
return size * BYTES_IN_KB;
}
bool Floppy::save(string filename) {
Image image(filename, size, code_page);
if (!image.is_open()) {
return false;
}
for (unsigned long i = 0; i < files.size(); i++) {
bool success = image << files[i];
if (!success) {
return false;
}
}
return true;
}
/*** Floppy class ends here ***/