mirror of https://github.com/MidnightCommander/mc
Minor optimization, type accuracy, includes fix.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
a4651d8d2b
commit
3af16693e2
89
lib/util.c
89
lib/util.c
|
@ -1450,8 +1450,6 @@ mc_mkstemps (char **pname, const char *prefix, const char *suffix)
|
|||
return -1;
|
||||
}
|
||||
|
||||
#define MAX_SAVED_BOOKMARKS 10
|
||||
|
||||
/*
|
||||
* Read and restore position for the given filename.
|
||||
* If there is no stored data, return line 1 and col 0.
|
||||
|
@ -1462,7 +1460,7 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||
char *fn;
|
||||
FILE *f;
|
||||
char buf[MC_MAXPATHLEN + 100];
|
||||
int len;
|
||||
const size_t len = strlen (filename);
|
||||
|
||||
/* defaults */
|
||||
*line = 1;
|
||||
|
@ -1473,17 +1471,15 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||
fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL);
|
||||
f = fopen (fn, "r");
|
||||
g_free (fn);
|
||||
if (!f)
|
||||
if (f == NULL)
|
||||
return;
|
||||
|
||||
/* prepare array for serialized bookmarks */
|
||||
(*bookmarks) = (long*) g_malloc ((MAX_SAVED_BOOKMARKS + 1) * sizeof(long));
|
||||
(*bookmarks) = (long *) g_malloc ((MAX_SAVED_BOOKMARKS + 1) * sizeof(long));
|
||||
(*bookmarks)[0] = -1;
|
||||
(*bookmarks)[MAX_SAVED_BOOKMARKS] = -2;
|
||||
|
||||
len = strlen (filename);
|
||||
|
||||
while (fgets (buf, sizeof (buf), f))
|
||||
while (fgets (buf, sizeof (buf), f) != NULL)
|
||||
{
|
||||
const char *p;
|
||||
gchar **pos_tokens;
|
||||
|
@ -1498,21 +1494,35 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||
|
||||
/* and string without spaces */
|
||||
p = &buf[len + 1];
|
||||
if (strchr (p, ' '))
|
||||
if (strchr (p, ' ') != NULL)
|
||||
continue;
|
||||
|
||||
|
||||
pos_tokens = g_strsplit_set (p, ";", 3 + MAX_SAVED_BOOKMARKS);
|
||||
if (pos_tokens[0] != NULL)
|
||||
pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
|
||||
if (pos_tokens[0] == NULL)
|
||||
{
|
||||
*line = 1;
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*line = strtol (pos_tokens[0], NULL, 10);
|
||||
if (pos_tokens[1] != NULL)
|
||||
if (pos_tokens[1] == NULL)
|
||||
{
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*column = strtol (pos_tokens[1], NULL, 10);
|
||||
if (pos_tokens[2] != NULL)
|
||||
if (pos_tokens[2] == NULL)
|
||||
*offset = 0;
|
||||
else
|
||||
{
|
||||
int i;
|
||||
size_t i;
|
||||
|
||||
*offset = strtoll (pos_tokens[2], NULL, 10);
|
||||
|
||||
for (i = 0; i < MAX_SAVED_BOOKMARKS; i++)
|
||||
{
|
||||
if (pos_tokens[3 + i] != NULL)
|
||||
|
@ -1524,23 +1534,12 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
|
|||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
*offset = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*line = 1;
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
|
||||
g_strfreev (pos_tokens);
|
||||
}
|
||||
|
||||
fclose (f);
|
||||
}
|
||||
|
||||
|
@ -1554,7 +1553,8 @@ save_file_position (const char *filename, long line, long column, off_t offset,
|
|||
FILE *f, *tmp_f;
|
||||
char buf[MC_MAXPATHLEN + 100];
|
||||
int i;
|
||||
gsize len;
|
||||
const size_t len = strlen (filename);
|
||||
gboolean src_error = FALSE;
|
||||
|
||||
if (filepos_max_saved_entries == 0)
|
||||
filepos_max_saved_entries = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
|
||||
|
@ -1564,8 +1564,6 @@ save_file_position (const char *filename, long line, long column, off_t offset,
|
|||
if (fn == NULL)
|
||||
goto early_error;
|
||||
|
||||
len = strlen (filename);
|
||||
|
||||
mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
|
||||
|
||||
/* open file */
|
||||
|
@ -1576,54 +1574,49 @@ save_file_position (const char *filename, long line, long column, off_t offset,
|
|||
tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
|
||||
tmp_f = fopen (tmp_fn, "r");
|
||||
if (tmp_f == NULL)
|
||||
{
|
||||
src_error = TRUE;
|
||||
goto open_source_error;
|
||||
}
|
||||
|
||||
/* put the new record */
|
||||
if (line != 1 || column != 0 || bookmarks != NULL)
|
||||
{
|
||||
if (fprintf (f, "%s %ld;%ld;%llu", filename, line, column, (unsigned long long) offset) < 0)
|
||||
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)
|
||||
goto write_position_error;
|
||||
}
|
||||
}
|
||||
|
||||
if (fprintf (f, "\n") < 0)
|
||||
goto write_position_error;
|
||||
}
|
||||
|
||||
i = 1;
|
||||
while (fgets (buf, sizeof (buf), tmp_f))
|
||||
while (fgets (buf, sizeof (buf), tmp_f) != NULL)
|
||||
{
|
||||
if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && !strchr (&buf[len + 1], ' '))
|
||||
if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && strchr (&buf[len + 1], ' ') == NULL)
|
||||
continue;
|
||||
|
||||
fprintf (f, "%s", buf);
|
||||
if (++i > filepos_max_saved_entries)
|
||||
break;
|
||||
}
|
||||
fclose (tmp_f);
|
||||
g_free (tmp_fn);
|
||||
fclose (f);
|
||||
mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
|
||||
g_free (fn);
|
||||
return;
|
||||
|
||||
write_position_error:
|
||||
fclose (tmp_f);
|
||||
open_source_error:
|
||||
g_free (tmp_fn);
|
||||
fclose (f);
|
||||
mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
|
||||
if (src_error)
|
||||
mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
|
||||
else
|
||||
mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
|
||||
open_target_error:
|
||||
g_free (fn);
|
||||
early_error:
|
||||
if (bookmarks != NULL)
|
||||
g_free(bookmarks);
|
||||
return;
|
||||
g_free (bookmarks);
|
||||
}
|
||||
|
||||
#undef TMP_SUFFIX
|
||||
|
|
|
@ -230,7 +230,7 @@ int hook_present (Hook * hook_list, void (*hook_fn) (void *));
|
|||
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);
|
||||
/* Save position for the given filename */
|
||||
|
|
|
@ -41,10 +41,8 @@
|
|||
|
||||
#include "lib/global.h"
|
||||
|
||||
#include "edit-impl.h"
|
||||
#include "edit-widget.h"
|
||||
|
||||
|
||||
/* note, if there is more than one bookmark on a line, then they are
|
||||
appended after each other and the last one is always the one found
|
||||
by book_mark_found() i.e. last in is the one seen */
|
||||
|
@ -54,7 +52,7 @@ double_marks (WEdit * edit, struct _book_mark *p)
|
|||
{
|
||||
(void) edit;
|
||||
|
||||
if (p->next)
|
||||
if (p->next != NULL)
|
||||
while (p->next->line == p->line)
|
||||
p = p->next;
|
||||
return p;
|
||||
|
@ -65,42 +63,43 @@ struct _book_mark *
|
|||
book_mark_find (WEdit * edit, int line)
|
||||
{
|
||||
struct _book_mark *p;
|
||||
if (!edit->book_mark)
|
||||
|
||||
if (edit->book_mark == NULL)
|
||||
{
|
||||
/* must have an imaginary top bookmark at line -1 to make things less complicated */
|
||||
edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
|
||||
edit->book_mark->line = -1;
|
||||
return edit->book_mark;
|
||||
}
|
||||
for (p = edit->book_mark; p; p = p->next)
|
||||
|
||||
for (p = edit->book_mark; p != NULL; p = p->next)
|
||||
{
|
||||
if (p->line > line)
|
||||
break; /* gone past it going downward */
|
||||
if (p->line <= line)
|
||||
|
||||
if (p->next != NULL)
|
||||
{
|
||||
if (p->next)
|
||||
{
|
||||
if (p->next->line > line)
|
||||
{
|
||||
edit->book_mark = p;
|
||||
return double_marks (edit, p);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (p->next->line > line)
|
||||
{
|
||||
edit->book_mark = p;
|
||||
return double_marks (edit, p);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
edit->book_mark = p;
|
||||
return double_marks (edit, p);
|
||||
}
|
||||
}
|
||||
for (p = edit->book_mark; p; p = p->prev)
|
||||
|
||||
for (p = edit->book_mark; p != NULL; p = p->prev)
|
||||
{
|
||||
if (p->next)
|
||||
if (p->next->line <= line)
|
||||
break; /* gone past it going upward */
|
||||
if (p->next != NULL && p->next->line <= line)
|
||||
break; /* gone past it going upward */
|
||||
|
||||
if (p->line <= line)
|
||||
{
|
||||
if (p->next)
|
||||
if (p->next != NULL)
|
||||
{
|
||||
if (p->next->line > line)
|
||||
{
|
||||
|
@ -115,7 +114,8 @@ book_mark_find (WEdit * edit, int line)
|
|||
}
|
||||
}
|
||||
}
|
||||
return 0; /* can't get here */
|
||||
|
||||
return NULL; /* can't get here */
|
||||
}
|
||||
|
||||
/* returns true if a bookmark exists at this line of color c */
|
||||
|
@ -123,9 +123,11 @@ int
|
|||
book_mark_query_color (WEdit * edit, int line, int c)
|
||||
{
|
||||
struct _book_mark *p;
|
||||
if (!edit->book_mark)
|
||||
|
||||
if (edit->book_mark == NULL)
|
||||
return 0;
|
||||
for (p = book_mark_find (edit, line); p; p = p->prev)
|
||||
|
||||
for (p = book_mark_find (edit, line); p != NULL; p = p->prev)
|
||||
{
|
||||
if (p->line != line)
|
||||
return 0;
|
||||
|
@ -140,6 +142,7 @@ void
|
|||
book_mark_insert (WEdit * edit, int line, int c)
|
||||
{
|
||||
struct _book_mark *p, *q;
|
||||
|
||||
p = book_mark_find (edit, line);
|
||||
#if 0
|
||||
if (p->line == line)
|
||||
|
@ -147,8 +150,8 @@ book_mark_insert (WEdit * edit, int line, int c)
|
|||
/* already exists, so just change the color */
|
||||
if (p->c != c)
|
||||
{
|
||||
edit->force |= REDRAW_LINE;
|
||||
p->c = c;
|
||||
edit->force |= REDRAW_LINE;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -161,7 +164,7 @@ book_mark_insert (WEdit * edit, int line, int c)
|
|||
q->next = p->next;
|
||||
/* insert into list */
|
||||
q->prev = p;
|
||||
if (p->next)
|
||||
if (p->next != NULL)
|
||||
p->next->prev = q;
|
||||
p->next = q;
|
||||
}
|
||||
|
@ -173,20 +176,22 @@ book_mark_clear (WEdit * edit, int line, int c)
|
|||
{
|
||||
struct _book_mark *p, *q;
|
||||
int r = 1;
|
||||
if (!edit->book_mark)
|
||||
|
||||
if (edit->book_mark == NULL)
|
||||
return r;
|
||||
for (p = book_mark_find (edit, line); p; p = q)
|
||||
|
||||
for (p = book_mark_find (edit, line); p != NULL; p = q)
|
||||
{
|
||||
q = p->prev;
|
||||
if (p->line == line && (p->c == c || c == -1))
|
||||
{
|
||||
r = 0;
|
||||
edit->force |= REDRAW_LINE;
|
||||
edit->book_mark = p->prev;
|
||||
p->prev->next = p->next;
|
||||
if (p->next)
|
||||
if (p->next != NULL)
|
||||
p->next->prev = p->prev;
|
||||
g_free (p);
|
||||
edit->force |= REDRAW_LINE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -194,7 +199,7 @@ book_mark_clear (WEdit * edit, int line, int c)
|
|||
if (edit->book_mark->line == -1 && !edit->book_mark->next)
|
||||
{
|
||||
g_free (edit->book_mark);
|
||||
edit->book_mark = 0;
|
||||
edit->book_mark = NULL;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
@ -204,27 +209,31 @@ void
|
|||
book_mark_flush (WEdit * edit, int c)
|
||||
{
|
||||
struct _book_mark *p, *q;
|
||||
if (!edit->book_mark)
|
||||
|
||||
if (edit->book_mark == NULL)
|
||||
return;
|
||||
edit->force |= REDRAW_PAGE;
|
||||
while (edit->book_mark->prev)
|
||||
|
||||
while (edit->book_mark->prev != NULL)
|
||||
edit->book_mark = edit->book_mark->prev;
|
||||
for (q = edit->book_mark->next; q; q = p)
|
||||
|
||||
for (q = edit->book_mark->next; q != NULL; q = p)
|
||||
{
|
||||
p = q->next;
|
||||
if (q->c == c || c == -1)
|
||||
{
|
||||
q->prev->next = q->next;
|
||||
if (p)
|
||||
if (p != NULL)
|
||||
p->prev = q->prev;
|
||||
g_free (q);
|
||||
}
|
||||
}
|
||||
if (!edit->book_mark->next)
|
||||
if (edit->book_mark->next == NULL)
|
||||
{
|
||||
g_free (edit->book_mark);
|
||||
edit->book_mark = 0;
|
||||
edit->book_mark = NULL;
|
||||
}
|
||||
|
||||
edit->force |= REDRAW_PAGE;
|
||||
}
|
||||
|
||||
/* shift down bookmarks after this line */
|
||||
|
@ -235,10 +244,8 @@ book_mark_inc (WEdit * edit, int line)
|
|||
{
|
||||
struct _book_mark *p;
|
||||
p = book_mark_find (edit, line);
|
||||
for (p = p->next; p; p = p->next)
|
||||
{
|
||||
for (p = p->next; p != NULL; p = p->next)
|
||||
p->line++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,14 +253,12 @@ book_mark_inc (WEdit * edit, int line)
|
|||
void
|
||||
book_mark_dec (WEdit * edit, int line)
|
||||
{
|
||||
if (edit->book_mark)
|
||||
if (edit->book_mark != NULL)
|
||||
{
|
||||
struct _book_mark *p;
|
||||
p = book_mark_find (edit, line);
|
||||
for (p = p->next; p; p = p->next)
|
||||
{
|
||||
for (p = p->next; p != NULL; p = p->next)
|
||||
p->line--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -264,17 +269,13 @@ book_mark_serialize (WEdit * edit, int color)
|
|||
struct _book_mark *p;
|
||||
long *bookmarks = edit->serialized_bookmarks;
|
||||
|
||||
if (edit->book_mark == NULL || bookmarks == NULL)
|
||||
return;
|
||||
|
||||
for (p = book_mark_find (edit, 0); p != NULL && *bookmarks != -2; p = p->next)
|
||||
if (edit->book_mark != NULL && bookmarks != NULL)
|
||||
{
|
||||
if (p->c == color && p->line != -1)
|
||||
{
|
||||
*bookmarks++ = p->line;
|
||||
}
|
||||
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;
|
||||
}
|
||||
*bookmarks = -1;
|
||||
}
|
||||
|
||||
/* restore bookmarks from saved line positions */
|
||||
|
@ -282,9 +283,8 @@ void
|
|||
book_mark_restore (WEdit * edit, int color)
|
||||
{
|
||||
long *bookmarks = edit->serialized_bookmarks;
|
||||
if (bookmarks == NULL)
|
||||
return;
|
||||
|
||||
for (; *bookmarks >= 0; bookmarks++)
|
||||
book_mark_insert (edit, *bookmarks, color);
|
||||
if (bookmarks != NULL)
|
||||
for (; *bookmarks >= 0; bookmarks++)
|
||||
book_mark_insert (edit, *bookmarks, color);
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
|
||||
#include "lib/vfs/mc-vfs/vfs.h"
|
||||
#include "lib/strutil.h" /* utf string functions */
|
||||
#include "lib/util.h" /* load_file_position(), save_file_position() */
|
||||
#include "lib/timefmt.h" /* time formatting */
|
||||
#include "lib/lock.h"
|
||||
|
||||
|
@ -805,8 +806,6 @@ edit_load_file (WEdit * edit)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#define MAX_SAVED_BOOKMARKS 10
|
||||
|
||||
/* Restore saved cursor position in the file */
|
||||
static void
|
||||
edit_load_position (WEdit * edit)
|
||||
|
|
|
@ -43,10 +43,11 @@
|
|||
#include "lib/global.h"
|
||||
#include "lib/vfs/mc-vfs/vfs.h"
|
||||
#include "lib/strutil.h"
|
||||
#include "lib/util.h" /* save_file_position() */
|
||||
#include "lib/lock.h" /* unlock_file() */
|
||||
|
||||
#include "src/wtools.h"
|
||||
#include "src/main.h"
|
||||
#include "lib/lock.h" /* unlock_file() */
|
||||
#include "src/charsets.h"
|
||||
#include "src/selcodepage.h"
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include "lib/tty/mouse.h"
|
||||
#include "lib/vfs/mc-vfs/vfs.h"
|
||||
#include "lib/strutil.h"
|
||||
#include "lib/util.h" /* load_file_position() */
|
||||
|
||||
#include "src/main.h"
|
||||
#include "src/charsets.h"
|
||||
|
|
Loading…
Reference in New Issue