Use GArray for bookmarks instead of raw array.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2010-09-07 16:05:24 +04:00 committed by Ilia Maslakov
parent 3af16693e2
commit ed18e2d093
8 changed files with 44 additions and 37 deletions

View File

@ -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.
*/
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;
FILE *f;
@ -1475,9 +1475,7 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
return;
/* prepare array for serialized bookmarks */
(*bookmarks) = (long *) g_malloc ((MAX_SAVED_BOOKMARKS + 1) * sizeof(long));
(*bookmarks)[0] = -1;
(*bookmarks)[MAX_SAVED_BOOKMARKS] = -2;
*bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
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);
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)
(*bookmarks)[i] = strtol (pos_tokens[3 + i], NULL, 10);
else
{
(*bookmarks)[i] = -1;
break;
}
size_t val;
val = strtoul (pos_tokens[3 + i], NULL, 10);
g_array_append_val (*bookmarks, val);
}
}
}
@ -1546,13 +1541,13 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
/* Save position for the given file */
#define TMP_SUFFIX ".tmp"
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;
FILE *f, *tmp_f;
char buf[MC_MAXPATHLEN + 100];
int i;
size_t i;
const size_t len = strlen (filename);
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)
goto write_position_error;
if (bookmarks != NULL)
for (i = 0; bookmarks[i] >= 0 && i < MAX_SAVED_BOOKMARKS; i++)
if (fprintf (f, ";%ld", bookmarks[i]) < 0)
for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
goto write_position_error;
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:
g_free (fn);
early_error:
g_free (bookmarks);
g_array_free (bookmarks, TRUE);
}
#undef TMP_SUFFIX

View File

@ -232,9 +232,11 @@ GList *list_append_unique (GList * list, char *text);
/* Position saving and restoring */
#define MAX_SAVED_BOOKMARKS 10
/* 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 */
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 */

View File

@ -40,6 +40,7 @@
#include <unistd.h>
#include "lib/global.h"
#include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
#include "edit-widget.h"
@ -139,7 +140,7 @@ book_mark_query_color (WEdit * edit, int line, int c)
/* insert a bookmark at this line */
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;
@ -159,7 +160,7 @@ book_mark_insert (WEdit * edit, int line, int c)
edit->force |= REDRAW_LINE;
/* create list entry */
q = g_malloc0 (sizeof (struct _book_mark));
q->line = line;
q->line = (int) line;
q->c = c;
q->next = p->next;
/* insert into list */
@ -267,14 +268,19 @@ void
book_mark_serialize (WEdit * edit, int color)
{
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 (p->c == color && p->line != -1)
*bookmarks++ = p->line;
*bookmarks = -1;
if (edit->serialized_bookmarks == NULL)
edit->serialized_bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t),
MAX_SAVED_BOOKMARKS);
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
book_mark_restore (WEdit * edit, int color)
{
long *bookmarks = edit->serialized_bookmarks;
if (edit->serialized_bookmarks != NULL)
{
size_t i;
if (bookmarks != NULL)
for (; *bookmarks >= 0; bookmarks++)
book_mark_insert (edit, *bookmarks, color);
for (i = 0; i < edit->serialized_bookmarks->len; i++)
book_mark_insert (edit, g_array_index (edit->serialized_bookmarks, size_t, i), color);
}
}

View File

@ -272,7 +272,7 @@ void edit_load_syntax (WEdit * edit, char ***pnames, const char *type);
void edit_free_syntax_rules (WEdit * edit);
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_all (WEdit * edit, int line, int *c);
struct _book_mark *book_mark_find (WEdit * edit, int line);

View File

@ -96,7 +96,7 @@ struct WEdit
long line_offsets[N_LINE_CACHES];
struct _book_mark *book_mark;
long *serialized_bookmarks;
GArray *serialized_bookmarks;
/* undo stack and pointers */
unsigned long stack_pointer;

View File

@ -844,13 +844,12 @@ edit_save_position (WEdit * edit)
{
char *filename;
if (!edit->filename || !*edit->filename)
if (edit->filename == NULL || *edit->filename == '\0')
return;
filename = vfs_canon (edit->filename);
book_mark_serialize (edit, BOOK_MARK_COLOR);
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, edit->serialized_bookmarks);
edit->serialized_bookmarks = NULL;

View File

@ -191,7 +191,7 @@ typedef struct mcview_struct
int search_numNeedSkipChar;
long *saved_bookmarks;
GArray *saved_bookmarks;
} mcview_t;
typedef struct mcview_nroff_struct

View File

@ -222,6 +222,8 @@ mcview_init (mcview_t * view)
view->move_dir = 0;
view->update_steps = 0;
view->update_activate = 0;
view->saved_bookmarks = NULL;
}
/* --------------------------------------------------------------------------------------------- */
@ -235,6 +237,7 @@ mcview_done (mcview_t * view)
char *canon_fname;
canon_fname = vfs_canon (view->filename);
save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
view->saved_bookmarks = NULL;
g_free (canon_fname);
}