-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiftjis.h
More file actions
54 lines (44 loc) · 1.55 KB
/
shiftjis.h
File metadata and controls
54 lines (44 loc) · 1.55 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
//
// Created by layetri on 7/25/22.
//
#ifndef SHIFTJISREADER_H
#define SHIFTJISREADER_H
#include <juce_core/juce_core.h>
#include <fstream>
#include <iconv.h>
class ShiftJIS {
public:
/**
* Convert a file from ShiftJIS to UTF-8 encoding
* @param file an instance of juce::File to convert
* @return the file's contents as a juce::StringArray
*/
static juce::StringArray read(const juce::File& file) {
juce::StringArray lines;
if(file.exists()) {
std::fstream filestream(file.getFullPathName().toStdString());
std::string res;
char sDst[1024];
while(getline(filestream, res)) {
auto nSrc = std::strlen(res.c_str());
auto pSrc = res.c_str();
size_t nDst = 1023;
iconv_t icd;
char* pDst = sDst;
while(0 < nSrc) {
icd = iconv_open("UTF-8", "Shift_JIS");
iconv(icd, const_cast<char **>(&pSrc), &nSrc, &pDst, &nDst);
iconv_close(icd);
}
juce::String str = juce::CharPointer_UTF8(sDst);
str = str.upToFirstOccurrenceOf("\r", false, true);
lines.add(str);
res = "";
}
} else {
std::cerr << "[ShiftJIS] File does not exist!\n";
}
return lines;
}
};
#endif //SHIFTJISREADER_H