From 4dd577fbe0ef689d05dc866413d4dfd005e89f57 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Tue, 30 Sep 2014 14:48:54 +0400 Subject: [PATCH] mceditor: reimplement syntax name list using GPtrArray. Signed-off-by: Andrew Borodin --- src/editor/choosesyntax.c | 55 ++++++++++--------- src/editor/edit-impl.h | 2 +- src/editor/syntax.c | 24 ++------ .../editor/editcmd__edit_complete_word_cmd.c | 2 +- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/editor/choosesyntax.c b/src/editor/choosesyntax.c index 6d389b176..b1d90f3f0 100644 --- a/src/editor/choosesyntax.c +++ b/src/editor/choosesyntax.c @@ -35,7 +35,7 @@ #include #include -#include +#include /* strcmp() */ #include "lib/global.h" #include "lib/widget.h" /* Listbox */ @@ -67,19 +67,23 @@ pstrcmp (const void *p1, const void *p2) /* --------------------------------------------------------------------------------------------- */ static int -exec_edit_syntax_dialog (const char **names, const char *current_syntax) +exec_edit_syntax_dialog (const GPtrArray * names, const char *current_syntax) { size_t i; + Listbox *syntaxlist; - Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN, - _("Choose syntax highlighting"), NULL); + syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN, + _("Choose syntax highlighting"), NULL); LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL); LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL); - for (i = 0; names[i] != NULL; i++) + for (i = 0; i < names->len; i++) { - LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL); - if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0)) + const char *name; + + name = g_ptr_array_index (names, i); + LISTBOX_APPEND_TEXT (syntaxlist, 0, name, NULL); + if (current_syntax != NULL && strcmp (name, current_syntax) == 0) listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES); } @@ -93,27 +97,27 @@ exec_edit_syntax_dialog (const char **names, const char *current_syntax) void edit_syntax_dialog (WEdit * edit) { - char *current_syntax; - int old_auto_syntax, syntax; - char **names; - gboolean force_reload = FALSE; - size_t count; + GPtrArray *names; + int syntax; - current_syntax = g_strdup (edit->syntax_type); - old_auto_syntax = option_auto_syntax; - - names = g_new0 (char *, 1); + names = g_ptr_array_new (); /* We fill the list of syntax files every time the editor is invoked. Instead we could save the list to a file and update it once the syntax file gets updated (either by testing or by explicit user command). */ - edit_load_syntax (NULL, &names, NULL); - count = g_strv_length (names); - qsort (names, count, sizeof (char *), pstrcmp); + edit_load_syntax (NULL, names, NULL); + g_ptr_array_sort (names, pstrcmp); - syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax); + syntax = exec_edit_syntax_dialog (names, edit->syntax_type); if (syntax >= 0) { + gboolean force_reload = FALSE; + char *current_syntax; + int old_auto_syntax; + + current_syntax = g_strdup (edit->syntax_type); + old_auto_syntax = option_auto_syntax; + switch (syntax) { case 0: /* auto syntax */ @@ -125,19 +129,20 @@ edit_syntax_dialog (WEdit * edit) default: option_auto_syntax = 0; g_free (edit->syntax_type); - edit->syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]); + edit->syntax_type = g_strdup (g_ptr_array_index (names, syntax - N_DFLT_ENTRIES)); } /* Load or unload syntax rules if the option has changed */ - if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax || - (current_syntax && edit->syntax_type && - (strcmp (current_syntax, edit->syntax_type) != 0)) || force_reload) + if (force_reload || (option_auto_syntax && !old_auto_syntax) || old_auto_syntax || + (current_syntax != NULL && edit->syntax_type != NULL && + strcmp (current_syntax, edit->syntax_type) != 0)) edit_load_syntax (edit, NULL, edit->syntax_type); g_free (current_syntax); } - g_strfreev (names); + g_ptr_array_foreach (names, (GFunc) g_free, NULL); + g_ptr_array_free (names, TRUE); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 9c7ea7f25..14dea5900 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -264,7 +264,7 @@ void edit_paste_from_history (WEdit * edit); void edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath); -void edit_load_syntax (WEdit * edit, char ***pnames, const char *type); +void edit_load_syntax (WEdit * edit, GPtrArray * pnames, const char *type); void edit_free_syntax_rules (WEdit * edit); int edit_get_syntax_color (WEdit * edit, off_t byte_index); diff --git a/src/editor/syntax.c b/src/editor/syntax.c index fe071e260..e8ee3d748 100644 --- a/src/editor/syntax.c +++ b/src/editor/syntax.c @@ -1230,18 +1230,15 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size) /* returns -1 on file error, line number on error in file syntax */ static int -edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file, +edit_read_syntax_file (WEdit * edit, GPtrArray * pnames, const char *syntax_file, const char *editor_file, const char *first_line, const char *type) { -#define NENTRIES 30 FILE *f, *g = NULL; char *args[1024], *l = NULL; long line = 0; int result = 0; - int count = 0; char *lib_file; gboolean found = FALSE; - char **tmpnames = NULL; f = fopen (syntax_file, "r"); if (f == NULL) @@ -1290,20 +1287,11 @@ edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file, result = line; break; } - if (pnames && *pnames) + + if (pnames != NULL) { /* 1: just collecting a list of names of rule sets */ - /* Reallocate the list if required */ - if (count % NENTRIES == 0) - { - tmpnames = - (char **) g_try_realloc (*pnames, (count + NENTRIES + 1) * sizeof (char *)); - if (tmpnames == NULL) - break; - *pnames = tmpnames; - } - (*pnames)[count++] = g_strdup (args[2]); - (*pnames)[count] = NULL; + g_ptr_array_add (pnames, g_strdup (args[2])); } else if (type) { @@ -1463,7 +1451,7 @@ edit_free_syntax_rules (WEdit * edit) * type must be edit->syntax_type or NULL */ void -edit_load_syntax (WEdit * edit, char ***pnames, const char *type) +edit_load_syntax (WEdit * edit, GPtrArray * pnames, const char *type) { int r; char *f = NULL; @@ -1483,7 +1471,7 @@ edit_load_syntax (WEdit * edit, char ***pnames, const char *type) if (!tty_use_colors ()) return; - if (!option_syntax_highlighting && (!pnames || !*pnames)) + if (!option_syntax_highlighting && (pnames == NULL || pnames->len == 0)) return; if (edit != NULL && edit->filename_vpath == NULL) diff --git a/tests/src/editor/editcmd__edit_complete_word_cmd.c b/tests/src/editor/editcmd__edit_complete_word_cmd.c index ab4a90413..940cd5d15 100644 --- a/tests/src/editor/editcmd__edit_complete_word_cmd.c +++ b/tests/src/editor/editcmd__edit_complete_word_cmd.c @@ -48,7 +48,7 @@ static WEdit *test_edit; /* --------------------------------------------------------------------------------------------- */ /* @Mock */ void -edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type) +edit_load_syntax (WEdit * _edit, GPtrArray * _pnames, const char *_type) { (void) _edit; (void) _pnames;