50 #include <nsComponentManagerUtils.h>
51 #include <nsICharsetDetector.h>
69 nsICharsetDetectionObserver)
81 NS_ENSURE_ARG_POINTER(aIsCharsetFound);
83 *aIsCharsetFound = mIsCharsetFound;
88 sbCharsetDetector::Detect(
const nsACString& aStringToDetect)
97 mDetector = do_CreateInstance(
98 NS_CHARSET_DETECTOR_CONTRACTID_BASE
"universal_charset_detector");
100 nsCOMPtr<nsICharsetDetectionObserver>
observer =
101 do_QueryInterface(NS_ISUPPORTS_CAST(nsICharsetDetectionObserver*,
this));
102 NS_ENSURE_TRUE(observer, NS_ERROR_NO_INTERFACE);
104 rv = mDetector->Init(observer);
105 NS_ENSURE_SUCCESS(rv, rv);
111 nsDependentCString raw(aStringToDetect.BeginReading());
114 mDetectedCharset.AssignLiteral(
"UTF-8");
119 rv = RunCharsetDetector(aStringToDetect);
120 if (NS_SUCCEEDED(rv) && !mLastCharset.IsEmpty()) {
121 mDetectedCharset.Assign(mLastCharset);
122 if (eSureAnswer == mLastConfidence || eBestAnswer == mLastConfidence)
123 mIsCharsetFound = PR_TRUE;
133 const char *
str = aStringToDetect.BeginReading();
134 int size = MultiByteToWideChar(CP_ACP,
135 MB_ERR_INVALID_CHARS,
137 aStringToDetect.Length(),
142 mDetectedCharset.AssignLiteral(
"CP_ACP");
148 mDetectedCharset.Truncate();
153 sbCharsetDetector::Finish(nsACString& _retval)
158 nsresult rv = mDetector->Done();
159 NS_ENSURE_SUCCESS(rv, rv);
161 if (!mLastCharset.IsEmpty())
162 mDetectedCharset = mLastCharset;
165 mLastConfidence = eNoAnswerYet;
166 mIsCharsetFound = PR_FALSE;
173 _retval = mDetectedCharset;
187 mLastCharset.AssignLiteral(aCharset);
188 mLastConfidence = aConf;
203 : mLastConfidence(eNoAnswerYet),
204 mIsCharsetFound(PR_FALSE),
224 nsresult sbCharsetDetector::RunCharsetDetector(
const nsACString& aStringToDetect)
226 NS_ENSURE_TRUE(mDetector, NS_ERROR_NOT_INITIALIZED);
229 if (NS_SUCCEEDED(rv)) {
230 PRUint32 chunkSize = aStringToDetect.Length();
231 const char *str = aStringToDetect.BeginReading();
232 rv = mDetector->DoIt(str, chunkSize, &mIsDone);
233 NS_ENSURE_SUCCESS(rv, rv);
235 rv = mDetector->Done();
236 NS_ENSURE_SUCCESS(rv, rv);
Songbird Charset Detector Definitions.
NS_DECL_ISUPPORTS NS_DECL_SBICHARSETDETECTOR sbCharsetDetector()
PRBool IsUTF8(const nsACString &aString)
NS_IMPL_THREADSAFE_ISUPPORTS2(sbCharsetDetector, sbICharsetDetector, nsICharsetDetectionObserver) NS_IMETHODIMP sbCharsetDetector
virtual ~sbCharsetDetector()
A helper class to detect the string charset.
PRBool IsLikelyUTF8(const nsACString &aString)
NS_IMETHOD Notify(const char *aCharset, nsDetectionConfident aConf)