From 65f23dcb158663ba9c2cacf344182f4d66239f2d Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Sun, 8 Dec 2002 06:51:22 +0000 Subject: [PATCH] * util.c: Add functions for saving and loading file positions. --- src/ChangeLog | 4 ++ src/util.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 17 +++++++++ 3 files changed, 121 insertions(+) diff --git a/src/ChangeLog b/src/ChangeLog index a7b4748aa..bd64eda78 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2002-12-08 Pavel Roskin + + * util.c: Add functions for saving and loading file positions. + 2002-12-07 Pavel Roskin * file.c (is_wildcarded): Move ... diff --git a/src/util.c b/src/util.c index 80989825b..2be2e8c73 100644 --- a/src/util.c +++ b/src/util.c @@ -1242,3 +1242,103 @@ mc_mkstemps (char **pname, const char *prefix, const char *suffix) return -1; } + + +/* + * Read and restore position for the given filename. + * If there is no stored data, return line 1 and col 0. + */ +void +load_file_position (char *filename, long *line, long *column) +{ + char *fn; + FILE *f; + char buf[MC_MAXPATHLEN + 20]; + int len; + + /* defaults */ + *line = 1; + *column = 0; + + /* open file with positions */ + fn = concat_dir_and_file (home_dir, MC_FILEPOS); + f = fopen (fn, "r"); + g_free (fn); + if (!f) { + g_free (filename); + return; + } + + len = strlen (filename); + + while (fgets (buf, sizeof (buf), f)) { + char *p; + + /* check if the filename matches the beginning of string */ + if (strncmp (buf, filename, len) != 0) + continue; + + /* followed by single space */ + if (buf[len] != ' ') + continue; + + /* and string without spaces */ + p = &buf[len + 1]; + if (strchr (p, ' ')) + continue; + + *line = atol (p); + p = strchr (buf, ';'); + *column = atol (&p[1]); + } + fclose (f); + g_free (filename); +} + +/* Save position for the given file */ +void +save_file_position (char *filename, long line, long column) +{ + char *tmp, *fn; + FILE *f, *t; + char buf[MC_MAXPATHLEN + 20]; + int i = 1; + int len; + + len = strlen (filename); + + tmp = concat_dir_and_file (home_dir, MC_FILEPOS_TMP); + fn = concat_dir_and_file (home_dir, MC_FILEPOS); + + /* open temporary file */ + t = fopen (tmp, "w"); + if (!t) { + g_free (tmp); + g_free (fn); + return; + } + + /* put the new record */ + fprintf (t, "%s %ld;%ld\n", filename, line, column); + + /* copy records from the old file */ + f = fopen (fn, "r"); + if (f) { + while (fgets (buf, sizeof (buf), f)) { + /* Skip entries for the current filename */ + if (strncmp (buf, filename, len) == 0 && buf[len] == ' ' + && !strchr (&buf[len + 1], ' ')) + continue; + + fprintf (t, "%s", buf); + if (++i > MC_FILEPOS_ENTRIES) + break; + } + fclose (f); + } + + fclose (t); + rename (tmp, fn); + g_free (tmp); + g_free (fn); +} diff --git a/src/util.h b/src/util.h index d5e6a2d07..653580268 100644 --- a/src/util.h +++ b/src/util.h @@ -128,6 +128,23 @@ void execute_hooks (Hook *hook_list); void delete_hook (Hook **hook_list, void (*hook_fn)(void *)); int hook_present (Hook *hook_list, void (*hook_fn)(void *)); + +/* Position saving and restoring */ + +/* file where positions are stored */ +#define MC_FILEPOS ".mc/filepos" +/* temporary file */ +#define MC_FILEPOS_TMP ".mc/filepos.tmp" +/* maximum entries in MC_FILEPOS */ +#define MC_FILEPOS_ENTRIES 1024 +/* Load position for the given filename */ +void load_file_position (char *filename, long *line, long *column); +/* Save position for the given filename */ +void save_file_position (char *filename, long line, long column); + + +/* OS specific defines */ + #ifdef NATIVE_WIN32 # define PATH_SEP '\\' # define PATH_SEP_STR "\\"