-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenRom.cpp
More file actions
146 lines (131 loc) · 3.66 KB
/
Copy pathGenRom.cpp
File metadata and controls
146 lines (131 loc) · 3.66 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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
void printUsage()
{
std::cout << "Usage: TrimFile -i <input_file> [-o <output_file>] -size <size> [-check] [-h]\n";
std::cout << "Options:\n";
std::cout << " -i <input_file> Specify the input file\n";
std::cout << " -o <output_file> Specify the output file (default: overwrite input file)\n";
std::cout << " -size <size> Specify the target size (e.g., 2048, 2048bit, 2048kb, 2048mb, 2048mbit)\n";
std::cout << " -check Check if the file size is within the specified limit\n";
std::cout << " -h Display this help message\n";
}
long long parseSize(const std::string& sizeStr)
{
long long size = 0;
if (sizeStr.ends_with("bit"))
{
size = std::stoll(sizeStr.substr(0, sizeStr.size() - 3)) / 8;
}
else if (sizeStr.ends_with("byte"))
{
size = std::stoll(sizeStr.substr(0, sizeStr.size() - 4));
}
else if (sizeStr.ends_with("kb"))
{
size = std::stoll(sizeStr.substr(0, sizeStr.size() - 2)) * 1024;
}
else if (sizeStr.ends_with("mb"))
{
size = std::stoll(sizeStr.substr(0, sizeStr.size() - 2)) * 1024 * 1024;
}
else if (sizeStr.ends_with("mbit"))
{
size = std::stoll(sizeStr.substr(0, sizeStr.size() - 4)) * 1024 * 1024 / 8;
}
else
{
size = std::stoll(sizeStr); // Default to bytes
}
return size;
}
void trimFile(const std::string& inputFile, const std::string& outputFile, long long size, bool check)
{
std::ifstream inFile(inputFile, std::ios::binary | std::ios::ate);
if (!inFile)
{
std::cerr << "Error: Cannot open input file.\n";
return;
}
long long fileSize = inFile.tellg();
inFile.seekg(0, std::ios::beg);
if (check)
{
if (fileSize > size)
{
std::cerr << "Error: File size exceeds specified size.\n";
}
else
{
std::cout << "File size is within the specified limit.\n";
}
return;
}
std::ofstream outFile(outputFile, std::ios::binary);
if (!outFile)
{
std::cerr << "Error: Cannot open output file.\n";
return;
}
char buffer[1024];
long long bytesToWrite = std::min(size, fileSize);
while (bytesToWrite > 0)
{
long long chunkSize = std::min(static_cast<long long>(sizeof(buffer)), bytesToWrite);
inFile.read(buffer, chunkSize);
outFile.write(buffer, chunkSize);
bytesToWrite -= chunkSize;
}
std::cout << "File successfully trimmed to " << size << " bytes.\n";
}
int main(int argc, char* argv[])
{
std::string inputFile, outputFile;
std::string sizeStr;
bool check = false;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-h")
{
printUsage();
return 0;
}
else if (arg == "-i" && i + 1 < argc)
{
inputFile = argv[++i];
}
else if (arg == "-o" && i + 1 < argc)
{
outputFile = argv[++i];
}
else if (arg == "-size" && i + 1 < argc)
{
sizeStr = argv[++i];
}
else if (arg == "-check")
{
check = true;
}
else
{
printUsage();
return 1;
}
}
if (inputFile.empty() || sizeStr.empty())
{
printUsage();
return 1;
}
if (outputFile.empty())
{
outputFile = inputFile;
}
long long size = parseSize(sizeStr);
trimFile(inputFile, outputFile, size, check);
return 0;
}