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: michael@0: michael@0: #include "nsEscCharsetProber.h" michael@0: #include "nsUniversalDetector.h" michael@0: michael@0: nsEscCharSetProber::nsEscCharSetProber(uint32_t aLanguageFilter) michael@0: { michael@0: for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) michael@0: mCodingSM[i] = nullptr; michael@0: if (aLanguageFilter & NS_FILTER_CHINESE_SIMPLIFIED) michael@0: { michael@0: mCodingSM[0] = new nsCodingStateMachine(&HZSMModel); michael@0: mCodingSM[1] = new nsCodingStateMachine(&ISO2022CNSMModel); michael@0: } michael@0: if (aLanguageFilter & NS_FILTER_JAPANESE) michael@0: mCodingSM[2] = new nsCodingStateMachine(&ISO2022JPSMModel); michael@0: if (aLanguageFilter & NS_FILTER_KOREAN) michael@0: mCodingSM[3] = new nsCodingStateMachine(&ISO2022KRSMModel); michael@0: mActiveSM = NUM_OF_ESC_CHARSETS; michael@0: mState = eDetecting; michael@0: mDetectedCharset = nullptr; michael@0: } michael@0: michael@0: nsEscCharSetProber::~nsEscCharSetProber(void) michael@0: { michael@0: for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) michael@0: delete mCodingSM[i]; michael@0: } michael@0: michael@0: void nsEscCharSetProber::Reset(void) michael@0: { michael@0: mState = eDetecting; michael@0: for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) michael@0: if (mCodingSM[i]) michael@0: mCodingSM[i]->Reset(); michael@0: mActiveSM = NUM_OF_ESC_CHARSETS; michael@0: mDetectedCharset = nullptr; michael@0: } michael@0: michael@0: nsProbingState nsEscCharSetProber::HandleData(const char* aBuf, uint32_t aLen) michael@0: { michael@0: nsSMState codingState; michael@0: int32_t j; michael@0: uint32_t i; michael@0: michael@0: for ( i = 0; i < aLen && mState == eDetecting; i++) michael@0: { michael@0: for (j = mActiveSM-1; j>= 0; j--) michael@0: { michael@0: if (mCodingSM[j]) michael@0: { michael@0: codingState = mCodingSM[j]->NextState(aBuf[i]); michael@0: if (codingState == eItsMe) michael@0: { michael@0: mState = eFoundIt; michael@0: mDetectedCharset = mCodingSM[j]->GetCodingStateMachine(); michael@0: return mState; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return mState; michael@0: } michael@0: