FREObject getSuggestions()

in Squiggly/ane/WinDLLHunspellANE/HunspellNativeExtension/HunspellNativeExtension.cpp [193:275]


FREObject getSuggestions(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
	const uint8_t *locale = 0;
	const uint8_t *inpWord = 0;
	uint32_t len = -1;
	FREObject retObj=0;
	FREObject oneSuggestion=0;

	U16Char_t* inpWordU16 = NULL;
	std::string  inpWordDictEncoded;
	bool isConversionUnSuccesful=false;
	char * oneSuggestionEncoded=NULL;
	U16Char_t * oneSuggestionU16=NULL;
	std::string oneSuggestionU8;

	//assign memory to retobj
	FRENewObject((const uint8_t*)"Array", 0, NULL, &retObj, 0);

	//get first argument
	if(FREGetObjectAsUTF8(argv[1], &len, &locale) != FRE_OK)
	return retObj;
	len = 0;
	
	//get second argument
	if(FREGetObjectAsUTF8(argv[0], &len, &inpWord) != FRE_OK)
	return retObj;
	
	//get the hunspell object from cache
	Hunspell * hunspellObject=getHunspellObject(std::string((char *)locale));
		
	if(!hunspellObject) return retObj;
	
	//convert input utf8 to u16 string
	inpWordU16 = EncConv::convCharStrToU16Str((char *)inpWord, "UTF-8");

	//get dictionary encoding
	HUNSPELL_GET_ENCODING getEncAdd=(HUNSPELL_GET_ENCODING) GetProcAddress(hinstLib, "hunspell_get_dic_encoding");
	char * m_MainEncoding= (getEncAdd)(hunspellObject);

	//convert u16 to dictionary encoding
	inpWordDictEncoded=EncConv::convU16StrToCharStr(inpWordU16,m_MainEncoding);

	if(inpWordDictEncoded.length()==0)
	{
		isConversionUnSuccesful = true;
	}

	//get suggestAddress &freelistAddress
	HUNSPELL_SUGGEST suggestAdd= (HUNSPELL_SUGGEST) GetProcAddress(hinstLib, "hunspell_suggest");
	HUNSPELL_FREE_LIST freeListAdd= (HUNSPELL_FREE_LIST) GetProcAddress(hinstLib, "hunspell_free_list");
	
	char** suggList = NULL;
	int numSugg = 0;

	//Try getting suggestions with encoded word else try with UTF8
	if(suggestAdd && !isConversionUnSuccesful)
	{
		numSugg = (suggestAdd)(hunspellObject,(char *) (inpWordDictEncoded.c_str()),&suggList);
	}
	else
	{
		numSugg = (suggestAdd)(hunspellObject,(char *) (inpWord),&suggList);
	}
	

	if(numSugg)
				{
					FRESetArrayLength( retObj, numSugg );
					
					for(int iCount=0; iCount <numSugg; iCount++)
					{
						oneSuggestionEncoded=suggList[iCount];
						oneSuggestionU16=EncConv::convCharStrToU16Str(oneSuggestionEncoded, m_MainEncoding);
						oneSuggestionU8= EncConv::convU16StrToCharStr(oneSuggestionU16, "UTF-8");
						FRENewObjectFromUTF8((uint32_t)(oneSuggestionU8.length()),(const uint8_t*)(oneSuggestionU8.c_str()), &oneSuggestion);
						FRESetArrayElementAt(retObj, iCount, oneSuggestion);
						EncConv::releaseU16Str(oneSuggestionU16);
					}
					(freeListAdd)(hunspellObject, &suggList,numSugg);
					
				}
	EncConv::releaseU16Str(inpWordU16);
	return retObj;
}