1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/universalchardet/src/base/nsEscCharsetProber.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#include "nsEscCharsetProber.h" 1.11 +#include "nsUniversalDetector.h" 1.12 + 1.13 +nsEscCharSetProber::nsEscCharSetProber(uint32_t aLanguageFilter) 1.14 +{ 1.15 + for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) 1.16 + mCodingSM[i] = nullptr; 1.17 + if (aLanguageFilter & NS_FILTER_CHINESE_SIMPLIFIED) 1.18 + { 1.19 + mCodingSM[0] = new nsCodingStateMachine(&HZSMModel); 1.20 + mCodingSM[1] = new nsCodingStateMachine(&ISO2022CNSMModel); 1.21 + } 1.22 + if (aLanguageFilter & NS_FILTER_JAPANESE) 1.23 + mCodingSM[2] = new nsCodingStateMachine(&ISO2022JPSMModel); 1.24 + if (aLanguageFilter & NS_FILTER_KOREAN) 1.25 + mCodingSM[3] = new nsCodingStateMachine(&ISO2022KRSMModel); 1.26 + mActiveSM = NUM_OF_ESC_CHARSETS; 1.27 + mState = eDetecting; 1.28 + mDetectedCharset = nullptr; 1.29 +} 1.30 + 1.31 +nsEscCharSetProber::~nsEscCharSetProber(void) 1.32 +{ 1.33 + for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) 1.34 + delete mCodingSM[i]; 1.35 +} 1.36 + 1.37 +void nsEscCharSetProber::Reset(void) 1.38 +{ 1.39 + mState = eDetecting; 1.40 + for (uint32_t i = 0; i < NUM_OF_ESC_CHARSETS; i++) 1.41 + if (mCodingSM[i]) 1.42 + mCodingSM[i]->Reset(); 1.43 + mActiveSM = NUM_OF_ESC_CHARSETS; 1.44 + mDetectedCharset = nullptr; 1.45 +} 1.46 + 1.47 +nsProbingState nsEscCharSetProber::HandleData(const char* aBuf, uint32_t aLen) 1.48 +{ 1.49 + nsSMState codingState; 1.50 + int32_t j; 1.51 + uint32_t i; 1.52 + 1.53 + for ( i = 0; i < aLen && mState == eDetecting; i++) 1.54 + { 1.55 + for (j = mActiveSM-1; j>= 0; j--) 1.56 + { 1.57 + if (mCodingSM[j]) 1.58 + { 1.59 + codingState = mCodingSM[j]->NextState(aBuf[i]); 1.60 + if (codingState == eItsMe) 1.61 + { 1.62 + mState = eFoundIt; 1.63 + mDetectedCharset = mCodingSM[j]->GetCodingStateMachine(); 1.64 + return mState; 1.65 + } 1.66 + } 1.67 + } 1.68 + } 1.69 + 1.70 + return mState; 1.71 +} 1.72 +