Reafctor panel format storage: use GSList.

Rename structure.
Constify some function arguments.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2019-01-01 12:50:30 +03:00
parent 27470f3989
commit 3e92c40a38
2 changed files with 92 additions and 117 deletions

View File

@ -129,11 +129,10 @@ typedef enum
/*
* This describes a format item. The parse_display_format routine parses
* the user specified format and creates a linked list of format_e structures.
* the user specified format and creates a linked list of format_item_t structures.
*/
typedef struct format_e
typedef struct format_item_t
{
struct format_e *next;
int requested_field_len;
int field_len;
align_crt_t just_mode;
@ -141,7 +140,7 @@ typedef struct format_e
const char *(*string_fn) (file_entry_t *, int len);
char *title;
const char *id;
} format_e;
} format_item_t;
/* File name scroll states */
typedef enum
@ -385,18 +384,13 @@ set_colors (const WPanel * panel)
}
/* --------------------------------------------------------------------------------------------- */
/** Delete format string, it is a linked list */
/** Delete format_item_t object */
static void
delete_format (format_e * format)
format_item_free (format_item_t * format)
{
while (format != NULL)
{
format_e *next = format->next;
g_free (format->title);
g_free (format);
format = next;
}
g_free (format->title);
g_free (format);
}
/* --------------------------------------------------------------------------------------------- */
@ -797,7 +791,7 @@ format_file (WPanel * panel, int file_index, int width, int attr, gboolean issta
{
int color = NORMAL_COLOR;
int length = 0;
format_e *format, *home;
GSList *format, *home;
file_entry_t *fe = NULL;
filename_scroll_flag_t res = FILENAME_NOSCROLL;
@ -811,9 +805,11 @@ format_file (WPanel * panel, int file_index, int width, int attr, gboolean issta
home = isstatus ? panel->status_format : panel->format;
for (format = home; format != NULL && length != width; format = format->next)
for (format = home; format != NULL && length != width; format = g_slist_next (format))
{
if (format->string_fn != NULL)
format_item_t *fi = (format_item_t *) format->data;
if (fi->string_fn != NULL)
{
const char *txt = " ";
int len, perm = 0;
@ -821,15 +817,15 @@ format_file (WPanel * panel, int file_index, int width, int attr, gboolean issta
int name_offset = 0;
if (fe != NULL)
txt = format->string_fn (fe, format->field_len);
txt = fi->string_fn (fe, fi->field_len);
len = format->field_len;
len = fi->field_len;
if (len + length > width)
len = width - length;
if (len <= 0)
break;
if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
if (!isstatus && panel->content_shift > -1 && strcmp (fi->id, "name") == 0)
{
int str_len;
int i;
@ -855,9 +851,9 @@ format_file (WPanel * panel, int file_index, int width, int attr, gboolean issta
if (panels_options.permission_mode)
{
if (strcmp (format->id, "perm") == 0)
if (strcmp (fi->id, "perm") == 0)
perm = 1;
else if (strcmp (format->id, "mode") == 0)
else if (strcmp (fi->id, "mode") == 0)
perm = 2;
}
@ -867,14 +863,12 @@ format_file (WPanel * panel, int file_index, int width, int attr, gboolean issta
tty_lowlevel_setcolor (-color);
if (!isstatus && panel->content_shift > -1)
prepared_text =
str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (fi->just_mode));
else
prepared_text = str_fit_to_term (txt, len, format->just_mode);
prepared_text = str_fit_to_term (txt, len, fi->just_mode);
if (perm != 0 && fe != NULL)
add_permission_string (prepared_text, format->field_len, fe, attr, color,
perm != 1);
add_permission_string (prepared_text, fi->field_len, fe, attr, color, perm != 1);
else
tty_print_string (prepared_text);
@ -1498,8 +1492,8 @@ panel_destroy (WPanel * p)
}
g_free (p->hist_name);
delete_format (p->format);
delete_format (p->status_format);
g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
g_free (p->user_format);
for (i = 0; i < LIST_FORMATS; i++)
@ -1573,23 +1567,24 @@ panel_print_header (const WPanel * panel)
for (i = 0; i < panel->list_cols; i++)
{
format_e *format;
GSList *format;
for (format = panel->format; format != NULL; format = format->next)
for (format = panel->format; format != NULL; format = g_slist_next (format))
{
if (format->string_fn != NULL)
format_item_t *fi = (format_item_t *) format->data;
if (fi->string_fn != NULL)
{
g_string_set_size (format_txt, 0);
if (panel->list_format == list_long
&& strcmp (format->id, panel->sort_field->id) == 0)
if (panel->list_format == list_long && strcmp (fi->id, panel->sort_field->id) == 0)
g_string_append (format_txt,
panel->sort_info.reverse
? panel_sort_up_sign : panel_sort_down_sign);
g_string_append (format_txt, format->title);
g_string_append (format_txt, fi->title);
if (panel->filter != NULL && strcmp (format->id, "name") == 0)
if (panel->filter != NULL && strcmp (fi->id, "name") == 0)
{
g_string_append (format_txt, " [");
g_string_append (format_txt, panel->filter);
@ -1597,8 +1592,7 @@ panel_print_header (const WPanel * panel)
}
tty_setcolor (HEADER_COLOR);
tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
J_CENTER_LEFT));
tty_print_string (str_fit_to_term (format_txt->str, fi->field_len, J_CENTER_LEFT));
}
else
{
@ -1666,28 +1660,30 @@ parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
return skip_separators (format);
}
/* --------------------------------------------------------------------------------------------- */
/* *INDENT-OFF* */
/* Format is:
all := panel_format? format
panel_format := [full|half] [1-9]
format := one_format_e
| format , one_format_e
format := one_format_item_t
| format , one_format_item_t
one_format_e := just format.id [opt_size]
just := [<=>]
opt_size := : size [opt_expand]
size := [0-9]+
opt_expand := +
one_format_item_t := just format.id [opt_size]
just := [<=>]
opt_size := : size [opt_expand]
size := [0-9]+
opt_expand := +
*/
/* *INDENT-ON* */
/* --------------------------------------------------------------------------------------------- */
static format_e *
static GSList *
parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
int *res_total_cols)
{
format_e *darr, *old = NULL, *home = NULL; /* The formats we return */
GSList *home = NULL; /* The formats we return */
int total_cols = 0; /* Used columns by the format */
size_t i;
@ -1712,19 +1708,13 @@ parse_display_format (WPanel * panel, const char *format, char **error, gboolean
while (*format != '\0')
{ /* format can be an empty string */
format_item_t *darr;
align_crt_t justify; /* Which mode. */
gboolean set_justify = TRUE; /* flag: set justification mode? */
gboolean found = FALSE;
darr = g_new0 (format_e, 1);
/* I'm so ugly, don't look at me :-) */
if (home == NULL)
home = old = darr;
old->next = darr;
darr->next = NULL;
old = darr;
darr = g_new0 (format_item_t, 1);
home = g_slist_append (home, darr);
format = skip_separators (format);
@ -1815,7 +1805,7 @@ parse_display_format (WPanel * panel, const char *format, char **error, gboolean
pos = 8;
tmp_format = g_strndup (format, pos);
delete_format (home);
g_slist_free_full (home, (GDestroyNotify) format_item_free);
*error =
g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
g_free (tmp_format);
@ -1832,14 +1822,14 @@ parse_display_format (WPanel * panel, const char *format, char **error, gboolean
/* --------------------------------------------------------------------------------------------- */
static format_e *
static GSList *
use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
{
#define MAX_EXPAND 4
int expand_top = 0; /* Max used element in expand */
int usable_columns; /* Usable columns in the panel */
int total_cols = 0;
format_e *darr, *home;
GSList *darr, *home;
if (format == NULL)
format = DEFAULT_USER_FORMAT;
@ -1861,10 +1851,12 @@ use_display_format (WPanel * panel, const char *format, char **error, gboolean i
}
/* Look for the expandable fields and set field_len based on the requested field len */
for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = darr->next)
for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = g_slist_next (darr))
{
darr->field_len = darr->requested_field_len;
if (darr->expand)
format_item_t *fi = (format_item_t *) darr->data;
fi->field_len = fi->requested_field_len;
if (fi->expand)
expand_top++;
}
@ -1880,12 +1872,16 @@ use_display_format (WPanel * panel, const char *format, char **error, gboolean i
{
pdif = dif;
for (darr = home; darr; darr = darr->next)
if (dif != 0 && darr->field_len != 1)
for (darr = home; darr != NULL; darr = g_slist_next (darr))
{
format_item_t *fi = (format_item_t *) darr->data;
if (dif != 0 && fi->field_len != 1)
{
darr->field_len--;
fi->field_len--;
dif--;
}
}
}
total_cols = usable_columns; /* give up, the rest should be truncated */
@ -1899,15 +1895,20 @@ use_display_format (WPanel * panel, const char *format, char **error, gboolean i
spaces = (usable_columns - total_cols) / expand_top;
for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
if (darr->expand)
for (i = 0, darr = home; darr != NULL && i < expand_top; darr = g_slist_next (darr))
{
format_item_t *fi = (format_item_t *) darr->data;
if (fi->expand)
{
darr->field_len += spaces;
fi->field_len += spaces;
if (i == 0)
darr->field_len += (usable_columns - total_cols) % expand_top;
fi->field_len += (usable_columns - total_cols) % expand_top;
i++;
}
}
}
return home;
}
@ -2982,33 +2983,20 @@ chdir_to_readlink (WPanel * panel)
move_down (panel);
}
/* --------------------------------------------------------------------------------------------- */
static gsize
panel_get_format_field_count (WPanel * panel)
{
format_e *format;
gsize lc_index;
for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++)
;
return lc_index;
}
/* --------------------------------------------------------------------------------------------- */
/**
function return 0 if not found and REAL_INDEX+1 if found
*/
static gsize
panel_get_format_field_index_by_name (WPanel * panel, const char *name)
panel_get_format_field_index_by_name (const WPanel * panel, const char *name)
{
format_e *format;
GSList *format;
gsize lc_index;
for (lc_index = 1, format = panel->format;
format != NULL && strcmp (format->title, name) != 0; format = format->next, lc_index++)
format != NULL && strcmp (((format_item_t *) format->data)->title, name) != 0;
format = g_slist_next (format), lc_index++)
;
if (format == NULL)
@ -3019,26 +3007,13 @@ panel_get_format_field_index_by_name (WPanel * panel, const char *name)
/* --------------------------------------------------------------------------------------------- */
static format_e *
panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
{
format_e *format;
for (format = panel->format; format != NULL && lc_index != 0; format = format->next, lc_index--)
;
return format;
}
/* --------------------------------------------------------------------------------------------- */
static const panel_field_t *
panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
panel_get_sortable_field_by_format (const WPanel * panel, gsize lc_index)
{
const panel_field_t *pfield;
format_e *format;
const format_item_t *format;
format = panel_get_format_field_by_index (panel, lc_index);
format = (const format_item_t *) g_slist_nth_data (panel->format, lc_index);
if (format == NULL)
return NULL;
@ -3073,7 +3048,7 @@ panel_toggle_sort_order_prev (WPanel * panel)
if (pfield == NULL)
{
/* Sortable field not found. Try to search in each array */
for (i = panel_get_format_field_count (panel);
for (i = g_slist_length (panel->format);
i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
;
}
@ -3095,7 +3070,7 @@ panel_toggle_sort_order_next (WPanel * panel)
gsize format_field_count;
const char *title;
format_field_count = panel_get_format_field_count (panel);
format_field_count = g_slist_length (panel->format);
title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
lc_index = panel_get_format_field_index_by_name (panel, title);
@ -3784,18 +3759,20 @@ mark_if_marking (WPanel * panel, const mouse_event_t * event)
static void
mouse_sort_col (WPanel * panel, int x)
{
int i;
int i = 0;
GSList *format = panel->format;
const char *lc_sort_name = NULL;
panel_field_t *col_sort_format = NULL;
format_e *format;
for (i = 0, format = panel->format; format != NULL; format = format->next)
for (; format != NULL; format = g_slist_next (format))
{
i += format->field_len;
format_item_t *fi = (format_item_t *) format->data;
i += fi->field_len;
if (x < i + 1)
{
/* found column */
lc_sort_name = format->title;
lc_sort_name = fi->title;
break;
}
}
@ -4455,7 +4432,7 @@ panel_reload (WPanel * panel)
int
set_panel_formats (WPanel * p)
{
format_e *form;
GSList *form;
char *err = NULL;
int retcode = 0;
@ -4468,7 +4445,7 @@ set_panel_formats (WPanel * p)
}
else
{
delete_format (p->format);
g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
p->format = form;
}
@ -4483,7 +4460,7 @@ set_panel_formats (WPanel * p)
}
else
{
delete_format (p->status_format);
g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
p->status_format = form;
}
}

View File

@ -67,8 +67,6 @@ enum cd_enum
/*** structures declarations (and typedefs of structures)*****************************************/
struct format_e;
typedef struct panel_field_struct
{
const char *id;
@ -122,8 +120,8 @@ typedef struct
char *user_format; /* User format */
char *user_status_format[LIST_FORMATS]; /* User format for status line */
struct format_e *format; /* Display format */
struct format_e *status_format; /* Mini status format */
GSList *format; /* Display format */
GSList *status_format; /* Mini status format */
char *panel_name; /* The panel name */
struct stat dir_stat; /* Stat of current dir: used by execute () */