/** * Set a specific option, that is known by aspell. * @param config the config object of a specific spellchecker. * @param key the option to set (eg: lang). * @param value the value of the option to set (eg: "us_US"). * @exception Exception if key not known, or value undefined. */ static void set_option(AspellConfig *config, char *key, char *value) { //printf("set option: %s = %s\n", key, value); if (aspell_config_replace(config, key, value) == 0) { rb_raise(cAspellError, "%s", aspell_config_error_message(config)); } //check config: if (aspell_config_error(config) != 0) { rb_raise(cAspellError, "%s", aspell_config_error_message(config)); } }
/** * Retrieve the value of a specific option as list. * @param word the option as string. */ static VALUE aspell_conf_retrieve_list(VALUE self, VALUE key) { AspellSpeller *speller = get_speller(self); AspellConfig *config = aspell_speller_config(speller); AspellStringList * list = new_aspell_string_list(); AspellMutableContainer * container = aspell_string_list_to_mutable_container(list); AspellStringEnumeration * els; VALUE result = rb_ary_new(); const char *option_value; //retrieve list aspell_config_retrieve_list(config, STR2CSTR(key), container); //check for error if (aspell_config_error(config) != 0) { char *tmp = strdup(aspell_config_error_message(config)); delete_aspell_string_list(list); rb_raise( cAspellError, "%s", tmp); } //iterate over list els = aspell_string_list_elements(list); while ( (option_value = aspell_string_enumeration_next(els)) != 0) { //push the option value to result rb_ary_push(result, rb_str_new2(option_value)); } //free list delete_aspell_string_enumeration(els); delete_aspell_string_list(list); return result; }
/** * Retrieve the value of a specific option. * The options are listed inside * Aspell::[DictionaryOptions|CheckerOptions|FilterOptions|RunTogetherOptions|MiscOptions|UtilityOptions] * @param word the option as string. */ static VALUE aspell_conf_retrieve(VALUE self, VALUE key) { AspellSpeller *speller = get_speller(self); AspellConfig *config = aspell_speller_config(speller); VALUE result = rb_str_new2(aspell_config_retrieve(config, STR2CSTR(key))); if (aspell_config_error(config) != 0) { rb_raise(cAspellError, "%s", aspell_config_error_message(config)); } return result; }
//__________________________________________________________________________ void Speller::Aspell::Suggest::checkConfigError() throw( std::runtime_error ) { if( aspell_config_error_number( fconfig ) != 0 ) { std::string err_msg = std::string( "(Aspell::Speller::Suggest::checkConfig" "Error): aspell speller error " ) + aspell_config_error_message( fconfig ); throw std::runtime_error( err_msg ); } }
/* method:ConfigKeys **********************************************************/ static PyObject* m_configkeys(PyObject* self, PyObject* args) { AspellConfig* config; AspellKeyInfoEnumeration *keys_enumeration; AspellStringList* lst; AspellMutableContainer* amc; const AspellKeyInfo *key_info; PyObject *key_list, *obj; const char* string; unsigned int integer; unsigned int boolean; char *key_type = 0; config = aspell_speller_config(Speller(self)); if (config == NULL) { PyErr_SetString(_AspellModuleException, "can't create config"); return NULL; } keys_enumeration = aspell_config_possible_elements(config, 1); if (!keys_enumeration) { PyErr_SetString(_AspellConfigException, "can't get list of config keys"); return NULL; } key_list = PyList_New(0); while ((key_info = aspell_key_info_enumeration_next(keys_enumeration))) { /* key type -> string */ switch (key_info->type) { case AspellKeyInfoString: key_type = "string"; string = aspell_config_retrieve(config, key_info->name); if (aspell_config_error(config) != NULL) goto config_get_error; obj = PyString_FromString( string ); break; case AspellKeyInfoInt: key_type = "integer"; integer = aspell_config_retrieve_int(config, key_info->name); if (aspell_config_error(config) != NULL) goto config_get_error; obj = PyInt_FromLong( integer ); break; case AspellKeyInfoBool: key_type = "boolean"; boolean = aspell_config_retrieve_bool(config, key_info->name); if (aspell_config_error(config) != NULL) goto config_get_error; obj = PyBool_FromLong( boolean ); break; case AspellKeyInfoList: key_type = "list"; lst = new_aspell_string_list(); amc = aspell_string_list_to_mutable_container(lst); aspell_config_retrieve_list(config, key_info->name, amc); if (aspell_config_error(config) != NULL) goto config_get_error; obj = AspellStringList2PythonList(lst); delete_aspell_string_list(lst); break; default: obj = NULL; break; } if (obj == NULL) { continue; } if (PyList_Append(key_list, Py_BuildValue("(ssO)", key_info->name, key_type, obj)) == -1) { PyErr_SetString(PyExc_Exception, "It is almost impossible, but happend! Can't append element to the list."); delete_aspell_key_info_enumeration(keys_enumeration); Py_DECREF(key_list); return NULL; } } delete_aspell_key_info_enumeration(keys_enumeration); return key_list; config_get_error: PyErr_SetString(_AspellConfigException, aspell_config_error_message(config)); delete_aspell_key_info_enumeration(keys_enumeration); Py_DECREF(key_list); return NULL; }
/* Create a new speller *******************************************************/ static PyObject* new_speller(PyObject* self, PyObject* args) { aspell_AspellObject* newobj; AspellSpeller* speller = 0; AspellConfig* config; AspellCanHaveError* possible_error; int i; int n; /* arg count */ char *key, *value; config = new_aspell_config(); if (config == NULL) { PyErr_SetString(_AspellModuleException, "can't create config"); return NULL; } /* check constructor arguments */ n = PyTuple_Size(args); switch (n) { case 0: /* no arguments passed */ break; case 2: /* constructor is called with single pair: key & value */ if (PyArg_ParseTuple(args, "ss", &key, &value)) { if (!aspell_config_replace(config, key, value)) { PyErr_SetString(_AspellConfigException, aspell_config_error_message(config)); goto arg_error; } break; } PyErr_Clear(); default: /* list of tuples key&value */ for (i=0; i<n; i++) { if (!PyArg_ParseTuple(PyTuple_GetItem(args, i), "ss", &key, &value)) { PyErr_Format(PyExc_TypeError, "argument %d: tuple of two strings (key, value) expeced", i); goto arg_error; } if (!aspell_config_replace(config, key, value)) { PyErr_SetString(_AspellConfigException, aspell_config_error_message(config)); goto arg_error; } } Py_DECREF(args); break; } /* try to create a new speller */ possible_error = new_aspell_speller(config); delete_aspell_config(config); if (aspell_error_number(possible_error) == 0) /* save a speller */ speller = to_aspell_speller(possible_error); else { /* or raise an exception */ PyErr_SetString(_AspellSpellerException, aspell_error_message(possible_error)); delete_aspell_can_have_error(possible_error); return NULL; } /* create a new py-object */ newobj = (aspell_AspellObject*)PyObject_New(aspell_AspellObject, &aspell_AspellType); newobj->speller = speller; return (PyObject*)newobj; /* argument error: before return NULL we need to delete speller's config we've created */ arg_error: delete_aspell_config(config); return NULL; }