1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsCyrillicProb.h"
10 #include "nsISupports.h"
11 #include "nsICharsetDetector.h"
12 #include "nsICharsetDetectionObserver.h"
13 #include "nsIStringCharsetDetector.h"
14 #include "nsCyrillicDetector.h"
16 //----------------------------------------------------------------------
17 // Interface nsISupports [implementation]
18 NS_IMPL_ISUPPORTS(nsCyrXPCOMDetector
, nsICharsetDetector
)
19 NS_IMPL_ISUPPORTS(nsCyrXPCOMStringDetector
, nsIStringCharsetDetector
)
21 void nsCyrillicDetector::HandleData(const char* aBuf
, uint32_t aLen
)
28 for(i
=0, b
=aBuf
;i
<aLen
;i
++,b
++)
30 for(unsigned j
=0;j
<mItems
;j
++)
33 cls
= mCyrillicClass
[j
][(*b
) & 0x7F];
36 NS_ASSERTION( cls
<= 32 , "illegal character class");
37 mProb
[j
] += gCyrillicProb
[mLastCls
[j
]][cls
];
41 // We now only based on the first block we receive
45 //---------------------------------------------------------------------
46 #define THRESHOLD_RATIO 1.5f
47 void nsCyrillicDetector::DataEnd()
54 for(j
=0;j
<mItems
;j
++) {
62 if( 0 == max
) // if we didn't get any 8 bits data
67 printf("Charset %s->\t%d\n", mCharsets
[j
], mProb
[j
]);
69 this->Report(mCharsets
[maxIdx
]);
73 //---------------------------------------------------------------------
74 nsCyrXPCOMDetector:: nsCyrXPCOMDetector(uint8_t aItems
,
75 const uint8_t ** aCyrillicClass
,
76 const char **aCharsets
)
77 : nsCyrillicDetector(aItems
, aCyrillicClass
, aCharsets
)
82 //---------------------------------------------------------------------
83 nsCyrXPCOMDetector::~nsCyrXPCOMDetector()
87 //---------------------------------------------------------------------
88 NS_IMETHODIMP
nsCyrXPCOMDetector::Init(
89 nsICharsetDetectionObserver
* aObserver
)
91 NS_ASSERTION(mObserver
== nullptr , "Init twice");
92 if(nullptr == aObserver
)
93 return NS_ERROR_ILLEGAL_VALUE
;
95 mObserver
= aObserver
;
99 //----------------------------------------------------------
100 NS_IMETHODIMP
nsCyrXPCOMDetector::DoIt(
101 const char* aBuf
, uint32_t aLen
, bool* oDontFeedMe
)
103 NS_ASSERTION(mObserver
!= nullptr , "have not init yet");
105 if((nullptr == aBuf
) || (nullptr == oDontFeedMe
))
106 return NS_ERROR_ILLEGAL_VALUE
;
108 this->HandleData(aBuf
, aLen
);
109 *oDontFeedMe
= false;
113 //----------------------------------------------------------
114 NS_IMETHODIMP
nsCyrXPCOMDetector::Done()
116 NS_ASSERTION(mObserver
!= nullptr , "have not init yet");
121 //----------------------------------------------------------
122 void nsCyrXPCOMDetector::Report(const char* aCharset
)
124 NS_ASSERTION(mObserver
!= nullptr , "have not init yet");
125 mObserver
->Notify(aCharset
, eBestAnswer
);
128 //---------------------------------------------------------------------
129 nsCyrXPCOMStringDetector:: nsCyrXPCOMStringDetector(uint8_t aItems
,
130 const uint8_t ** aCyrillicClass
,
131 const char **aCharsets
)
132 : nsCyrillicDetector(aItems
, aCyrillicClass
, aCharsets
)
136 //---------------------------------------------------------------------
137 nsCyrXPCOMStringDetector::~nsCyrXPCOMStringDetector()
141 //---------------------------------------------------------------------
142 void nsCyrXPCOMStringDetector::Report(const char *aCharset
)
147 //---------------------------------------------------------------------
148 NS_IMETHODIMP
nsCyrXPCOMStringDetector::DoIt(const char* aBuf
, uint32_t aLen
,
149 const char** oCharset
, nsDetectionConfident
&oConf
)
153 this->HandleData(aBuf
, aLen
);