Move unicode to charset convertors out of spellcheck glue into myspell.
bug 226756, r=dwitte, sr=alecf git-svn-id: svn://10.0.0.236/trunk@150626 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
b2a8799420
commit
9d60a30a3c
@ -46,12 +46,6 @@
|
||||
|
||||
interface mozIPersonalDictionary : nsISupports {
|
||||
|
||||
/* The language being spell checked (In case we want to qualify words by language */
|
||||
attribute wstring language;
|
||||
|
||||
/* the charset that the spell checker is using */
|
||||
attribute wstring charset;
|
||||
|
||||
/* Initialize the dictionary */
|
||||
void Init();
|
||||
|
||||
@ -65,10 +59,7 @@ interface mozIPersonalDictionary : nsISupports {
|
||||
void GetWordList([array, size_is(count)] out wstring words, out PRUint32 count);
|
||||
|
||||
/* Check a unicode string */
|
||||
boolean CheckUnicode(in wstring word);
|
||||
|
||||
/* Check a string in the current charset */
|
||||
boolean Check(in string word);
|
||||
boolean Check(in wstring word, in wstring lang);
|
||||
|
||||
/* Add a word to the personal dictionary */
|
||||
void AddWord(in wstring word, in wstring lang);
|
||||
|
||||
@ -50,9 +50,6 @@ interface mozISpellCheckingEngine : nsISupports {
|
||||
/* The name of the current dictionary */
|
||||
attribute wstring dictionary;
|
||||
|
||||
/* The charset that we are using, somone may want to know. */
|
||||
readonly attribute wstring charset;
|
||||
|
||||
/* The language we think that we're checking */
|
||||
readonly attribute wstring language;
|
||||
|
||||
|
||||
@ -52,14 +52,18 @@ interface mozISpellI18NUtil : nsISupports {
|
||||
/* The language being spell checked*/
|
||||
readonly attribute wstring language;
|
||||
|
||||
/* The charset that the spell checker is using */
|
||||
attribute wstring charset;
|
||||
|
||||
/* Given a word return a list of possible root forms of that word */
|
||||
void GetRootForm(in wstring word, in PRUint32 type, [array, size_is(count)] out string words, out PRUint32 count);
|
||||
void getRootForm(in wstring word,
|
||||
in PRUint32 type,
|
||||
[array, size_is(count)] out wstring words,
|
||||
out PRUint32 count);
|
||||
|
||||
/* Given a word return a list of possible root forms of that word */
|
||||
void FromRootForm(in wstring word, [array, size_is(icount)] in string iwords, in PRUint32 icount, [array, size_is(ocount)] out wstring owords, out PRUint32 ocount);
|
||||
void FromRootForm(in wstring word,
|
||||
[array, size_is(icount)] in wstring iwords,
|
||||
in PRUint32 icount,
|
||||
[array, size_is(ocount)] out wstring owords,
|
||||
out PRUint32 ocount);
|
||||
|
||||
/* Given a unicode string and an offset find the beginning and end of the next word
|
||||
* begin and end are -1 if there are no words remaining in the string
|
||||
|
||||
@ -113,8 +113,9 @@ NS_IMETHODIMP mozMySpell::SetDictionary(const PRUnichar * aDictionary)
|
||||
NS_WARNING("Dictionary load failed");
|
||||
return res;
|
||||
}
|
||||
mSMgr.setup(mAMgr.get_try_string(),64,&mAMgr);
|
||||
nsString encoding=mAMgr.get_encoding();
|
||||
nsAutoString tryString;
|
||||
mAMgr.get_try_string(tryString);
|
||||
mSMgr.setup(tryString, 64, &mAMgr);
|
||||
nsString language;
|
||||
PRInt32 pos = mDictionary.FindChar('-');
|
||||
if(pos == -1){
|
||||
@ -126,28 +127,11 @@ NS_IMETHODIMP mozMySpell::SetDictionary(const PRUnichar * aDictionary)
|
||||
nsCOMPtr<mozISpellI18NManager> serv(do_GetService("@mozilla.org/spellchecker/i18nmanager;1", &res));
|
||||
if(serv && NS_SUCCEEDED(res)){
|
||||
res = serv->GetUtil(language.get(),getter_AddRefs(mConverter));
|
||||
if(NS_SUCCEEDED(res))
|
||||
res=mConverter->SetCharset(encoding.get());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* readonly attribute wstring charset; */
|
||||
NS_IMETHODIMP mozMySpell::GetCharset(PRUnichar * *aCharset)
|
||||
{
|
||||
nsresult res=NS_OK;
|
||||
NS_PRECONDITION(aCharset != nsnull, "null ptr");
|
||||
if(!aCharset){
|
||||
res = NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
else{
|
||||
*aCharset = ToNewUnicode(mAMgr.get_encoding());
|
||||
if(!aCharset) res = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* readonly attribute wstring language; */
|
||||
NS_IMETHODIMP mozMySpell::GetLanguage(PRUnichar * *aLanguage)
|
||||
{
|
||||
@ -279,47 +263,50 @@ NS_IMETHODIMP mozMySpell::GetDictionaryList(PRUnichar ***dictionaries, PRUint32
|
||||
}
|
||||
|
||||
/* boolean Check (in wstring word); */
|
||||
NS_IMETHODIMP mozMySpell::Check(const PRUnichar *aWord, PRBool *_retval)
|
||||
NS_IMETHODIMP mozMySpell::Check(const PRUnichar *aWord, PRBool *aResult)
|
||||
{
|
||||
if(!aWord || !_retval || !mConverter )
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
char **tmpPtr;
|
||||
PRUint32 count,i;
|
||||
nsresult res;
|
||||
*_retval = PR_FALSE;
|
||||
NS_ENSURE_ARG_POINTER(aWord);
|
||||
NS_ENSURE_ARG_POINTER(aResult);
|
||||
NS_ENSURE_ARG_POINTER(mConverter);
|
||||
|
||||
res = mConverter->GetRootForm(aWord, mozISpellI18NUtil::kCheck, &tmpPtr, &count);
|
||||
if(NS_FAILED(res)) return res;
|
||||
for(i=0;i<count;i++){
|
||||
*_retval = mAMgr.check(nsDependentCString(tmpPtr[i]));
|
||||
if(*_retval) break;
|
||||
PRUnichar **tmpPtr;
|
||||
PRUint32 count,i;
|
||||
*aResult = PR_FALSE;
|
||||
|
||||
nsresult rv = mConverter->GetRootForm(aWord, mozISpellI18NUtil::kCheck, &tmpPtr, &count);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
for(i=0 ; i<count ; i++){
|
||||
*aResult = mAMgr.check(nsDependentString(tmpPtr[i]));
|
||||
if (*aResult) break;
|
||||
}
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, tmpPtr);
|
||||
return res;
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* void Suggest (in wstring word, [array, size_is (count)] out wstring suggestions, out PRUint32 count); */
|
||||
NS_IMETHODIMP mozMySpell::Suggest(const PRUnichar *aword, PRUnichar ***suggestions, PRUint32 *scount)
|
||||
NS_IMETHODIMP mozMySpell::Suggest(const PRUnichar *aWord, PRUnichar ***aSuggestions, PRUint32 *aSuggestionCount)
|
||||
{
|
||||
if(!suggestions || !scount || !mConverter){
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
*suggestions = 0;
|
||||
*scount=0;
|
||||
char **tmpPtr;
|
||||
nsAutoString word(aword);
|
||||
char **slst=nsnull;
|
||||
NS_ENSURE_ARG_POINTER(aSuggestions);
|
||||
NS_ENSURE_ARG_POINTER(aSuggestionCount);
|
||||
NS_ENSURE_ARG_POINTER(mConverter);
|
||||
|
||||
*aSuggestions = 0;
|
||||
*aSuggestionCount=0;
|
||||
PRUnichar **tmpPtr;
|
||||
nsAutoString word(aWord);
|
||||
PRUnichar **slst = nsnull;
|
||||
PRUint32 count;
|
||||
PRUint32 ccount=0;
|
||||
nsresult res;
|
||||
res = mConverter->GetRootForm(aword, mozISpellI18NUtil::kSuggest, &tmpPtr, &count);
|
||||
if(NS_FAILED(res)) return res;
|
||||
for(PRUint32 i=0;(i<count)&&!NS_FAILED(res);i++){
|
||||
res = mSMgr.suggest(&slst,nsDependentCString(tmpPtr[i]),&ccount);
|
||||
|
||||
nsresult rv = mConverter->GetRootForm(aWord, mozISpellI18NUtil::kSuggest, &tmpPtr, &count);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
for (PRUint32 i = 0; (i < count) && NS_SUCCEEDED(rv) ; i++){
|
||||
rv = mSMgr.suggest(&slst, nsDependentString(tmpPtr[i]), &ccount);
|
||||
}
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, tmpPtr);
|
||||
res=mConverter->FromRootForm(aword,(const char **)slst,ccount,suggestions,scount);
|
||||
rv=mConverter->FromRootForm(aWord, (const PRUnichar **)slst, ccount, aSuggestions, aSuggestionCount);
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(ccount, slst);
|
||||
return res;
|
||||
return rv;
|
||||
|
||||
}
|
||||
|
||||
@ -60,7 +60,11 @@
|
||||
#include "nsDirectoryServiceDefs.h"
|
||||
#include "plstr.h"
|
||||
#include "nsNetUtil.h"
|
||||
#include "nsICharsetConverterManager.h"
|
||||
#include "nsUnicharUtilCIID.h"
|
||||
|
||||
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
||||
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
|
||||
|
||||
static PRInt32 SplitString(nsACString &in,nsSharableCString out[],PRInt32 size);
|
||||
static void doubleReverseHack(nsACString &s);
|
||||
@ -134,17 +138,9 @@ myspAffixMgr::Load(const nsString& aDictionary)
|
||||
if(!affStream)return NS_ERROR_FAILURE;
|
||||
res = parse_file(affStream);
|
||||
|
||||
|
||||
res = mPersonalDictionary->SetCharset(mEncoding.get());
|
||||
if(NS_FAILED(res)) return res;
|
||||
|
||||
PRInt32 pos=aDictionary.FindChar('-');
|
||||
if(pos<1) pos = 2; // FIXME should be min of 2 and aDictionary.Length()
|
||||
nsAutoString lang;
|
||||
lang.Assign(Substring(aDictionary,0,pos));
|
||||
res = mPersonalDictionary->SetLanguage(lang.get());
|
||||
if(NS_FAILED(res)) return res;
|
||||
|
||||
mLanguage = Substring(aDictionary,0,pos);
|
||||
|
||||
// load the dictionary
|
||||
nsCOMPtr<nsIInputStream> dicStream;
|
||||
@ -163,8 +159,7 @@ nsresult myspAffixMgr::parse_file(nsIInputStream *strm)
|
||||
PRInt32 j;
|
||||
PRInt32 numents;
|
||||
nsLineBuffer *lineBuffer;
|
||||
nsresult res;
|
||||
res= NS_InitLineBuffer(&lineBuffer);
|
||||
nsresult rv = NS_InitLineBuffer(&lineBuffer);
|
||||
nsCAutoString line;
|
||||
PRBool moreData=PR_TRUE;
|
||||
PRInt32 pos;
|
||||
@ -199,10 +194,8 @@ nsresult myspAffixMgr::parse_file(nsIInputStream *strm)
|
||||
|
||||
pos = line.FindChar(' ');
|
||||
if(pos != -1){
|
||||
nsCAutoString cencoding;
|
||||
cencoding.Assign(Substring(line,pos+1,line.Length()-pos-1));
|
||||
cencoding.CompressWhitespace(PR_TRUE,PR_TRUE);
|
||||
mEncoding.AssignWithConversion(cencoding.get());
|
||||
mEncoding.Assign(Substring(line,pos+1,line.Length()-pos-1));
|
||||
mEncoding.CompressWhitespace(PR_TRUE,PR_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -282,7 +275,18 @@ nsresult myspAffixMgr::parse_file(nsIInputStream *strm)
|
||||
}
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
|
||||
// We do this here, instead of where we set the charset,
|
||||
// to prevent all kind of leakage in case it fails.
|
||||
nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(kCharsetConverterManagerCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = ccm->GetUnicodeDecoder(mEncoding.get(), getter_AddRefs(mDecoder));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
rv = ccm->GetUnicodeEncoder(mEncoding.get(), getter_AddRefs(mEncoder));
|
||||
if (mEncoder && NS_SUCCEEDED(rv)) {
|
||||
mEncoder->SetOutputErrorBehavior(mEncoder->kOnError_Signal, nsnull, '?');
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
@ -332,23 +336,24 @@ myspAffixMgr::LoadDictionary(nsIInputStream *strm)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// return text encoding of dictionary
|
||||
nsString myspAffixMgr::get_encoding()
|
||||
{
|
||||
return mEncoding;
|
||||
}
|
||||
|
||||
|
||||
// return the preferred try string for suggestions
|
||||
nsCString myspAffixMgr::get_try_string()
|
||||
void myspAffixMgr::get_try_string(nsAString &aTryString)
|
||||
{
|
||||
return trystring;
|
||||
PRInt32 outLength;
|
||||
PRInt32 inLength = trystring.Length();
|
||||
nsresult rv = mDecoder->GetMaxLength(trystring.get(), inLength, &outLength);
|
||||
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
PRUnichar *tmpPtr = (PRUnichar *) malloc(sizeof(PRUnichar) * (outLength + 1));
|
||||
if (tmpPtr) {
|
||||
rv = mDecoder->Convert(trystring.get(), &inLength, tmpPtr, &outLength);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
tmpPtr[outLength] = 0;
|
||||
aTryString = tmpPtr;
|
||||
}
|
||||
free(tmpPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PRBool
|
||||
@ -428,19 +433,34 @@ PRBool myspAffixMgr::suffixCheck(const nsAFlatCString &word,PRBool cross,char cr
|
||||
return PR_FALSE;
|
||||
}
|
||||
|
||||
PRBool myspAffixMgr::check(const nsAFlatCString &word)
|
||||
PRBool myspAffixMgr::check(const nsAFlatString &word)
|
||||
{
|
||||
const char * he = NULL;
|
||||
const char *he = nsnull;
|
||||
|
||||
he = mHashTable.Get(word.get());;
|
||||
PRInt32 inLength = word.Length();
|
||||
PRInt32 outLength;
|
||||
nsresult rv = mEncoder->GetMaxLength(word.get(), inLength, &outLength);
|
||||
// NS_ERROR_UENC_NOMAPPING is a NS_SUCCESS, no error.
|
||||
if (NS_FAILED(rv) || rv == NS_ERROR_UENC_NOMAPPING) {
|
||||
// not a word in the current charset, so likely not
|
||||
// a word in the current language
|
||||
return PR_FALSE;
|
||||
}
|
||||
char *charsetWord = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
rv = mEncoder->Convert(word.get(), &inLength, charsetWord, &outLength);
|
||||
charsetWord[outLength] = '\0';
|
||||
|
||||
he = mHashTable.Get(charsetWord);
|
||||
|
||||
if(he != nsnull) return PR_TRUE;
|
||||
if(prefixCheck(word))return PR_TRUE;
|
||||
if(suffixCheck(word))return PR_TRUE;
|
||||
if(prefixCheck(nsDependentCString(charsetWord)))
|
||||
return PR_TRUE;
|
||||
if(suffixCheck(nsDependentCString(charsetWord)))
|
||||
return PR_TRUE;
|
||||
|
||||
PRBool good=PR_FALSE;
|
||||
nsresult res = mPersonalDictionary->Check(word.get(),&good);
|
||||
if(NS_FAILED(res))
|
||||
rv = mPersonalDictionary->Check(word.get(), mLanguage.get(), &good);
|
||||
if(NS_FAILED(rv))
|
||||
return PR_FALSE;
|
||||
return good;
|
||||
}
|
||||
|
||||
@ -60,6 +60,8 @@
|
||||
#include "mozCStr2CStrHashtable.h"
|
||||
#include "mozAffixMod.h"
|
||||
#include "nsNetUtil.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
|
||||
/* Modifications for mozilla Copyright 2001 David Einstein Deinst@world.std.com */
|
||||
|
||||
@ -89,10 +91,9 @@ public:
|
||||
~myspAffixMgr();
|
||||
nsresult GetPersonalDictionary(mozIPersonalDictionary * *aPersonalDictionary);
|
||||
nsresult SetPersonalDictionary(mozIPersonalDictionary * aPersonalDictionary);
|
||||
PRBool check(const nsAFlatCString &word);
|
||||
nsString get_encoding();
|
||||
nsCString get_try_string();
|
||||
nsresult Load(const nsString& aDictionary);
|
||||
PRBool check(const nsAFlatString &word);
|
||||
void get_try_string(nsAString &aTryString);
|
||||
nsresult Load(const nsString &aDictionary);
|
||||
|
||||
protected:
|
||||
|
||||
@ -105,10 +106,13 @@ protected:
|
||||
mozAffixState prefixes;
|
||||
mozAffixState suffixes;
|
||||
|
||||
nsCString trystring;
|
||||
nsString mEncoding;
|
||||
nsCString trystring;
|
||||
nsCString mEncoding;
|
||||
nsString mLanguage;
|
||||
mozCStr2CStrHashtable mHashTable;
|
||||
nsCOMPtr<mozIPersonalDictionary> mPersonalDictionary;
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
||||
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -70,7 +70,7 @@ myspSuggestMgr::~myspSuggestMgr()
|
||||
}
|
||||
|
||||
void
|
||||
myspSuggestMgr::setup(const nsAFlatCString &tryme, int maxn, myspAffixMgr *aptr)
|
||||
myspSuggestMgr::setup(const nsAFlatString &tryme, int maxn, myspAffixMgr *aptr)
|
||||
{
|
||||
// register affix manager and check in string of chars to
|
||||
// try when building candidate suggestions
|
||||
@ -83,21 +83,21 @@ myspSuggestMgr::setup(const nsAFlatCString &tryme, int maxn, myspAffixMgr *aptr)
|
||||
// generate suggestions for a mispelled word
|
||||
// pass in address of array of char * pointers
|
||||
|
||||
nsresult myspSuggestMgr::suggest(char ***slst,const nsAFlatCString &word, PRUint32 *num)
|
||||
nsresult myspSuggestMgr::suggest(PRUnichar ***slst,const nsAFlatString &word, PRUint32 *num)
|
||||
{
|
||||
if(!num || !slst)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
NS_ENSURE_ARG_POINTER(num);
|
||||
NS_ENSURE_ARG_POINTER(slst);
|
||||
|
||||
nsresult res;
|
||||
PRUint32 nsug;
|
||||
PRUint32 i;
|
||||
char **wlst;
|
||||
PRUnichar **wlst;
|
||||
if(!(*slst)){
|
||||
nsug=0;
|
||||
wlst=(char **)nsMemory::Alloc(sizeof(char *)*maxSug);
|
||||
wlst=(PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *) * maxSug);
|
||||
if(!wlst)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
for(i=0;i<maxSug;i++)
|
||||
wlst[i]=nsnull;
|
||||
memset(wlst, nsnull, sizeof(PRUnichar*) * maxSug);
|
||||
}
|
||||
else{
|
||||
wlst=*slst;
|
||||
@ -143,15 +143,15 @@ nsresult myspSuggestMgr::suggest(char ***slst,const nsAFlatCString &word, PRUint
|
||||
|
||||
|
||||
// error is wrong char in place of correct one
|
||||
nsresult myspSuggestMgr::badchar(char ** wlst,const nsAFlatCString &word, PRUint32 *ns)
|
||||
nsresult myspSuggestMgr::badchar(PRUnichar ** wlst,const nsAFlatString &word, PRUint32 *ns)
|
||||
{
|
||||
char tmpc;
|
||||
nsSharableCString candidate;
|
||||
PRUnichar tmpc;
|
||||
nsAutoString candidate;
|
||||
PRBool cwrd;
|
||||
PRUint32 i,j,k;
|
||||
PRUint32 wl = word.Length();
|
||||
candidate.Assign(word);
|
||||
nsACString::iterator candIt;
|
||||
nsASingleFragmentString::char_iterator candIt;
|
||||
for (i=0,candidate.BeginWriting(candIt); i < wl; i++,candIt++) {
|
||||
tmpc = *candIt;
|
||||
for (j=0; j < ctry.Length(); j++) {
|
||||
@ -166,7 +166,7 @@ nsresult myspSuggestMgr::badchar(char ** wlst,const nsAFlatCString &word, PRUint
|
||||
}
|
||||
if (cwrd && pAMgr->check(candidate)) {
|
||||
if (*ns < maxSug) {
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
@ -180,19 +180,19 @@ nsresult myspSuggestMgr::badchar(char ** wlst,const nsAFlatCString &word, PRUint
|
||||
|
||||
|
||||
// error is word has an extra letter it does not need
|
||||
nsresult myspSuggestMgr::extrachar(char ** wlst,const nsAFlatCString &word, PRUint32 *ns)
|
||||
nsresult myspSuggestMgr::extrachar(PRUnichar ** wlst,const nsAFlatString &word, PRUint32 *ns)
|
||||
{
|
||||
PRBool cwrd;
|
||||
nsString stCand;
|
||||
nsSharableCString candidate;
|
||||
nsAutoString candidate;
|
||||
PRUint32 k;
|
||||
PRUint32 wl = word.Length();
|
||||
if (wl < 2) return 0;
|
||||
|
||||
// try omitting one char of word at a time
|
||||
candidate.Assign(Substring(word,1,wl-1));
|
||||
nsACString::iterator r;
|
||||
nsACString::const_iterator p,end;
|
||||
nsASingleFragmentString::char_iterator r;
|
||||
nsASingleFragmentString::const_char_iterator p,end;
|
||||
word.EndReading(end);
|
||||
|
||||
for (word.BeginReading(p),candidate.BeginWriting(r); p != end; ) {
|
||||
@ -205,7 +205,7 @@ nsresult myspSuggestMgr::extrachar(char ** wlst,const nsAFlatCString &word, PRUi
|
||||
}
|
||||
if (cwrd && pAMgr->check(candidate)) {
|
||||
if (*ns < maxSug) {
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
@ -218,16 +218,15 @@ nsresult myspSuggestMgr::extrachar(char ** wlst,const nsAFlatCString &word, PRUi
|
||||
|
||||
|
||||
// error is mising a letter it needs
|
||||
nsresult myspSuggestMgr::forgotchar(char **wlst,const nsAFlatCString &word, PRUint32 *ns)
|
||||
nsresult myspSuggestMgr::forgotchar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *ns)
|
||||
{
|
||||
PRBool cwrd;
|
||||
nsString stCand;
|
||||
nsSharableCString candidate;
|
||||
nsAutoString candidate;
|
||||
PRUint32 i,k;
|
||||
candidate.Assign(" ");
|
||||
candidate.Append(word);
|
||||
nsACString::iterator q;
|
||||
nsACString::const_iterator p,end;
|
||||
candidate = NS_LITERAL_STRING(" ") + word;
|
||||
nsASingleFragmentString::char_iterator q;
|
||||
nsASingleFragmentString::const_char_iterator p,end;
|
||||
word.EndReading(end);
|
||||
|
||||
// try inserting a tryme character before every letter
|
||||
@ -243,7 +242,7 @@ nsresult myspSuggestMgr::forgotchar(char **wlst,const nsAFlatCString &word, PRUi
|
||||
}
|
||||
if (cwrd && pAMgr->check(candidate)) {
|
||||
if (*ns < maxSug) {
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
@ -265,7 +264,7 @@ nsresult myspSuggestMgr::forgotchar(char **wlst,const nsAFlatCString &word, PRUi
|
||||
}
|
||||
if (cwrd && pAMgr->check(candidate)) {
|
||||
if (*ns < maxSug) {
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
@ -277,15 +276,14 @@ nsresult myspSuggestMgr::forgotchar(char **wlst,const nsAFlatCString &word, PRUi
|
||||
|
||||
|
||||
/* error is should have been two words */
|
||||
nsresult myspSuggestMgr::twowords(char ** wlst,const nsAFlatCString &word, PRUint32 *ns)
|
||||
nsresult myspSuggestMgr::twowords(PRUnichar ** wlst,const nsAFlatString &word, PRUint32 *ns)
|
||||
{
|
||||
nsSharableCString candidate;
|
||||
nsString stCand;
|
||||
nsAutoString candidate;
|
||||
PRUint32 pos;
|
||||
PRUint32 wl=word.Length();
|
||||
if (wl < 3) return NS_OK;
|
||||
candidate.Assign(word);
|
||||
nsSharableCString temp;
|
||||
nsAutoString temp;
|
||||
|
||||
// split the string into two pieces after every char
|
||||
// if both pieces are good words make them a suggestion
|
||||
@ -295,8 +293,8 @@ nsresult myspSuggestMgr::twowords(char ** wlst,const nsAFlatCString &word, PRUin
|
||||
temp.Assign(Substring(candidate,pos,wl-pos));
|
||||
if (pAMgr->check(temp)) {
|
||||
if (*ns < maxSug) {
|
||||
candidate.Insert(' ',pos);
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
candidate.Insert(PRUnichar(' '),pos);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
@ -309,15 +307,14 @@ nsresult myspSuggestMgr::twowords(char ** wlst,const nsAFlatCString &word, PRUin
|
||||
|
||||
|
||||
// error is adjacent letter were swapped
|
||||
nsresult myspSuggestMgr::swapchar(char **wlst,const nsAFlatCString &word, PRUint32 *ns)
|
||||
nsresult myspSuggestMgr::swapchar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *ns)
|
||||
{
|
||||
nsSharableCString candidate;
|
||||
char tmpc;
|
||||
nsAutoString candidate;
|
||||
PRUnichar tmpc;
|
||||
PRBool cwrd;
|
||||
nsString stCand;
|
||||
PRUint32 k;
|
||||
candidate.Assign(word);
|
||||
nsACString::iterator p,q,end;
|
||||
nsASingleFragmentString::char_iterator p,q,end;
|
||||
candidate.EndWriting(end);
|
||||
|
||||
for (candidate.BeginWriting(p),q=p, q++; q != end; p++,q++) {
|
||||
@ -333,7 +330,7 @@ nsresult myspSuggestMgr::swapchar(char **wlst,const nsAFlatCString &word, PRUint
|
||||
}
|
||||
if (cwrd && pAMgr->check(candidate)) {
|
||||
if (*ns < maxSug) {
|
||||
wlst[*ns] = ToNewCString(candidate);
|
||||
wlst[*ns] = ToNewUnicode(candidate);
|
||||
if(!wlst[*ns])
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
(*ns)++;
|
||||
|
||||
@ -68,23 +68,23 @@
|
||||
|
||||
class myspSuggestMgr
|
||||
{
|
||||
nsSharableCString ctry;
|
||||
myspAffixMgr* pAMgr;
|
||||
PRUint32 maxSug;
|
||||
nsString ctry;
|
||||
myspAffixMgr* pAMgr;
|
||||
PRUint32 maxSug;
|
||||
|
||||
public:
|
||||
myspSuggestMgr();
|
||||
~myspSuggestMgr();
|
||||
|
||||
void setup(const nsAFlatCString &tryme, int maxn, myspAffixMgr *aptr);
|
||||
nsresult suggest(char ***slst, const nsAFlatCString &word, PRUint32 *num);
|
||||
void setup(const nsAFlatString &tryme, int maxn, myspAffixMgr *aptr);
|
||||
nsresult suggest(PRUnichar ***slst, const nsAFlatString &word, PRUint32 *num);
|
||||
|
||||
protected:
|
||||
nsresult forgotchar(char **wlst,const nsAFlatCString &word, PRUint32 *num);
|
||||
nsresult swapchar(char **wlst,const nsAFlatCString &word, PRUint32 *num);
|
||||
nsresult extrachar(char **wlst,const nsAFlatCString &word, PRUint32 *num);
|
||||
nsresult badchar(char **wlst,const nsAFlatCString &word, PRUint32 *num);
|
||||
nsresult twowords(char **wlst,const nsAFlatCString &word, PRUint32 *num);
|
||||
nsresult forgotchar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *num);
|
||||
nsresult swapchar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *num);
|
||||
nsresult extrachar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *num);
|
||||
nsresult badchar(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *num);
|
||||
nsresult twowords(PRUnichar **wlst,const nsAFlatString &word, PRUint32 *num);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -36,94 +36,42 @@
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "mozEnglishWordUtils.h"
|
||||
#include "nsICharsetConverterManager.h"
|
||||
#include "nsICharsetAlias.h"
|
||||
#include "nsUnicharUtilCIID.h"
|
||||
#include "nsReadableUtils.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsCRT.h"
|
||||
#include "cattable.h"
|
||||
|
||||
|
||||
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
||||
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
|
||||
|
||||
NS_IMPL_ISUPPORTS1(mozEnglishWordUtils, mozISpellI18NUtil)
|
||||
|
||||
mozEnglishWordUtils::mozEnglishWordUtils()
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
/* member initializers and constructor code */
|
||||
mLanguage.Assign(NS_LITERAL_STRING("en"));
|
||||
}
|
||||
|
||||
mozEnglishWordUtils::~mozEnglishWordUtils()
|
||||
{
|
||||
/* destructor code */
|
||||
}
|
||||
|
||||
/* attribute wstring language; */
|
||||
NS_IMETHODIMP mozEnglishWordUtils::GetLanguage(PRUnichar * *aLanguage)
|
||||
{
|
||||
nsresult res=NS_OK;
|
||||
NS_PRECONDITION(aLanguage != nsnull, "null ptr");
|
||||
if(!aLanguage){
|
||||
res = NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
else{
|
||||
*aLanguage = ToNewUnicode(mLanguage);
|
||||
if(!aLanguage) res = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
nsresult rv = NS_OK;
|
||||
NS_ENSURE_ARG_POINTER(aLanguage);
|
||||
|
||||
/* attribute wstring charset; */
|
||||
NS_IMETHODIMP mozEnglishWordUtils::GetCharset(PRUnichar * *aCharset)
|
||||
{
|
||||
nsresult res=NS_OK;
|
||||
NS_PRECONDITION(aCharset != nsnull, "null ptr");
|
||||
if(!aCharset){
|
||||
res = NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
else{
|
||||
*aCharset = ToNewUnicode(mCharset);
|
||||
if(!aCharset) res = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
NS_IMETHODIMP mozEnglishWordUtils::SetCharset(const PRUnichar * aCharset)
|
||||
{
|
||||
nsresult res;
|
||||
*aLanguage = ToNewUnicode(mLanguage);
|
||||
if(!aLanguage) rv = NS_ERROR_OUT_OF_MEMORY;
|
||||
return rv;
|
||||
}
|
||||
|
||||
mCharset = aCharset;
|
||||
|
||||
nsCAutoString convCharset;
|
||||
convCharset.AssignWithConversion(mCharset);
|
||||
|
||||
nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(kCharsetConverterManagerCID, &res);
|
||||
if (NS_FAILED(res)) return res;
|
||||
if(!ccm) return NS_ERROR_FAILURE;
|
||||
res=ccm->GetUnicodeEncoder(convCharset.get(),getter_AddRefs(mEncoder));
|
||||
if(mEncoder && NS_SUCCEEDED(res)){
|
||||
res=mEncoder->SetOutputErrorBehavior(mEncoder->kOnError_Signal,nsnull,'?');
|
||||
}
|
||||
if (NS_FAILED(res)) return res;
|
||||
res=ccm->GetUnicodeDecoder(convCharset.get(),getter_AddRefs(mDecoder));
|
||||
if (NS_FAILED(res)) return res;
|
||||
res = nsServiceManager::GetService(kUnicharUtilCID,NS_GET_IID(nsICaseConversion), getter_AddRefs(mCaseConv));
|
||||
return res;
|
||||
}
|
||||
|
||||
/* void GetRootForm (in wstring aWord, in PRUint32 type, [array, size_is (count)] out string words, out PRUint32 count); */
|
||||
// convert aWord to the spellcheck charset and return the possible root forms.
|
||||
// If convertion errors occur, return an empty list.
|
||||
NS_IMETHODIMP mozEnglishWordUtils::GetRootForm(const PRUnichar *aWord, PRUint32 type, char ***words, PRUint32 *count)
|
||||
/* void GetRootForm (in wstring aWord, in PRUint32 type, [array, size_is (count)] out wstring words, out PRUint32 count); */
|
||||
// return the possible root forms of aWord.
|
||||
NS_IMETHODIMP mozEnglishWordUtils::GetRootForm(const PRUnichar *aWord, PRUint32 type, PRUnichar ***words, PRUint32 *count)
|
||||
{
|
||||
nsAutoString word(aWord);
|
||||
nsresult res;
|
||||
char **tmpPtr;
|
||||
PRUnichar *tWord;
|
||||
PRInt32 inLength,outLength;
|
||||
PRUnichar **tmpPtr;
|
||||
PRInt32 length = word.Length();
|
||||
|
||||
*count = 0;
|
||||
|
||||
mozEnglishWordUtils::myspCapitalization ct = captype(word);
|
||||
@ -131,22 +79,13 @@ NS_IMETHODIMP mozEnglishWordUtils::GetRootForm(const PRUnichar *aWord, PRUint32
|
||||
{
|
||||
case HuhCap:
|
||||
case NoCap:
|
||||
tmpPtr = (char **)nsMemory::Alloc(sizeof(char *));
|
||||
inLength = word.Length();
|
||||
res = mEncoder->GetMaxLength(word.get(),inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[0] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(aWord,&inLength,tmpPtr[0],&outLength);
|
||||
tmpPtr[0][outLength]='\0';
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
tmpPtr = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *));
|
||||
if (!tmpPtr)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
tmpPtr[0] = ToNewUnicode(word);
|
||||
if (!tmpPtr[0]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(0, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
*words = tmpPtr;
|
||||
*count = 1;
|
||||
@ -154,130 +93,66 @@ NS_IMETHODIMP mozEnglishWordUtils::GetRootForm(const PRUnichar *aWord, PRUint32
|
||||
|
||||
|
||||
case AllCap:
|
||||
tmpPtr = (char **)nsMemory::Alloc(sizeof(char *)*3);
|
||||
inLength = word.Length();
|
||||
res = mEncoder->GetMaxLength(word.get(),inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
tmpPtr = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *) * 3);
|
||||
if (!tmpPtr)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
tmpPtr[0] = ToNewUnicode(word);
|
||||
if (!tmpPtr[0]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(0, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
tmpPtr[0] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(aWord,&inLength,tmpPtr[0],&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[0][outLength]='\0';
|
||||
mCaseConv->ToLower(tmpPtr[0], tmpPtr[0], length);
|
||||
|
||||
inLength = word.Length();
|
||||
tWord=ToNewUnicode(word);
|
||||
mCaseConv->ToLower(tWord,tWord,inLength);
|
||||
res = mEncoder->GetMaxLength(tWord,inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
tmpPtr[1] = ToNewUnicode(word);
|
||||
if (!tmpPtr[1]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(1, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
tmpPtr[1] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(tWord,&inLength,tmpPtr[1],&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr[1]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[1][outLength]='\0';
|
||||
nsMemory::Free(tWord);
|
||||
mCaseConv->ToLower(tmpPtr[1], tmpPtr[1], length);
|
||||
mCaseConv->ToUpper(tmpPtr[1], tmpPtr[1], 1);
|
||||
|
||||
tWord=ToNewUnicode(word);
|
||||
mCaseConv->ToLower(tWord,tWord,inLength);
|
||||
mCaseConv->ToUpper(tWord,tWord,1);
|
||||
res = mEncoder->GetMaxLength(tWord,inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr[1]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
tmpPtr[2] = ToNewUnicode(word);
|
||||
if (!tmpPtr[2]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(2, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
tmpPtr[2] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(tWord,&inLength,tmpPtr[2],&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr[1]);
|
||||
nsMemory::Free(tmpPtr[2]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[2][outLength]='\0';
|
||||
nsMemory::Free(tWord);
|
||||
|
||||
|
||||
*words = tmpPtr;
|
||||
*count = 3;
|
||||
break;
|
||||
|
||||
|
||||
case InitCap:
|
||||
tmpPtr = (char **)nsMemory::Alloc(sizeof(char *)*2);
|
||||
inLength = word.Length();
|
||||
res = mEncoder->GetMaxLength(word.get(),inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[0] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(aWord,&inLength,tmpPtr[0],&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
}
|
||||
tmpPtr[0][outLength]='\0';
|
||||
tmpPtr = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *) * 2);
|
||||
if (!tmpPtr)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
tWord=ToNewUnicode(word);
|
||||
inLength = word.Length();
|
||||
mCaseConv->ToLower(tWord,tWord,inLength);
|
||||
res = mEncoder->GetMaxLength(tWord,inLength,&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
tmpPtr[0] = ToNewUnicode(word);
|
||||
if (!tmpPtr[0]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(0, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
tmpPtr[1] = (char *) nsMemory::Alloc(sizeof(char) * (outLength+1));
|
||||
res = mEncoder->Convert(tWord,&inLength,tmpPtr[1],&outLength);
|
||||
if(NS_FAILED(res)|| res == NS_ERROR_UENC_NOMAPPING){
|
||||
nsMemory::Free(tmpPtr[1]);
|
||||
nsMemory::Free(tmpPtr[0]);
|
||||
nsMemory::Free(tmpPtr);
|
||||
*words=nsnull;
|
||||
break;
|
||||
mCaseConv->ToLower(tmpPtr[0], tmpPtr[0], length);
|
||||
|
||||
tmpPtr[1] = ToNewUnicode(word);
|
||||
if (!tmpPtr[1]) {
|
||||
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(1, tmpPtr);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsMemory::Free(tWord);
|
||||
tmpPtr[1][outLength]='\0';
|
||||
mCaseConv->ToLower(tmpPtr[1], tmpPtr[1], length);
|
||||
|
||||
*words = tmpPtr;
|
||||
*count = 2;
|
||||
break;
|
||||
default:
|
||||
res=NS_ERROR_FAILURE; // should never get here;
|
||||
break;
|
||||
return NS_ERROR_FAILURE; // should never get here;
|
||||
}
|
||||
return res;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// This needs vast improvement -- take the charset from he ISpell dictionary
|
||||
// This needs vast improvement
|
||||
static PRBool ucIsAlpha(PRUnichar c)
|
||||
{
|
||||
if(5==GetCat(c)) return PR_TRUE;
|
||||
return PR_FALSE;
|
||||
return (5 == GetCat(c));
|
||||
}
|
||||
|
||||
/* void FindNextWord (in wstring word, in PRUint32 length, in PRUint32 offset, out PRUint32 begin, out PRUint32 end); */
|
||||
@ -338,27 +213,25 @@ mozEnglishWordUtils::captype(const nsString &word)
|
||||
nsMemory::Free(lword);
|
||||
return HuhCap;
|
||||
}
|
||||
|
||||
// Convert the list of words in iwords to the same capitalization aWord and
|
||||
// convert them to unicode then return them in owords.
|
||||
NS_IMETHODIMP mozEnglishWordUtils::FromRootForm(const PRUnichar *aWord, const char **iwords, PRUint32 icount, PRUnichar ***owords, PRUint32 *ocount)
|
||||
// return them in owords.
|
||||
NS_IMETHODIMP mozEnglishWordUtils::FromRootForm(const PRUnichar *aWord, const PRUnichar **iwords, PRUint32 icount, PRUnichar ***owords, PRUint32 *ocount)
|
||||
{
|
||||
nsAutoString word(aWord);
|
||||
nsresult res = NS_OK;
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
PRInt32 inLength,outLength;
|
||||
PRInt32 length;
|
||||
PRUnichar **tmpPtr = (PRUnichar **)nsMemory::Alloc(sizeof(PRUnichar *)*icount);
|
||||
if (!tmpPtr)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
mozEnglishWordUtils::myspCapitalization ct = captype(word);
|
||||
for(PRUint32 i=0;i<icount;i++){
|
||||
inLength = nsCRT::strlen(iwords[i]);
|
||||
res = mDecoder->GetMaxLength(iwords[i],inLength,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
break;
|
||||
tmpPtr[i] = (PRUnichar *) nsMemory::Alloc(sizeof(PRUnichar *) * (outLength+1));
|
||||
res = mDecoder->Convert(iwords[i],&inLength,tmpPtr[i],&outLength);
|
||||
tmpPtr[i][outLength]=0;
|
||||
for(PRUint32 i = 0; i < icount; ++i) {
|
||||
length = nsCRT::strlen(iwords[i]);
|
||||
tmpPtr[i] = (PRUnichar *) nsMemory::Alloc(sizeof(PRUnichar) * (length + 1));
|
||||
memcpy(tmpPtr[i], iwords[i], (length + 1) * sizeof(PRUnichar));
|
||||
|
||||
nsAutoString capTest(tmpPtr[i]);
|
||||
mozEnglishWordUtils::myspCapitalization newCt=captype(capTest);
|
||||
if(newCt == NoCap){
|
||||
@ -368,22 +241,22 @@ NS_IMETHODIMP mozEnglishWordUtils::FromRootForm(const PRUnichar *aWord, const ch
|
||||
case NoCap:
|
||||
break;
|
||||
case AllCap:
|
||||
res=mCaseConv->ToUpper(tmpPtr[i],tmpPtr[i],outLength);
|
||||
rv = mCaseConv->ToUpper(tmpPtr[i],tmpPtr[i],length);
|
||||
break;
|
||||
case InitCap:
|
||||
res=mCaseConv->ToUpper(tmpPtr[i],tmpPtr[i],1);
|
||||
rv = mCaseConv->ToUpper(tmpPtr[i],tmpPtr[i],1);
|
||||
break;
|
||||
default:
|
||||
res=NS_ERROR_FAILURE; // should never get here;
|
||||
rv = NS_ERROR_FAILURE; // should never get here;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(NS_SUCCEEDED(res)){
|
||||
if (NS_SUCCEEDED(rv)){
|
||||
*owords = tmpPtr;
|
||||
*ocount = icount;
|
||||
}
|
||||
return res;
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
@ -63,8 +63,6 @@ protected:
|
||||
|
||||
nsString mLanguage;
|
||||
nsString mCharset;
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
||||
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
||||
nsCOMPtr<nsICaseConversion> mCaseConv;
|
||||
};
|
||||
|
||||
|
||||
@ -57,24 +57,14 @@ NS_IMETHODIMP mozGenericWordUtils::GetLanguage(PRUnichar * *aLanguage)
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute wstring charset; */
|
||||
NS_IMETHODIMP mozGenericWordUtils::GetCharset(PRUnichar * *aCharset)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
NS_IMETHODIMP mozGenericWordUtils::SetCharset(const PRUnichar * aCharset)
|
||||
/* void GetRootForm (in wstring word, in PRUint32 type, [array, size_is (count)] out wstring words, out PRUint32 count); */
|
||||
NS_IMETHODIMP mozGenericWordUtils::GetRootForm(const PRUnichar *word, PRUint32 type, PRUnichar ***words, PRUint32 *count)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* void GetRootForm (in wstring word, in PRUint32 type, [array, size_is (count)] out string words, out PRUint32 count); */
|
||||
NS_IMETHODIMP mozGenericWordUtils::GetRootForm(const PRUnichar *word, PRUint32 type, char ***words, PRUint32 *count)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* void FromRootForm (in wstring word, [array, size_is (icount)] in string iwords, in PRUint32 icount, [array, size_is (ocount)] out wstring owords, out PRUint32 ocount); */
|
||||
NS_IMETHODIMP mozGenericWordUtils::FromRootForm(const PRUnichar *word, const char **iwords, PRUint32 icount, PRUnichar ***owords, PRUint32 *ocount)
|
||||
/* void FromRootForm (in wstring word, [array, size_is (icount)] in wstring iwords, in PRUint32 icount, [array, size_is (ocount)] out wstring owords, out PRUint32 ocount); */
|
||||
NS_IMETHODIMP mozGenericWordUtils::FromRootForm(const PRUnichar *word, const PRUnichar **iwords, PRUint32 icount, PRUnichar ***owords, PRUint32 *ocount)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
@ -83,17 +83,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* String comparitor for charset tree
|
||||
**/
|
||||
class CStringNodeComparitor: public nsAVLNodeComparitor {
|
||||
public:
|
||||
CStringNodeComparitor(){}
|
||||
virtual ~CStringNodeComparitor(){}
|
||||
virtual PRInt32 operator() (void *item1,void *item2){
|
||||
return nsCRT::strcmp((char *)item1,(char *)item2);
|
||||
}
|
||||
};
|
||||
/*
|
||||
* the generic deallocator
|
||||
**/
|
||||
@ -108,48 +97,10 @@ public:
|
||||
};
|
||||
|
||||
static StringNodeComparitor *gStringNodeComparitor;
|
||||
static CStringNodeComparitor *gCStringNodeComparitor;
|
||||
static DeallocatorFunctor *gDeallocatorFunctor;
|
||||
|
||||
/*
|
||||
* Functor for copying the unicode tree to the charset tree with conversion
|
||||
**/
|
||||
class ConvertedCopyFunctor: public nsAVLNodeFunctor {
|
||||
public:
|
||||
ConvertedCopyFunctor(nsIUnicodeEncoder* anEncoder, nsAVLTree *aNewTree):newTree(aNewTree),encoder(anEncoder){
|
||||
res = NS_OK;
|
||||
}
|
||||
virtual ~ConvertedCopyFunctor(){}
|
||||
nsresult GetResult(){return res;}
|
||||
virtual void* operator() (void * anItem){
|
||||
if(NS_SUCCEEDED(res)){
|
||||
PRUnichar * word=(PRUnichar *) anItem;
|
||||
PRInt32 inLength,outLength;
|
||||
inLength = nsCRT::strlen(word);
|
||||
res = encoder->GetMaxLength(word,inLength,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return nsnull;
|
||||
char * tmp = (char *) nsMemory::Alloc(sizeof(PRUnichar *) * (outLength+1));
|
||||
res = encoder->Convert(word,&inLength,tmp,&outLength);
|
||||
if(res == NS_ERROR_UENC_NOMAPPING){
|
||||
res = NS_OK; // word just doesnt fit in our charset -- assume that it is the wrong language.
|
||||
nsMemory::Free(tmp);
|
||||
}
|
||||
else{
|
||||
tmp[outLength]='\0';
|
||||
newTree->AddItem(tmp);
|
||||
}
|
||||
}
|
||||
return nsnull;
|
||||
}
|
||||
protected:
|
||||
nsresult res;
|
||||
nsAVLTree *newTree;
|
||||
nsCOMPtr<nsIUnicodeEncoder> encoder;
|
||||
};
|
||||
|
||||
/*
|
||||
* Copy the tree to a waiiting aray of Unichar pointers.
|
||||
* Copy the tree to a waiting array of Unichar pointers.
|
||||
* All the strings are newly created. It is the callers responsibility to free them
|
||||
**/
|
||||
class CopyToArrayFunctor: public nsAVLNodeFunctor {
|
||||
@ -201,14 +152,11 @@ protected:
|
||||
nsIOutputStream* mStream;
|
||||
};
|
||||
|
||||
mozPersonalDictionary::mozPersonalDictionary():mUnicodeTree(nsnull),mCharsetTree(nsnull),mUnicodeIgnoreTree(nsnull),mCharsetIgnoreTree(nsnull)
|
||||
mozPersonalDictionary::mozPersonalDictionary():mUnicodeTree(nsnull),mUnicodeIgnoreTree(nsnull)
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
|
||||
NS_ASSERTION(!gStringNodeComparitor,"Someone's been writing in my statics! I'm a Singleton Bear!");
|
||||
if(!gStringNodeComparitor){
|
||||
gStringNodeComparitor = new StringNodeComparitor;
|
||||
gCStringNodeComparitor = new CStringNodeComparitor;
|
||||
gDeallocatorFunctor = new DeallocatorFunctor;
|
||||
}
|
||||
}
|
||||
@ -216,9 +164,7 @@ mozPersonalDictionary::mozPersonalDictionary():mUnicodeTree(nsnull),mCharsetTree
|
||||
mozPersonalDictionary::~mozPersonalDictionary()
|
||||
{
|
||||
delete mUnicodeTree;
|
||||
delete mCharsetTree;
|
||||
delete mUnicodeIgnoreTree;
|
||||
delete mCharsetIgnoreTree;
|
||||
}
|
||||
|
||||
int PR_CALLBACK
|
||||
@ -270,78 +216,6 @@ NS_IMETHODIMP mozPersonalDictionary::Init()
|
||||
return Load();
|
||||
}
|
||||
|
||||
/* attribute wstring language; */
|
||||
NS_IMETHODIMP mozPersonalDictionary::GetLanguage(PRUnichar * *aLanguage)
|
||||
{
|
||||
nsresult res=NS_OK;
|
||||
NS_PRECONDITION(aLanguage != nsnull, "null ptr");
|
||||
if(!aLanguage){
|
||||
res = NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
else{
|
||||
*aLanguage = ToNewUnicode(mLanguage);
|
||||
if(!aLanguage) res = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP mozPersonalDictionary::SetLanguage(const PRUnichar * aLanguage)
|
||||
{
|
||||
mLanguage = aLanguage;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* attribute wstring charset; */
|
||||
NS_IMETHODIMP mozPersonalDictionary::GetCharset(PRUnichar * *aCharset)
|
||||
{
|
||||
nsresult res=NS_OK;
|
||||
NS_PRECONDITION(aCharset != nsnull, "null ptr");
|
||||
if(!aCharset){
|
||||
res = NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
else{
|
||||
*aCharset = ToNewUnicode(mLanguage);
|
||||
if(!aCharset) res = NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP mozPersonalDictionary::SetCharset(const PRUnichar * aCharset)
|
||||
{
|
||||
nsresult res;
|
||||
mCharset = aCharset;
|
||||
|
||||
nsCAutoString convCharset;
|
||||
convCharset.AssignWithConversion(mCharset);
|
||||
|
||||
nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(kCharsetConverterManagerCID, &res);
|
||||
if (NS_FAILED(res)) return res;
|
||||
if(!ccm) return NS_ERROR_FAILURE;
|
||||
|
||||
res=ccm->GetUnicodeEncoder(convCharset.get(),getter_AddRefs(mEncoder));
|
||||
|
||||
if (NS_FAILED(res)) return res;
|
||||
if(!mEncoder) return NS_ERROR_FAILURE;
|
||||
|
||||
if(mEncoder && NS_SUCCEEDED(res)){
|
||||
res=mEncoder->SetOutputErrorBehavior(mEncoder->kOnError_Signal,nsnull,'?');
|
||||
}
|
||||
|
||||
if(mEncoder && mUnicodeTree){
|
||||
delete mCharsetTree;
|
||||
mCharsetTree = new nsAVLTree(*gCStringNodeComparitor,gDeallocatorFunctor);
|
||||
ConvertedCopyFunctor converter(mEncoder,mCharsetTree);
|
||||
mUnicodeTree->ForEachDepthFirst(converter);
|
||||
}
|
||||
if(mEncoder && mUnicodeIgnoreTree){
|
||||
delete mCharsetIgnoreTree;
|
||||
mCharsetIgnoreTree = new nsAVLTree(*gCStringNodeComparitor,gDeallocatorFunctor);
|
||||
ConvertedCopyFunctor converter(mEncoder,mCharsetIgnoreTree);
|
||||
mUnicodeIgnoreTree->ForEachDepthFirst(converter);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* void Load (); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::Load()
|
||||
{
|
||||
@ -451,8 +325,8 @@ NS_IMETHODIMP mozPersonalDictionary::GetWordList(PRUnichar ***words, PRUint32 *c
|
||||
return res;
|
||||
}
|
||||
|
||||
/* boolean CheckUnicode (in wstring word); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::CheckUnicode(const PRUnichar *word, PRBool *_retval)
|
||||
/* boolean Check (in wstring word, in wstring language); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::Check(const PRUnichar *word, const PRUnichar *aLanguage, PRBool *_retval)
|
||||
{
|
||||
if(!word || !_retval || !mUnicodeTree)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
@ -470,25 +344,6 @@ NS_IMETHODIMP mozPersonalDictionary::CheckUnicode(const PRUnichar *word, PRBool
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* boolean Check (in string word); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::Check(const char *word, PRBool *_retval)
|
||||
{
|
||||
if(!word || !_retval || !mCharsetTree)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(mCharsetTree->FindItem((void *)word)){
|
||||
*_retval = PR_TRUE;
|
||||
}
|
||||
else{
|
||||
if(mCharsetIgnoreTree&&mCharsetIgnoreTree->FindItem((void *)word)){
|
||||
*_retval = PR_TRUE;
|
||||
}
|
||||
else{
|
||||
*_retval = PR_FALSE;
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void AddWord (in wstring word); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::AddWord(const PRUnichar *word, const PRUnichar *lang)
|
||||
{
|
||||
@ -496,22 +351,7 @@ NS_IMETHODIMP mozPersonalDictionary::AddWord(const PRUnichar *word, const PRUnic
|
||||
if(mUnicodeTree) mUnicodeTree->AddItem(ToNewUnicode(nsDependentString(word)));
|
||||
mDirty=PR_TRUE;
|
||||
|
||||
nsresult res=NS_OK;
|
||||
if(mCharsetTree&&mEncoder){
|
||||
PRInt32 inLength,outLength;
|
||||
inLength = nsCRT::strlen(word);
|
||||
res = mEncoder->GetMaxLength(word,inLength,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
char * tmp = (char *) nsMemory::Alloc(sizeof(PRUnichar *) * (outLength+1));
|
||||
res = mEncoder->Convert(word,&inLength,tmp,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
tmp[outLength]='\0';
|
||||
mCharsetTree->AddItem(tmp);
|
||||
}
|
||||
|
||||
return res;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void RemoveWord (in wstring word); */
|
||||
@ -521,22 +361,7 @@ NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const PRUnichar *word, const PRU
|
||||
if(mUnicodeTree) mUnicodeTree->RemoveItem((void *)word);
|
||||
mDirty=PR_TRUE;
|
||||
|
||||
nsresult res=NS_OK;
|
||||
if(mCharsetTree&&mEncoder){
|
||||
PRInt32 inLength,outLength;
|
||||
inLength = nsCRT::strlen(word);
|
||||
res = mEncoder->GetMaxLength(word,inLength,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
char * tmp = (char *) nsMemory::Alloc(sizeof(PRUnichar *) * (outLength+1));
|
||||
res = mEncoder->Convert(word,&inLength,tmp,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
tmp[outLength]='\0';
|
||||
mCharsetTree->AddItem(tmp);
|
||||
}
|
||||
|
||||
return res;
|
||||
return NS_OK;
|
||||
}
|
||||
/* void IgnoreWord (in wstring word); */
|
||||
NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const PRUnichar *word)
|
||||
@ -548,27 +373,6 @@ NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const PRUnichar *word)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mUnicodeIgnoreTree->AddItem(ToNewUnicode(nsDependentString(word)));
|
||||
if(!mCharsetIgnoreTree){
|
||||
mCharsetIgnoreTree=new nsAVLTree(*gCStringNodeComparitor,gDeallocatorFunctor);
|
||||
}
|
||||
if(!mCharsetIgnoreTree){
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
if(mCharsetIgnoreTree&&mEncoder){
|
||||
PRInt32 inLength,outLength;
|
||||
nsresult res;
|
||||
inLength = nsCRT::strlen(word);
|
||||
res = mEncoder->GetMaxLength(word,inLength,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
char * tmp = (char *) nsMemory::Alloc(sizeof(PRUnichar *) * (outLength+1));
|
||||
res = mEncoder->Convert(word,&inLength,tmp,&outLength);
|
||||
if(NS_FAILED(res))
|
||||
return res;
|
||||
tmp[outLength]='\0';
|
||||
mCharsetIgnoreTree->AddItem(tmp);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
@ -578,10 +382,8 @@ NS_IMETHODIMP mozPersonalDictionary::EndSession()
|
||||
{
|
||||
if(SessionSave)Save();
|
||||
delete mUnicodeIgnoreTree;
|
||||
delete mCharsetIgnoreTree;
|
||||
mUnicodeIgnoreTree=nsnull;
|
||||
mCharsetIgnoreTree=nsnull;
|
||||
return NS_OK;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void AddCorrection (in wstring word, in wstring correction); */
|
||||
@ -608,24 +410,16 @@ NS_IMETHODIMP mozPersonalDictionary::Observe(nsISupports *aSubject, const char *
|
||||
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
||||
Save();
|
||||
delete mUnicodeTree;
|
||||
delete mCharsetTree;
|
||||
delete mUnicodeIgnoreTree;
|
||||
delete mCharsetIgnoreTree;
|
||||
mUnicodeTree=nsnull;
|
||||
mCharsetTree=nsnull;
|
||||
mUnicodeIgnoreTree=nsnull;
|
||||
mCharsetIgnoreTree=nsnull;
|
||||
}
|
||||
else if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
||||
Save();
|
||||
delete mUnicodeTree;
|
||||
delete mCharsetTree;
|
||||
delete mUnicodeIgnoreTree;
|
||||
delete mCharsetIgnoreTree;
|
||||
mUnicodeTree=nsnull;
|
||||
mCharsetTree=nsnull;
|
||||
mUnicodeIgnoreTree=nsnull;
|
||||
mCharsetIgnoreTree=nsnull;
|
||||
}
|
||||
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
||||
Load();
|
||||
|
||||
@ -69,12 +69,8 @@ public:
|
||||
protected:
|
||||
nsStringArray mDictionary; /* use something a little smarter eventually*/
|
||||
PRBool mDirty; /* has the dictionary been modified */
|
||||
nsString mCharset; /* charset that the spell checker is using */
|
||||
nsString mLanguage; /* the name of the language currently in use */
|
||||
nsAVLTree *mUnicodeTree; /* the dictionary entries */
|
||||
nsAVLTree *mCharsetTree; /* the dictionary entries in the current charset */
|
||||
nsAVLTree *mUnicodeIgnoreTree; /* the ignore all entries */
|
||||
nsAVLTree *mCharsetIgnoreTree; /* the ignore all entries in the current charset */
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder; /*Encoder to use to compare with spellchecker word */
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user