-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanzi_harvester.cpp
56 lines (47 loc) · 1.38 KB
/
hanzi_harvester.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
#include <fstream>
#include <iostream>
#include <set>
#include <string>
using namespace std;
set<string> getCharacters(const string& filename) {
ifstream file(filename, ios::binary); // Open file in binary mode to read bytes instead of characters
set<string> characters; // Look at me, I'm using a set!
if (file.is_open()) {
string character;
char c;
while (file.get(c)) { //while there's still characters to read
if ((c & 0x80) == 0) { //we have a single-byte character bruh bitwise is confusing
if (!character.empty()) {
characters.insert(character);
character.clear();
}
character += c;
} else if ((c & 0xC0) == 0xC0) { // Start of new multi-byte character
if (!character.empty()) {
characters.insert(character);
}
character = c;
} else { // Continuation byte of a multi-byte character
character += c;
}
}
if (!character.empty()) {
characters.insert(character);
}
}
return characters;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: cpp_program <filename>" << endl;
return 1;
}
string filename = argv[1];
set<string> characters = getCharacters(filename);
ofstream myfile("characters.txt");
for (const string& character : characters) {
myfile << character << endl;
}
myfile.close();
return 0;
}