src/editor/spell.c: refactoring: use GPtrArray instead of GArray for string arrays.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2023-05-21 14:11:50 +03:00
parent 583adea34e
commit 2e78166c23
2 changed files with 24 additions and 47 deletions

View File

@ -360,7 +360,7 @@ aspell_clean (void)
*/ */
unsigned int unsigned int
aspell_get_lang_list (GArray * lang_list) aspell_get_lang_list (GPtrArray * lang_list)
{ {
AspellDictInfoList *dlist; AspellDictInfoList *dlist;
AspellDictInfoEnumeration *elem; AspellDictInfoEnumeration *elem;
@ -375,16 +375,11 @@ aspell_get_lang_list (GArray * lang_list)
elem = mc_aspell_dict_info_list_elements (dlist); elem = mc_aspell_dict_info_list_elements (dlist);
while ((entry = mc_aspell_dict_info_enumeration_next (elem)) != NULL) while ((entry = mc_aspell_dict_info_enumeration_next (elem)) != NULL)
{
if (entry->name != NULL) if (entry->name != NULL)
{ {
char *tmp; g_ptr_array_add (lang_list, g_strdup (entry->name));
tmp = g_strdup (entry->name);
g_array_append_val (lang_list, tmp);
i++; i++;
} }
}
mc_delete_aspell_dict_info_enumeration (elem); mc_delete_aspell_dict_info_enumeration (elem);
@ -399,21 +394,10 @@ aspell_get_lang_list (GArray * lang_list)
*/ */
void void
aspell_array_clean (GArray * array) aspell_array_clean (GPtrArray * array)
{ {
if (array != NULL) if (array != NULL)
{ g_ptr_array_free (array, TRUE);
guint i = 0;
for (i = 0; i < array->len; ++i)
{
char *tmp;
tmp = g_array_index (array, char *, i);
g_free (tmp);
}
g_array_free (array, TRUE);
}
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -510,7 +494,7 @@ aspell_check (const char *word, const int word_size)
*/ */
unsigned int unsigned int
aspell_suggest (GArray * suggest, const char *word, const int word_size) aspell_suggest (GPtrArray * suggest, const char *word, const int word_size)
{ {
unsigned int size = 0; unsigned int size = 0;
@ -531,9 +515,9 @@ aspell_suggest (GArray * suggest, const char *word, const int word_size)
{ {
const char *cur_sugg_word; const char *cur_sugg_word;
cur_sugg_word = g_strdup (mc_aspell_string_enumeration_next (elements)); cur_sugg_word = mc_aspell_string_enumeration_next (elements);
if (cur_sugg_word != NULL) if (cur_sugg_word != NULL)
g_array_append_val (suggest, cur_sugg_word); g_ptr_array_add (suggest, g_strdup (cur_sugg_word));
} }
mc_delete_aspell_string_enumeration (elements); mc_delete_aspell_string_enumeration (elements);
@ -603,11 +587,11 @@ edit_suggest_current_word (WEdit * edit)
{ {
if (!aspell_check (match_word->str, (int) word_len)) if (!aspell_check (match_word->str, (int) word_len))
{ {
GArray *suggest; GPtrArray *suggest;
unsigned int res; unsigned int res;
guint i; guint i;
suggest = g_array_new (TRUE, FALSE, sizeof (char *)); suggest = g_ptr_array_new_with_free_func (g_free);
res = aspell_suggest (suggest, match_word->str, (int) word_len); res = aspell_suggest (suggest, match_word->str, (int) word_len);
if (res != 0) if (res != 0)
@ -651,14 +635,7 @@ edit_suggest_current_word (WEdit * edit)
aspell_add_to_dict (match_word->str, (int) word_len); aspell_add_to_dict (match_word->str, (int) word_len);
} }
for (i = 0; i < suggest->len; i++) g_ptr_array_free (suggest, TRUE);
{
char *cur_sugg_word;
cur_sugg_word = g_array_index (suggest, char *, i);
g_free (cur_sugg_word);
}
g_array_free (suggest, TRUE);
edit->found_start = 0; edit->found_start = 0;
edit->found_len = 0; edit->found_len = 0;
} }
@ -706,9 +683,9 @@ edit_spellcheck_file (WEdit * edit)
void void
edit_set_spell_lang (void) edit_set_spell_lang (void)
{ {
GArray *lang_list; GPtrArray *lang_list;
lang_list = g_array_new (TRUE, FALSE, sizeof (char *)); lang_list = g_ptr_array_new_with_free_func (g_free);
if (aspell_get_lang_list (lang_list) != 0) if (aspell_get_lang_list (lang_list) != 0)
{ {
char *lang; char *lang;
@ -735,7 +712,8 @@ edit_set_spell_lang (void)
*/ */
int int
spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word, GArray * suggest) spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word,
const GPtrArray * suggest)
{ {
int sug_dlg_h = 14; /* dialog height */ int sug_dlg_h = 14; /* dialog height */
@ -796,8 +774,8 @@ spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word
sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL); sug_list = listbox_new (5, 2, sug_dlg_h - 7, 24, FALSE, NULL);
for (i = 0; i < suggest->len; i++) for (i = 0; i < suggest->len; i++)
listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_array_index (suggest, char *, i), listbox_add_item (sug_list, LISTBOX_APPEND_AT_END, 0, g_ptr_array_index (suggest, i), NULL,
NULL, FALSE); FALSE);
group_add_widget (g, sug_list); group_add_widget (g, sug_list);
group_add_widget (g, add_btn); group_add_widget (g, add_btn);
@ -832,7 +810,7 @@ spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word
*/ */
char * char *
spell_dialog_lang_list_show (GArray * languages) spell_dialog_lang_list_show (const GPtrArray * languages)
{ {
int lang_dlg_h = 12; /* dialog height */ int lang_dlg_h = 12; /* dialog height */
@ -847,14 +825,13 @@ spell_dialog_lang_list_show (GArray * languages)
_("Select language"), "[ASpell]"); _("Select language"), "[ASpell]");
for (i = 0; i < languages->len; i++) for (i = 0; i < languages->len; i++)
LISTBOX_APPEND_TEXT (lang_list, 0, g_array_index (languages, char *, i), NULL, FALSE); LISTBOX_APPEND_TEXT (lang_list, 0, g_ptr_array_index (languages, i), NULL, FALSE);
res = listbox_run (lang_list); res = listbox_run (lang_list);
if (res >= 0) if (res >= 0)
selected_lang = g_strdup (g_array_index (languages, char *, (unsigned int) res)); selected_lang = g_strdup (g_ptr_array_index (languages, (unsigned int) res));
return selected_lang; return selected_lang;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -16,9 +16,9 @@
void aspell_init (void); void aspell_init (void);
void aspell_clean (void); void aspell_clean (void);
gboolean aspell_check (const char *word, const int word_size); gboolean aspell_check (const char *word, const int word_size);
unsigned int aspell_suggest (GArray * suggest, const char *word, const int word_size); unsigned int aspell_suggest (GPtrArray * suggest, const char *word, const int word_size);
void aspell_array_clean (GArray * array); void aspell_array_clean (GPtrArray * array);
unsigned int aspell_get_lang_list (GArray * lang_list); unsigned int aspell_get_lang_list (GPtrArray * lang_list);
const char *aspell_get_lang (void); const char *aspell_get_lang (void);
gboolean aspell_set_lang (const char *lang); gboolean aspell_set_lang (const char *lang);
gboolean aspell_add_to_dict (const char *word, const int word_size); gboolean aspell_add_to_dict (const char *word, const int word_size);
@ -28,8 +28,8 @@ void edit_spellcheck_file (WEdit * edit);
void edit_set_spell_lang (void); void edit_set_spell_lang (void);
int spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word, int spell_dialog_spell_suggest_show (WEdit * edit, const char *word, char **new_word,
GArray * suggest); const GPtrArray * suggest);
char *spell_dialog_lang_list_show (GArray * languages); char *spell_dialog_lang_list_show (const GPtrArray * languages);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/