mceditor: reimplement syntax name list using GPtrArray.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2014-09-30 14:48:54 +04:00
parent 7f8135d071
commit 4dd577fbe0
4 changed files with 38 additions and 45 deletions

View File

@ -35,7 +35,7 @@
#include <config.h> #include <config.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <string.h> /* strcmp() */
#include "lib/global.h" #include "lib/global.h"
#include "lib/widget.h" /* Listbox */ #include "lib/widget.h" /* Listbox */
@ -67,19 +67,23 @@ pstrcmp (const void *p1, const void *p2)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static int 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; size_t i;
Listbox *syntaxlist;
Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN, syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
_("Choose syntax highlighting"), NULL); _("Choose syntax highlighting"), NULL);
LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL); LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), 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); const char *name;
if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0))
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); 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 void
edit_syntax_dialog (WEdit * edit) edit_syntax_dialog (WEdit * edit)
{ {
char *current_syntax; GPtrArray *names;
int old_auto_syntax, syntax; int syntax;
char **names;
gboolean force_reload = FALSE;
size_t count;
current_syntax = g_strdup (edit->syntax_type); names = g_ptr_array_new ();
old_auto_syntax = option_auto_syntax;
names = g_new0 (char *, 1);
/* We fill the list of syntax files every time the editor is invoked. /* 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 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). */ file gets updated (either by testing or by explicit user command). */
edit_load_syntax (NULL, &names, NULL); edit_load_syntax (NULL, names, NULL);
count = g_strv_length (names); g_ptr_array_sort (names, pstrcmp);
qsort (names, count, sizeof (char *), pstrcmp);
syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax); syntax = exec_edit_syntax_dialog (names, edit->syntax_type);
if (syntax >= 0) 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) switch (syntax)
{ {
case 0: /* auto syntax */ case 0: /* auto syntax */
@ -125,19 +129,20 @@ edit_syntax_dialog (WEdit * edit)
default: default:
option_auto_syntax = 0; option_auto_syntax = 0;
g_free (edit->syntax_type); 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 */ /* Load or unload syntax rules if the option has changed */
if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax || if (force_reload || (option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
(current_syntax && edit->syntax_type && (current_syntax != NULL && edit->syntax_type != NULL &&
(strcmp (current_syntax, edit->syntax_type) != 0)) || force_reload) strcmp (current_syntax, edit->syntax_type) != 0))
edit_load_syntax (edit, NULL, edit->syntax_type); edit_load_syntax (edit, NULL, edit->syntax_type);
g_free (current_syntax); g_free (current_syntax);
} }
g_strfreev (names); g_ptr_array_foreach (names, (GFunc) g_free, NULL);
g_ptr_array_free (names, TRUE);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -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_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); void edit_free_syntax_rules (WEdit * edit);
int edit_get_syntax_color (WEdit * edit, off_t byte_index); int edit_get_syntax_color (WEdit * edit, off_t byte_index);

View File

@ -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 */ /* returns -1 on file error, line number on error in file syntax */
static int 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) const char *editor_file, const char *first_line, const char *type)
{ {
#define NENTRIES 30
FILE *f, *g = NULL; FILE *f, *g = NULL;
char *args[1024], *l = NULL; char *args[1024], *l = NULL;
long line = 0; long line = 0;
int result = 0; int result = 0;
int count = 0;
char *lib_file; char *lib_file;
gboolean found = FALSE; gboolean found = FALSE;
char **tmpnames = NULL;
f = fopen (syntax_file, "r"); f = fopen (syntax_file, "r");
if (f == NULL) if (f == NULL)
@ -1290,20 +1287,11 @@ edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file,
result = line; result = line;
break; break;
} }
if (pnames && *pnames)
if (pnames != NULL)
{ {
/* 1: just collecting a list of names of rule sets */ /* 1: just collecting a list of names of rule sets */
/* Reallocate the list if required */ g_ptr_array_add (pnames, g_strdup (args[2]));
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;
} }
else if (type) else if (type)
{ {
@ -1463,7 +1451,7 @@ edit_free_syntax_rules (WEdit * edit)
* type must be edit->syntax_type or NULL * type must be edit->syntax_type or NULL
*/ */
void void
edit_load_syntax (WEdit * edit, char ***pnames, const char *type) edit_load_syntax (WEdit * edit, GPtrArray * pnames, const char *type)
{ {
int r; int r;
char *f = NULL; char *f = NULL;
@ -1483,7 +1471,7 @@ edit_load_syntax (WEdit * edit, char ***pnames, const char *type)
if (!tty_use_colors ()) if (!tty_use_colors ())
return; return;
if (!option_syntax_highlighting && (!pnames || !*pnames)) if (!option_syntax_highlighting && (pnames == NULL || pnames->len == 0))
return; return;
if (edit != NULL && edit->filename_vpath == NULL) if (edit != NULL && edit->filename_vpath == NULL)

View File

@ -48,7 +48,7 @@ static WEdit *test_edit;
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/* @Mock */ /* @Mock */
void void
edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type) edit_load_syntax (WEdit * _edit, GPtrArray * _pnames, const char *_type)
{ {
(void) _edit; (void) _edit;
(void) _pnames; (void) _pnames;