forked from Hexagenic/wanikaniwallpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.cpp
99 lines (80 loc) · 2.1 KB
/
order.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
#include "order.hpp"
#include "kanji.hpp"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <utf8.h>
#include <vector>
using std::string;
namespace wanikani
{
Order::Order(string filename)
{
std::ifstream orderFile;
orderFile.open(filename.c_str());
if(orderFile.fail())
{
std::cerr << "Could no open file " << filename << " for reading.\n";
exit(1);
}
orderFile.seekg(0, orderFile.end);
int length = orderFile.tellg();
orderFile.seekg(0, orderFile.beg);
char *buffer = new char[length];
orderFile.read(buffer, length);
orderFile.close();
std::vector<int> wideString;
utf8::utf8to32(buffer, buffer + length, back_inserter(wideString));
int index = 0;
for(int i = 0; i < wideString.size(); i++)
{
int character = wideString[i];
if(character != 10)
{
std::string utf8Character;
utf8::utf32to8(wideString.begin() + i, wideString.begin() + i + 1, back_inserter(utf8Character));
Kanji kanji(character, utf8Character);
intToChar_[index] = character;
if(charToInt_.find(character) != charToInt_.end())
std::cout << "Duplicate character: " << kanji.utf8Character()
<< ", utf32: " << std::hex << character << std::dec << std::endl;
charToInt_[character] = index;
charToKanji_[character] = kanji;
index++;
}
}
}
void Order::update(std::vector<Kanji> list)
{
for(std::vector<Kanji>::iterator i = list.begin(); i!= list.end(); i++)
{
std::map<int, Kanji>::iterator k = charToKanji_.find(i->character());
if(k != charToKanji_.end())
{
k->second = *i;
}
else
std::cout << "Kanji from WaniKani not found in file: " << i->utf8Character()
<< ", utf32: " << std::hex << i->character() << std::dec << std::endl;
}
}
int Order::position(int character)
{
return charToInt_[character];
}
int Order::character(int position)
{
return intToChar_[position];
}
Kanji Order::kanji(int position)
{
const int character = intToChar_[position];
return charToKanji_[character];
}
int Order::size()
{
return intToChar_.size();
}
} //wanikani