Пример #1
0
// Dicts returns the list of available aspell dictionaries.
func Dicts() []Dict {
	config := C.new_aspell_config()
	dlist := C.get_aspell_dict_info_list(config)
	C.delete_aspell_config(config)

	count := int(C.aspell_dict_info_list_size(dlist))
	result := make([]Dict, count)

	dels := C.aspell_dict_info_list_elements(dlist)
	for i := 0; i < count; i++ {
		entry := C.aspell_dict_info_enumeration_next(dels)
		if entry == nil {
			break
		}
		result[i] = Dict{
			name:   C.GoString(entry.name),
			code:   C.GoString(entry.code),
			jargon: C.GoString(entry.jargon),
			size:   C.GoString(entry.size_str),
			module: C.GoString(entry.module.name),
		}
	}
	C.delete_aspell_dict_info_enumeration(dels)

	return result
}
Пример #2
0
// NewSpeller creates a new speller instance with configuration options
// given as a map. At least the language option should be specified
// (see example below).
//
// The returned value is a speller struct. The second returned value
// contains error data in case of error or nil if NewSpeller succeeded.
//
// In the most common case you would like to pass the language option
// which accepts two letter ISO 639 language code and an optional
// two letter ISO 3166 country code after a dash or underscore:
//
// 		opts := map[string] string {
// 			"lang": "en_US", // American English
// 		}
// 		speller, err := aspell.NewSpeller(opts)
// 		if err != nil {
// 			panic("Aspell error: " + err.Error())
// 		}
// 		defer speller.Delete()
//
// See available options at http://aspell.net/man-html/The-Options.html
//
// Because aspell package is a binding to Aspell C library, memory
// allocated by NewSpeller() call has to be disposed explicitly.
// This is why the above example contains a deferred call to Delete().
func NewSpeller(options map[string]string) (Speller, error) {
	var s Speller

	// Pass configuration options
	s.config = C.new_aspell_config()
	if _, hasEnc := options["encoding"]; !hasEnc {
		options["encoding"] = "utf-8"
	}
	for k, v := range options {
		optName := C.CString(k)
		optValue := C.CString(v)
		res := C.aspell_config_replace(s.config, optName, optValue)
		C.free(unsafe.Pointer(optName))
		C.free(unsafe.Pointer(optValue))
		if res == 0 {
			msg := C.aspell_config_error_message(s.config)
			err := errors.New(C.GoString(msg))
			C.free(unsafe.Pointer(msg))
			return s, err
		}
	}

	// Attempt to initialize the speller
	var probErr *C.AspellCanHaveError
	probErr = C.new_aspell_speller(s.config)
	C.delete_aspell_config(s.config)
	if C.aspell_error_number(probErr) != 0 {
		msg := C.aspell_error_message(probErr)
		err := errors.New(C.GoString(msg))
		C.free(unsafe.Pointer(msg))
		C.delete_aspell_can_have_error(probErr)
		return s, err
	}

	// Successful speller initialization
	s.speller = C.to_aspell_speller(probErr)
	s.config = C.aspell_speller_config(s.speller)

	return s, nil
}