This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
152 lines (128 loc) · 2.75 KB
/
Main.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
// 1048576 -> 1 megabyte
constexpr uint64_t max_size = 1048576 * 512;
char toLowerCase(const char &c)
{
if (c >= 'A' && c <= 'Z') {
return c + 'a' - 'A';
}
else {
return c;
}
}
int main()
{
string filename;
string input_size;
string size_unit;
uint64_t size = 0;
cout << "Type file name: ";
getline(cin, filename);
do {
cout << "Type file size: ";
getline(cin, input_size);
stringstream stream(input_size);
stream >> size;
if (stream.fail()) {
cout << endl << "Invalid input!" << endl << endl;
}
else {
if (size == 0) {
break;
}
else {
stream >> size_unit;
char c = '\0';
if (size_unit.empty()) {
c = 'b';
}
else if (size_unit.size() == 2 && toLowerCase(size_unit[1]) == 'b') {
c = toLowerCase(size_unit[0]);
}
if (c != 'b' && c != 'k' && c != 'm' && c != 'g' && c != 't') {
cout << endl << "Invalid input!" << endl << endl;
}
else {
bool to_large = false;
if (c == 'k' || c == 'm' || c == 'g' || c == 't') {
if (size * 1024 > size) {
size *= 1024;
if (c == 'm' || c == 'g' || c == 't') {
if (size * 1024 > size) {
size *= 1024;
if (c == 'g' || c == 't') {
if (size * 1024 > size) {
size *= 1024;
if (c == 't') {
if (size * 1024 > size) {
size *= 1024;
}
else {
to_large = true;
}
}
}
else {
to_large = true;
}
}
}
else {
to_large = true;
}
}
}
else {
to_large = true;
}
}
if (to_large) {
cout << endl << "File size too large!" << endl << endl;
}
else {
break;
}
}
}
}
} while (true);
ofstream file(filename, ios::out | ios::binary);
if (file.is_open()) {
cout << endl << "Generating content...";
char *content = nullptr;
if (size > max_size) {
content = (char *)calloc((size_t)max_size, sizeof(char));
}
else {
content = (char *)calloc((size_t)size, sizeof(char));
}
if (content != nullptr) {
cout << " Done!" << endl;
uint64_t times = size / max_size;
uint64_t rest = size % max_size;
cout << "Writing content...";
for (uint64_t i = 0; i < times && !file.bad(); ++i) {
file.write(content, max_size);
}
if (rest > 0) {
file.write(content, rest);
}
free(content);
if (!file.bad()) {
cout << " Done!" << endl;
}
else {
cout << " ERROR! (Something went wrong...)" << endl;
}
}
else {
cout << " ERROR! (Unable to allocate enough memory)" << endl;
}
file.close();
}
return 0;
}