1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 10:19:33 +02:00

language/encoding detection (in progress)

This commit is contained in:
Nikolay Pultsin 2012-03-18 03:12:25 +00:00
parent bc97e8420a
commit 092c1f00d2
4 changed files with 14 additions and 20 deletions

View file

@ -101,6 +101,17 @@ JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPl
extern "C" extern "C"
JNIEXPORT void JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_detectLanguageAndEncoding(JNIEnv* env, jobject thiz, jobject javaBook) { JNIEXPORT void JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_detectLanguageAndEncoding(JNIEnv* env, jobject thiz, jobject javaBook) {
// TODO: implement // TODO: implement
shared_ptr<FormatPlugin> plugin = findCppPlugin(env, thiz);
if (plugin.isNull()) {
return;
}
shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook);
if (!plugin->readLanguageAndEncoding(*book)) {
return;
}
//fillLanguageAndEncoding(env, javaBook, *book);
} }
static bool initBookModel(JNIEnv *env, jobject javaModel, BookModel &model) { static bool initBookModel(JNIEnv *env, jobject javaModel, BookModel &model) {

View file

@ -31,24 +31,20 @@ void FormatPlugin::detectEncodingAndLanguage(Book &book, ZLInputStream &stream)
std::string language = book.language(); std::string language = book.language();
std::string encoding = book.encoding(); std::string encoding = book.encoding();
if (!encoding.empty() && !language.empty()) { if (!encoding.empty()) {
return; return;
} }
PluginCollection &collection = PluginCollection::Instance(); PluginCollection &collection = PluginCollection::Instance();
if (language.empty()) {
language = collection.defaultLanguage();
}
if (encoding.empty()) { if (encoding.empty()) {
encoding = collection.defaultEncoding(); encoding = "utf-8";
} }
if (collection.isLanguageAutoDetectEnabled() && stream.open()) { if (collection.isLanguageAutoDetectEnabled() && stream.open()) {
static const int BUFSIZE = 65536; static const int BUFSIZE = 65536;
char *buffer = new char[BUFSIZE]; char *buffer = new char[BUFSIZE];
const size_t size = stream.read(buffer, BUFSIZE); const size_t size = stream.read(buffer, BUFSIZE);
stream.close(); stream.close();
shared_ptr<ZLLanguageDetector::LanguageInfo> info = shared_ptr<ZLLanguageDetector::LanguageInfo> info = ZLLanguageDetector().findInfo(buffer, size);
ZLLanguageDetector().findInfo(buffer, size);
delete[] buffer; delete[] buffer;
if (!info.isNull()) { if (!info.isNull()) {
if (!info->Language.empty()) { if (!info->Language.empty()) {
@ -71,9 +67,6 @@ void FormatPlugin::detectLanguage(Book &book, ZLInputStream &stream) {
} }
PluginCollection &collection = PluginCollection::Instance(); PluginCollection &collection = PluginCollection::Instance();
if (language.empty()) {
language = collection.defaultLanguage();
}
if (collection.isLanguageAutoDetectEnabled() && stream.open()) { if (collection.isLanguageAutoDetectEnabled() && stream.open()) {
static const int BUFSIZE = 65536; static const int BUFSIZE = 65536;
char *buffer = new char[BUFSIZE]; char *buffer = new char[BUFSIZE];

View file

@ -84,8 +84,6 @@ public:
shared_ptr<FormatPlugin> pluginByType(const std::string &fileType) const; shared_ptr<FormatPlugin> pluginByType(const std::string &fileType) const;
bool isLanguageAutoDetectEnabled(); bool isLanguageAutoDetectEnabled();
std::string defaultLanguage();
std::string defaultEncoding();
private: private:
static PluginCollection *ourInstance; static PluginCollection *ourInstance;

View file

@ -95,11 +95,3 @@ shared_ptr<FormatPlugin> PluginCollection::pluginByType(const std::string &fileT
bool PluginCollection::isLanguageAutoDetectEnabled() { bool PluginCollection::isLanguageAutoDetectEnabled() {
return true; return true;
} }
std::string PluginCollection::defaultLanguage() {
return "en";
}
std::string PluginCollection::defaultEncoding() {
return "windows-1252";
}