This source file includes following definitions.
- mc_config_history_get
- mc_config_history_get_recent_item
- mc_config_history_load
- mc_config_history_save
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
27
28
29
30
31
32
33
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include <sys/types.h>
39
40 #include "lib/global.h"
41
42 #include "lib/fileloc.h"
43 #include "lib/strutil.h"
44 #include "lib/util.h"
45
46 #include "lib/mcconfig.h"
47
48
49
50
51 int num_history_items_recorded = 60;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 GList *
73 mc_config_history_get (const char *name)
74 {
75 GList *hist = NULL;
76 char *profile;
77 mc_config_t *cfg;
78
79 if (num_history_items_recorded == 0)
80 return NULL;
81 if (name == NULL || *name == '\0')
82 return NULL;
83
84 profile = mc_config_get_full_path (MC_HISTORY_FILE);
85 cfg = mc_config_init (profile, TRUE);
86
87 hist = mc_config_history_load (cfg, name);
88
89 mc_config_deinit (cfg);
90 g_free (profile);
91
92 return hist;
93 }
94
95
96
97
98
99
100
101
102
103 char *
104 mc_config_history_get_recent_item (const char *name)
105 {
106 GList *history;
107 char *item = NULL;
108
109 history = mc_config_history_get (name);
110 if (history != NULL)
111 {
112
113 item = (char *) history->data;
114 history->data = NULL;
115 history = g_list_first (history);
116 g_list_free_full (history, g_free);
117 }
118
119 return item;
120 }
121
122
123
124
125
126
127 GList *
128 mc_config_history_load (mc_config_t *cfg, const char *name)
129 {
130 size_t i;
131 GList *hist = NULL;
132 char **keys;
133 size_t keys_num = 0;
134 GIConv conv = INVALID_CONV;
135 GString *buffer;
136
137 if (name == NULL || *name == '\0')
138 return NULL;
139
140
141 keys = mc_config_get_keys (cfg, name, &keys_num);
142 g_strfreev (keys);
143
144
145
146 if (!mc_global.utf8_display)
147 conv = str_crt_conv_from ("UTF-8");
148
149 buffer = g_string_sized_new (64);
150
151 for (i = 0; i < keys_num; i++)
152 {
153 char key[BUF_TINY];
154 char *this_entry;
155
156 g_snprintf (key, sizeof (key), "%lu", (unsigned long) i);
157 this_entry = mc_config_get_string_raw (cfg, name, key, "");
158
159 if (this_entry == NULL)
160 continue;
161
162 if (conv == INVALID_CONV)
163 hist = list_append_unique (hist, this_entry);
164 else
165 {
166 g_string_set_size (buffer, 0);
167 if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE)
168 hist = list_append_unique (hist, this_entry);
169 else
170 {
171 hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len));
172 g_free (this_entry);
173 }
174 }
175 }
176
177 g_string_free (buffer, TRUE);
178 if (conv != INVALID_CONV)
179 str_close_conv (conv);
180
181
182 return g_list_last (hist);
183 }
184
185
186
187
188
189
190 void
191 mc_config_history_save (mc_config_t *cfg, const char *name, GList *h)
192 {
193 GIConv conv = INVALID_CONV;
194 GString *buffer;
195 int i;
196
197 if (name == NULL || *name == '\0' || h == NULL)
198 return;
199
200
201 h = g_list_last (h);
202
203
204 for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++)
205 h = g_list_previous (h);
206
207 mc_config_del_group (cfg, name);
208
209
210
211 if (!mc_global.utf8_display)
212 conv = str_crt_conv_to ("UTF-8");
213
214 buffer = g_string_sized_new (64);
215
216
217 for (i = 0; h != NULL; h = g_list_next (h))
218 {
219 char key[BUF_TINY];
220 char *text = (char *) h->data;
221
222
223 if (text == NULL)
224 continue;
225
226 g_snprintf (key, sizeof (key), "%d", i++);
227
228 if (conv == INVALID_CONV)
229 mc_config_set_string_raw (cfg, name, key, text);
230 else
231 {
232 g_string_set_size (buffer, 0);
233 if (str_convert (conv, text, buffer) == ESTR_FAILURE)
234 mc_config_set_string_raw (cfg, name, key, text);
235 else
236 mc_config_set_string_raw (cfg, name, key, buffer->str);
237 }
238 }
239
240 g_string_free (buffer, TRUE);
241 if (conv != INVALID_CONV)
242 str_close_conv (conv);
243 }
244
245