FREObject initHunspellObject()

in Squiggly/ane/MacFWHunspellANE/HunspellNativeExtension/HunspellNativeExtension.cpp [78:166]


    FREObject initHunspellObject(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){
        const uint8_t *locale = 0;
        const uint8_t *dictionaryPath = 0;
        uint32_t len = -1;
        FREObject retObj=0;
       
        Hunspell * hunspellObject;
        //get first argument
        if(FREGetObjectAsUTF8(argv[1], &len, &dictionaryPath) != FRE_OK)
            return retObj;
        len = 0;
        
        //get second argument
        if(FREGetObjectAsUTF8(argv[0], &len, &locale) != FRE_OK)
            return retObj;
        
        //check in cache and return if already present
        if(!(Hunspell_cache.find(std::string((char *)locale))==Hunspell_cache.end()))
		{
			FRENewObjectFromInt32(HUNSPELL_INIT_SUCCESS, &retObj);
			return retObj;
		}
        
       // U16Char_t * dic_path_U16= EncConv::convCharStrToU16Str((char *)dictionaryPath, "UTF-8");
       // U16Char_t *locale_U16= EncConv::convCharStrToU16Str((char *)locale, "UTF-8");
  
       // std::wstring aff_file= std::wstring((wchar_t *)dic_path_U16)+std::wstring(L"/Dictionaries/")+std::wstring((wchar_t *)locale_U16)+std::wstring(L"/")+std::wstring((wchar_t *)locale_U16)+std::wstring(L".aff");
       // std::wstring dic_file= std::wstring((wchar_t *)dic_path_U16)+std::wstring(L"/Dictionaries/")+std::wstring((wchar_t *)locale_U16)+std::wstring(L"/")+std::wstring((wchar_t *)locale_U16)+std::wstring(L".dic");
       // std::wstring lib_file= std::wstring((wchar_t *)dic_path_U16)+std::wstring(L"/libhunspell.dylib");
     
        
        
        //creating OS specific aff,dic file paths refer: DanishOS Issue of CS6 AHP 
   //     std::string aff_file_U8= makeString(aff_file.c_str());
   //     std::string dic_file_U8= makeString(dic_file.c_str());
        
        //make the file url's
        std::string lib_file_U8= std::string((char *)dictionaryPath)+std::string("/libhunspell.dylib");
        std::string aff_file_U8= std::string((char *)dictionaryPath)+std::string("/Dictionaries/")+std::string((char *)locale)+std::string("/")+std::string((char *)locale)+std::string(".aff");
        std::string dic_file_U8= std::string((char *)dictionaryPath)+std::string("/Dictionaries/")+std::string((char *)locale)+std::string("/")+std::string((char *)locale)+std::string(".dic");
        
      
    //    fprintf(stderr,"Path, %s", lib_file_U8.c_str());
    //    fprintf(stderr,"Path, %s", aff_file_U8.c_str());
    //    fprintf(stderr,"Path, %s", lib_file_U8.c_str());
        
        
        //check for file is present
        
        if( (FileExists(aff_file_U8.c_str()) && FileExists(dic_file_U8.c_str()) && FileExists(lib_file_U8.c_str())) == false )
        {
            FRENewObjectFromInt32(RESOURCE_FILES_MISSING, &retObj);
            return retObj;
        }
        
        //make a new hunspell object
    //    hinstLib = dlopen("/Users/ugoyal/Desktop/ThirdPartyResources/libhunspell.dylib", RTLD_LAZY); 
        hinstLib = dlopen(lib_file_U8.c_str(), RTLD_LAZY); 
        
        if (hinstLib != NULL) 
        { 
            HUNSPELL_INIT initAdd = (HUNSPELL_INIT) dlsym(hinstLib, "Hunspell_create"); 
            
            // If the function address is valid, call the function.
            
            if (NULL != initAdd) 
            {
                hunspellObject = (initAdd) ((char *)aff_file_U8.c_str(), (char *)dic_file_U8.c_str());
              // hunspellObject = (*initAdd)("/Users/ugoyal/Desktop/ThirdPartyResources/Dictionaries/de_DE/de_DE.aff","/Users/ugoyal/Desktop/ThirdPartyResources/Dictionaries/de_DE/de_DE.dic");
            }
            
        } 
        
        //add to hunspell cache	
        if(Hunspell_cache.find(std::string((char *)locale))==Hunspell_cache.end())	
            Hunspell_cache[std::string((char *)locale)]= hunspellObject;
        
     
        if(hunspellObject)
        {
            FRENewObjectFromInt32(HUNSPELL_INIT_SUCCESS, &retObj);
        }
        else
        {
            FRENewObjectFromInt32(HUNSPELL_INIT_FAIL, &retObj);
        }
        
        return retObj;
    }