michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "nscore.h" michael@0: #include "nsCyrillicProb.h" michael@0: #include michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsISupports.h" michael@0: #include "nsICharsetDetector.h" michael@0: #include "nsICharsetDetectionObserver.h" michael@0: #include "nsIStringCharsetDetector.h" michael@0: #include "nsCyrillicDetector.h" michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Interface nsISupports [implementation] michael@0: NS_IMPL_ISUPPORTS(nsCyrXPCOMDetector, nsICharsetDetector) michael@0: NS_IMPL_ISUPPORTS(nsCyrXPCOMStringDetector, nsIStringCharsetDetector) michael@0: michael@0: void nsCyrillicDetector::HandleData(const char* aBuf, uint32_t aLen) michael@0: { michael@0: uint8_t cls; michael@0: const char* b; michael@0: uint32_t i; michael@0: if(mDone) michael@0: return; michael@0: for(i=0, b=aBuf;i max) michael@0: { michael@0: max = mProb[j]; michael@0: maxIdx= j; michael@0: } michael@0: } michael@0: michael@0: if( 0 == max ) // if we didn't get any 8 bits data michael@0: return; michael@0: michael@0: #ifdef DEBUG michael@0: for(j=0;j\t%d\n", mCharsets[j], mProb[j]); michael@0: #endif michael@0: this->Report(mCharsets[maxIdx]); michael@0: mDone = true; michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsCyrXPCOMDetector:: nsCyrXPCOMDetector(uint8_t aItems, michael@0: const uint8_t ** aCyrillicClass, michael@0: const char **aCharsets) michael@0: : nsCyrillicDetector(aItems, aCyrillicClass, aCharsets) michael@0: { michael@0: mObserver = nullptr; michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsCyrXPCOMDetector::~nsCyrXPCOMDetector() michael@0: { michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: NS_IMETHODIMP nsCyrXPCOMDetector::Init( michael@0: nsICharsetDetectionObserver* aObserver) michael@0: { michael@0: NS_ASSERTION(mObserver == nullptr , "Init twice"); michael@0: if(nullptr == aObserver) michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: michael@0: mObserver = aObserver; michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------- michael@0: NS_IMETHODIMP nsCyrXPCOMDetector::DoIt( michael@0: const char* aBuf, uint32_t aLen, bool* oDontFeedMe) michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: michael@0: if((nullptr == aBuf) || (nullptr == oDontFeedMe)) michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: michael@0: this->HandleData(aBuf, aLen); michael@0: *oDontFeedMe = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------- michael@0: NS_IMETHODIMP nsCyrXPCOMDetector::Done() michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: this->DataEnd(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //---------------------------------------------------------- michael@0: void nsCyrXPCOMDetector::Report(const char* aCharset) michael@0: { michael@0: NS_ASSERTION(mObserver != nullptr , "have not init yet"); michael@0: mObserver->Notify(aCharset, eBestAnswer); michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsCyrXPCOMStringDetector:: nsCyrXPCOMStringDetector(uint8_t aItems, michael@0: const uint8_t ** aCyrillicClass, michael@0: const char **aCharsets) michael@0: : nsCyrillicDetector(aItems, aCyrillicClass, aCharsets) michael@0: { michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: nsCyrXPCOMStringDetector::~nsCyrXPCOMStringDetector() michael@0: { michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: void nsCyrXPCOMStringDetector::Report(const char *aCharset) michael@0: { michael@0: mResult = aCharset; michael@0: } michael@0: michael@0: //--------------------------------------------------------------------- michael@0: NS_IMETHODIMP nsCyrXPCOMStringDetector::DoIt(const char* aBuf, uint32_t aLen, michael@0: const char** oCharset, nsDetectionConfident &oConf) michael@0: { michael@0: mResult = nullptr; michael@0: mDone = false; michael@0: this->HandleData(aBuf, aLen); michael@0: this->DataEnd(); michael@0: *oCharset=mResult; michael@0: oConf = eBestAnswer; michael@0: return NS_OK; michael@0: } michael@0: michael@0: