mirror of
https://github.com/MidnightCommander/mc
synced 2024-12-23 04:46:55 +03:00
Use GArray for bookmarks instead of raw array.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
3af16693e2
commit
ed18e2d093
31
lib/util.c
31
lib/util.c
@ -1455,7 +1455,7 @@ mc_mkstemps (char **pname, const char *prefix, const char *suffix)
|
|||||||
* If there is no stored data, return line 1 and col 0.
|
* If there is no stored data, return line 1 and col 0.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks)
|
load_file_position (const char *filename, long *line, long *column, off_t * offset, GArray **bookmarks)
|
||||||
{
|
{
|
||||||
char *fn;
|
char *fn;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
@ -1475,9 +1475,7 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* prepare array for serialized bookmarks */
|
/* prepare array for serialized bookmarks */
|
||||||
(*bookmarks) = (long *) g_malloc ((MAX_SAVED_BOOKMARKS + 1) * sizeof(long));
|
*bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
|
||||||
(*bookmarks)[0] = -1;
|
|
||||||
(*bookmarks)[MAX_SAVED_BOOKMARKS] = -2;
|
|
||||||
|
|
||||||
while (fgets (buf, sizeof (buf), f) != NULL)
|
while (fgets (buf, sizeof (buf), f) != NULL)
|
||||||
{
|
{
|
||||||
@ -1523,15 +1521,12 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||||||
|
|
||||||
*offset = strtoll (pos_tokens[2], NULL, 10);
|
*offset = strtoll (pos_tokens[2], NULL, 10);
|
||||||
|
|
||||||
for (i = 0; i < MAX_SAVED_BOOKMARKS; i++)
|
for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
|
||||||
{
|
{
|
||||||
if (pos_tokens[3 + i] != NULL)
|
size_t val;
|
||||||
(*bookmarks)[i] = strtol (pos_tokens[3 + i], NULL, 10);
|
|
||||||
else
|
val = strtoul (pos_tokens[3 + i], NULL, 10);
|
||||||
{
|
g_array_append_val (*bookmarks, val);
|
||||||
(*bookmarks)[i] = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1546,13 +1541,13 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||||||
/* Save position for the given file */
|
/* Save position for the given file */
|
||||||
#define TMP_SUFFIX ".tmp"
|
#define TMP_SUFFIX ".tmp"
|
||||||
void
|
void
|
||||||
save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks)
|
save_file_position (const char *filename, long line, long column, off_t offset, GArray *bookmarks)
|
||||||
{
|
{
|
||||||
static int filepos_max_saved_entries = 0;
|
static size_t filepos_max_saved_entries = 0;
|
||||||
char *fn, *tmp_fn;
|
char *fn, *tmp_fn;
|
||||||
FILE *f, *tmp_f;
|
FILE *f, *tmp_f;
|
||||||
char buf[MC_MAXPATHLEN + 100];
|
char buf[MC_MAXPATHLEN + 100];
|
||||||
int i;
|
size_t i;
|
||||||
const size_t len = strlen (filename);
|
const size_t len = strlen (filename);
|
||||||
gboolean src_error = FALSE;
|
gboolean src_error = FALSE;
|
||||||
|
|
||||||
@ -1585,8 +1580,8 @@ save_file_position (const char *filename, long line, long column, off_t offset,
|
|||||||
if (fprintf (f, "%s %ld;%ld;%ju", filename, line, column, offset) < 0)
|
if (fprintf (f, "%s %ld;%ld;%ju", filename, line, column, offset) < 0)
|
||||||
goto write_position_error;
|
goto write_position_error;
|
||||||
if (bookmarks != NULL)
|
if (bookmarks != NULL)
|
||||||
for (i = 0; bookmarks[i] >= 0 && i < MAX_SAVED_BOOKMARKS; i++)
|
for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
|
||||||
if (fprintf (f, ";%ld", bookmarks[i]) < 0)
|
if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
|
||||||
goto write_position_error;
|
goto write_position_error;
|
||||||
|
|
||||||
if (fprintf (f, "\n") < 0)
|
if (fprintf (f, "\n") < 0)
|
||||||
@ -1616,7 +1611,7 @@ save_file_position (const char *filename, long line, long column, off_t offset,
|
|||||||
open_target_error:
|
open_target_error:
|
||||||
g_free (fn);
|
g_free (fn);
|
||||||
early_error:
|
early_error:
|
||||||
g_free (bookmarks);
|
g_array_free (bookmarks, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef TMP_SUFFIX
|
#undef TMP_SUFFIX
|
||||||
|
@ -232,9 +232,11 @@ GList *list_append_unique (GList * list, char *text);
|
|||||||
/* Position saving and restoring */
|
/* Position saving and restoring */
|
||||||
#define MAX_SAVED_BOOKMARKS 10
|
#define MAX_SAVED_BOOKMARKS 10
|
||||||
/* Load position for the given filename */
|
/* Load position for the given filename */
|
||||||
void load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks);
|
void load_file_position (const char *filename, long *line, long *column, off_t * offset,
|
||||||
|
GArray **bookmarks);
|
||||||
/* Save position for the given filename */
|
/* Save position for the given filename */
|
||||||
void save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks);
|
void save_file_position (const char *filename, long line, long column, off_t offset,
|
||||||
|
GArray *bookmarks);
|
||||||
|
|
||||||
|
|
||||||
/* OS specific defines */
|
/* OS specific defines */
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "lib/global.h"
|
#include "lib/global.h"
|
||||||
|
#include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
|
||||||
|
|
||||||
#include "edit-widget.h"
|
#include "edit-widget.h"
|
||||||
|
|
||||||
@ -139,7 +140,7 @@ book_mark_query_color (WEdit * edit, int line, int c)
|
|||||||
|
|
||||||
/* insert a bookmark at this line */
|
/* insert a bookmark at this line */
|
||||||
void
|
void
|
||||||
book_mark_insert (WEdit * edit, int line, int c)
|
book_mark_insert (WEdit * edit, size_t line, int c)
|
||||||
{
|
{
|
||||||
struct _book_mark *p, *q;
|
struct _book_mark *p, *q;
|
||||||
|
|
||||||
@ -159,7 +160,7 @@ book_mark_insert (WEdit * edit, int line, int c)
|
|||||||
edit->force |= REDRAW_LINE;
|
edit->force |= REDRAW_LINE;
|
||||||
/* create list entry */
|
/* create list entry */
|
||||||
q = g_malloc0 (sizeof (struct _book_mark));
|
q = g_malloc0 (sizeof (struct _book_mark));
|
||||||
q->line = line;
|
q->line = (int) line;
|
||||||
q->c = c;
|
q->c = c;
|
||||||
q->next = p->next;
|
q->next = p->next;
|
||||||
/* insert into list */
|
/* insert into list */
|
||||||
@ -267,14 +268,19 @@ void
|
|||||||
book_mark_serialize (WEdit * edit, int color)
|
book_mark_serialize (WEdit * edit, int color)
|
||||||
{
|
{
|
||||||
struct _book_mark *p;
|
struct _book_mark *p;
|
||||||
long *bookmarks = edit->serialized_bookmarks;
|
|
||||||
|
|
||||||
if (edit->book_mark != NULL && bookmarks != NULL)
|
if (edit->serialized_bookmarks != NULL)
|
||||||
|
g_array_set_size (edit->serialized_bookmarks, 0);
|
||||||
|
|
||||||
|
if (edit->book_mark != NULL)
|
||||||
{
|
{
|
||||||
for (p = book_mark_find (edit, 0); p != NULL && *bookmarks != -2; p = p->next)
|
if (edit->serialized_bookmarks == NULL)
|
||||||
if (p->c == color && p->line != -1)
|
edit->serialized_bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t),
|
||||||
*bookmarks++ = p->line;
|
MAX_SAVED_BOOKMARKS);
|
||||||
*bookmarks = -1;
|
|
||||||
|
for (p = book_mark_find (edit, 0); p != NULL; p = p->next)
|
||||||
|
if (p->c == color && p->line >= 0)
|
||||||
|
g_array_append_val (edit->serialized_bookmarks, p->line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,9 +288,11 @@ book_mark_serialize (WEdit * edit, int color)
|
|||||||
void
|
void
|
||||||
book_mark_restore (WEdit * edit, int color)
|
book_mark_restore (WEdit * edit, int color)
|
||||||
{
|
{
|
||||||
long *bookmarks = edit->serialized_bookmarks;
|
if (edit->serialized_bookmarks != NULL)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
if (bookmarks != NULL)
|
for (i = 0; i < edit->serialized_bookmarks->len; i++)
|
||||||
for (; *bookmarks >= 0; bookmarks++)
|
book_mark_insert (edit, g_array_index (edit->serialized_bookmarks, size_t, i), color);
|
||||||
book_mark_insert (edit, *bookmarks, color);
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +272,7 @@ void edit_load_syntax (WEdit * edit, char ***pnames, const char *type);
|
|||||||
void edit_free_syntax_rules (WEdit * edit);
|
void edit_free_syntax_rules (WEdit * edit);
|
||||||
void edit_get_syntax_color (WEdit * edit, long byte_index, int *color);
|
void edit_get_syntax_color (WEdit * edit, long byte_index, int *color);
|
||||||
|
|
||||||
void book_mark_insert (WEdit * edit, int line, int c);
|
void book_mark_insert (WEdit * edit, size_t line, int c);
|
||||||
int book_mark_query_color (WEdit * edit, int line, int c);
|
int book_mark_query_color (WEdit * edit, int line, int c);
|
||||||
int book_mark_query_all (WEdit * edit, int line, int *c);
|
int book_mark_query_all (WEdit * edit, int line, int *c);
|
||||||
struct _book_mark *book_mark_find (WEdit * edit, int line);
|
struct _book_mark *book_mark_find (WEdit * edit, int line);
|
||||||
|
@ -96,7 +96,7 @@ struct WEdit
|
|||||||
long line_offsets[N_LINE_CACHES];
|
long line_offsets[N_LINE_CACHES];
|
||||||
|
|
||||||
struct _book_mark *book_mark;
|
struct _book_mark *book_mark;
|
||||||
long *serialized_bookmarks;
|
GArray *serialized_bookmarks;
|
||||||
|
|
||||||
/* undo stack and pointers */
|
/* undo stack and pointers */
|
||||||
unsigned long stack_pointer;
|
unsigned long stack_pointer;
|
||||||
|
@ -844,13 +844,12 @@ edit_save_position (WEdit * edit)
|
|||||||
{
|
{
|
||||||
char *filename;
|
char *filename;
|
||||||
|
|
||||||
if (!edit->filename || !*edit->filename)
|
if (edit->filename == NULL || *edit->filename == '\0')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
filename = vfs_canon (edit->filename);
|
filename = vfs_canon (edit->filename);
|
||||||
|
|
||||||
book_mark_serialize (edit, BOOK_MARK_COLOR);
|
book_mark_serialize (edit, BOOK_MARK_COLOR);
|
||||||
|
|
||||||
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, edit->serialized_bookmarks);
|
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, edit->serialized_bookmarks);
|
||||||
edit->serialized_bookmarks = NULL;
|
edit->serialized_bookmarks = NULL;
|
||||||
|
|
||||||
|
@ -191,7 +191,7 @@ typedef struct mcview_struct
|
|||||||
|
|
||||||
int search_numNeedSkipChar;
|
int search_numNeedSkipChar;
|
||||||
|
|
||||||
long *saved_bookmarks;
|
GArray *saved_bookmarks;
|
||||||
} mcview_t;
|
} mcview_t;
|
||||||
|
|
||||||
typedef struct mcview_nroff_struct
|
typedef struct mcview_nroff_struct
|
||||||
|
@ -222,6 +222,8 @@ mcview_init (mcview_t * view)
|
|||||||
view->move_dir = 0;
|
view->move_dir = 0;
|
||||||
view->update_steps = 0;
|
view->update_steps = 0;
|
||||||
view->update_activate = 0;
|
view->update_activate = 0;
|
||||||
|
|
||||||
|
view->saved_bookmarks = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
@ -235,6 +237,7 @@ mcview_done (mcview_t * view)
|
|||||||
char *canon_fname;
|
char *canon_fname;
|
||||||
canon_fname = vfs_canon (view->filename);
|
canon_fname = vfs_canon (view->filename);
|
||||||
save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
|
save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
|
||||||
|
view->saved_bookmarks = NULL;
|
||||||
g_free (canon_fname);
|
g_free (canon_fname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user