This source file includes following definitions.
- file_history_parse_entry
- file_history_write_entry
- file_history_list_read
- file_history_list_write
- file_history_create_item
- file_history_release_item
- file_history_free_item
- show_file_history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28 #include <stdio.h>
29
30 #include "lib/global.h"
31
32 #include "lib/fileloc.h"
33 #include "lib/mcconfig.h"
34 #include "lib/strutil.h"
35 #include "lib/util.h"
36
37 #include "file_history.h"
38
39
40
41
42
43 #define TMP_SUFFIX ".tmp"
44
45
46
47 typedef struct file_history_data_t
48 {
49 char *file_name;
50 char *file_pos;
51 } file_history_data_t;
52
53
54
55
56
57 static const char SEPARATOR[] = " ";
58
59
60
61
62
63 static void
64 file_history_parse_entry (const char *buf, GList **file_list)
65 {
66 file_history_data_t *fhd;
67 const char *s = strrchr (buf, *SEPARATOR);
68
69
70 if (s == NULL || s == buf)
71 return;
72
73 fhd = g_new (file_history_data_t, 1);
74 fhd->file_name = str_unescape (buf, s - buf, "", TRUE);
75 fhd->file_pos = g_strdup (s + 1);
76 str_rstrip_eol (fhd->file_pos);
77
78 *file_list = g_list_prepend (*file_list, fhd);
79 }
80
81
82
83 static void
84 file_history_write_entry (const file_history_data_t *fhd, GString *s)
85 {
86 char *file_name;
87
88 file_name = str_escape (fhd->file_name, -1, "", TRUE);
89 g_string_append (s, file_name);
90 g_free (file_name);
91
92 if (fhd->file_pos != NULL)
93 {
94 g_string_append_c (s, *SEPARATOR);
95 g_string_append (s, fhd->file_pos);
96 }
97 }
98
99
100
101 static GList *
102 file_history_list_read (void)
103 {
104 char *fn;
105 FILE *f;
106 char buf[MC_MAXPATHLEN + 100];
107 GList *file_list = NULL;
108
109
110 fn = mc_config_get_full_path (MC_FILEPOS_FILE);
111 if (fn == NULL)
112 return NULL;
113
114 f = fopen (fn, "r");
115 g_free (fn);
116 if (f == NULL)
117 return NULL;
118
119 while (fgets (buf, sizeof (buf), f) != NULL)
120 file_history_parse_entry (buf, &file_list);
121
122 fclose (f);
123
124 return file_list;
125 }
126
127
128
129 static void
130 file_history_list_write (const GList *file_list)
131 {
132 char *fn;
133 FILE *f;
134 gboolean write_error = FALSE;
135
136 fn = mc_config_get_full_path (MC_FILEPOS_FILE);
137 if (fn == NULL)
138 return;
139
140 mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
141
142 f = fopen (fn, "w");
143 if (f != NULL)
144 {
145 GString *s;
146
147 s = g_string_sized_new (128);
148
149 for (; file_list != NULL && !write_error; file_list = g_list_next (file_list))
150 {
151 file_history_write_entry (file_list->data, s);
152 write_error = (fprintf (f, "%s\n", s->str) < 0);
153 g_string_truncate (s, 0);
154 }
155
156 g_string_free (s, TRUE);
157
158 fclose (f);
159 }
160
161 if (write_error)
162 mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
163 else
164 mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
165
166 g_free (fn);
167 }
168
169
170
171 static void
172 file_history_create_item (history_descriptor_t *hd, void *data)
173 {
174 file_history_data_t *fhd = (file_history_data_t *) data;
175 size_t width;
176
177 width = str_term_width1 (fhd->file_name);
178 hd->max_width = MAX (width, hd->max_width);
179
180 listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, fhd->file_name, fhd->file_pos, TRUE);
181
182 fhd->file_pos = NULL;
183 }
184
185
186
187 static void *
188 file_history_release_item (history_descriptor_t *hd, WLEntry *le)
189 {
190 file_history_data_t *fhd;
191
192 (void) hd;
193
194 fhd = g_new (file_history_data_t, 1);
195 fhd->file_name = le->text;
196 le->text = NULL;
197 fhd->file_pos = (char *) le->data;
198 le->data = NULL;
199
200 return fhd;
201 }
202
203
204
205 static void
206 file_history_free_item (void *data)
207 {
208 file_history_data_t *fhd = (file_history_data_t *) data;
209
210 g_free (fhd->file_name);
211 g_free (fhd->file_pos);
212 g_free (fhd);
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227 char *
228 show_file_history (const Widget *w, int *action)
229 {
230 GList *file_list;
231 size_t len;
232 history_descriptor_t hd;
233
234 file_list = file_history_list_read ();
235 if (file_list == NULL)
236 return NULL;
237
238 len = g_list_length (file_list);
239
240 file_list = g_list_last (file_list);
241
242 history_descriptor_init (&hd, w->rect.y, w->rect.x, file_list, 0);
243
244 hd.create = file_history_create_item;
245 hd.release = file_history_release_item;
246 hd.free = file_history_free_item;
247
248 history_show (&hd);
249
250 hd.list = g_list_first (hd.list);
251
252
253 if (len != g_list_length (hd.list))
254 {
255 hd.list = g_list_reverse (hd.list);
256 file_history_list_write (hd.list);
257 }
258
259 g_list_free_full (hd.list, (GDestroyNotify) file_history_free_item);
260
261 *action = hd.action;
262
263 return hd.text;
264 }
265
266