1
1

* util.c: Add functions for saving and loading file positions.

Этот коммит содержится в:
Pavel Roskin 2002-12-08 06:51:22 +00:00
родитель 0d0ede9ec4
Коммит 65f23dcb15
3 изменённых файлов: 121 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,7 @@
2002-12-08 Pavel Roskin <proski@gnu.org>
* util.c: Add functions for saving and loading file positions.
2002-12-07 Pavel Roskin <proski@gnu.org>
* file.c (is_wildcarded): Move ...

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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 "\\"