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

synchronization with C++ version

This commit is contained in:
Nikolay Pultsin 2012-10-27 02:30:03 +04:00
parent e790aeb68a
commit 8ac2815d94
19 changed files with 640 additions and 518 deletions

View file

@ -22,22 +22,24 @@
#include <ZLInputStream.h>
#include <ZLLanguageDetector.h>
#include <ZLImage.h>
#include <ZLEncodingConverter.h>
#include "FormatPlugin.h"
#include "../library/Book.h"
void FormatPlugin::detectEncodingAndLanguage(Book &book, ZLInputStream &stream) {
bool FormatPlugin::detectEncodingAndLanguage(Book &book, ZLInputStream &stream, bool force) {
std::string language = book.language();
std::string encoding = book.encoding();
if (!encoding.empty()) {
return;
if (!force && !encoding.empty()) {
return true;
}
bool detected = false;
PluginCollection &collection = PluginCollection::Instance();
if (encoding.empty()) {
encoding = "utf-8";
encoding = ZLEncodingConverter::UTF8;
}
if (collection.isLanguageAutoDetectEnabled() && stream.open()) {
static const int BUFSIZE = 65536;
@ -47,25 +49,30 @@ void FormatPlugin::detectEncodingAndLanguage(Book &book, ZLInputStream &stream)
shared_ptr<ZLLanguageDetector::LanguageInfo> info = ZLLanguageDetector().findInfo(buffer, size);
delete[] buffer;
if (!info.isNull()) {
detected = true;
if (!info->Language.empty()) {
language = info->Language;
}
encoding = info->Encoding;
if ((encoding == "us-ascii") || (encoding == "iso-8859-1")) {
if (encoding == ZLEncodingConverter::ASCII || encoding == "iso-8859-1") {
encoding = "windows-1252";
}
}
}
book.setEncoding(encoding);
book.setLanguage(language);
return detected;
}
void FormatPlugin::detectLanguage(Book &book, ZLInputStream &stream) {
bool FormatPlugin::detectLanguage(Book &book, ZLInputStream &stream, const std::string &encoding, bool force) {
std::string language = book.language();
if (!language.empty()) {
return;
if (!force && !language.empty()) {
return true;
}
bool detected = false;
PluginCollection &collection = PluginCollection::Instance();
if (collection.isLanguageAutoDetectEnabled() && stream.open()) {
static const int BUFSIZE = 65536;
@ -73,15 +80,18 @@ void FormatPlugin::detectLanguage(Book &book, ZLInputStream &stream) {
const size_t size = stream.read(buffer, BUFSIZE);
stream.close();
shared_ptr<ZLLanguageDetector::LanguageInfo> info =
ZLLanguageDetector().findInfoForEncoding(book.encoding(), buffer, size, -20000);
ZLLanguageDetector().findInfoForEncoding(encoding, buffer, size, -20000);
delete[] buffer;
if (!info.isNull()) {
detected = true;
if (!info->Language.empty()) {
language = info->Language;
}
}
}
book.setLanguage(language);
return detected;
}
const std::string &FormatPlugin::tryOpen(const ZLFile&) const {