-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexNames.cpp
More file actions
66 lines (58 loc) · 1.49 KB
/
HexNames.cpp
File metadata and controls
66 lines (58 loc) · 1.49 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
#include <iostream>
#include <filesystem>
#include <stdio.h>
#include <fstream>
#include <iterator>
#include <vector>
namespace fs = std::filesystem;
std::string extract_name(const fs::path path, uint32_t offset)
{
std::vector<unsigned char> bytes;
std::ifstream file_stream;
file_stream.open(path, std::ios::binary);
if (file_stream.fail())
{
std::cout << "Failed to open " << path << '\n';
return "";
}
file_stream.seekg(offset, std::ios::beg);
while (!file_stream.eof())
{
unsigned char byte;
file_stream >> byte;
if (file_stream.fail())
{
return "";
}
if (byte < 0x20 || byte > 0x7F) // range of ASCII printable characters (excluding extended)
{
break;
}
bytes.push_back(byte);
}
file_stream.close();
return std::string(bytes.begin(), bytes.end());
}
int main(int argc, char* argv[])
{
uint32_t offset;
std::cout << "Enter offset: ";
std::cin >> std::hex >> offset;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (int i{ 1 }; i < argc; ++i)
{
fs::path file_path{ argv[i] };
if (!fs::exists(file_path))
continue;
std::string extracted_name{ extract_name(file_path, offset) };
if (extracted_name.empty())
{
std::cout << "Failed to find name for " << file_path.string() << '\n';
continue;
}
fs::path new_file_path{ file_path.replace_filename(extracted_name).string() + file_path.extension().string() };
fs::rename(file_path, new_file_path);
}
std::cout << "Operation finished." << '\n';
static_cast<void>(getchar());
}