2010-05-28 09:40:21 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file charset_ucd.cpp
|
|
|
|
/// @brief Character set detection using Universalchardet
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2010-06-17 02:23:44 +02:00
|
|
|
#include "charset_ucd.h"
|
2010-05-28 09:40:21 +02:00
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2010-05-28 09:40:21 +02:00
|
|
|
#include "../../universalchardet/nsCharSetProber.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libaegisub/io.h"
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
namespace charset {
|
|
|
|
|
2010-06-17 02:23:44 +02:00
|
|
|
UCDetect::UCDetect(const std::string &file): nsUniversalDetector(NS_FILTER_ALL) {
|
2010-05-28 09:40:21 +02:00
|
|
|
{
|
2010-06-24 03:24:09 +02:00
|
|
|
std::auto_ptr<std::ifstream> fp(io::Open(file));
|
2010-05-28 09:40:21 +02:00
|
|
|
|
|
|
|
while (!mDone && !fp->eof()) {
|
|
|
|
char buf[512];
|
|
|
|
fp->read(buf, 512);
|
|
|
|
size_t bytes = fp->gcount();
|
|
|
|
HandleData(buf, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEnd();
|
|
|
|
|
|
|
|
if (mDetectedCharset) {
|
2010-05-28 15:08:00 +02:00
|
|
|
list.insert(CLDPair(1, mDetectedCharset));
|
2010-05-28 09:40:21 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (mInputState) {
|
|
|
|
case eHighbyte: {
|
|
|
|
for (PRInt32 i=0; i<NUM_OF_CHARSET_PROBERS; i++) {
|
|
|
|
if (mCharSetProbers[i]) {
|
|
|
|
float conf = mCharSetProbers[i]->GetConfidence();
|
|
|
|
if (conf > 0.01f) {
|
2010-05-28 15:08:00 +02:00
|
|
|
list.insert(CLDPair(conf, mCharSetProbers[i]->GetCharSetName()));
|
2010-05-28 09:40:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ePureAscii:
|
2010-05-28 15:08:00 +02:00
|
|
|
list.insert(CLDPair(1, "US-ASCII"));
|
2010-05-28 09:40:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw UnknownCharset("Unknown chararacter set.");
|
|
|
|
}
|
|
|
|
|
2010-05-28 15:08:00 +02:00
|
|
|
if (list.empty() && (mInputState == eHighbyte))
|
2010-05-28 09:40:21 +02:00
|
|
|
throw UnknownCharset("Unknown chararacter set.");
|
|
|
|
|
|
|
|
|
|
|
|
} // if mDetectedCharset else
|
|
|
|
}
|
|
|
|
|
2010-05-28 15:08:00 +02:00
|
|
|
std::string UCDetect::Single() {
|
|
|
|
/// @todo Add a debug log here since this shouldn't happen.
|
|
|
|
if (list.empty()) {
|
|
|
|
throw UnknownCharset("Unknown chararacter set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
CharsetListDetected::const_iterator i_lst = list.begin();
|
|
|
|
return i_lst->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 09:40:21 +02:00
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
} // namespace agi
|